finish draft of member list loading method

This commit is contained in:
Bruno Windels 2020-08-19 16:13:47 +02:00
parent 9edd1bb0bb
commit 6abdcd6b58

View file

@ -22,6 +22,7 @@ import {Timeline} from "./timeline/Timeline.js";
import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js"; import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js";
import {SendQueue} from "./sending/SendQueue.js"; import {SendQueue} from "./sending/SendQueue.js";
import {WrappedError} from "../error.js" import {WrappedError} from "../error.js"
import {RoomMember} from "./RoomMember.js";
export class Room extends EventEmitter { export class Room extends EventEmitter {
constructor({roomId, storage, hsApi, emitCollectionChange, sendScheduler, pendingEvents, user}) { constructor({roomId, storage, hsApi, emitCollectionChange, sendScheduler, pendingEvents, user}) {
@ -92,42 +93,72 @@ export class Room extends EventEmitter {
} }
async loadMemberList() { async loadMemberList() {
let members; if (this._memberList) {
if (!this._summary.hasFetchedMembers) { this._memberList.retain();
// we need to get the syncToken here! return this._memberList;
const memberResponse = await this._hsApi.members(this._roomId, syncToken).response; } else {
let members;
if (!this._summary.hasFetchedMembers) {
const paginationToken = throw new Error("not implemented");
// TODO: move all of this out of Room
const txn = await this._storage.readWriteTxn([ // if any members are changed by sync while we're fetching members,
this._storage.storeNames.roomSummary, // they will end up here, so we check not to override them
this._storage.storeNames.roomMembers, this._changedMembersDuringSync = new Map();
]);
const summaryChanges = this._summary.writeHasFetchedMembers(true, txn); const memberResponse = await this._hsApi.members(this._roomId, {at: paginationToken}).response;
const {roomMembers} = txn;
const memberEvents = memberResponse.chunk; const txn = await this._storage.readWriteTxn([
if (!Array.isArray(memberEvents)) { this._storage.storeNames.roomSummary,
throw new Error("malformed"); this._storage.storeNames.roomMembers,
} ]);
members = await Promise.all(memberEvents.map(async memberEvent => { const summaryChanges = this._summary.writeHasFetchedMembers(true, txn);
const userId = memberEvent && memberEvent.state_key; const {roomMembers} = txn;
if (!userId) { const memberEvents = memberResponse.chunk;
if (!Array.isArray(memberEvents)) {
throw new Error("malformed"); throw new Error("malformed");
} }
const memberData = await roomMembers.get(this._roomId, userId); members = await Promise.all(memberEvents.map(async memberEvent => {
const member = updateOrCreateMember(this._roomId, memberData, event); const userId = memberEvent?.state_key;
if (member) { if (!userId) {
roomMembers.set(member.serialize()); throw new Error("malformed");
} }
return member; // this member was changed during a sync that happened while calling /members
})); // and thus is more recent. Fetch it instead of overwriting.
await txn.complete(); if (this._changedMembersDuringSync.has(userId)) {
this._summary.applyChanges(summaryChanges); const memberData = await roomMembers.get(this._roomId, userId);
if (memberData) {
return new RoomMember(memberData);
}
} else {
const member = RoomMember.fromMemberEvent(this._roomId, memberEvent);
if (member) {
roomMembers.set(member.serialize());
}
return member;
}
}));
this._changedMembersDuringSync = null;
await txn.complete();
this._summary.applyChanges(summaryChanges);
} else {
const txn = await this._storage.readTxn([
this._storage.storeNames.roomMembers,
]);
const memberDatas = await txn.roomMembers.getAll(this._roomId);
members = memberDatas.map(d => new RoomMember(d));
}
this._memberList = new MemberList({
members,
closeCallback: () => { this._memberList = null; }
});
return this._memberList;
} }
return new MemberList(this._roomId, members, this._storage);
} }
/** @public */ /** @public */
async fillGap(fragmentEntry, amount) { async fillGap(fragmentEntry, amount) {
// TODO move some/all of this out of Room
if (fragmentEntry.edgeReached) { if (fragmentEntry.edgeReached) {
return; return;
} }