summary refs log tree commit diff
diff options
context:
space:
mode:
authorH. Shay <hillerys@element.io>2022-08-31 15:30:05 -0700
committerH. Shay <hillerys@element.io>2022-09-19 10:11:14 -0700
commitb80daa3b2b8201a72755646818d720efa222c6ae (patch)
tree515456cd105764b807d10fedea48bc0fb9c5a344
parentfix test to align with new behaviour (diff)
downloadsynapse-b80daa3b2b8201a72755646818d720efa222c6ae.tar.xz
reduce duplicated code
-rw-r--r--synapse/handlers/message.py131
-rw-r--r--synapse/handlers/room.py7
2 files changed, 33 insertions, 105 deletions
diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py
index 3b0d19a9a0..959413eec4 100644
--- a/synapse/handlers/message.py
+++ b/synapse/handlers/message.py
@@ -554,98 +554,6 @@ class EventCreationHandler:
                 expiry_ms=30 * 60 * 1000,
             )
 
-    async def create_event_for_batch(
-        self,
-        requester: Requester,
-        event_dict: dict,
-        prev_event_ids: List[str],
-        txn_id: Optional[str] = None,
-        auth_event_ids: Optional[List[str]] = None,
-        require_consent: bool = True,
-        outlier: bool = False,
-        historical: bool = False,
-        depth: Optional[int] = None,
-    ) -> EventBase:
-        """
-        Create an event for batch persisting. Notably skips computing event context.
-        """
-        await self.auth_blocking.check_auth_blocking(requester=requester)
-
-        if event_dict["type"] == EventTypes.Create and event_dict["state_key"] == "":
-            room_version_id = event_dict["content"]["room_version"]
-            maybe_room_version_obj = KNOWN_ROOM_VERSIONS.get(room_version_id)
-            if not maybe_room_version_obj:
-                # this can happen if support is withdrawn for a room version
-                raise UnsupportedRoomVersionError(room_version_id)
-            room_version_obj = maybe_room_version_obj
-        else:
-            try:
-                room_version_obj = await self.store.get_room_version(
-                    event_dict["room_id"]
-                )
-            except NotFoundError:
-                raise AuthError(403, "Unknown room")
-
-        builder = self.event_builder_factory.for_room_version(
-            room_version_obj, event_dict
-        )
-
-        self.validator.validate_builder(builder)
-
-        if builder.type == EventTypes.Member:
-            membership = builder.content.get("membership", None)
-            target = UserID.from_string(builder.state_key)
-
-            if membership in self.membership_types_to_include_profile_data_in:
-                # If event doesn't include a display name, add one.
-                profile = self.profile_handler
-                content = builder.content
-
-                try:
-                    if "displayname" not in content:
-                        displayname = await profile.get_displayname(target)
-                        if displayname is not None:
-                            content["displayname"] = displayname
-                    if "avatar_url" not in content:
-                        avatar_url = await profile.get_avatar_url(target)
-                        if avatar_url is not None:
-                            content["avatar_url"] = avatar_url
-                except Exception as e:
-                    logger.info(
-                        "Failed to get profile information for %r: %s", target, e
-                    )
-
-        is_exempt = await self._is_exempt_from_privacy_policy(builder, requester)
-        if require_consent and not is_exempt:
-            await self.assert_accepted_privacy_policy(requester)
-
-        if requester.access_token_id is not None:
-            builder.internal_metadata.token_id = requester.access_token_id
-
-        if txn_id is not None:
-            builder.internal_metadata.txn_id = txn_id
-
-        builder.internal_metadata.outlier = outlier
-
-        builder.internal_metadata.historical = historical
-
-        event = await builder.build(
-            prev_event_ids=prev_event_ids,
-            auth_event_ids=auth_event_ids,
-            depth=depth,
-        )
-
-        # Pass on the outlier property from the builder to the event
-        # after it is created
-        if builder.internal_metadata.outlier:
-            event.internal_metadata.outlier = True
-
-        self.validator.validate_new(event, self.config)
-        await self._validate_event_relation(event)
-        logger.debug("Created event %s", event.event_id)
-
-        return event
-
     async def create_event(
         self,
         requester: Requester,
@@ -659,6 +567,7 @@ class EventCreationHandler:
         outlier: bool = False,
         historical: bool = False,
         depth: Optional[int] = None,
+        for_batch: bool = False,
     ) -> Tuple[EventBase, EventContext]:
         """
         Given a dict from a client, create a new event.
@@ -710,6 +619,8 @@ class EventCreationHandler:
             depth: Override the depth used to order the event in the DAG.
                 Should normally be set to None, which will cause the depth to be calculated
                 based on the prev_events.
+            for_batch: Whether this event is being created for batch sending. Notably events
+                created for batch sending do not have their event context computed
 
         Raises:
             ResourceLimitError if server is blocked to some resource being
@@ -777,15 +688,27 @@ class EventCreationHandler:
 
         builder.internal_metadata.historical = historical
 
-        event, context = await self.create_new_client_event(
-            builder=builder,
-            requester=requester,
-            allow_no_prev_events=allow_no_prev_events,
-            prev_event_ids=prev_event_ids,
-            auth_event_ids=auth_event_ids,
-            state_event_ids=state_event_ids,
-            depth=depth,
-        )
+        if for_batch:
+            event = await builder.build(
+                prev_event_ids=prev_event_ids,
+                auth_event_ids=auth_event_ids,
+                depth=depth,
+            )
+            # Pass on the outlier property from the builder to the event
+            # after it is created
+            if builder.internal_metadata.outlier:
+                event.internal_metadata.outlier = True
+
+        else:
+            event, context = await self.create_new_client_event(
+                builder=builder,
+                requester=requester,
+                allow_no_prev_events=allow_no_prev_events,
+                prev_event_ids=prev_event_ids,
+                auth_event_ids=auth_event_ids,
+                state_event_ids=state_event_ids,
+                depth=depth,
+            )
 
         # In an ideal world we wouldn't need the second part of this condition. However,
         # this behaviour isn't spec'd yet, meaning we should be able to deactivate this
@@ -824,7 +747,11 @@ class EventCreationHandler:
                 )
 
         self.validator.validate_new(event, self.config)
-        return event, context
+
+        if for_batch:
+            return event
+        else:
+            return event, context
 
     async def _is_exempt_from_privacy_policy(
         self, builder: EventBuilder, requester: Requester
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index eef6b0c65f..740937e739 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -1080,12 +1080,13 @@ class RoomCreationHandler:
 
             event_dict = create_event_dict(etype, content, **kwargs)
 
-            event = await self.event_creation_handler.create_event_for_batch(
+            event = await self.event_creation_handler.create_event(
                 creator,
                 event_dict,
-                prev_event,
-                depth=depth,
+                prev_event_ids=prev_event,
                 auth_event_ids=auth_event_ids,
+                depth=depth,
+                for_batch=True,
             )
             depth += 1
             prev_event = [event.event_id]