1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-03 08:42:03 +03:00

Handle redactions (both live and historic).

This commit is contained in:
Kegan Dougal
2015-07-21 17:14:11 +01:00
parent 6653d294d9
commit 90e778cb84
2 changed files with 24 additions and 2 deletions

View File

@@ -35,6 +35,7 @@ function Room(roomId, storageToken) {
this.currentState = new RoomState(roomId); this.currentState = new RoomState(roomId);
this.summary = null; this.summary = null;
this.storageToken = storageToken; this.storageToken = storageToken;
this._redactions = [];
} }
utils.inherits(Room, EventEmitter); utils.inherits(Room, EventEmitter);
@@ -94,6 +95,10 @@ utils.inherits(Room, EventEmitter);
Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline) { Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline) {
var stateContext = toStartOfTimeline ? this.oldState : this.currentState; var stateContext = toStartOfTimeline ? this.oldState : this.currentState;
for (var i = 0; i < events.length; i++) { for (var i = 0; i < events.length; i++) {
if (toStartOfTimeline && this._redactions.indexOf(events[i].getId()) >= 0) {
continue; // do not add the redacted event.
}
setEventMetadata(events[i], stateContext, toStartOfTimeline); setEventMetadata(events[i], stateContext, toStartOfTimeline);
// modify state // modify state
if (events[i].isState()) { if (events[i].isState()) {
@@ -105,6 +110,20 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline) {
setEventMetadata(events[i], stateContext, toStartOfTimeline); setEventMetadata(events[i], stateContext, toStartOfTimeline);
} }
} }
if (events[i].getType() === "m.room.redaction") {
// try to remove the element
var removed = utils.removeElement(this.timeline, function(e) {
return e.getId() === events[i].event.redacts
}, true);
if (!removed && toStartOfTimeline) {
// redactions will trickle in BEFORE the event redacted so make
// a note of the redacted event; we'll check it later.
this._redactions.push(events[i].event.redacts);
}
// NB: We continue to add the redaction event to the timeline so clients
// can say "so and so redacted an event" if they wish to.
}
// TODO: pass through filter to see if this should be added to the timeline. // TODO: pass through filter to see if this should be added to the timeline.
if (toStartOfTimeline) { if (toStartOfTimeline) {
this.timeline.unshift(events[i]); this.timeline.unshift(events[i]);

View File

@@ -148,6 +148,7 @@ module.exports.findElement = function(array, fn, reverse) {
* function signature <code>fn(element, index, array)</code>. Return true to * function signature <code>fn(element, index, array)</code>. Return true to
* remove this element and break. * remove this element and break.
* @param {boolean} reverse True to search in reverse order. * @param {boolean} reverse True to search in reverse order.
* @return True if an element was removed.
*/ */
module.exports.removeElement = function(array, fn, reverse) { module.exports.removeElement = function(array, fn, reverse) {
var i; var i;
@@ -155,17 +156,19 @@ module.exports.removeElement = function(array, fn, reverse) {
for (i = array.length - 1; i >= 0; i--) { for (i = array.length - 1; i >= 0; i--) {
if (fn(array[i], i, array)) { if (fn(array[i], i, array)) {
array.splice(i, 1); array.splice(i, 1);
return; } return true;
}
} }
} }
else { else {
for (i = 0; i < array.length; i++) { for (i = 0; i < array.length; i++) {
if (fn(array[i], i, array)) { if (fn(array[i], i, array)) {
array.splice(i, 1); array.splice(i, 1);
return; return true;
} }
} }
} }
return false;
}; };
/** /**