mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	user_status: Add backend changes to support status emoji.
In this commit: * We update the `UserStatus` model to accept `AbstractReaction` as a base class so, we can get all the fields related to store status emoji. * We update the user status endpoint (`users/me/status`) to accept status emoji fields. * We update the user status event to add status emoji fields. Co-authored-by: Yash Rathore <33805964+YashRE42@users.noreply.github.com>
This commit is contained in:
		@@ -8,6 +8,7 @@ from django.utils.translation import gettext as _
 | 
			
		||||
 | 
			
		||||
from zerver.decorator import human_users_only
 | 
			
		||||
from zerver.lib.actions import do_update_user_status, update_user_presence
 | 
			
		||||
from zerver.lib.emoji import check_emoji_request, emoji_name_to_emoji_code
 | 
			
		||||
from zerver.lib.exceptions import JsonableError
 | 
			
		||||
from zerver.lib.presence import get_presence_for_user, get_presence_response
 | 
			
		||||
from zerver.lib.request import REQ, get_request_notes, has_request_variables
 | 
			
		||||
@@ -18,6 +19,7 @@ from zerver.models import (
 | 
			
		||||
    UserActivity,
 | 
			
		||||
    UserPresence,
 | 
			
		||||
    UserProfile,
 | 
			
		||||
    UserStatus,
 | 
			
		||||
    get_active_user,
 | 
			
		||||
    get_active_user_profile_by_id_in_realm,
 | 
			
		||||
)
 | 
			
		||||
@@ -68,14 +70,50 @@ def update_user_status_backend(
 | 
			
		||||
    user_profile: UserProfile,
 | 
			
		||||
    away: Optional[bool] = REQ(json_validator=check_bool, default=None),
 | 
			
		||||
    status_text: Optional[str] = REQ(str_validator=check_capped_string(60), default=None),
 | 
			
		||||
    emoji_name: Optional[str] = REQ(default=None),
 | 
			
		||||
    emoji_code: Optional[str] = REQ(default=None),
 | 
			
		||||
    # TODO: emoji_type is the more appropriate name for this parameter, but changing
 | 
			
		||||
    # that requires nontrivial work on the API documentation, since it's not clear
 | 
			
		||||
    # that the reactions endpoint would prefer such a change.
 | 
			
		||||
    emoji_type: Optional[str] = REQ("reaction_type", default=None),
 | 
			
		||||
) -> HttpResponse:
 | 
			
		||||
 | 
			
		||||
    if status_text is not None:
 | 
			
		||||
        status_text = status_text.strip()
 | 
			
		||||
 | 
			
		||||
    if (away is None) and (status_text is None):
 | 
			
		||||
    if (away is None) and (status_text is None) and (emoji_name is None):
 | 
			
		||||
        raise JsonableError(_("Client did not pass any new values."))
 | 
			
		||||
 | 
			
		||||
    if emoji_name == "":
 | 
			
		||||
        # Reset the emoji_code and reaction_type if emoji_name is empty.
 | 
			
		||||
        # This should clear the user's configured emoji.
 | 
			
		||||
        emoji_code = ""
 | 
			
		||||
        emoji_type = UserStatus.UNICODE_EMOJI
 | 
			
		||||
 | 
			
		||||
    elif emoji_name is not None:
 | 
			
		||||
        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(user_profile.realm, emoji_name)[0]
 | 
			
		||||
 | 
			
		||||
        if emoji_type is None:
 | 
			
		||||
            emoji_type = emoji_name_to_emoji_code(user_profile.realm, emoji_name)[1]
 | 
			
		||||
 | 
			
		||||
    elif emoji_type or emoji_code:
 | 
			
		||||
        raise JsonableError(
 | 
			
		||||
            _("Client must pass emoji_name if they pass either emoji_code or reaction_type.")
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    # If we're asking to set an emoji (not clear it ("") or not adjust
 | 
			
		||||
    # it (None)), we need to verify the emoji is valid.
 | 
			
		||||
    if emoji_name not in ["", None]:
 | 
			
		||||
        assert emoji_name is not None
 | 
			
		||||
        assert emoji_code is not None
 | 
			
		||||
        assert emoji_type is not None
 | 
			
		||||
        check_emoji_request(user_profile.realm, emoji_name, emoji_code, emoji_type)
 | 
			
		||||
 | 
			
		||||
    client = get_request_notes(request).client
 | 
			
		||||
    assert client is not None
 | 
			
		||||
    do_update_user_status(
 | 
			
		||||
@@ -83,6 +121,9 @@ def update_user_status_backend(
 | 
			
		||||
        away=away,
 | 
			
		||||
        status_text=status_text,
 | 
			
		||||
        client_id=client.id,
 | 
			
		||||
        emoji_name=emoji_name,
 | 
			
		||||
        emoji_code=emoji_code,
 | 
			
		||||
        reaction_type=emoji_type,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    return json_success()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user