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.

attachment events: Use AttachmentAdd/Update/Remove pydantic types.

Evy Kassirer0f53a9c71514
diff --git a/zerver/actions/uploads.py b/zerver/actions/uploads.py
index a4703d6b9c9..fa7facd719b 100644
--- a/zerver/actions/uploads.py
+++ b/zerver/actions/uploads.py
@@ -5,6 +5,8 @@ from typing import Any
from django.db import transaction
from zerver.lib.attachments import get_old_unclaimed_attachments, validate_attachment_request
+from zerver.lib.event_types import Attachment as AttachmentData
+from zerver.lib.event_types import AttachmentUpdateEvent
from zerver.lib.markdown import MessageRenderingResult
from zerver.lib.upload import claim_attachment, delete_message_attachments
from zerver.models import (
@@ -14,7 +16,6 @@ from zerver.models import (
Message,
ScheduledMessage,
Stream,
- UserProfile,
)
from zerver.tornado.django_api import send_event_on_commit
@@ -25,18 +26,6 @@ class AttachmentChangeResult:
detached_attachments: list[dict[str, Any]]
-def notify_attachment_update(
- user_profile: UserProfile, op: str, attachment_dict: dict[str, Any]
-) -> None:
- event = {
- "type": "attachment",
- "op": op,
- "attachment": attachment_dict,
- "upload_space_used": user_profile.realm.currently_used_upload_space_bytes(),
- }
- send_event_on_commit(user_profile.realm, event, [user_profile.id])
-
-
def do_claim_attachments(
message: Message | ScheduledMessage, potential_path_ids: list[str]
) -> bool:
@@ -83,7 +72,11 @@ def do_claim_attachments(
if not isinstance(message, ScheduledMessage):
# attachment update events don't say anything about scheduled messages,
# so sending an event is pointless.
- notify_attachment_update(user_profile, "update", attachment.to_dict())
+ event = AttachmentUpdateEvent(
+ attachment=AttachmentData(**attachment.to_dict()),
+ upload_space_used=user_profile.realm.currently_used_upload_space_bytes(),
+ )
+ send_event_on_commit(user_profile.realm, event, [user_profile.id])
return claimed
diff --git a/zerver/lib/event_types.py b/zerver/lib/event_types.py
index 118811cf65c..b4feb36fbd9 100644
--- a/zerver/lib/event_types.py
+++ b/zerver/lib/event_types.py
@@ -40,8 +40,8 @@ class Attachment(BaseModel):
class AttachmentAddEvent(BaseEvent):
- type: Literal["attachment"]
- op: Literal["add"]
+ type: Literal["attachment"] = "attachment"
+ op: Literal["add"] = "add"
attachment: Attachment
upload_space_used: int
@@ -51,15 +51,15 @@ class AttachmentFieldForAttachmentRemoveEvent(BaseModel):
class AttachmentRemoveEvent(BaseEvent):
- type: Literal["attachment"]
- op: Literal["remove"]
+ type: Literal["attachment"] = "attachment"
+ op: Literal["remove"] = "remove"
attachment: AttachmentFieldForAttachmentRemoveEvent
upload_space_used: int
class AttachmentUpdateEvent(BaseEvent):
- type: Literal["attachment"]
- op: Literal["update"]
+ type: Literal["attachment"] = "attachment"
+ op: Literal["update"] = "update"
attachment: Attachment
upload_space_used: int
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.py
index 61c38b684be..a1eb5939adc 100644
--- a/zerver/lib/upload/__init__.py
+++ b/zerver/lib/upload/__init__.py
@@ -150,9 +150,15 @@ def create_attachment(
content_type=content_type,
)
maybe_thumbnail(file_vips_data, content_type, path_id, realm.id)
- from zerver.actions.uploads import notify_attachment_update
+ from zerver.lib.event_types import Attachment as AttachmentData
+ from zerver.lib.event_types import AttachmentAddEvent
+ from zerver.tornado.django_api import send_event_on_commit
- notify_attachment_update(user_profile, "add", attachment.to_dict())
+ event = AttachmentAddEvent(
+ attachment=AttachmentData(**attachment.to_dict()),
+ upload_space_used=user_profile.realm.currently_used_upload_space_bytes(),
+ )
+ send_event_on_commit(user_profile.realm, event, [user_profile.id])
def get_file_info(user_file: UploadedFile[bytes]) -> tuple[str, str]:
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 13429a913bd..b4bc9fb56fd 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -2338,8 +2338,8 @@ class UploadSpaceTests(UploadSerializeMixin, ZulipTestCase):
data = b"zulip!"
upload_message_attachment("dummy.txt", "text/plain", data, self.user_profile)
- # notify_attachment_update function calls currently_used_upload_space_bytes which
- # updates the cache.
+ # Sending the attachment event calls currently_used_upload_space_bytes,
+ # which updates the cache.
self.assert_length(data, cache_get(get_realm_used_upload_space_cache_key(self.realm.id))[0])
self.assert_length(data, self.realm.currently_used_upload_space_bytes())
diff --git a/zerver/views/attachments.py b/zerver/views/attachments.py
index 2997888f611..b7bc9061492 100644
--- a/zerver/views/attachments.py
+++ b/zerver/views/attachments.py
@@ -1,10 +1,11 @@
from django.db import transaction
from django.http import HttpRequest, HttpResponse
-from zerver.actions.uploads import notify_attachment_update
from zerver.lib.attachments import access_attachment_by_id, remove_attachment, user_attachments
+from zerver.lib.event_types import AttachmentFieldForAttachmentRemoveEvent, AttachmentRemoveEvent
from zerver.lib.response import json_success
from zerver.models import UserProfile
+from zerver.tornado.django_api import send_event_on_commit
def list_by_user(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
@@ -21,5 +22,9 @@ def list_by_user(request: HttpRequest, user_profile: UserProfile) -> HttpRespons
def remove(request: HttpRequest, user_profile: UserProfile, attachment_id: int) -> HttpResponse:
attachment = access_attachment_by_id(user_profile, attachment_id, needs_owner=True)
remove_attachment(user_profile, attachment)
- notify_attachment_update(user_profile, "remove", {"id": attachment_id})
+ event = AttachmentRemoveEvent(
+ attachment=AttachmentFieldForAttachmentRemoveEvent(id=attachment_id),
+ upload_space_used=user_profile.realm.currently_used_upload_space_bytes(),
+ )
+ send_event_on_commit(user_profile.realm, event, [user_profile.id])
return json_success(request)