1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

fix things until they almost work again...

This commit is contained in:
Matthew Hodgson
2016-08-30 00:36:52 +01:00
parent c1c2ca3ec1
commit 58031ab21d
2 changed files with 120 additions and 49 deletions

View File

@@ -44,9 +44,10 @@ var EventTimeline = require("./event-timeline");
* map from event_id to timeline and index. * map from event_id to timeline and index.
*/ */
function EventTimelineList(roomId, opts) { function EventTimelineList(roomId, opts) {
this.roomId = roomId;
this._timelineSupport = Boolean(opts.timelineSupport); this._timelineSupport = Boolean(opts.timelineSupport);
this._liveTimeline = new EventTimeline(this.roomId); this._liveTimeline = new EventTimeline(this.roomId);
this._fixUpLegacyTimelineFields();
// just a list - *not* ordered. // just a list - *not* ordered.
this._timelines = [this._liveTimeline]; this._timelines = [this._liveTimeline];
@@ -86,8 +87,6 @@ EventTimelineList.prototype.getLiveTimeline = function(filterId) {
* <p>This is used when /sync returns a 'limited' timeline. * <p>This is used when /sync returns a 'limited' timeline.
* *
* @param {string=} backPaginationToken token for back-paginating the new timeline * @param {string=} backPaginationToken token for back-paginating the new timeline
*
* @fires module:client~MatrixClient#event:"Room.timelineReset"
*/ */
EventTimelineList.prototype.resetLiveTimeline = function(backPaginationToken) { EventTimelineList.prototype.resetLiveTimeline = function(backPaginationToken) {
var newTimeline; var newTimeline;
@@ -119,23 +118,6 @@ EventTimelineList.prototype.resetLiveTimeline = function(backPaginationToken) {
newTimeline.setPaginationToken(backPaginationToken, EventTimeline.BACKWARDS); newTimeline.setPaginationToken(backPaginationToken, EventTimeline.BACKWARDS);
this._liveTimeline = newTimeline; this._liveTimeline = newTimeline;
this._fixUpLegacyTimelineFields();
this.emit("Room.timelineReset", this);
};
/**
* Fix up this.timeline, this.oldState and this.currentState
*
* @private
*/
EventTimelineList.prototype._fixUpLegacyTimelineFields = function() {
// maintain this.timeline as a reference to the live timeline,
// and this.oldState and this.currentState as references to the
// state at the start and end of that timeline. These are more
// for backwards-compatibility than anything else.
this.timeline = this._liveTimeline.getEvents();
this.oldState = this._liveTimeline.getState(EventTimeline.BACKWARDS);
this.currentState = this._liveTimeline.getState(EventTimeline.FORWARDS);
}; };
/** /**
@@ -217,6 +199,11 @@ EventTimelineList.prototype.addEventsToTimeline = function(events, toStartOfTime
); );
} }
if (this._filter) {
var events = this._filter.filterRoomTimeline(events);
if (!events) return;
}
var direction = toStartOfTimeline ? EventTimeline.BACKWARDS : var direction = toStartOfTimeline ? EventTimeline.BACKWARDS :
EventTimeline.FORWARDS; EventTimeline.FORWARDS;
var inverseDirection = toStartOfTimeline ? EventTimeline.FORWARDS : var inverseDirection = toStartOfTimeline ? EventTimeline.FORWARDS :
@@ -423,6 +410,16 @@ EventTimelineList.prototype.addEventToTimeline = function(event, timeline, toSta
this.emit("Room.timeline", event, this, Boolean(toStartOfTimeline), false, data); this.emit("Room.timeline", event, this, Boolean(toStartOfTimeline), false, data);
}; };
EventTimelineList.prototype.replaceOrAddEvent = function(localEvent, oldEventId, newEventId) {
var existingTimeline = this._eventIdToTimeline[oldEventId];
if (existingTimeline) {
delete this._eventIdToTimeline[oldEventId];
this._eventIdToTimeline[newEventId] = existingTimeline;
} else {
this.addEventToTimeline(localEvent, this._liveTimeline, false);
}
};
/** /**
* Helper method to set sender and target properties, private to Room and EventTimelineList * Helper method to set sender and target properties, private to Room and EventTimelineList
*/ */

View File

@@ -163,6 +163,7 @@ function Room(roomId, opts) {
// all our per-room timeline lists. the first one is the unfiltered ones; // all our per-room timeline lists. the first one is the unfiltered ones;
// the subsequent ones are the filtered ones in no particular order. // the subsequent ones are the filtered ones in no particular order.
this._timelineLists = [ new EventTimelineList(roomId, opts) ]; this._timelineLists = [ new EventTimelineList(roomId, opts) ];
this._fixUpLegacyTimelineFields();
// any filtered timeline lists we're maintaining for this room // any filtered timeline lists we're maintaining for this room
this._filteredTimelineLists = { this._filteredTimelineLists = {
@@ -196,6 +197,50 @@ Room.prototype.getPendingEvents = function() {
return this._pendingEventList; return this._pendingEventList;
}; };
/**
* Get the live unfiltered timeline for this room.
*
* @return {module:models/event-timeline~EventTimeline} live timeline
*/
Room.prototype.getLiveTimeline = function(filterId) {
return this._timelineLists[0].getLiveTimeline();
};
/**
* Reset the live timeline, and start a new one.
*
* <p>This is used when /sync returns a 'limited' timeline.
*
* @param {string=} backPaginationToken token for back-paginating the new timeline
*
* @fires module:client~MatrixClient#event:"Room.timelineReset"
*/
Room.prototype.resetLiveTimeline = function(backPaginationToken) {
var newTimeline;
for (var i = 0; i < this._timelineLists.length; i++) {
this._timelineLists[i].resetLiveTimeline(backPaginationToken);
}
this._fixUpLegacyTimelineFields();
this.emit("Room.timelineReset", this);
};
/**
* Fix up this.timeline, this.oldState and this.currentState
*
* @private
*/
Room.prototype._fixUpLegacyTimelineFields = function() {
// maintain this.timeline as a reference to the live timeline,
// and this.oldState and this.currentState as references to the
// state at the start and end of that timeline. These are more
// for backwards-compatibility than anything else.
this.timeline = this._timelineLists[0].getLiveTimeline().getEvents();
this.oldState = this._timelineLists[0].getLiveTimeline().getState(EventTimeline.BACKWARDS);
this.currentState = this._timelineLists[0].getLiveTimeline().getState(EventTimeline.FORWARDS);
};
/** /**
* Get one of the notification counts for this room * Get one of the notification counts for this room
@@ -289,6 +334,32 @@ Room.prototype.getCanonicalAlias = function() {
return null; return null;
}; };
/**
* Add events to a timeline
*
* <p>Will fire "Room.timeline" for each event added.
*
* @param {MatrixEvent[]} events A list of events to add.
*
* @param {boolean} toStartOfTimeline True to add these events to the start
* (oldest) instead of the end (newest) of the timeline. If true, the oldest
* event will be the <b>last</b> element of 'events'.
*
* @param {module:models/event-timeline~EventTimeline} timeline timeline to
* add events to.
*
* @param {string=} paginationToken token for the next batch of events
*
* @fires module:client~MatrixClient#event:"Room.timeline"
*
*/
Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline,
timeline, paginationToken) {
for (var i = 0; i < this._timelineLists.length; i++) {
this._timelineLists[0]
}
};
/** /**
* Get a member from the current room state. * Get a member from the current room state.
* @param {string} userId The user ID of the member. * @param {string} userId The user ID of the member.
@@ -368,7 +439,7 @@ Room.prototype.removeFilteredTimelineList = function(filter) {
delete this._filteredTimelineLists[filter.filterId]; delete this._filteredTimelineLists[filter.filterId];
var i = this._timelineLists.indexOf(timelineList); var i = this._timelineLists.indexOf(timelineList);
if (i > -1) { if (i > -1) {
this._timelineList.splice(i, 1); this._timelineLists.splice(i, 1);
} }
}; };
@@ -385,11 +456,14 @@ Room.prototype._addLiveEvent = function(event, duplicateStrategy) {
if (event.getType() === "m.room.redaction") { if (event.getType() === "m.room.redaction") {
var redactId = event.event.redacts; var redactId = event.event.redacts;
for (var i = 0; i < this._timelineLists.length; i++) {
var timelineList = this._timelineLists[i];
// if we know about this event, redact its contents now. // if we know about this event, redact its contents now.
var redactedEvent = this.findEventById(redactId); var redactedEvent = timelineList.findEventById(redactId);
if (redactedEvent) { if (redactedEvent) {
redactedEvent.makeRedacted(event); redactedEvent.makeRedacted(event);
this.emit("Room.redaction", event, this); // FIXME: these should be emitted from EventTimelineList probably
this.emit("Room.redaction", event, this, timelineList);
// TODO: we stash user displaynames (among other things) in // TODO: we stash user displaynames (among other things) in
// RoomMember objects which are then attached to other events // RoomMember objects which are then attached to other events
@@ -398,6 +472,9 @@ Room.prototype._addLiveEvent = function(event, duplicateStrategy) {
// they are based on are changed. // they are based on are changed.
} }
// FIXME: apply redactions to notification list
}
// NB: We continue to add the redaction event to the timeline so // 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. Also // clients can say "so and so redacted an event" if they wish to. Also
// this may be needed to trigger an update. // this may be needed to trigger an update.
@@ -535,13 +612,7 @@ Room.prototype._handleRemoteEcho = function(remoteEvent, localEvent) {
var timelineList = this._timelineLists[i]; var timelineList = this._timelineLists[i];
// if it's already in the timeline, update the timeline map. If it's not, add it. // if it's already in the timeline, update the timeline map. If it's not, add it.
var existingTimeline = timelineList._eventIdToTimeline[oldEventId]; timelineList.replaceOrAddEvent(localEvent, oldEventId, newEventId);
if (existingTimeline) {
delete timelineList._eventIdToTimeline[oldEventId];
timelineList._eventIdToTimeline[newEventId] = existingTimeline;
} else {
timelineList._addEventToTimeline(localEvent, timelineList._liveTimeline, false);
}
} }
this.emit("Room.localEchoUpdated", localEvent, this, this.emit("Room.localEchoUpdated", localEvent, this,
@@ -672,17 +743,20 @@ Room.prototype.addLiveEvents = function(events, duplicateStrategy) {
} }
// sanity check that the live timeline is still live // sanity check that the live timeline is still live
if (this._liveTimeline.getPaginationToken(EventTimeline.FORWARDS)) { for (var i = 0; i < this._timelineLists.length; i++) {
var liveTimeline = this._timelineLists[i].getLiveTimeline();
if (liveTimeline.getPaginationToken(EventTimeline.FORWARDS)) {
throw new Error( throw new Error(
"live timeline is no longer live - it has a pagination token (" + "live timeline "+i+" is no longer live - it has a pagination token (" +
this._liveTimeline.getPaginationToken(EventTimeline.FORWARDS) + ")" timelineList.getPaginationToken(EventTimeline.FORWARDS) + ")"
); );
} }
if (this._liveTimeline.getNeighbouringTimeline(EventTimeline.FORWARDS)) { if (liveTimeline.getNeighbouringTimeline(EventTimeline.FORWARDS)) {
throw new Error( throw new Error(
"live timeline is no longer live - it has a neighbouring timeline" "live timeline "+i+" is no longer live - it has a neighbouring timeline"
); );
} }
}
for (var i = 0; i < events.length; i++) { for (var i = 0; i < events.length; i++) {
if (events[i].getType() === "m.typing") { if (events[i].getType() === "m.typing") {
@@ -883,7 +957,7 @@ Room.prototype._addReceiptsToStructure = function(event, receipts) {
// than the one we already have. (This is managed // than the one we already have. (This is managed
// server-side, but because we synthesize RRs locally we // server-side, but because we synthesize RRs locally we
// have to do it here too.) // have to do it here too.)
var ordering = self.compareEventOrdering( var ordering = self._timelineLists[0].compareEventOrdering(
existingReceipt.eventId, eventId); existingReceipt.eventId, eventId);
if (ordering !== null && ordering >= 0) { if (ordering !== null && ordering >= 0) {
return; return;