fix changed member scenario (no need to fetch from storage) and errors

This commit is contained in:
Bruno Windels 2020-08-19 16:51:16 +02:00
parent 4144b0b281
commit faaabd1837

View file

@ -37,33 +37,44 @@ async function fetchMembers({summary, roomId, hsApi, storage, setChangedMembersM
storage.storeNames.roomSummary, storage.storeNames.roomSummary,
storage.storeNames.roomMembers, storage.storeNames.roomMembers,
]); ]);
const summaryChanges = summary.writeHasFetchedMembers(true, txn);
const {roomMembers} = txn; let summaryChanges;
const memberEvents = memberResponse.chunk; let members;
if (!Array.isArray(memberEvents)) {
throw new Error("malformed"); try {
} summaryChanges = summary.writeHasFetchedMembers(true, txn);
const members = await Promise.all(memberEvents.map(async memberEvent => { const {roomMembers} = txn;
const userId = memberEvent?.state_key; const memberEvents = memberResponse.chunk;
if (!userId) { if (!Array.isArray(memberEvents)) {
throw new Error("malformed"); throw new Error("malformed");
} }
// this member was changed during a sync that happened while calling /members members = await Promise.all(memberEvents.map(async memberEvent => {
// and thus is more recent, so don't overwrite const userId = memberEvent?.state_key;
if (changedMembersDuringSync.has(userId)) { if (!userId) {
const memberData = await roomMembers.get(roomId, userId); throw new Error("malformed");
if (memberData) {
return new RoomMember(memberData);
} }
} else { // this member was changed during a sync that happened while calling /members
const member = RoomMember.fromMemberEvent(roomId, memberEvent); // and thus is more recent, so don't overwrite
if (member) { const changedMember = changedMembersDuringSync.get(userId);
roomMembers.set(member.serialize()); if (changedMember) {
return changedMember;
} else {
const member = RoomMember.fromMemberEvent(roomId, memberEvent);
if (member) {
roomMembers.set(member.serialize());
}
return member;
} }
return member; }));
} } catch (err) {
})); // abort txn on any error
setChangedMembersMap(null); txn.abort();
throw err;
} finally {
// important this gets cleared
// or otherwise Room remains in "fetching-members" mode
setChangedMembersMap(null);
}
await txn.complete(); await txn.complete();
summary.applyChanges(summaryChanges); summary.applyChanges(summaryChanges);
return members; return members;