emojis: Use get_emoji_data.

The previous function was poorly named, asked for a
Realm object when realm_id sufficed, and returned a
tuple of strings that had different semantics.

I also avoid calling it duplicate times in a couple
places, although it was probably rarely the case that
both invocations actually happened if upstream
validations were working.

Note that there is a TypedDict called EmojiInfo, so I
chose EmojiData here.  Perhaps a better name would be
TinyEmojiData or something.

I also simplify the reaction tests with a verify
helper.
This commit is contained in:
Steve Howell
2023-07-14 12:25:57 +00:00
committed by Tim Abbott
parent b742f1241f
commit 67cdf1a7b4
9 changed files with 77 additions and 67 deletions

View File

@@ -3,7 +3,7 @@ from typing import Any, Dict, Optional
from django.utils.translation import gettext as _
from zerver.actions.create_user import create_historical_user_messages
from zerver.lib.emoji import check_emoji_request, emoji_name_to_emoji_code
from zerver.lib.emoji import check_emoji_request, get_emoji_data
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import access_message, update_to_dict_cache
from zerver.lib.stream_subscription import subscriber_ids_with_stream_history_access
@@ -96,15 +96,18 @@ def check_add_reaction(
) -> None:
message, user_message = access_message(user_profile, message_id, lock_message=True)
if emoji_code is None:
# The emoji_code argument is only required for rare corner
# cases discussed in the long block comment below. For simple
# API clients, we allow specifying just the name, and just
# look up the code using the current name->code mapping.
emoji_code = emoji_name_to_emoji_code(message.sender.realm, emoji_name)[0]
if emoji_code is None or reaction_type is None:
emoji_data = get_emoji_data(message.sender.realm_id, emoji_name)
if reaction_type is None:
reaction_type = emoji_name_to_emoji_code(message.sender.realm, emoji_name)[1]
if emoji_code is None:
# The emoji_code argument is only required for rare corner
# cases discussed in the long block comment below. For simple
# API clients, we allow specifying just the name, and just
# look up the code using the current name->code mapping.
emoji_code = emoji_data.emoji_code
if reaction_type is None:
reaction_type = emoji_data.reaction_type
if Reaction.objects.filter(
user_profile=user_profile,