Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate emoji storage to group aliases together for each canonical name. #1084

Merged
merged 4 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ ignore=
E221,
# E252 - Spaces in parameters, a style deliberately avoided
E252,
# E501 - Ignore max-line-length property - this is already defined by black
E501,
neiljp marked this conversation as resolved.
Show resolved Hide resolved
# F841 - assigned to but never used - FIXME: indicates potential issues
F841,
# Q000 - inline quotes - FIXME: standardize on " or ' in future?
Expand Down
39 changes: 28 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ def realm_emojis():
def realm_emojis_data():
return OrderedDict(
[
("joker", {"code": "202020", "type": "realm_emoji"}),
("singing", {"code": "3", "type": "realm_emoji"}),
("zulip", {"code": "4", "type": "realm_emoji"}),
("joker", {"code": "202020", "aliases": [], "type": "realm_emoji"}),
("singing", {"code": "3", "aliases": [], "type": "realm_emoji"}),
("zulip", {"code": "4", "aliases": [], "type": "realm_emoji"}),
]
)

Expand All @@ -301,20 +301,37 @@ def realm_emojis_data():
def unicode_emojis():
return OrderedDict(
[
("happy", {"code": "1f600", "type": "unicode_emoji"}),
("joker", {"code": "1f0cf", "type": "unicode_emoji"}),
("joy_cat", {"code": "1f639", "type": "unicode_emoji"}),
("rock_on", {"code": "1f918", "type": "unicode_emoji"}),
("smile", {"code": "263a", "type": "unicode_emoji"}),
("smiley", {"code": "1f603", "type": "unicode_emoji"}),
("smirk", {"code": "1f60f", "type": "unicode_emoji"}),
(
"happy",
{"code": "1f600", "aliases": ["grinning"], "type": "unicode_emoji"},
),
("joker", {"code": "1f0cf", "aliases": [], "type": "unicode_emoji"}),
("joy_cat", {"code": "1f639", "aliases": [], "type": "unicode_emoji"}),
(
"rock_on",
{
"code": "1f918",
"aliases": ["sign_of_the_horns"],
"type": "unicode_emoji",
},
),
("smile", {"code": "263a", "aliases": [], "type": "unicode_emoji"}),
("smiley", {"code": "1f603", "aliases": [], "type": "unicode_emoji"}),
("smirk", {"code": "1f60f", "aliases": ["smug"], "type": "unicode_emoji"}),
]
)


@pytest.fixture
def zulip_emoji():
return OrderedDict([("zulip", {"code": "zulip", "type": "zulip_extra_emoji"})])
return OrderedDict(
[
(
"zulip",
{"code": "zulip", "aliases": [], "type": "zulip_extra_emoji"},
)
]
)


def display_recipient_factory(recipient_details_list: List[Tuple[int, str]]):
Expand Down
24 changes: 23 additions & 1 deletion tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def test_init(
key=lambda e: e[0],
)
)
assert model.all_emoji_names == sorted(
[
name
for emoji in {
**unicode_emojis,
**realm_emojis_data,
**zulip_emoji,
}.items()
for name in [emoji[0], *emoji[1]["aliases"]]
]
)
# Deactivated emoji is removed from active emojis set
assert "green_tick" not in model.active_emoji_data
# Custom emoji replaces unicode emoji with same name.
Expand Down Expand Up @@ -2772,14 +2783,25 @@ def test_generate_all_emoji_data(
realm_emojis_data,
realm_emojis,
):
all_emoji_data = model.generate_all_emoji_data(realm_emojis)
all_emoji_data, all_emoji_names = model.generate_all_emoji_data(realm_emojis)

assert all_emoji_data == OrderedDict(
sorted(
{**unicode_emojis, **realm_emojis_data, **zulip_emoji}.items(),
key=lambda e: e[0],
)
)
assert model.all_emoji_names == sorted(
[
name
for emoji in {
**unicode_emojis,
**realm_emojis_data,
**zulip_emoji,
}.items()
for name in [emoji[0], *emoji[1]["aliases"]]
]
)
# custom emoji added to active_emoji_data
assert all_emoji_data["singing"]["type"] == "realm_emoji"
# Deactivated custom emoji is removed from active emojis set
Expand Down
1 change: 1 addition & 0 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def write_box(
user_dict,
):
self.view.model.active_emoji_data = unicode_emojis
self.view.model.all_emoji_names = list(unicode_emojis.keys())
write_box = WriteBox(self.view)
write_box.view.users = users_fixture
write_box.model.user_dict = user_dict
Expand Down
18 changes: 10 additions & 8 deletions tools/convert-unicode-emoji-data
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ SCRIPT_NAME = PurePath(__file__).name
emoji_dict = EMOJI_NAME_MAPS

emoji_map = {}
for emoji_code, emoji_names in emoji_dict.items():
emoji_map[emoji_names["canonical_name"]] = emoji_code
for emoji in emoji_names["aliases"]:
emoji_map[emoji] = emoji_code
for emoji_code, emoji_data in emoji_dict.items():
emoji_map[emoji_data["canonical_name"]] = {
"code": emoji_code,
"aliases": emoji_data["aliases"],
}

ordered_emojis = OrderedDict(sorted(emoji_map.items()))

with OUTPUT_FILE.open("w") as f:
f.write(
"from collections import OrderedDict\n\n\n"
"# fmt: off\n"
f"# Generated automatically by tools/{SCRIPT_NAME}\n"
"# Do not modify.\n\n"
"EMOJI_DATA = OrderedDict(\n [\n"
)
for emoji_code, emoji_name in ordered_emojis.items():
# {'smile': {'code': '263a'}}
f.write(f' ("{emoji_code}", {{"code": "{emoji_name}"}}),\n')
f.write(" ]\n)\n")
for emoji_name, emoji in ordered_emojis.items():
# {'smile': {'code': '263a', 'aliases': []}}
f.write(f' ("{emoji_name}", {emoji}),\n')
f.write(" ]\n)\n# fmt: on\n")

print(f"Emoji list saved in {OUTPUT_FILE}")

Expand Down
1 change: 1 addition & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class StreamData(TypedDict):

class EmojiData(TypedDict):
code: str
aliases: List[str]
type: EmojiType


Expand Down
22 changes: 15 additions & 7 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(self, controller: Any) -> None:

self._store_content_length_restrictions()

self.active_emoji_data = self.generate_all_emoji_data(
self.active_emoji_data, self.all_emoji_names = self.generate_all_emoji_data(
self.initial_data["realm_emoji"]
)

Expand Down Expand Up @@ -488,29 +488,35 @@ def update_stream_message(

def generate_all_emoji_data(
self, custom_emoji: Dict[str, RealmEmojiData]
) -> NamedEmojiData:
) -> Tuple[NamedEmojiData, List[str]]:
unicode_emoji_data = unicode_emojis.EMOJI_DATA
for name, data in unicode_emoji_data.items():
data["type"] = "unicode_emoji"
typed_unicode_emoji_data = cast(NamedEmojiData, unicode_emoji_data)
custom_emoji_data: NamedEmojiData = {
emoji["name"]: {
"code": emoji_code,
"aliases": [],
"type": "realm_emoji",
}
for emoji_code, emoji in custom_emoji.items()
if not emoji["deactivated"]
}
zulip_extra_emoji: NamedEmojiData = {
"zulip": {"code": "zulip", "type": "zulip_extra_emoji"}
"zulip": {"code": "zulip", "aliases": [], "type": "zulip_extra_emoji"}
}
all_emoji_data = {
**typed_unicode_emoji_data,
**custom_emoji_data,
**zulip_extra_emoji,
}.items()
active_emoji_data = OrderedDict(sorted(all_emoji_data, key=lambda e: e[0]))
return active_emoji_data
}
all_emoji_names = []
for emoji_name, emoji_data in all_emoji_data.items():
all_emoji_names.append(emoji_name)
all_emoji_names.extend(emoji_data["aliases"])
all_emoji_names = sorted(all_emoji_names)
active_emoji_data = OrderedDict(sorted(all_emoji_data.items()))
return active_emoji_data, all_emoji_names

def get_messages(
self, *, num_after: int, num_before: int, anchor: Optional[int]
Expand Down Expand Up @@ -1428,7 +1434,9 @@ def _handle_update_emoji_event(self, event: Event) -> None:
# by the users in the organisation along with a boolean value
# representing the active state of each emoji.
assert event["type"] == "realm_emoji"
self.active_emoji_data = self.generate_all_emoji_data(event["realm_emoji"])
self.active_emoji_data, self.all_emoji_names = self.generate_all_emoji_data(
event["realm_emoji"]
)

def _update_rendered_view(self, msg_id: int) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def autocomplete_streams(
def autocomplete_emojis(
self, text: str, prefix_string: str
) -> Tuple[List[str], List[str]]:
emoji_list = list(self.model.active_emoji_data.keys())
emoji_list = self.model.all_emoji_names
emojis = [emoji for emoji in emoji_list if match_emoji(emoji, text[1:])]
emoji_typeahead = format_string(emojis, ":{}:")

Expand Down
Loading