You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
based on PR review, rewrite account_data support to avoid tracking the section that events came from, and instead having /sync results piped into the right bit of the room directly
This commit is contained in:
@@ -53,9 +53,6 @@ module.exports.EventStatus = {
|
|||||||
* @prop {boolean} forwardLooking True if this event is 'forward looking', meaning
|
* @prop {boolean} forwardLooking True if this event is 'forward looking', meaning
|
||||||
* that getDirectionalContent() will return event.content and not event.prev_content.
|
* that getDirectionalContent() will return event.content and not event.prev_content.
|
||||||
* Default: true. <strong>This property is experimental and may change.</strong>
|
* Default: true. <strong>This property is experimental and may change.</strong>
|
||||||
* @prop {String} section The type of /sync section this event came from, if any
|
|
||||||
* one of 'state', 'account_data', 'ephemeral', or 'timeline'. Useful for recognising
|
|
||||||
* 'account_data' events.
|
|
||||||
*/
|
*/
|
||||||
module.exports.MatrixEvent = function MatrixEvent(event, encrypted) {
|
module.exports.MatrixEvent = function MatrixEvent(event, encrypted) {
|
||||||
this.event = event || {};
|
this.event = event || {};
|
||||||
@@ -64,7 +61,6 @@ module.exports.MatrixEvent = function MatrixEvent(event, encrypted) {
|
|||||||
this.status = null;
|
this.status = null;
|
||||||
this.forwardLooking = true;
|
this.forwardLooking = true;
|
||||||
this.encrypted = Boolean(encrypted);
|
this.encrypted = Boolean(encrypted);
|
||||||
this.section = null;
|
|
||||||
};
|
};
|
||||||
module.exports.MatrixEvent.prototype = {
|
module.exports.MatrixEvent.prototype = {
|
||||||
|
|
||||||
@@ -182,14 +178,6 @@ module.exports.MatrixEvent.prototype = {
|
|||||||
return this.event.state_key !== undefined;
|
return this.event.state_key !== undefined;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if this event is account_data.
|
|
||||||
* @return {boolean} True if this event describes account_data
|
|
||||||
*/
|
|
||||||
isAccountData: function() {
|
|
||||||
return this.section === "account_data";
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the event is encrypted.
|
* Check if the event is encrypted.
|
||||||
* @return {boolean} True if this event is encrypted.
|
* @return {boolean} True if this event is encrypted.
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ function synthesizeReceipt(userId, event, receiptType) {
|
|||||||
* are any metadata associated with the tag - e.g. { "fav" : { order: 1 } }
|
* are any metadata associated with the tag - e.g. { "fav" : { order: 1 } }
|
||||||
* @prop {object} accountData Dict of per-room account_data events; the keys are the
|
* @prop {object} accountData Dict of per-room account_data events; the keys are the
|
||||||
* event type and the values are the events.
|
* event type and the values are the events.
|
||||||
* are any metadata associated with the tag - e.g. { "fav" : { order: 1 } }
|
|
||||||
* @prop {RoomState} oldState The state of the room at the time of the oldest
|
* @prop {RoomState} oldState The state of the room at the time of the oldest
|
||||||
* event in the timeline.
|
* event in the timeline.
|
||||||
* @prop {RoomState} currentState The state of the room at the time of the
|
* @prop {RoomState} currentState The state of the room at the time of the
|
||||||
@@ -339,13 +338,8 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
|
|||||||
else if (events[i].getType() === "m.receipt") {
|
else if (events[i].getType() === "m.receipt") {
|
||||||
this.addReceipt(events[i]);
|
this.addReceipt(events[i]);
|
||||||
}
|
}
|
||||||
else if (events[i].getType() === "m.tag") {
|
// N.B. account_data is added directly by /sync to avoid
|
||||||
this.addTags(events[i]);
|
// having to maintain an event.isAccountData() here
|
||||||
}
|
|
||||||
else if (events[i].isAccountData()) {
|
|
||||||
this.addAccountData(events[i]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
if (duplicateStrategy) {
|
if (duplicateStrategy) {
|
||||||
// is there a duplicate?
|
// is there a duplicate?
|
||||||
@@ -612,11 +606,17 @@ Room.prototype.addTags = function(event) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the account_data events for this room, overwriting events of the same type.
|
* Update the account_data events for this room, overwriting events of the same type.
|
||||||
* @param {MatrixEvent} event the account_data event
|
* @param {Array<MatrixEvent>} events an array of account_data events to add
|
||||||
*/
|
*/
|
||||||
Room.prototype.addAccountData = function(event) {
|
Room.prototype.addAccountData = function(events) {
|
||||||
|
for (var i = 0; i < events.length; i++) {
|
||||||
|
var event = events[i];
|
||||||
|
if (event.getType() === "m.tag") {
|
||||||
|
this.addTags(event);
|
||||||
|
}
|
||||||
this.accountData[event.getType()] = event;
|
this.accountData[event.getType()] = event;
|
||||||
this.emit("Room.accountData", event, this);
|
this.emit("Room.accountData", event, this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
37
lib/sync.js
37
lib/sync.js
@@ -139,8 +139,8 @@ SyncApi.prototype.syncLeftRooms = function() {
|
|||||||
}
|
}
|
||||||
leaveObj.timeline = leaveObj.timeline || {};
|
leaveObj.timeline = leaveObj.timeline || {};
|
||||||
var timelineEvents =
|
var timelineEvents =
|
||||||
self._mapSyncEventsFormat(leaveObj.timeline, room, 'timeline');
|
self._mapSyncEventsFormat(leaveObj.timeline, room);
|
||||||
var stateEvents = self._mapSyncEventsFormat(leaveObj.state, room, 'state');
|
var stateEvents = self._mapSyncEventsFormat(leaveObj.state, room);
|
||||||
var paginationToken = (
|
var paginationToken = (
|
||||||
leaveObj.timeline.limited ? leaveObj.timeline.prev_batch : null
|
leaveObj.timeline.limited ? leaveObj.timeline.prev_batch : null
|
||||||
);
|
);
|
||||||
@@ -326,7 +326,7 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
|
|||||||
inviteRooms.forEach(function(inviteObj) {
|
inviteRooms.forEach(function(inviteObj) {
|
||||||
var room = inviteObj.room;
|
var room = inviteObj.room;
|
||||||
var stateEvents =
|
var stateEvents =
|
||||||
self._mapSyncEventsFormat(inviteObj.invite_state, room, 'state');
|
self._mapSyncEventsFormat(inviteObj.invite_state, room);
|
||||||
self._processRoomEvents(room, stateEvents);
|
self._processRoomEvents(room, stateEvents);
|
||||||
if (inviteObj.isBrandNewRoom) {
|
if (inviteObj.isBrandNewRoom) {
|
||||||
room.recalculate(client.credentials.userId);
|
room.recalculate(client.credentials.userId);
|
||||||
@@ -339,14 +339,10 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
|
|||||||
// Handle joins
|
// Handle joins
|
||||||
joinRooms.forEach(function(joinObj) {
|
joinRooms.forEach(function(joinObj) {
|
||||||
var room = joinObj.room;
|
var room = joinObj.room;
|
||||||
var stateEvents = self._mapSyncEventsFormat(joinObj.state, room, 'state');
|
var stateEvents = self._mapSyncEventsFormat(joinObj.state, room);
|
||||||
var timelineEvents =
|
var timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room);
|
||||||
self._mapSyncEventsFormat(joinObj.timeline, room, 'timeline');
|
var ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral);
|
||||||
var ephemeralEvents =
|
var accountDataEvents = self._mapSyncEventsFormat(joinObj.account_data);
|
||||||
self._mapSyncEventsFormat(joinObj.ephemeral, undefined, 'ephemeral');
|
|
||||||
var accountDataEvents =
|
|
||||||
self._mapSyncEventsFormat(joinObj.account_data, undefined,
|
|
||||||
'account_data');
|
|
||||||
|
|
||||||
// we do this first so it's correct when any of the events fire
|
// we do this first so it's correct when any of the events fire
|
||||||
room.unread_notification_count = joinObj.unread_notification_count;
|
room.unread_notification_count = joinObj.unread_notification_count;
|
||||||
@@ -368,8 +364,15 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
|
|||||||
self._processRoomEvents(
|
self._processRoomEvents(
|
||||||
room, stateEvents, timelineEvents, paginationToken
|
room, stateEvents, timelineEvents, paginationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// XXX: should we be adding ephemeralEvents to the timeline?
|
||||||
|
// It feels like that for symmetry with room.addAccountData()
|
||||||
|
// there should be a room.addEphemeralEvents() or similar.
|
||||||
room.addEvents(ephemeralEvents);
|
room.addEvents(ephemeralEvents);
|
||||||
room.addEvents(accountDataEvents);
|
|
||||||
|
// we deliberately don't add accountData to the timeline
|
||||||
|
room.addAccountData(accountDataEvents);
|
||||||
|
|
||||||
room.recalculate(client.credentials.userId);
|
room.recalculate(client.credentials.userId);
|
||||||
if (joinObj.isBrandNewRoom) {
|
if (joinObj.isBrandNewRoom) {
|
||||||
client.store.storeRoom(room);
|
client.store.storeRoom(room);
|
||||||
@@ -386,7 +389,7 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
|
|||||||
// Do the bear minimum to register rejected invites / you leaving rooms
|
// Do the bear minimum to register rejected invites / you leaving rooms
|
||||||
var room = leaveObj.room;
|
var room = leaveObj.room;
|
||||||
var timelineEvents =
|
var timelineEvents =
|
||||||
self._mapSyncEventsFormat(leaveObj.timeline, room, 'timeline');
|
self._mapSyncEventsFormat(leaveObj.timeline, room);
|
||||||
room.addEvents(timelineEvents);
|
room.addEvents(timelineEvents);
|
||||||
timelineEvents.forEach(function(e) { client.emit("event", e); });
|
timelineEvents.forEach(function(e) { client.emit("event", e); });
|
||||||
});
|
});
|
||||||
@@ -464,11 +467,9 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) {
|
|||||||
/**
|
/**
|
||||||
* @param {Object} obj
|
* @param {Object} obj
|
||||||
* @param {Room} room
|
* @param {Room} room
|
||||||
* @param {String} section /sync section
|
|
||||||
* (timeline, state, ephemeral, account_data)
|
|
||||||
* @return {MatrixEvent[]}
|
* @return {MatrixEvent[]}
|
||||||
*/
|
*/
|
||||||
SyncApi.prototype._mapSyncEventsFormat = function(obj, room, section) {
|
SyncApi.prototype._mapSyncEventsFormat = function(obj, room) {
|
||||||
if (!obj || !utils.isArray(obj.events)) {
|
if (!obj || !utils.isArray(obj.events)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -477,9 +478,7 @@ SyncApi.prototype._mapSyncEventsFormat = function(obj, room, section) {
|
|||||||
if (room) {
|
if (room) {
|
||||||
e.room_id = room.roomId;
|
e.room_id = room.roomId;
|
||||||
}
|
}
|
||||||
var event = mapper(e);
|
return mapper(e);
|
||||||
event.section = section;
|
|
||||||
return event;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user