2016-12-25 00:44:26 +01:00
|
|
|
|
2016-11-03 18:49:00 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2016-12-25 00:44:26 +01:00
|
|
|
from typing import Text
|
2016-11-03 18:49:00 +01:00
|
|
|
|
|
|
|
from zerver.decorator import authenticated_json_post_view,\
|
|
|
|
has_request_variables, REQ, to_non_negative_int
|
2016-11-30 08:14:46 +01:00
|
|
|
from zerver.lib.actions import do_add_reaction, do_remove_reaction
|
2017-01-17 08:42:52 +01:00
|
|
|
from zerver.lib.emoji import check_valid_emoji
|
2016-11-03 18:49:00 +01:00
|
|
|
from zerver.lib.message import access_message
|
|
|
|
from zerver.lib.request import JsonableError
|
|
|
|
from zerver.lib.response import json_success
|
2017-03-23 04:15:32 +01:00
|
|
|
from zerver.models import Reaction, UserMessage, UserProfile
|
2016-11-03 18:49:00 +01:00
|
|
|
|
|
|
|
@has_request_variables
|
2016-12-04 10:50:32 +01:00
|
|
|
def add_reaction_backend(request, user_profile, message_id, emoji_name):
|
2016-12-25 00:44:26 +01:00
|
|
|
# type: (HttpRequest, UserProfile, int, Text) -> HttpResponse
|
2016-11-03 18:49:00 +01:00
|
|
|
|
|
|
|
# access_message will throw a JsonableError exception if the user
|
|
|
|
# cannot see the message (e.g. for messages to private streams).
|
2017-03-23 04:15:32 +01:00
|
|
|
message, user_message = access_message(user_profile, message_id)
|
2016-11-03 18:49:00 +01:00
|
|
|
|
2016-12-31 02:45:16 +01:00
|
|
|
check_valid_emoji(message.sender.realm, emoji_name)
|
2016-11-03 18:49:00 +01:00
|
|
|
|
|
|
|
# We could probably just make this check be a try/except for the
|
|
|
|
# IntegrityError from it already existing, but this is a bit cleaner.
|
|
|
|
if Reaction.objects.filter(user_profile=user_profile,
|
|
|
|
message=message,
|
|
|
|
emoji_name=emoji_name).exists():
|
|
|
|
raise JsonableError(_("Reaction already exists"))
|
|
|
|
|
2017-03-23 04:15:32 +01:00
|
|
|
if user_message is None:
|
|
|
|
# Users can see and react to messages sent to streams they
|
|
|
|
# were not a subscriber to; in order to receive events for
|
|
|
|
# those, we give the user a `historical` UserMessage objects
|
|
|
|
# for the message. This is the same trick we use for starring
|
|
|
|
# messages.
|
|
|
|
UserMessage.objects.create(user_profile=user_profile,
|
|
|
|
message=message,
|
|
|
|
flags=UserMessage.flags.historical | UserMessage.flags.read)
|
|
|
|
|
2016-11-03 18:49:00 +01:00
|
|
|
do_add_reaction(user_profile, message, emoji_name)
|
|
|
|
|
|
|
|
return json_success()
|
2016-11-30 08:14:46 +01:00
|
|
|
|
|
|
|
@has_request_variables
|
2016-12-04 10:50:32 +01:00
|
|
|
def remove_reaction_backend(request, user_profile, message_id, emoji_name):
|
2016-12-25 00:44:26 +01:00
|
|
|
# type: (HttpRequest, UserProfile, int, Text) -> HttpResponse
|
2016-11-30 08:14:46 +01:00
|
|
|
|
|
|
|
# access_message will throw a JsonableError exception if the user
|
|
|
|
# cannot see the message (e.g. for messages to private streams).
|
|
|
|
message = access_message(user_profile, message_id)[0]
|
|
|
|
|
|
|
|
# We could probably just make this check be a try/except for the
|
|
|
|
# IntegrityError from it already existing, but this is a bit cleaner.
|
|
|
|
if not Reaction.objects.filter(user_profile=user_profile,
|
|
|
|
message=message,
|
|
|
|
emoji_name=emoji_name).exists():
|
|
|
|
raise JsonableError(_("Reaction does not exist"))
|
|
|
|
|
|
|
|
do_remove_reaction(user_profile, message, emoji_name)
|
|
|
|
|
|
|
|
return json_success()
|