Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
cache: Avoid flushing invalid cache for realm emoji.
Zixuan James Li172a1661594e
diff --git a/zerver/models.py b/zerver/models.pyindex 9a2626cbe42..19fabad26f2 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -1121,6 +1121,7 @@ def get_realm_emoji_dicts(realm: Realm, only_active_emojis: bool = False) -> Dicauthor_id = Noneif realm_emoji.author:author_id = realm_emoji.author_id+ assert realm_emoji.file_name is not Noneemoji_url = get_emoji_url(realm_emoji.file_name, realm_emoji.realm_id)emoji_dict: EmojiInfo = dict(@@ -1159,6 +1160,16 @@ def get_active_realm_emoji_uncached(realm: Realm) -> Dict[str, EmojiInfo]:def flush_realm_emoji(*, instance: RealmEmoji, **kwargs: object) -> None:+ if instance.file_name is None:+ # Because we construct RealmEmoji.file_name using the ID for+ # the RealmEmoji object, it will always have file_name=None,+ # and then it'll be updated with the actual filename as soon+ # as the upload completes successfully.+ #+ # Doing nothing when file_name=None is the best option, since+ # such an object shouldn't have been cached yet, and this+ # function will be called again when file_name is set.+ returnrealm = instance.realmcache_set(get_realm_emoji_cache_key(realm), get_realm_emoji_uncached(realm), timeout=3600 * 24 * 7diff --git a/zerver/tests/test_realm_emoji.py b/zerver/tests/test_realm_emoji.pyindex 04bfa0d127f..ec55bf4f5a2 100644--- a/zerver/tests/test_realm_emoji.py+++ b/zerver/tests/test_realm_emoji.py@@ -23,7 +23,7 @@ class RealmEmojiTest(ZulipTestCase):return realm_emojidef create_test_emoji_with_no_author(self, name: str, realm: Realm) -> RealmEmoji:- realm_emoji = RealmEmoji.objects.create(realm=realm, name=name)+ realm_emoji = RealmEmoji.objects.create(realm=realm, name=name, file_name=name)return realm_emojidef test_list(self) -> None: