Skip to content

Commit f96e7c0

Browse files
committed
model: Fetch realm emoji from initial data.
Earlier, an API call to get_realm_emoji() was used to fetch changes to realm emoji. This commit allows the initial update of realm emoji directly from initial_data. The process_realm_emoji_data() function processes the dictionary passed to it and converts it to the NamedEmojiData type. The existing code to obtain all_emoji_data is used in the generate_all_emoji_data() function to create the dictionary containing all the active emojis whenever required.
1 parent fb3c5b5 commit f96e7c0

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,22 @@ def initial_data(logged_on_user, users_fixture, streams_fixture):
538538
}
539539
}
540540
},
541+
"realm_emoji": {
542+
"1": {
543+
"author_id": 5,
544+
"deactivated": False,
545+
"id": "1",
546+
"name": "green_tick",
547+
"source_url": "/user_avatars/1/emoji/images/1.png"
548+
},
549+
"202020": {
550+
"author_id": 5,
551+
"deactivated": False,
552+
"id": "202020",
553+
"name": "joker",
554+
"source_url": "/user_avatars/202020/emoji/images/202020.png"
555+
},
556+
},
541557
'last_event_id': -1,
542558
'muted_topics': [],
543559
'realm_user_groups': [],

tests/model/test_model.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ def model(self, mocker, initial_data, user_profile,
4242
self.client.get_profile.return_value = user_profile
4343
mocker.patch('zulipterminal.model.unicode_emojis',
4444
EMOJI_DATA=unicode_emojis)
45-
mocker.patch('zulipterminal.model.Model.fetch_custom_emojis',
46-
return_value=custom_emojis)
4745
model = Model(self.controller)
4846
return model
4947

@@ -81,8 +79,10 @@ def test_init(self, model, initial_data, user_profile,
8179
zulip_emoji = {
8280
'zulip': {'code': 'zulip', 'type': 'zulip_extra_emoji'}
8381
}
82+
initial_realm_emoji = initial_data['realm_emoji']
83+
realm_emojis = model.process_realm_emoji_data(initial_realm_emoji)
8484
assert model.active_emoji_data == OrderedDict(sorted(
85-
{**unicode_emojis, **custom_emojis, **zulip_emoji}.items(),
85+
{**unicode_emojis, **realm_emojis, **zulip_emoji}.items(),
8686
key=lambda e: e[0]
8787
))
8888
# Custom emoji replaces unicode emoji with same name.
@@ -187,6 +187,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
187187
'muted_topics',
188188
'realm_user',
189189
'realm_user_groups',
190+
'realm_emoji',
190191
'zulip_version',
191192
]
192193
model.client.register.assert_called_once_with(
@@ -1995,15 +1996,25 @@ def test_user_name_from_id_invalid(self, model, user_id):
19951996
'id': '100',
19961997
'name': 'urwid',
19971998
},
1999+
'3': {
2000+
'deactivated': False,
2001+
'id': '3',
2002+
'name': 'dancing',
2003+
},
19982004
'20': {
19992005
'deactivated': False,
20002006
'id': '20',
20012007
'name': 'snape',
20022008
},
2003-
'3': {
2009+
'202020': {
20042010
'deactivated': False,
2005-
'id': '3',
2006-
'name': 'dancing',
2011+
'id': '202020',
2012+
'name': 'joker',
2013+
},
2014+
'12345': {
2015+
'deactivated': False,
2016+
'id': '12345',
2017+
'name': 'zulip',
20072018
},
20082019
'81': { # Not included in custom_emojis because deactivated.
20092020
'deactivated': True,
@@ -2015,11 +2026,10 @@ def test_user_name_from_id_invalid(self, model, user_id):
20152026
'result': 'success'
20162027
}
20172028
])
2018-
def test_fetch_custom_emojis(self, mocker, model, custom_emojis,
2019-
response):
2020-
self.client.get_realm_emoji.return_value = response
2021-
2022-
fetched_custom_emojis = model.fetch_custom_emojis()
2029+
def test_process_realm_emoji_data(self, mocker, model,
2030+
custom_emojis, response):
2031+
realm_emojis = response['emoji']
2032+
fetched_custom_emojis = model.process_realm_emoji_data(realm_emojis)
20232033

20242034
assert fetched_custom_emojis == custom_emojis
20252035

zulipterminal/model.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
'stream_id': int,
5454
'stream_ids': List[int], # NOTE: replaces 'stream_id' in ZFL 35 for peer*
5555
'value': bool,
56-
'message_ids': List[int] # Present when subject of msg(s) is updated
56+
'message_ids': List[int], # Present when subject of msg(s) is updated
57+
# realm emoji:
58+
'realm_emoji': Dict[str, Any]
5759
}, total=False) # Each Event will only have a subset of these
5860

5961
EditPropagateMode = Literal['change_one', 'change_all', 'change_later']
@@ -141,18 +143,9 @@ def __init__(self, controller: Any) -> None:
141143
self.unread_counts = classify_unread_counts(self)
142144

143145
self._draft = None # type: Optional[Message]
144-
unicode_emoji_data = unicode_emojis.EMOJI_DATA
145-
for name, data in unicode_emoji_data.items():
146-
data['type'] = 'unicode_emoji'
147-
typed_unicode_emoji_data = cast(NamedEmojiData, unicode_emoji_data)
148-
custom_emoji_data = self.fetch_custom_emojis()
149-
zulip_extra_emoji = {
150-
'zulip': {'code': 'zulip', 'type': 'zulip_extra_emoji'}
151-
} # type: NamedEmojiData
152-
all_emoji_data = {**typed_unicode_emoji_data,
153-
**custom_emoji_data,
154-
**zulip_extra_emoji}.items()
155-
self.active_emoji_data = OrderedDict(sorted(all_emoji_data,
146+
all_emoji_data = self.generate_all_emoji_data(
147+
self.initial_data['realm_emoji'])
148+
self.active_emoji_data = OrderedDict(sorted(all_emoji_data.items(),
156149
key=lambda e: e[0]))
157150

158151
self.new_user_input = True
@@ -397,13 +390,28 @@ def update_stream_message(self, topic: str, message_id: int,
397390
display_error_if_present(response, self.controller)
398391
return response['result'] == 'success'
399392

400-
def fetch_custom_emojis(self) -> NamedEmojiData:
401-
response = self.client.get_realm_emoji()
393+
def generate_all_emoji_data(self,
394+
custom_emoji: Dict[str,
395+
Any]) -> NamedEmojiData:
396+
unicode_emoji_data = unicode_emojis.EMOJI_DATA
397+
for name, data in unicode_emoji_data.items():
398+
data['type'] = 'unicode_emoji'
399+
typed_unicode_emoji_data = cast(NamedEmojiData, unicode_emoji_data)
400+
custom_emoji_data = self.process_realm_emoji_data(custom_emoji)
401+
zulip_extra_emoji = {
402+
'zulip': {'code': 'zulip', 'type': 'zulip_extra_emoji'}
403+
} # type: NamedEmojiData
404+
all_emoji_data = {**typed_unicode_emoji_data,
405+
**custom_emoji_data,
406+
**zulip_extra_emoji}
407+
return all_emoji_data
408+
409+
def process_realm_emoji_data(self,
410+
emoji_data: Dict[str, Any]) -> NamedEmojiData:
402411
custom_emojis = {emoji['name']: {'code': emoji_code,
403412
'type': 'realm_emoji'}
404-
for emoji_code, emoji in response['emoji'].items()
413+
for emoji_code, emoji in emoji_data.items()
405414
if not emoji['deactivated']} # type: NamedEmojiData
406-
display_error_if_present(response, self.controller)
407415
return custom_emojis
408416

409417
def get_messages(self, *,
@@ -1184,6 +1192,7 @@ def _register_desired_events(self, *, fetch_data: bool=False) -> str:
11841192
'muted_topics',
11851193
'realm_user', # Enables cross_realm_bots
11861194
'realm_user_groups',
1195+
'realm_emoji',
11871196
# zulip_version and zulip_feature_level are always returned in
11881197
# POST /register from Feature level 3.
11891198
'zulip_version',

0 commit comments

Comments
 (0)