hook up device tracker with sync

This commit is contained in:
Bruno Windels 2020-08-31 14:13:21 +02:00
parent 8b358379e8
commit afb9ae4391
2 changed files with 19 additions and 2 deletions

View file

@ -19,6 +19,7 @@ import { ObservableMap } from "../observable/index.js";
import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js"; import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js";
import {User} from "./User.js"; import {User} from "./User.js";
import {Account as E2EEAccount} from "./e2ee/Account.js"; import {Account as E2EEAccount} from "./e2ee/Account.js";
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
const PICKLE_KEY = "DEFAULT_KEY"; const PICKLE_KEY = "DEFAULT_KEY";
export class Session { export class Session {
@ -34,6 +35,11 @@ export class Session {
this._user = new User(sessionInfo.userId); this._user = new User(sessionInfo.userId);
this._olm = olm; this._olm = olm;
this._e2eeAccount = null; this._e2eeAccount = null;
this._deviceTracker = olm ? new DeviceTracker({
storage,
getSyncToken: () => this.syncToken,
olm,
}) : null;
} }
async beforeFirstSync(isNewLogin) { async beforeFirstSync(isNewLogin) {
@ -152,7 +158,7 @@ export class Session {
return room; return room;
} }
writeSync(syncResponse, syncFilterId, txn) { async writeSync(syncResponse, syncFilterId, roomChanges, txn) {
const changes = {}; const changes = {};
const syncToken = syncResponse.next_batch; const syncToken = syncResponse.next_batch;
const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count; const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count;
@ -166,6 +172,17 @@ export class Session {
txn.session.set("sync", syncInfo); txn.session.set("sync", syncInfo);
changes.syncInfo = 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; return changes;
} }

View file

@ -133,7 +133,6 @@ export class Sync {
const roomChanges = []; const roomChanges = [];
let sessionChanges; let sessionChanges;
try { try {
sessionChanges = this._session.writeSync(response, syncFilterId, syncTxn);
// to_device // to_device
// presence // presence
if (response.rooms) { if (response.rooms) {
@ -153,6 +152,7 @@ export class Sync {
}); });
await Promise.all(promises); await Promise.all(promises);
} }
sessionChanges = await this._session.writeSync(response, syncFilterId, roomChanges, syncTxn);
} catch(err) { } catch(err) {
console.warn("aborting syncTxn because of error"); console.warn("aborting syncTxn because of error");
console.error(err); console.error(err);