UNPKG

36.2 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/sync-accumulator.js"],"names":["SyncAccumulator","opts","maxTimelineEntries","accountData","inviteRooms","joinRooms","nextBatch","groups","invite","join","leave","syncResponse","_accumulateRooms","_accumulateGroups","_accumulateAccountData","next_batch","account_data","events","forEach","e","type","rooms","roomId","_accumulateRoom","category","data","_accumulateInviteState","_accumulateJoinState","console","error","invite_state","currentData","hasAdded","i","length","current","state_key","push","_currentState","_timeline","_accountData","_unreadNotifications","_summary","_readReceipts","unread_notifications","summary","HEROES_KEY","INVITED_COUNT_KEY","JOINED_COUNT_KEY","acc","sum","ephemeral","content","eventId","userId","timeline","limited","state","setState","index","event","token","prev_batch","startIndex","slice","groupId","_accumulateGroup","cat","roomData","roomJson","evType","receiptEvent","room_id","receiptData","msgData","rollBackState","timelineEvent","undefined","prevStateEvent","utils","deepCopy","unsigned","prev_content","prev_sender","sender","stateKey","ev","accData","roomsData","groupsData","eventMap","module","exports"],"mappings":";;;;;;;;;;;;;;;;;;AAsBA;;;;;;AAGA;;;;;;;;;;IAUMA,e;AACF;;;;;;;;;AASA,6BAAYC,IAAZ,EAAkB;AAAA;;AACdA,eAAOA,QAAQ,EAAf;AACAA,aAAKC,kBAAL,GAA0BD,KAAKC,kBAAL,IAA2B,EAArD;AACA,aAAKD,IAAL,GAAYA,IAAZ;AACA,aAAKE,WAAL,GAAmB;AACf;AADe,SAAnB;AAGA,aAAKC,WAAL,GAAmB;AACf;AADe,SAAnB;AAGA,aAAKC,SAAL,GAAiB;AACb;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAjBa,SAAjB;AAmBA;AACA;AACA;AACA;AACA,aAAKC,SAAL,GAAiB,IAAjB;;AAEA;AACA,aAAKC,MAAL,GAAc;AACVC,oBAAQ,EADE;AAEVC,kBAAM,EAFI;AAGVC,mBAAO;AAHG,SAAd;AAKH;;;;mCAEUC,Y,EAAc;AACrB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IAFR;AAGHgE,4BAAY,KAAKlF,MAHd;AAIHJ,6BAAaoF;AAJV,aAAP;AAMH;;;4CAEmB;AAChB,mBAAO,KAAKjF,SAAZ;AACH;;;KA/hBL;;;;;;;;;;;;;;;;;AAiBA;;;;;AAihBA,SAASoD,QAAT,CAAkBgC,QAAlB,EAA4B9B,KAA5B,EAAmC;AAC/B,QAAIA,MAAMxB,SAAN,KAAoB,IAApB,IAA4BwB,MAAMxB,SAAN,KAAoByC,SAAhD,IAA6D,CAACjB,MAAMxC,IAAxE,EAA8E;AAC1E;AACH;AACD,QAAI,CAACsE,SAAS9B,MAAMxC,IAAf,CAAL,EAA2B;AACvBsE,iBAAS9B,MAAMxC,IAAf,IAAuB,sBAAc,IAAd,CAAvB;AACH;AACDsE,aAAS9B,MAAMxC,IAAf,EAAqBwC,MAAMxB,SAA3B,IAAwCwB,KAAxC;AACH;;AAED+B,OAAOC,OAAP,GAAiB5F,eAAjB","file":"sync-accumulator.js","sourcesContent":["/*\nCopyright 2017 Vector Creations Ltd\nCopyright 2018 New Vector Ltd\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/**\n * This is an internal module. See {@link SyncAccumulator} for the public class.\n * @module sync-accumulator\n */\n\nimport utils from \"./utils\";\n\n\n/**\n * The purpose of this class is to accumulate /sync responses such that a\n * complete \"initial\" JSON response can be returned which accurately represents\n * the sum total of the /sync responses accumulated to date. It only handles\n * room data: that is, everything under the \"rooms\" top-level key.\n *\n * This class is used when persisting room data so a complete /sync response can\n * be loaded from disk and incremental syncs can be performed on the server,\n * rather than asking the server to do an initial sync on startup.\n */\nclass SyncAccumulator {\n /**\n * @param {Object} opts\n * @param {Number=} opts.maxTimelineEntries The ideal maximum number of\n * timeline entries to keep in the sync response. This is best-effort, as\n * clients do not always have a back-pagination token for each event, so\n * it's possible there may be slightly *less* than this value. There will\n * never be more. This cannot be 0 or else it makes it impossible to scroll\n * back in a room. Default: 50.\n */\n constructor(opts) {\n opts = opts || {};\n opts.maxTimelineEntries = opts.maxTimelineEntries || 50;\n this.opts = opts;\n this.accountData = {\n //$event_type: Object\n };\n this.inviteRooms = {\n //$roomId: { ... sync 'invite' json data ... }\n };\n this.joinRooms = {\n //$roomId: {\n // _currentState: { $event_type: { $state_key: json } },\n // _timeline: [\n // { event: $event, token: null|token },\n // { event: $event, token: null|token },\n // { event: $event, token: null|token },\n // ...\n // ],\n // _summary: {\n // m.heroes: [ $user_id ],\n // m.joined_member_count: $count,\n // m.invited_member_count: $count\n // },\n // _accountData: { $event_type: json },\n // _unreadNotifications: { ... unread_notifications JSON ... },\n // _readReceipts: { $user_id: { data: $json, eventId: $event_id }}\n //}\n };\n // the /sync token which corresponds to the last time rooms were\n // accumulated. We remember this so that any caller can obtain a\n // coherent /sync response and know at what point they should be\n // streaming from without losing events.\n this.nextBatch = null;\n\n // { ('invite'|'join'|'leave'): $groupId: { ... sync 'group' data } }\n this.groups = {\n invite: {},\n join: {},\n leave: {},\n };\n }\n\n accumulate(syncResponse) {\n this._accumulateRooms(syncResponse);\n this._accumulateGroups(syncResponse);\n this._accumulateAccountData(syncResponse);\n this.nextBatch = syncResponse.next_batch;\n }\n\n _accumulateAccountData(syncResponse) {\n if (!syncResponse.account_data || !syncResponse.account_data.events) {\n return;\n }\n // Clobbers based on event type.\n syncResponse.account_data.events.forEach((e) => {\n this.accountData[e.type] = e;\n });\n }\n\n /**\n * Accumulate incremental /sync room data.\n * @param {Object} syncResponse the complete /sync JSON\n */\n _accumulateRooms(syncResponse) {\n if (!syncResponse.rooms) {\n return;\n }\n if (syncResponse.rooms.invite) {\n Object.keys(syncResponse.rooms.invite).forEach((roomId) => {\n this._accumulateRoom(\n roomId, \"invite\", syncResponse.rooms.invite[roomId],\n );\n });\n }\n if (syncResponse.rooms.join) {\n Object.keys(syncResponse.rooms.join).forEach((roomId) => {\n this._accumulateRoom(\n roomId, \"join\", syncResponse.rooms.join[roomId],\n );\n });\n }\n if (syncResponse.rooms.leave) {\n Object.keys(syncResponse.rooms.leave).forEach((roomId) => {\n this._accumulateRoom(\n roomId, \"leave\", syncResponse.rooms.leave[roomId],\n );\n });\n }\n }\n\n _accumulateRoom(roomId, category, data) {\n // Valid /sync state transitions\n // +--------+ <======+ 1: Accept an invite\n // +== | INVITE | | (5) 2: Leave a room\n // | +--------+ =====+ | 3: Join a public room previously\n // |(1) (4) | | left (handle as if new room)\n // V (2) V | 4: Reject an invite\n // +------+ ========> +--------+ 5: Invite to a room previously\n // | JOIN | (3) | LEAVE* | left (handle as if new room)\n // +------+ <======== +--------+\n //\n // * equivalent to \"no state\"\n switch (category) {\n case \"invite\": // (5)\n this._accumulateInviteState(roomId, data);\n break;\n case \"join\":\n if (this.inviteRooms[roomId]) { // (1)\n // was previously invite, now join. We expect /sync to give\n // the entire state and timeline on 'join', so delete previous\n // invite state\n delete this.inviteRooms[roomId];\n }\n // (3)\n this._accumulateJoinState(roomId, data);\n break;\n case \"leave\":\n if (this.inviteRooms[roomId]) { // (4)\n delete this.inviteRooms[roomId];\n } else { // (2)\n delete this.joinRooms[roomId];\n }\n break;\n default:\n console.error(\"Unknown cateogory: \", category);\n }\n }\n\n _accumulateInviteState(roomId, data) {\n if (!data.invite_state || !data.invite_state.events) { // no new data\n return;\n }\n if (!this.inviteRooms[roomId]) {\n this.inviteRooms[roomId] = {\n invite_state: data.invite_state,\n };\n return;\n }\n // accumulate extra keys for invite->invite transitions\n // clobber based on event type / state key\n // We expect invite_state to be small, so just loop over the events\n const currentData = this.inviteRooms[roomId];\n data.invite_state.events.forEach((e) => {\n let hasAdded = false;\n for (let i = 0; i < currentData.invite_state.events.length; i++) {\n const current = currentData.invite_state.events[i];\n if (current.type === e.type && current.state_key == e.state_key) {\n currentData.invite_state.events[i] = e; // update\n hasAdded = true;\n }\n }\n if (!hasAdded) {\n currentData.invite_state.events.push(e);\n }\n });\n }\n\n // Accumulate timeline and state events in a room.\n _accumulateJoinState(roomId, data) {\n // We expect this function to be called a lot (every /sync) so we want\n // this to be fast. /sync stores events in an array but we often want\n // to clobber based on type/state_key. Rather than convert arrays to\n // maps all the time, just keep private maps which contain\n // the actual current accumulated sync state, and array-ify it when\n // getJSON() is called.\n\n // State resolution:\n // The 'state' key is the delta from the previous sync (or start of time\n // if no token was supplied), to the START of the timeline. To obtain\n // the current state, we need to \"roll forward\" state by reading the\n // timeline. We want to store the current state so we can drop events\n // out the end of the timeline based on opts.maxTimelineEntries.\n //\n // 'state' 'timeline' current state\n // |-------x<======================>x\n // T I M E\n //\n // When getJSON() is called, we 'roll back' the current state by the\n // number of entries in the timeline to work out what 'state' should be.\n\n // Back-pagination:\n // On an initial /sync, the server provides a back-pagination token for\n // the start of the timeline. When /sync deltas come down, they also\n // include back-pagination tokens for the start of the timeline. This\n // means not all events in the timeline have back-pagination tokens, as\n // it is only the ones at the START of the timeline which have them.\n // In order for us to have a valid timeline (and back-pagination token\n // to match), we need to make sure that when we remove old timeline\n // events, that we roll forward to an event which has a back-pagination\n // token. This means we can't keep a strict sliding-window based on\n // opts.maxTimelineEntries, and we may have a few less. We should never\n // have more though, provided that the /sync limit is less than or equal\n // to opts.maxTimelineEntries.\n\n if (!this.joinRooms[roomId]) {\n // Create truly empty objects so event types of 'hasOwnProperty' and co\n // don't cause this code to break.\n this.joinRooms[roomId] = {\n _currentState: Object.create(null),\n _timeline: [],\n _accountData: Object.create(null),\n _unreadNotifications: {},\n _summary: {},\n _readReceipts: {},\n };\n }\n const currentData = this.joinRooms[roomId];\n\n if (data.account_data && data.account_data.events) {\n // clobber based on type\n data.account_data.events.forEach((e) => {\n currentData._accountData[e.type] = e;\n });\n }\n\n // these probably clobber, spec is unclear.\n if (data.unread_notifications) {\n currentData._unreadNotifications = data.unread_notifications;\n }\n if (data.summary) {\n const HEROES_KEY = \"m.heroes\";\n const INVITED_COUNT_KEY = \"m.invited_member_count\";\n const JOINED_COUNT_KEY = \"m.joined_member_count\";\n\n const acc = currentData._summary;\n const sum = data.summary;\n acc[HEROES_KEY] = sum[HEROES_KEY] || acc[HEROES_KEY];\n acc[JOINED_COUNT_KEY] = sum[JOINED_COUNT_KEY] || acc[JOINED_COUNT_KEY];\n acc[INVITED_COUNT_KEY] = sum[INVITED_COUNT_KEY] || acc[INVITED_COUNT_KEY];\n }\n\n if (data.ephemeral && data.ephemeral.events) {\n data.ephemeral.events.forEach((e) => {\n // We purposefully do not persist m.typing events.\n // Technically you could refresh a browser before the timer on a\n // typing event is up, so it'll look like you aren't typing when\n // you really still are. However, the alternative is worse. If\n // we do persist typing events, it will look like people are\n // typing forever until someone really does start typing (which\n // will prompt Synapse to send down an actual m.typing event to\n // clobber the one we persisted).\n if (e.type !== \"m.receipt\" || !e.content) {\n // This means we'll drop unknown ephemeral events but that\n // seems okay.\n return;\n }\n // Handle m.receipt events. They clobber based on:\n // (user_id, receipt_type)\n // but they are keyed in the event as:\n // content:{ $event_id: { $receipt_type: { $user_id: {json} }}}\n // so store them in the former so we can accumulate receipt deltas\n // quickly and efficiently (we expect a lot of them). Fold the\n // receipt type into the key name since we only have 1 at the\n // moment (m.read) and nested JSON objects are slower and more\n // of a hassle to work with. We'll inflate this back out when\n // getJSON() is called.\n Object.keys(e.content).forEach((eventId) => {\n if (!e.content[eventId][\"m.read\"]) {\n return;\n }\n Object.keys(e.content[eventId][\"m.read\"]).forEach((userId) => {\n // clobber on user ID\n currentData._readReceipts[userId] = {\n data: e.content[eventId][\"m.read\"][userId],\n eventId: eventId,\n };\n });\n });\n });\n }\n\n // if we got a limited sync, we need to remove all timeline entries or else\n // we will have gaps in the timeline.\n if (data.timeline && data.timeline.limited) {\n currentData._timeline = [];\n }\n\n // Work out the current state. The deltas need to be applied in the order:\n // - existing state which didn't come down /sync.\n // - State events under the 'state' key.\n // - State events in the 'timeline'.\n if (data.state && data.state.events) {\n data.state.events.forEach((e) => {\n setState(currentData._currentState, e);\n });\n }\n if (data.timeline && data.timeline.events) {\n data.timeline.events.forEach((e, index) => {\n // this nops if 'e' isn't a state event\n setState(currentData._currentState, e);\n // append the event to the timeline. The back-pagination token\n // corresponds to the first event in the timeline\n currentData._timeline.push({\n event: e,\n token: index === 0 ? data.timeline.prev_batch : null,\n });\n });\n }\n\n // attempt to prune the timeline by jumping between events which have\n // pagination tokens.\n if (currentData._timeline.length > this.opts.maxTimelineEntries) {\n const startIndex = (\n currentData._timeline.length - this.opts.maxTimelineEntries\n );\n for (let i = startIndex; i < currentData._timeline.length; i++) {\n if (currentData._timeline[i].token) {\n // keep all events after this, including this one\n currentData._timeline = currentData._timeline.slice(\n i, currentData._timeline.length,\n );\n break;\n }\n }\n }\n }\n\n /**\n * Accumulate incremental /sync group data.\n * @param {Object} syncResponse the complete /sync JSON\n */\n _accumulateGroups(syncResponse) {\n if (!syncResponse.groups) {\n return;\n }\n if (syncResponse.groups.invite) {\n Object.keys(syncResponse.groups.invite).forEach((groupId) => {\n this._accumulateGroup(\n groupId, \"invite\", syncResponse.groups.invite[groupId],\n );\n });\n }\n if (syncResponse.groups.join) {\n Object.keys(syncResponse.groups.join).forEach((groupId) => {\n this._accumulateGroup(\n groupId, \"join\", syncResponse.groups.join[groupId],\n );\n });\n }\n if (syncResponse.groups.leave) {\n Object.keys(syncResponse.groups.leave).forEach((groupId) => {\n this._accumulateGroup(\n groupId, \"leave\", syncResponse.groups.leave[groupId],\n );\n });\n }\n }\n\n _accumulateGroup(groupId, category, data) {\n for (const cat of ['invite', 'join', 'leave']) {\n delete this.groups[cat][groupId];\n }\n this.groups[category][groupId] = data;\n }\n\n /**\n * Return everything under the 'rooms' key from a /sync response which\n * represents all room data that should be stored. This should be paired\n * with the sync token which represents the most recent /sync response\n * provided to accumulate().\n * @return {Object} An object with a \"nextBatch\", \"roomsData\" and \"accountData\"\n * keys.\n * The \"nextBatch\" key is a string which represents at what point in the\n * /sync stream the accumulator reached. This token should be used when\n * restarting a /sync stream at startup. Failure to do so can lead to missing\n * events. The \"roomsData\" key is an Object which represents the entire\n * /sync response from the 'rooms' key onwards. The \"accountData\" key is\n * a list of raw events which represent global account data.\n */\n getJSON() {\n const data = {\n join: {},\n invite: {},\n // always empty. This is set by /sync when a room was previously\n // in 'invite' or 'join'. On fresh startup, the client won't know\n // about any previous room being in 'invite' or 'join' so we can\n // just omit mentioning it at all, even if it has previously come\n // down /sync.\n // The notable exception is when a client is kicked or banned:\n // we may want to hold onto that room so the client can clearly see\n // why their room has disappeared. We don't persist it though because\n // it is unclear *when* we can safely remove the room from the DB.\n // Instead, we assume that if you're loading from the DB, you've\n // refreshed the page, which means you've seen the kick/ban already.\n leave: {},\n };\n Object.keys(this.inviteRooms).forEach((roomId) => {\n data.invite[roomId] = this.inviteRooms[roomId];\n });\n Object.keys(this.joinRooms).forEach((roomId) => {\n const roomData = this.joinRooms[roomId];\n const roomJson = {\n ephemeral: { events: [] },\n account_data: { events: [] },\n state: { events: [] },\n timeline: {\n events: [],\n prev_batch: null,\n },\n unread_notifications: roomData._unreadNotifications,\n summary: roomData._summary,\n };\n // Add account data\n Object.keys(roomData._accountData).forEach((evType) => {\n roomJson.account_data.events.push(roomData._accountData[evType]);\n });\n\n // Add receipt data\n const receiptEvent = {\n type: \"m.receipt\",\n room_id: roomId,\n content: {\n // $event_id: { \"m.read\": { $user_id: $json } }\n },\n };\n Object.keys(roomData._readReceipts).forEach((userId) => {\n const receiptData = roomData._readReceipts[userId];\n if (!receiptEvent.content[receiptData.eventId]) {\n receiptEvent.content[receiptData.eventId] = {\n \"m.read\": {},\n };\n }\n receiptEvent.content[receiptData.eventId][\"m.read\"][userId] = (\n receiptData.data\n );\n });\n // add only if we have some receipt data\n if (Object.keys(receiptEvent.content).length > 0) {\n roomJson.ephemeral.events.push(receiptEvent);\n }\n\n // Add timeline data\n roomData._timeline.forEach((msgData) => {\n if (!roomJson.timeline.prev_batch) {\n // the first event we add to the timeline MUST match up to\n // the prev_batch token.\n if (!msgData.token) {\n return; // this shouldn't happen as we prune constantly.\n }\n roomJson.timeline.prev_batch = msgData.token;\n }\n roomJson.timeline.events.push(msgData.event);\n });\n\n // Add state data: roll back current state to the start of timeline,\n // by \"reverse clobbering\" from the end of the timeline to the start.\n // Convert maps back into arrays.\n const rollBackState = Object.create(null);\n for (let i = roomJson.timeline.events.length - 1; i >=0; i--) {\n const timelineEvent = roomJson.timeline.events[i];\n if (timelineEvent.state_key === null ||\n timelineEvent.state_key === undefined) {\n continue; // not a state event\n }\n // since we're going back in time, we need to use the previous\n // state value else we'll break causality. We don't have the\n // complete previous state event, so we need to create one.\n const prevStateEvent = utils.deepCopy(timelineEvent);\n if (prevStateEvent.unsigned) {\n if (prevStateEvent.unsigned.prev_content) {\n prevStateEvent.content = prevStateEvent.unsigned.prev_content;\n }\n if (prevStateEvent.unsigned.prev_sender) {\n prevStateEvent.sender = prevStateEvent.unsigned.prev_sender;\n }\n }\n setState(rollBackState, prevStateEvent);\n }\n Object.keys(roomData._currentState).forEach((evType) => {\n Object.keys(roomData._currentState[evType]).forEach((stateKey) => {\n let ev = roomData._currentState[evType][stateKey];\n if (rollBackState[evType] && rollBackState[evType][stateKey]) {\n // use the reverse clobbered event instead.\n ev = rollBackState[evType][stateKey];\n }\n roomJson.state.events.push(ev);\n });\n });\n data.join[roomId] = roomJson;\n });\n\n // Add account data\n const accData = [];\n Object.keys(this.accountData).forEach((evType) => {\n accData.push(this.accountData[evType]);\n });\n\n return {\n nextBatch: this.nextBatch,\n roomsData: data,\n groupsData: this.groups,\n accountData: accData,\n };\n }\n\n getNextBatchToken() {\n return this.nextBatch;\n }\n}\n\nfunction setState(eventMap, event) {\n if (event.state_key === null || event.state_key === undefined || !event.type) {\n return;\n }\n if (!eventMap[event.type]) {\n eventMap[event.type] = Object.create(null);\n }\n eventMap[event.type][event.state_key] = event;\n}\n\nmodule.exports = SyncAccumulator;\n"]}
\No newline at end of file