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