2020-11-14 08:55:38 +01:00
|
|
|
from django.db import transaction
|
2016-11-03 18:49:00 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-11-03 18:49:00 +01:00
|
|
|
|
2022-04-14 23:54:01 +02:00
|
|
|
from zerver.actions.reactions import check_add_reaction, do_remove_reaction
|
2023-07-14 14:25:57 +02:00
|
|
|
from zerver.lib.emoji import get_emoji_data
|
2023-07-18 19:33:27 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError, ReactionDoesNotExistError
|
2016-11-03 18:49:00 +01:00
|
|
|
from zerver.lib.message import access_message
|
|
|
|
from zerver.lib.response import json_success
|
2024-04-20 10:12:00 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2021-04-12 22:47:31 +02:00
|
|
|
from zerver.models import Reaction, UserProfile
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-08 16:28:02 +02:00
|
|
|
|
2020-11-14 08:55:38 +01:00
|
|
|
# transaction.atomic is required since we use FOR UPDATE queries in access_message
|
|
|
|
@transaction.atomic
|
2024-04-20 10:12:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def add_reaction(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
message_id: int,
|
2024-04-20 10:12:00 +02:00
|
|
|
*,
|
|
|
|
emoji_name: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
emoji_code: str | None = None,
|
|
|
|
reaction_type: str | None = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-04-12 22:47:31 +02:00
|
|
|
check_add_reaction(user_profile, message_id, emoji_name, emoji_code, reaction_type)
|
2017-10-08 09:34:59 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-10-08 09:34:59 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-11-14 08:55:38 +01:00
|
|
|
# transaction.atomic is required since we use FOR UPDATE queries in access_message
|
|
|
|
@transaction.atomic
|
2024-04-20 10:12:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def remove_reaction(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
message_id: int,
|
2024-04-20 10:12:00 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
emoji_name: str | None = None,
|
|
|
|
emoji_code: str | None = None,
|
2024-04-20 10:12:00 +02:00
|
|
|
reaction_type: str = "unicode_emoji",
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2024-03-22 06:45:17 +01:00
|
|
|
message = access_message(user_profile, message_id, lock_message=True)
|
2017-10-08 09:34:59 +02:00
|
|
|
|
2018-07-09 10:48:42 +02:00
|
|
|
if emoji_code is None:
|
|
|
|
if emoji_name is None:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise JsonableError(
|
|
|
|
_(
|
2021-02-12 08:20:45 +01:00
|
|
|
"At least one of the following arguments "
|
|
|
|
"must be present: emoji_name, emoji_code"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2018-07-09 10:48:42 +02:00
|
|
|
# A correct full Zulip client implementation should always
|
|
|
|
# pass an emoji_code, because of the corner cases discussed in
|
|
|
|
# the long block comments elsewhere in this file. However, to
|
|
|
|
# make it easy for simple API clients to use the reactions API
|
|
|
|
# without needing the mapping between emoji names and codes,
|
|
|
|
# we allow instead passing the emoji_name and looking up the
|
|
|
|
# corresponding code using the current data.
|
2023-08-10 05:59:25 +02:00
|
|
|
emoji_code = get_emoji_data(message.realm_id, emoji_name).emoji_code
|
2018-07-09 10:48:42 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
if not Reaction.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
message=message,
|
|
|
|
emoji_code=emoji_code,
|
|
|
|
reaction_type=reaction_type,
|
|
|
|
).exists():
|
2023-07-18 19:33:27 +02:00
|
|
|
raise ReactionDoesNotExistError
|
2017-10-08 09:34:59 +02:00
|
|
|
|
|
|
|
# Unlike adding reactions, while deleting a reaction, we don't
|
|
|
|
# check whether the provided (emoji_type, emoji_code) pair is
|
|
|
|
# valid in this realm. Since there's a row in the database, we
|
|
|
|
# know it was valid when the user added their reaction in the
|
|
|
|
# first place, so it is safe to just remove the reaction if it
|
|
|
|
# exists. And the (reaction_type, emoji_code) pair may no longer be
|
|
|
|
# valid in legitimate situations (e.g. if a realm emoji was
|
|
|
|
# deactivated by an administrator in the meantime).
|
|
|
|
do_remove_reaction(user_profile, message, emoji_code, reaction_type)
|
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|