Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

reactions: Add error code for duplicate addition/removal.

Alex Vandiver871a668dd25d
diff --git a/version.py b/version.py
index 5b4f60e63c6..7b47b9c9723 100644
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# Changes should be accompanied by documentation explaining what the
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 192
+API_FEATURE_LEVEL = 193
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/actions/reactions.py b/zerver/actions/reactions.py
index e8b378966c4..2247c3b13c4 100644
--- a/zerver/actions/reactions.py
+++ b/zerver/actions/reactions.py
@@ -1,10 +1,8 @@
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, get_emoji_data
-from zerver.lib.exceptions import JsonableError
+from zerver.lib.exceptions import ReactionExistsError
from zerver.lib.message import access_message, update_to_dict_cache
from zerver.lib.stream_subscription import subscriber_ids_with_stream_history_access
from zerver.models import Message, Reaction, Recipient, Stream, UserMessage, UserProfile
@@ -115,7 +113,7 @@ def check_add_reaction(
emoji_code=emoji_code,
reaction_type=reaction_type,
).exists():
- raise JsonableError(_("Reaction already exists."))
+ raise ReactionExistsError
query = Reaction.objects.filter(
message=message, emoji_code=emoji_code, reaction_type=reaction_type
diff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.py
index 51d57f557bc..f736052fbcb 100644
--- a/zerver/lib/exceptions.py
+++ b/zerver/lib/exceptions.py
@@ -40,6 +40,8 @@ class ErrorCode(Enum):
UNAUTHORIZED = auto()
REQUEST_TIMEOUT = auto()
MOVE_MESSAGES_TIME_LIMIT_EXCEEDED = auto()
+ REACTION_ALREADY_EXISTS = auto()
+ REACTION_DOES_NOT_EXIST = auto()
class JsonableError(Exception):
@@ -503,3 +505,25 @@ class MessageMoveError(JsonableError):
return _(
"You only have permission to move the {total_messages_allowed_to_move}/{total_messages_in_topic} most recent messages in this topic."
)
+
+
+class ReactionExistsError(JsonableError):
+ code = ErrorCode.REACTION_ALREADY_EXISTS
+
+ def __init__(self) -> None:
+ pass
+
+ @staticmethod
+ def msg_format() -> str:
+ return _("Reaction already exists.")
+
+
+class ReactionDoesNotExistError(JsonableError):
+ code = ErrorCode.REACTION_DOES_NOT_EXIST
+
+ def __init__(self) -> None:
+ pass
+
+ @staticmethod
+ def msg_format() -> str:
+ return _("Reaction doesn't exist.")
diff --git a/zerver/views/reactions.py b/zerver/views/reactions.py
index 1df333c8bde..316d82eb0d5 100644
--- a/zerver/views/reactions.py
+++ b/zerver/views/reactions.py
@@ -6,7 +6,7 @@ from django.utils.translation import gettext as _
from zerver.actions.reactions import check_add_reaction, do_remove_reaction
from zerver.lib.emoji import get_emoji_data
-from zerver.lib.exceptions import JsonableError
+from zerver.lib.exceptions import JsonableError, ReactionDoesNotExistError
from zerver.lib.message import access_message
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
@@ -65,7 +65,7 @@ def remove_reaction(
emoji_code=emoji_code,
reaction_type=reaction_type,
).exists():
- raise JsonableError(_("Reaction doesn't exist."))
+ raise ReactionDoesNotExistError
# Unlike adding reactions, while deleting a reaction, we don't
# check whether the provided (emoji_type, emoji_code) pair is