Skip to content

Commit

Permalink
Change to event-level beforeRedaction event for efficiency
Browse files Browse the repository at this point in the history
To avoid an O(n^2) situation with every relations container trying to process
every redaction that may occur in a room, this switches to an event-level
notification, so that the specific relations container who cares can listen to
just the events it wants to know about.
  • Loading branch information
jryans committed May 13, 2019
1 parent f411d50 commit 3a20114
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ utils.extend(module.exports.MatrixEvent.prototype, {
throw new Error("invalid redaction_event in makeRedacted");
}

this.emit("Event.beforeRedaction", this, redaction_event);

// we attempt to replicate what we would see from the server if
// the event had been redacted before we saw it.
//
Expand Down
10 changes: 5 additions & 5 deletions src/models/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export default class Relations extends EventEmitter {
this._annotationsByKey = {};
this._annotationsBySender = {};
this._sortedAnnotationsByKey = [];

if (room) {
room.on("Room.beforeRedaction", this._onBeforeRedaction);
}
}

/**
Expand Down Expand Up @@ -78,6 +74,8 @@ export default class Relations extends EventEmitter {

this._relations.add(event);

event.on("Event.beforeRedaction", this._onBeforeRedaction);

this.emit("Relations.add", event);
}

Expand Down Expand Up @@ -127,7 +125,7 @@ export default class Relations extends EventEmitter {
* For relations that have been redacted, we want to remove them from
* aggregation data sets and emit an update event.
*
* To do so, we listen for `Room.beforeRedaction`, which happens:
* To do so, we listen for `Event.beforeRedaction`, which happens:
* - after the server accepted the redaction and remote echoed back to us
* - before the original event has been marked redacted in the client
*
Expand Down Expand Up @@ -162,6 +160,8 @@ export default class Relations extends EventEmitter {
});
}

redactedEvent.removeListener("Event.beforeRedaction", this._onBeforeRedaction);

// Dispatch a redaction event on this collection. `setTimeout` is used
// to wait until the next event loop iteration by which time the event
// has actually been marked as redacted.
Expand Down
1 change: 0 additions & 1 deletion src/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,6 @@ Room.prototype._addLiveEvent = function(event, duplicateStrategy) {
// if we know about this event, redact its contents now.
const redactedEvent = this.getUnfilteredTimelineSet().findEventById(redactId);
if (redactedEvent) {
this.emit("Room.beforeRedaction", redactedEvent, event, this);
redactedEvent.makeRedacted(event);
this.emit("Room.redaction", event, this);

Expand Down

0 comments on commit 3a20114

Please sign in to comment.