summary refs log tree commit diff
path: root/synapse/events
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-05-14 16:59:21 +0100
committerErik Johnston <erik@matrix.org>2019-05-16 16:54:45 +0100
commitd46aab3fa8bc02d8db9616490e849b9443fed8e7 (patch)
treeb52676b1f7b2739e90e65d6c1b2172c74d6fa735 /synapse/events
parentMerge pull request #5192 from matrix-org/erikj/relations_aggregations (diff)
downloadsynapse-d46aab3fa8bc02d8db9616490e849b9443fed8e7.tar.xz
Add basic editing support
Diffstat (limited to 'synapse/events')
-rw-r--r--synapse/events/utils.py30
1 files changed, 26 insertions, 4 deletions
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index 16d0c64372..2019ce9b1c 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -346,7 +346,7 @@ class EventClientSerializer(object):
             defer.returnValue(event)
 
         event_id = event.event_id
-        event = serialize_event(event, time_now, **kwargs)
+        serialized_event = serialize_event(event, time_now, **kwargs)
 
         # If MSC1849 is enabled then we need to look if thre are any relations
         # we need to bundle in with the event
@@ -359,14 +359,36 @@ class EventClientSerializer(object):
             )
 
             if annotations.chunk:
-                r = event["unsigned"].setdefault("m.relations", {})
+                r = serialized_event["unsigned"].setdefault("m.relations", {})
                 r[RelationTypes.ANNOTATION] = annotations.to_dict()
 
             if references.chunk:
-                r = event["unsigned"].setdefault("m.relations", {})
+                r = serialized_event["unsigned"].setdefault("m.relations", {})
                 r[RelationTypes.REFERENCES] = references.to_dict()
 
-        defer.returnValue(event)
+            edit = None
+            if event.type == EventTypes.Message:
+                edit = yield self.store.get_applicable_edit(
+                    event.event_id, event.type, event.sender,
+                )
+
+            if edit:
+                # If there is an edit replace the content, preserving existing
+                # relations.
+
+                relations = event.content.get("m.relates_to")
+                serialized_event["content"] = edit.content.get("m.new_content", {})
+                if relations:
+                    serialized_event["content"]["m.relates_to"] = relations
+                else:
+                    serialized_event["content"].pop("m.relates_to", None)
+
+                r = serialized_event["unsigned"].setdefault("m.relations", {})
+                r[RelationTypes.REPLACES] = {
+                    "event_id": edit.event_id,
+                }
+
+        defer.returnValue(serialized_event)
 
     def serialize_events(self, events, time_now, **kwargs):
         """Serializes multiple events.