2021-07-24 06:54:31 +02:00
|
|
|
import time
|
|
|
|
from functools import wraps
|
2023-08-17 02:34:42 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Literal, Union
|
2021-07-24 06:54:31 +02:00
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2022-01-13 22:59:43 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2023-08-17 02:34:42 +02:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from typing_extensions import Annotated, Concatenate, ParamSpec
|
2021-07-24 06:54:31 +02:00
|
|
|
|
|
|
|
from zerver.lib.addressee import get_user_profiles_by_ids
|
|
|
|
from zerver.lib.exceptions import JsonableError, ResourceNotFoundError
|
|
|
|
from zerver.lib.message import normalize_body, truncate_topic
|
2022-04-14 23:29:05 +02:00
|
|
|
from zerver.lib.recipient_users import recipient_for_user_profiles
|
2021-07-24 06:54:31 +02:00
|
|
|
from zerver.lib.streams import access_stream_by_id
|
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2023-08-17 02:34:42 +02:00
|
|
|
from zerver.lib.typed_endpoint import RequiredStringConstraint
|
2021-07-24 06:54:31 +02:00
|
|
|
from zerver.models import Draft, UserProfile
|
2021-07-24 06:56:56 +02:00
|
|
|
from zerver.tornado.django_api import send_event
|
2021-07-24 06:54:31 +02:00
|
|
|
|
2022-08-21 04:03:39 +02:00
|
|
|
ParamT = ParamSpec("ParamT")
|
2023-08-17 02:34:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DraftData(BaseModel):
|
|
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
|
|
|
|
type: Literal["private", "stream", ""]
|
|
|
|
to: List[int]
|
|
|
|
topic: str
|
|
|
|
content: Annotated[str, RequiredStringConstraint()]
|
|
|
|
timestamp: Union[int, float, None] = None
|
2021-07-24 06:54:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def further_validated_draft_dict(
|
2023-08-17 02:34:42 +02:00
|
|
|
draft_dict: DraftData, user_profile: UserProfile
|
2021-07-24 06:54:31 +02:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""Take a draft_dict that was already validated by draft_dict_validator then
|
|
|
|
further sanitize, validate, and transform it. Ultimately return this "further
|
|
|
|
validated" draft dict. It will have a slightly different set of keys the values
|
|
|
|
for which can be used to directly create a Draft object."""
|
|
|
|
|
2023-08-17 02:34:42 +02:00
|
|
|
content = normalize_body(draft_dict.content)
|
2021-07-24 06:54:31 +02:00
|
|
|
|
2023-08-17 02:34:42 +02:00
|
|
|
timestamp = draft_dict.timestamp
|
|
|
|
if timestamp is None:
|
|
|
|
timestamp = time.time()
|
2021-07-24 06:54:31 +02:00
|
|
|
timestamp = round(timestamp, 6)
|
|
|
|
if timestamp < 0:
|
|
|
|
# While it's not exactly an invalid timestamp, it's not something
|
|
|
|
# we want to allow either.
|
|
|
|
raise JsonableError(_("Timestamp must not be negative."))
|
|
|
|
last_edit_time = timestamp_to_datetime(timestamp)
|
|
|
|
|
|
|
|
topic = ""
|
2023-07-21 12:35:07 +02:00
|
|
|
recipient_id = None
|
2023-08-17 02:34:42 +02:00
|
|
|
to = draft_dict.to
|
|
|
|
if draft_dict.type == "stream":
|
|
|
|
topic = truncate_topic(draft_dict.topic)
|
2021-07-24 06:54:31 +02:00
|
|
|
if "\0" in topic:
|
|
|
|
raise JsonableError(_("Topic must not contain null bytes"))
|
|
|
|
if len(to) != 1:
|
|
|
|
raise JsonableError(_("Must specify exactly 1 stream ID for stream messages"))
|
|
|
|
stream, sub = access_stream_by_id(user_profile, to[0])
|
2023-07-21 12:35:07 +02:00
|
|
|
recipient_id = stream.recipient_id
|
2023-08-17 02:34:42 +02:00
|
|
|
elif draft_dict.type == "private" and len(to) != 0:
|
2021-07-24 06:54:31 +02:00
|
|
|
to_users = get_user_profiles_by_ids(set(to), user_profile.realm)
|
|
|
|
try:
|
2023-07-21 12:35:07 +02:00
|
|
|
recipient_id = recipient_for_user_profiles(to_users, False, None, user_profile).id
|
2021-07-24 06:54:31 +02:00
|
|
|
except ValidationError as e: # nocoverage
|
|
|
|
raise JsonableError(e.messages[0])
|
|
|
|
|
|
|
|
return {
|
2023-07-21 12:35:07 +02:00
|
|
|
"recipient_id": recipient_id,
|
2021-07-24 06:54:31 +02:00
|
|
|
"topic": topic,
|
|
|
|
"content": content,
|
|
|
|
"last_edit_time": last_edit_time,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-21 04:03:39 +02:00
|
|
|
def draft_endpoint(
|
|
|
|
view_func: Callable[Concatenate[HttpRequest, UserProfile, ParamT], HttpResponse]
|
|
|
|
) -> Callable[Concatenate[HttpRequest, UserProfile, ParamT], HttpResponse]:
|
2021-07-24 06:54:31 +02:00
|
|
|
@wraps(view_func)
|
|
|
|
def draft_view_func(
|
2022-08-21 04:03:39 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
/,
|
|
|
|
*args: ParamT.args,
|
|
|
|
**kwargs: ParamT.kwargs,
|
2021-07-24 06:54:31 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
if not user_profile.enable_drafts_synchronization:
|
|
|
|
raise JsonableError(_("User has disabled synchronizing drafts."))
|
|
|
|
return view_func(request, user_profile, *args, **kwargs)
|
|
|
|
|
2022-08-21 04:03:39 +02:00
|
|
|
return draft_view_func
|
2021-07-24 06:54:31 +02:00
|
|
|
|
|
|
|
|
2023-08-17 02:34:42 +02:00
|
|
|
def do_create_drafts(drafts: List[DraftData], user_profile: UserProfile) -> List[Draft]:
|
2021-07-24 06:54:31 +02:00
|
|
|
"""Create drafts in bulk for a given user based on the draft dicts. Since
|
|
|
|
currently, the only place this method is being used (apart from tests) is from
|
|
|
|
the create_draft view, we assume that the drafts_dicts are syntactically valid
|
|
|
|
(i.e. they satisfy the draft_dict_validator)."""
|
|
|
|
draft_objects = []
|
2023-08-17 02:34:42 +02:00
|
|
|
for draft in drafts:
|
|
|
|
valid_draft_dict = further_validated_draft_dict(draft, user_profile)
|
2021-07-24 06:54:31 +02:00
|
|
|
draft_objects.append(
|
|
|
|
Draft(
|
|
|
|
user_profile=user_profile,
|
2023-07-21 12:35:07 +02:00
|
|
|
recipient_id=valid_draft_dict["recipient_id"],
|
2021-07-24 06:54:31 +02:00
|
|
|
topic=valid_draft_dict["topic"],
|
|
|
|
content=valid_draft_dict["content"],
|
|
|
|
last_edit_time=valid_draft_dict["last_edit_time"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
created_draft_objects = Draft.objects.bulk_create(draft_objects)
|
2021-07-24 06:56:56 +02:00
|
|
|
|
|
|
|
event = {
|
|
|
|
"type": "drafts",
|
|
|
|
"op": "add",
|
|
|
|
"drafts": [draft.to_dict() for draft in created_draft_objects],
|
|
|
|
}
|
|
|
|
send_event(user_profile.realm, event, [user_profile.id])
|
|
|
|
|
2021-07-24 06:54:31 +02:00
|
|
|
return created_draft_objects
|
|
|
|
|
|
|
|
|
2023-08-17 02:34:42 +02:00
|
|
|
def do_edit_draft(draft_id: int, draft: DraftData, user_profile: UserProfile) -> None:
|
2021-07-24 06:54:31 +02:00
|
|
|
"""Edit/update a single draft for a given user. Since the only place this method is being
|
|
|
|
used from (apart from tests) is the edit_draft view, we assume that the drafts_dict is
|
|
|
|
syntactically valid (i.e. it satisfies the draft_dict_validator)."""
|
|
|
|
try:
|
|
|
|
draft_object = Draft.objects.get(id=draft_id, user_profile=user_profile)
|
|
|
|
except Draft.DoesNotExist:
|
|
|
|
raise ResourceNotFoundError(_("Draft does not exist"))
|
2023-08-17 02:34:42 +02:00
|
|
|
valid_draft_dict = further_validated_draft_dict(draft, user_profile)
|
2021-07-24 06:54:31 +02:00
|
|
|
draft_object.content = valid_draft_dict["content"]
|
|
|
|
draft_object.topic = valid_draft_dict["topic"]
|
2023-07-21 12:35:07 +02:00
|
|
|
draft_object.recipient_id = valid_draft_dict["recipient_id"]
|
2021-07-24 06:54:31 +02:00
|
|
|
draft_object.last_edit_time = valid_draft_dict["last_edit_time"]
|
|
|
|
draft_object.save()
|
|
|
|
|
2021-07-24 06:56:56 +02:00
|
|
|
event = {"type": "drafts", "op": "update", "draft": draft_object.to_dict()}
|
|
|
|
send_event(user_profile.realm, event, [user_profile.id])
|
|
|
|
|
2021-07-24 06:54:31 +02:00
|
|
|
|
|
|
|
def do_delete_draft(draft_id: int, user_profile: UserProfile) -> None:
|
|
|
|
"""Delete a draft belonging to a particular user."""
|
|
|
|
try:
|
|
|
|
draft_object = Draft.objects.get(id=draft_id, user_profile=user_profile)
|
|
|
|
except Draft.DoesNotExist:
|
|
|
|
raise ResourceNotFoundError(_("Draft does not exist"))
|
2021-07-24 06:56:56 +02:00
|
|
|
|
|
|
|
draft_id = draft_object.id
|
2021-07-24 06:54:31 +02:00
|
|
|
draft_object.delete()
|
2021-07-24 06:56:56 +02:00
|
|
|
|
|
|
|
event = {"type": "drafts", "op": "remove", "draft_id": draft_id}
|
|
|
|
send_event(user_profile.realm, event, [user_profile.id])
|