Merge pull request #412 from MidhunSureshR/member

Member panel - PR 1 - Add method to fetch member
This commit is contained in:
Bruno Windels 2021-08-05 11:13:52 +00:00 committed by GitHub
commit 73884cff0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 1 deletions

View file

@ -130,6 +130,10 @@ export class HomeServerApi {
{}, {}, options);
}
state(roomId, eventType, stateKey, options = null) {
return this._get(`/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/${encodeURIComponent(stateKey)}`, {}, null, options);
}
passwordLogin(username, password, initialDeviceDisplayName, options = null) {
return this._unauthedRequest("POST", this._url("/login"), null, {
"type": "m.login.password",

View file

@ -21,7 +21,7 @@ import {RelationWriter} from "./timeline/persistence/RelationWriter.js";
import {Timeline} from "./timeline/Timeline.js";
import {FragmentIdComparer} from "./timeline/FragmentIdComparer.js";
import {WrappedError} from "../error.js"
import {fetchOrLoadMembers} from "./members/load.js";
import {fetchOrLoadMembers, fetchOrLoadMember} from "./members/load.js";
import {MemberList} from "./members/MemberList.js";
import {Heroes} from "./members/Heroes.js";
import {EventEntry} from "./timeline/entries/EventEntry.js";
@ -54,6 +54,7 @@ export class BaseRoom extends EventEmitter {
this._observedEvents = null;
this._powerLevels = null;
this._powerLevelLoading = null;
this._observedMembers = null;
}
async _eventIdsToEntries(eventIds, txn) {
@ -214,6 +215,32 @@ export class BaseRoom extends EventEmitter {
}
}
async observeMember(userId) {
if (!this._observedMembers) {
this._observedMembers = new Map();
}
const mapMember = this._observedMembers.get(userId);
if (mapMember) {
// Hit, we're already observing this member
return mapMember;
}
// Miss, load from storage/hs and set in map
const member = await fetchOrLoadMember({
summary: this._summary,
roomId: this._roomId,
userId,
storage: this._storage,
hsApi: this._hsApi
}, this._platform.logger);
if (!member) {
return null;
}
const observableMember = new RetainedObservableValue(member, () => this._observedMembers.delete(userId));
this._observedMembers.set(userId, observableMember);
return observableMember;
}
/** @public */
async loadMemberList(log = null) {
if (this._memberList) {

View file

@ -209,6 +209,9 @@ export class Room extends BaseRoom {
if (this._memberList) {
this._memberList.afterSync(memberChanges);
}
if (this._observedMembers) {
this._updateObservedMembers(memberChanges);
}
if (this._timeline) {
for (const [userId, memberChange] of memberChanges.entries()) {
if (userId === this._user.id) {
@ -250,6 +253,15 @@ export class Room extends BaseRoom {
}
}
_updateObservedMembers(memberChanges) {
for (const [userId, memberChange] of memberChanges) {
const observableMember = this._observedMembers.get(userId);
if (observableMember) {
observableMember.set(memberChange.member);
}
}
}
needsAfterSyncCompleted({shouldFlushKeyShares}) {
return shouldFlushKeyShares;
}

View file

@ -90,3 +90,49 @@ export async function fetchOrLoadMembers(options, logger) {
return loadMembers(options);
}
}
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 member = await txn.roomMembers.get(roomId, userId);
return member? new RoomMember(member) : null;
}
async function fetchMember({roomId, userId, hsApi, storage}, log) {
let memberData;
try {
memberData = await hsApi.state(roomId, "m.room.member", userId, { log }).response();
}
catch (error) {
if (error.name === "HomeServerError" && error.errcode === "M_NOT_FOUND") {
return null;
}
throw error;
}
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;
}