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.

drafts: Add an API endpoint for editing drafts.

Hemanth V. Alluri2d307c760e15
diff --git a/zerver/tests/test_drafts.py b/zerver/tests/test_drafts.py
index 43e960eee06..72381be1488 100644
--- a/zerver/tests/test_drafts.py
+++ b/zerver/tests/test_drafts.py
@@ -1,4 +1,5 @@
import time
+from copy import deepcopy
from typing import Any, Dict, List
import ujson
@@ -301,3 +302,97 @@ class DraftCreationTests(ZulipTestCase):
draft_dicts,
"Topic must not contain null bytes"
)
+
+class DraftEditTests(ZulipTestCase):
+ def test_edit_draft_successfully(self) -> None:
+ hamlet = self.example_user("hamlet")
+ visible_streams = self.get_streams(hamlet)
+ stream_a = self.get_stream_id(visible_streams[0])
+ stream_b = self.get_stream_id(visible_streams[1])
+
+ # Make sure that there are no drafts at the start of this test.
+ self.assertEqual(Draft.objects.count(), 0)
+
+ # Create a draft.
+ draft_dict = {
+ "type": "stream",
+ "to": [stream_a],
+ "topic": "drafts",
+ "content": "The API should be good",
+ "timestamp": 1595505700.85247
+ }
+ resp = self.api_post(hamlet, "/api/v1/drafts", {"drafts": ujson.dumps([draft_dict])})
+ self.assert_json_success(resp)
+ new_draft_id = ujson.loads(resp.content)["ids"][0]
+
+ # Change the draft data.
+ draft_dict["content"] = "The API needs to be structured yet simple to use."
+ draft_dict["to"] = [stream_b]
+ draft_dict["topic"] = "designing drafts"
+ draft_dict["timestamp"] = 1595505800.84923
+
+ # Update this change in the backend.
+ resp = self.api_patch(hamlet, f"/api/v1/drafts/{new_draft_id}",
+ {"draft": ujson.dumps(draft_dict)})
+ self.assert_json_success(resp)
+
+ # Now make sure that the change was made successfully.
+ new_draft = Draft.objects.get(id=new_draft_id, user_profile=hamlet)
+ new_draft_dict = new_draft.to_dict()
+ self.assertEqual(new_draft_dict, draft_dict)
+
+ def test_edit_non_existant_draft(self) -> None:
+ hamlet = self.example_user("hamlet")
+
+ # Make sure that no draft exists in the first place.
+ self.assertEqual(Draft.objects.count(), 0)
+
+ # Try to update a draft that doesn't exist.
+ draft_dict = {
+ "type": "stream",
+ "to": [10],
+ "topic": "drafts",
+ "content": "The API should be good",
+ "timestamp": 1595505700.85247
+ }
+ resp = self.api_patch(hamlet, "/api/v1/drafts/999999999",
+ {"draft": ujson.dumps(draft_dict)})
+ self.assert_json_error(resp, "Draft does not exist", status_code=404)
+
+ # Now make sure that no changes were made.
+ self.assertEqual(Draft.objects.count(), 0)
+
+ def test_edit_unowned_draft(self) -> None:
+ hamlet = self.example_user("hamlet")
+ visible_streams = self.get_streams(hamlet)
+ stream_id = self.get_stream_id(visible_streams[0])
+
+ # Make sure that there are no drafts at the start of this test.
+ self.assertEqual(Draft.objects.count(), 0)
+
+ # Create a draft.
+ draft_dict = {
+ "type": "stream",
+ "to": [stream_id],
+ "topic": "drafts",
+ "content": "The API should be good",
+ "timestamp": 1595505700.85247
+ }
+ resp = self.api_post(hamlet, "/api/v1/drafts", {"drafts": ujson.dumps([draft_dict])})
+ self.assert_json_success(resp)
+ new_draft_id = ujson.loads(resp.content)["ids"][0]
+
+ # Change the draft data.
+ modified_draft_dict = deepcopy(draft_dict)
+ modified_draft_dict["content"] = "???"
+
+ # Update this change in the backend as a different user.
+ zoe = self.example_user("ZOE")
+ resp = self.api_patch(zoe, f"/api/v1/drafts/{new_draft_id}",
+ {"draft": ujson.dumps(draft_dict)})
+ self.assert_json_error(resp, "Draft does not exist", status_code=404)
+
+ # Now make sure that no changes were made.
+ existing_draft = Draft.objects.get(id=new_draft_id, user_profile=hamlet)
+ existing_draft_dict = existing_draft.to_dict()
+ self.assertEqual(existing_draft_dict, draft_dict)
diff --git a/zerver/views/drafts.py b/zerver/views/drafts.py
index b63ddc310a8..972cdc70a0b 100644
--- a/zerver/views/drafts.py
+++ b/zerver/views/drafts.py
@@ -10,7 +10,7 @@ from zerver.lib.addressee import get_user_profiles_by_ids
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import truncate_body, truncate_topic
from zerver.lib.request import REQ, has_request_variables
-from zerver.lib.response import json_success
+from zerver.lib.response import json_error, json_success
from zerver.lib.streams import access_stream_by_id
from zerver.lib.timestamp import timestamp_to_datetime
from zerver.lib.validator import (
@@ -103,3 +103,21 @@ def create_drafts(request: HttpRequest, user_profile: UserProfile,
created_draft_objects = Draft.objects.bulk_create(draft_objects)
draft_ids = [draft_object.id for draft_object in created_draft_objects]
return json_success({"ids": draft_ids})
+
+@has_request_variables
+def edit_draft(request: HttpRequest, user_profile: UserProfile, draft_id: int,
+ draft_dict: Dict[str, Any]=REQ("draft", validator=draft_dict_validator),
+ ) -> HttpResponse:
+ try:
+ draft_object = Draft.objects.get(id=draft_id, user_profile=user_profile)
+ except Draft.DoesNotExist:
+ return json_error(_("Draft does not exist"), status=404)
+
+ valid_draft_dict = further_validated_draft_dict(draft_dict, user_profile)
+ draft_object.content = valid_draft_dict["content"]
+ draft_object.topic = valid_draft_dict["topic"]
+ draft_object.recipient = valid_draft_dict["recipient"]
+ draft_object.last_edit_time = valid_draft_dict["last_edit_time"]
+ draft_object.save()
+
+ return json_success()
diff --git a/zproject/urls.py b/zproject/urls.py
index 1b10df736bf..9cfe71ad3d5 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -197,6 +197,9 @@ v1_api_and_json_patterns = [
path('drafts', rest_dispatch,
{'POST': ('zerver.views.drafts.create_drafts',
{'intentionally_undocumented'})}),
+ path('drafts/<int:draft_id>', rest_dispatch,
+ {'PATCH': ('zerver.views.drafts.edit_draft',
+ {'intentionally_undocumented'})}),
# messages -> zerver.views.message*
# GET returns messages, possibly filtered, POST sends a message