Fetch member data from from state event if needed

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-08-02 22:50:02 +05:30
parent 6cfb4cfbe0
commit bb68b4d367
2 changed files with 41 additions and 4 deletions

View file

@ -21,7 +21,7 @@ import {RelationWriter} from "./timeline/persistence/RelationWriter.js";
import {Timeline} from "./timeline/Timeline.js"; import {Timeline} from "./timeline/Timeline.js";
import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js"; import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js";
import {WrappedError} from "../error.js" import {WrappedError} from "../error.js"
import {fetchOrLoadMembers, loadMember} from "./members/load.js"; import {fetchOrLoadMembers, fetchOrLoadMember} from "./members/load.js";
import {MemberList} from "./members/MemberList.js"; import {MemberList} from "./members/MemberList.js";
import {Heroes} from "./members/Heroes.js"; import {Heroes} from "./members/Heroes.js";
import {EventEntry} from "./timeline/entries/EventEntry.js"; import {EventEntry} from "./timeline/entries/EventEntry.js";
@ -224,8 +224,14 @@ export class BaseRoom extends EventEmitter {
// Hit, we're already observing this member // Hit, we're already observing this member
return mapMember; return mapMember;
} }
// Miss, load from storage and set in map // Miss, load from storage/hs and set in map
const member = await loadMember({roomId: this._roomId, userId, storage: this._storage}); const member = await fetchOrLoadMember({
summary: this._summary,
roomId: this._roomId,
userId,
storage: this._storage,
hsApi: this._hsApi
}, this._platform.logger);
if (!member) { if (!member) {
return false; return false;
} }

View file

@ -91,8 +91,39 @@ export async function fetchOrLoadMembers(options, logger) {
} }
} }
export async function loadMember({roomId, userId, storage}) { export async function fetchOrLoadMember(options, logger) {
const member = await loadMember(options);
const {summary} = options;
if (!summary.data.hasFetchedMembers && !member) {
// We haven't fetched the memberlist yet; so ping the hs to see if this member does exist
return logger.wrapOrRun(options.log, "fetchMember", log => fetchMember(options, log));
}
return member;
}
async function loadMember({roomId, userId, storage}) {
const txn = await storage.readTxn([storage.storeNames.roomMembers,]); const txn = await storage.readTxn([storage.storeNames.roomMembers,]);
const member = await txn.roomMembers.get(roomId, userId); const member = await txn.roomMembers.get(roomId, userId);
return member? new RoomMember(member) : undefined; return member? new RoomMember(member) : undefined;
} }
async function fetchMember({roomId, userId, hsApi, storage}, log) {
const memberData = await hsApi.state(roomId, "m.room.member", userId, {log}).response();
const member = new RoomMember({
roomId,
userId,
membership: memberData.membership,
avatarUrl: memberData.avatar_url,
displayName: memberData.displayname,
});
const txn = await storage.readWriteTxn([storage.storeNames.roomMembers]);
try {
txn.roomMembers.set(member.serialize());
}
catch(e) {
txn.abort();
throw e;
}
await txn.complete();
return member;
}