diff --git a/src/matrix/Session.js b/src/matrix/Session.js index bc52227a..6cf9225c 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -19,6 +19,7 @@ import { ObservableMap } from "../observable/index.js"; import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js"; import {User} from "./User.js"; import {Account as E2EEAccount} from "./e2ee/Account.js"; +import {DeviceTracker} from "./e2ee/DeviceTracker.js"; const PICKLE_KEY = "DEFAULT_KEY"; export class Session { @@ -34,6 +35,11 @@ export class Session { this._user = new User(sessionInfo.userId); this._olm = olm; this._e2eeAccount = null; + this._deviceTracker = olm ? new DeviceTracker({ + storage, + getSyncToken: () => this.syncToken, + olm, + }) : null; } async beforeFirstSync(isNewLogin) { @@ -152,7 +158,7 @@ export class Session { return room; } - writeSync(syncResponse, syncFilterId, txn) { + async writeSync(syncResponse, syncFilterId, roomChanges, txn) { const changes = {}; const syncToken = syncResponse.next_batch; const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count; @@ -166,6 +172,17 @@ export class Session { txn.session.set("sync", syncInfo); changes.syncInfo = syncInfo; } + if (this._deviceTracker) { + const deviceLists = syncResponse.device_lists; + if (deviceLists) { + await this._deviceTracker.writeDeviceChanges(deviceLists, txn); + } + for (const {room, changes} of roomChanges) { + if (room.isTrackingMembers && changes.memberChanges?.size) { + await this._deviceTracker.writeMemberChanges(room, changes.memberChanges, txn); + } + } + } return changes; } diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index c7aaaa99..4da24ba6 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -133,7 +133,6 @@ export class Sync { const roomChanges = []; let sessionChanges; try { - sessionChanges = this._session.writeSync(response, syncFilterId, syncTxn); // to_device // presence if (response.rooms) { @@ -153,6 +152,7 @@ export class Sync { }); await Promise.all(promises); } + sessionChanges = await this._session.writeSync(response, syncFilterId, roomChanges, syncTxn); } catch(err) { console.warn("aborting syncTxn because of error"); console.error(err);