give room state handler access to member sync to get sender profile info

This commit is contained in:
Bruno Windels 2022-06-02 15:55:08 +02:00
parent a530944f7d
commit a52740ed1b
5 changed files with 22 additions and 12 deletions

View file

@ -123,7 +123,7 @@ export class Room extends BaseRoom {
txn.roomState.removeAllForRoom(this.id); txn.roomState.removeAllForRoom(this.id);
txn.roomMembers.removeAllForRoom(this.id); txn.roomMembers.removeAllForRoom(this.id);
} }
const {entries: newEntries, updatedEntries, newLiveKey, memberChanges} = const {entries: newEntries, updatedEntries, newLiveKey, memberChanges, memberSync} =
await log.wrap("syncWriter", log => this._syncWriter.writeSync( await log.wrap("syncWriter", log => this._syncWriter.writeSync(
roomResponse, isRejoin, summaryChanges.hasFetchedMembers, txn, log), log.level.Detail); roomResponse, isRejoin, summaryChanges.hasFetchedMembers, txn, log), log.level.Detail);
if (decryptChanges) { if (decryptChanges) {
@ -180,7 +180,7 @@ export class Room extends BaseRoom {
removedPendingEvents = await this._sendQueue.removeRemoteEchos(roomResponse.timeline.events, txn, log); removedPendingEvents = await this._sendQueue.removeRemoteEchos(roomResponse.timeline.events, txn, log);
} }
const powerLevelsEvent = this._getPowerLevelsEvent(roomResponse); const powerLevelsEvent = this._getPowerLevelsEvent(roomResponse);
this._runRoomStateHandlers(roomResponse, txn, log); await this._runRoomStateHandlers(roomResponse, memberSync, txn, log);
return { return {
roomResponse, roomResponse,
summaryChanges, summaryChanges,
@ -453,14 +453,16 @@ export class Room extends BaseRoom {
return this._sendQueue.pendingEvents; return this._sendQueue.pendingEvents;
} }
/** global room state handlers, run during write sync step */ /** global room state handlers, run during writeSync step */
_runRoomStateHandlers(roomResponse, txn, log) { _runRoomStateHandlers(roomResponse, memberSync, txn, log) {
const promises = [];
iterateResponseStateEvents(roomResponse, event => { iterateResponseStateEvents(roomResponse, event => {
this._roomStateHandler.handleRoomState(this, event, txn, log); promises.push(this._roomStateHandler.handleRoomState(this, event, memberSync, txn, log));
}); });
return Promise.all(promises);
} }
/** local room state observers, run during after sync step */ /** local room state observers, run during afterSync step */
_emitSyncRoomState(roomResponse) { _emitSyncRoomState(roomResponse) {
iterateResponseStateEvents(roomResponse, event => { iterateResponseStateEvents(roomResponse, event => {
for (const handler of this._roomStateObservers) { for (const handler of this._roomStateObservers) {

View file

@ -20,14 +20,17 @@ import type {Transaction} from "../../storage/idb/Transaction";
import type {Room} from "../Room"; import type {Room} from "../Room";
import type {MemberChange} from "../members/RoomMember"; import type {MemberChange} from "../members/RoomMember";
import type {RoomStateHandler} from "./types"; import type {RoomStateHandler} from "./types";
import type {MemberSync} from "../timeline/persistence/MemberWriter.js";
import {BaseObservable} from "../../../observable/BaseObservable"; import {BaseObservable} from "../../../observable/BaseObservable";
/** keeps track of all handlers registered with Session.observeRoomState */ /** keeps track of all handlers registered with Session.observeRoomState */
export class RoomStateHandlerSet extends BaseObservable<RoomStateHandler> implements RoomStateHandler { export class RoomStateHandlerSet extends BaseObservable<RoomStateHandler> implements RoomStateHandler {
handleRoomState(room: Room, stateEvent: StateEvent, txn: Transaction, log: ILogItem) { async handleRoomState(room: Room, stateEvent: StateEvent, memberSync: MemberSync, txn: Transaction, log: ILogItem): Promise<void> {
const promises: Promise<void>[] = [];
for(let h of this._handlers) { for(let h of this._handlers) {
h.handleRoomState(room, stateEvent, txn, log); promises.push(h.handleRoomState(room, stateEvent, memberSync, txn, log));
} }
await Promise.all(promises);
} }
updateRoomMembers(room: Room, memberChanges: Map<string, MemberChange>) { updateRoomMembers(room: Room, memberChanges: Map<string, MemberChange>) {
for(let h of this._handlers) { for(let h of this._handlers) {

View file

@ -19,12 +19,13 @@ import type {StateEvent} from "../../storage/types";
import type {Transaction} from "../../storage/idb/Transaction"; import type {Transaction} from "../../storage/idb/Transaction";
import type {ILogItem} from "../../../logging/types"; import type {ILogItem} from "../../../logging/types";
import type {MemberChange} from "../members/RoomMember"; import type {MemberChange} from "../members/RoomMember";
import type {MemberSync} from "../timeline/persistence/MemberWriter";
/** used for Session.observeRoomState, which observes in all room, but without loading from storage /** used for Session.observeRoomState, which observes in all room, but without loading from storage
* It receives the sync write transaction, so other stores can be updated as part of the same transaction. */ * It receives the sync write transaction, so other stores can be updated as part of the same transaction. */
export interface RoomStateHandler { export interface RoomStateHandler {
handleRoomState(room: Room, stateEvent: StateEvent, syncWriteTxn: Transaction, log: ILogItem); handleRoomState(room: Room, stateEvent: StateEvent, memberSync: MemberSync, syncWriteTxn: Transaction, log: ILogItem): Promise<void>;
updateRoomMembers(room: Room, memberChanges: Map<string, MemberChange>); updateRoomMembers(room: Room, memberChanges: Map<string, MemberChange>): void;
} }
/** /**

View file

@ -56,7 +56,11 @@ export class MemberWriter {
} }
} }
class MemberSync { /** Represents the member changes in a given sync.
* Used to write the changes to storage and historical member
* information for events in the same sync.
**/
export class MemberSync {
constructor(memberWriter, stateEvents, timelineEvents, hasFetchedMembers) { constructor(memberWriter, stateEvents, timelineEvents, hasFetchedMembers) {
this._memberWriter = memberWriter; this._memberWriter = memberWriter;
this._timelineEvents = timelineEvents; this._timelineEvents = timelineEvents;

View file

@ -244,7 +244,7 @@ export class SyncWriter {
const {currentKey, entries, updatedEntries} = const {currentKey, entries, updatedEntries} =
await this._writeTimeline(timelineEvents, timeline, memberSync, this._lastLiveKey, txn, log); await this._writeTimeline(timelineEvents, timeline, memberSync, this._lastLiveKey, txn, log);
const memberChanges = await memberSync.write(txn); const memberChanges = await memberSync.write(txn);
return {entries, updatedEntries, newLiveKey: currentKey, memberChanges}; return {entries, updatedEntries, newLiveKey: currentKey, memberChanges, memberSync};
} }
afterSync(newLiveKey) { afterSync(newLiveKey) {