Skip to content

Commit

Permalink
Handle edits which are bundled with an event, per MSC3925 (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh authored Jan 12, 2023
1 parent de176db commit 9e37980
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/event-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import { MatrixClient } from "./client";
import { IEvent, MatrixEvent, MatrixEventEvent } from "./models/event";
import { RelationType } from "./@types/event";

export type EventMapper = (obj: Partial<IEvent>) => MatrixEvent;

Expand Down Expand Up @@ -55,6 +56,19 @@ export function eventMapperFor(client: MatrixClient, options: MapperOpts): Event
preventReEmit = true;
}

// if there is a complete edit bundled alongside the event, perform the replacement.
// (prior to MSC3925, events were automatically replaced on the server-side. MSC3925 proposes that that doesn't
// happen automatically but the server does provide us with the whole content of the edit event.)
const bundledEdit = event.getServerAggregatedRelation<Partial<IEvent>>(RelationType.Replace);
if (bundledEdit?.content) {
const replacement = mapper(bundledEdit);
// XXX: it's worth noting that the spec says we should only respect encrypted edits if, once decrypted, the
// replacement has a `m.new_content` property. The problem is that we haven't yet decrypted the replacement
// (it should be happening in the background), so we can't enforce this. Possibly we should for decryption
// to complete, but that sounds a bit racy. For now, we just assume it's ok.
event.makeReplaced(replacement);
}

const thread = room?.findThreadForEvent(event);
if (thread) {
event.setThread(thread);
Expand Down

0 comments on commit 9e37980

Please sign in to comment.