UNPKG

matrix-js-sdk

Version:
1,215 lines (1,115 loc) 166 kB
/* Copyright 2015 - 2023 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { M_POLL_START } from "matrix-events-sdk"; import { DuplicateStrategy, EventTimelineSet, type EventTimelineSetHandlerMap, type IAddLiveEventOptions, } from "./event-timeline-set.ts"; import { Direction, EventTimeline } from "./event-timeline.ts"; import { getHttpUriForMxc } from "../content-repo.ts"; import * as utils from "../utils.ts"; import { normalize, noUnsafeEventProps, removeElement } from "../utils.ts"; import { type IEvent, type IThreadBundledRelationship, MatrixEvent, MatrixEventEvent, type MatrixEventHandlerMap, } from "./event.ts"; import { EventStatus } from "./event-status.ts"; import { RoomMember } from "./room-member.ts"; import { type Hero, type IRoomSummary, RoomSummary } from "./room-summary.ts"; import { logger } from "../logger.ts"; import { TypedReEmitter } from "../ReEmitter.ts"; import { EVENT_VISIBILITY_CHANGE_TYPE, EventType, RelationType, RoomCreateTypeField, RoomType, UNSIGNED_THREAD_ID_FIELD, UNSTABLE_ELEMENT_FUNCTIONAL_USERS, } from "../@types/event.ts"; import { type MatrixClient, PendingEventOrdering } from "../client.ts"; import { type GuestAccess, type HistoryVisibility, type JoinRule, type ResizeMethod } from "../@types/partials.ts"; import { Filter, type IFilterDefinition } from "../filter.ts"; import { type RoomState, RoomStateEvent, type RoomStateEventHandlerMap } from "./room-state.ts"; import { BeaconEvent, type BeaconEventHandlerMap } from "./beacon.ts"; import { FILTER_RELATED_BY_REL_TYPES, FILTER_RELATED_BY_SENDERS, Thread, THREAD_RELATION_TYPE, ThreadEvent, type ThreadEventHandlerMap as ThreadHandlerMap, ThreadFilterType, } from "./thread.ts"; import { type CachedReceiptStructure, MAIN_ROOM_TIMELINE, type Receipt, type ReceiptContent, ReceiptType, } from "../@types/read_receipts.ts"; import { type IStateEventWithRoomId } from "../@types/search.ts"; import { RelationsContainer } from "./relations-container.ts"; import { ReadReceipt, synthesizeReceipt } from "./read-receipt.ts"; import { isPollEvent, Poll, PollEvent } from "./poll.ts"; import { RoomReceipts } from "./room-receipts.ts"; import { compareEventOrdering } from "./compare-event-ordering.ts"; import { KnownMembership, type Membership } from "../@types/membership.ts"; import { type Capabilities, type IRoomVersionsCapability, RoomVersionStability } from "../serverCapabilities.ts"; import { type MSC4186Hero } from "../sliding-sync.ts"; import { RoomStickyEventsStore, RoomStickyEventsEvent, type RoomStickyEventsMap } from "./room-sticky-events.ts"; // These constants are used as sane defaults when the homeserver doesn't support // the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be // the same as the common default room version whereas SAFE_ROOM_VERSIONS are the // room versions which are considered okay for people to run without being asked // to upgrade (ie: "stable"). Eventually, we should remove these when all homeservers // return an m.room_versions capability. export const KNOWN_SAFE_ROOM_VERSION = "10"; const SAFE_ROOM_VERSIONS = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; interface IOpts { /** * Controls where pending messages appear in a room's timeline. * If "<b>chronological</b>", messages will appear in the timeline when the call to `sendEvent` was made. * If "<b>detached</b>", pending messages will appear in a separate list, * accessible via {@link Room#getPendingEvents}. * Default: "chronological". */ pendingEventOrdering?: PendingEventOrdering; /** * Set to true to enable improved timeline support. */ timelineSupport?: boolean; lazyLoadMembers?: boolean; } export interface IRecommendedVersion { version: string; needsUpgrade: boolean; urgent: boolean; } // When inserting a visibility event affecting event `eventId`, we // need to scan through existing visibility events for `eventId`. // In theory, this could take an unlimited amount of time if: // // - the visibility event was sent by a moderator; and // - `eventId` already has many visibility changes (usually, it should // be 2 or less); and // - for some reason, the visibility changes are received out of order // (usually, this shouldn't happen at all). // // For this reason, we limit the number of events to scan through, // expecting that a broken visibility change for a single event in // an extremely uncommon case (possibly a DoS) is a small // price to pay to keep matrix-js-sdk responsive. const MAX_NUMBER_OF_VISIBILITY_EVENTS_TO_SCAN_THROUGH = 30; export type NotificationCount = Partial<Record<NotificationCountType, number>>; export enum NotificationCountType { Highlight = "highlight", Total = "total", } export interface ICreateFilterOpts { // Populate the filtered timeline with already loaded events in the room // timeline. Useful to disable for some filters that can't be achieved by the // client in an efficient manner prepopulateTimeline?: boolean; useSyncEvents?: boolean; pendingEvents?: boolean; } export enum RoomEvent { MyMembership = "Room.myMembership", Tags = "Room.tags", AccountData = "Room.accountData", Receipt = "Room.receipt", Name = "Room.name", Redaction = "Room.redaction", RedactionCancelled = "Room.redactionCancelled", LocalEchoUpdated = "Room.localEchoUpdated", Timeline = "Room.timeline", TimelineReset = "Room.timelineReset", TimelineRefresh = "Room.TimelineRefresh", OldStateUpdated = "Room.OldStateUpdated", CurrentStateUpdated = "Room.CurrentStateUpdated", HistoryImportedWithinTimeline = "Room.historyImportedWithinTimeline", UnreadNotifications = "Room.UnreadNotifications", Summary = "Room.Summary", } export type RoomEmittedEvents = | RoomEvent | RoomStateEvent.Events | RoomStateEvent.Members | RoomStateEvent.NewMember | RoomStateEvent.Update | RoomStateEvent.Marker | RoomStickyEventsEvent.Update | ThreadEvent.New | ThreadEvent.Update | ThreadEvent.NewReply | ThreadEvent.Delete | MatrixEventEvent.BeforeRedaction | BeaconEvent.New | BeaconEvent.Update | BeaconEvent.Destroy | BeaconEvent.LivenessChange | PollEvent.New; export type RoomEventHandlerMap = { /** * Fires when the logged in user's membership in the room is updated. * * @param room - The room in which the membership has been updated * @param membership - The new membership value * @param prevMembership - The previous membership value */ [RoomEvent.MyMembership]: (room: Room, membership: Membership, prevMembership?: Membership) => void; /** * Fires whenever a room's tags are updated. * @param event - The tags event * @param room - The room whose Room.tags was updated. * @example * ``` * matrixClient.on("Room.tags", function(event, room){ * var newTags = event.getContent().tags; * if (newTags["favourite"]) showStar(room); * }); * ``` */ [RoomEvent.Tags]: (event: MatrixEvent, room: Room) => void; /** * Fires whenever a room's account_data is updated. * @param event - The account_data event * @param room - The room whose account_data was updated. * @param prevEvent - The event being replaced by * the new account data, if known. * @example * ``` * matrixClient.on("Room.accountData", function(event, room, oldEvent){ * if (event.getType() === "m.room.colorscheme") { * applyColorScheme(event.getContents()); * } * }); * ``` */ [RoomEvent.AccountData]: (event: MatrixEvent, room: Room, prevEvent?: MatrixEvent) => void; /** * Fires whenever a receipt is received for a room * @param event - The receipt event * @param room - The room whose receipts was updated. * @example * ``` * matrixClient.on("Room.receipt", function(event, room){ * var receiptContent = event.getContent(); * }); * ``` */ [RoomEvent.Receipt]: (event: MatrixEvent, room: Room) => void; /** * Fires whenever the name of a room is updated. * @param room - The room whose Room.name was updated. * @example * ``` * matrixClient.on("Room.name", function(room){ * var newName = room.name; * }); * ``` */ [RoomEvent.Name]: (room: Room) => void; /** * Fires when an event we had previously received is redacted. * * (Note this is *not* fired when the redaction happens before we receive the * event). * * @param event - The matrix redaction event * @param room - The room containing the redacted event * @param threadId - The thread containing the redacted event (before it was redacted) */ [RoomEvent.Redaction]: (event: MatrixEvent, room: Room, threadId?: string) => void; /** * Fires when an event that was previously redacted isn't anymore. * This happens when the redaction couldn't be sent and * was subsequently cancelled by the user. Redactions have a local echo * which is undone in this scenario. * * @param event - The matrix redaction event that was cancelled. * @param room - The room containing the unredacted event */ [RoomEvent.RedactionCancelled]: (event: MatrixEvent, room: Room) => void; /** * Fires when the status of a transmitted event is updated. * * <p>When an event is first transmitted, a temporary copy of the event is * inserted into the timeline, with a temporary event id, and a status of * 'SENDING'. * * <p>Once the echo comes back from the server, the content of the event * (MatrixEvent.event) is replaced by the complete event from the homeserver, * thus updating its event id, as well as server-generated fields such as the * timestamp. Its status is set to null. * * <p>Once the /send request completes, if the remote echo has not already * arrived, the event is updated with a new event id and the status is set to * 'SENT'. The server-generated fields are of course not updated yet. * * <p>If the /send fails, In this case, the event's status is set to * 'NOT_SENT'. If it is later resent, the process starts again, setting the * status to 'SENDING'. Alternatively, the message may be cancelled, which * removes the event from the room, and sets the status to 'CANCELLED'. * * <p>This event is raised to reflect each of the transitions above. * * @param event - The matrix event which has been updated * * @param room - The room containing the redacted event * * @param oldEventId - The previous event id (the temporary event id, * except when updating a successfully-sent event when its echo arrives) * * @param oldStatus - The previous event status. */ [RoomEvent.LocalEchoUpdated]: ( event: MatrixEvent, room: Room, oldEventId?: string, oldStatus?: EventStatus | null, ) => void; [RoomEvent.OldStateUpdated]: (room: Room, previousRoomState: RoomState, roomState: RoomState) => void; [RoomEvent.CurrentStateUpdated]: (room: Room, previousRoomState: RoomState, roomState: RoomState) => void; [RoomEvent.HistoryImportedWithinTimeline]: (markerEvent: MatrixEvent, room: Room) => void; [RoomEvent.UnreadNotifications]: (unreadNotifications?: NotificationCount, threadId?: string) => void; [RoomEvent.TimelineRefresh]: (room: Room, eventTimelineSet: EventTimelineSet) => void; /** * Fires when a new room summary is returned by `/sync`. * * See https://spec.matrix.org/v1.8/client-server-api/#_matrixclientv3sync_roomsummary * for full details * @param summary - the room summary object */ [RoomEvent.Summary]: (summary: IRoomSummary) => void; [ThreadEvent.New]: (thread: Thread, toStartOfTimeline: boolean) => void; /** * Fires when a new poll instance is added to the room state * @param poll - the new poll */ [PollEvent.New]: (poll: Poll) => void; } & Pick<ThreadHandlerMap, ThreadEvent.Update | ThreadEvent.NewReply | ThreadEvent.Delete> & EventTimelineSetHandlerMap & Pick<MatrixEventHandlerMap, MatrixEventEvent.BeforeRedaction> & Pick<RoomStickyEventsMap, RoomStickyEventsEvent.Update> & Pick< RoomStateEventHandlerMap, | RoomStateEvent.Events | RoomStateEvent.Members | RoomStateEvent.NewMember | RoomStateEvent.Update | RoomStateEvent.Marker | BeaconEvent.New > & Pick<BeaconEventHandlerMap, BeaconEvent.Update | BeaconEvent.Destroy | BeaconEvent.LivenessChange>; export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> { public readonly reEmitter: TypedReEmitter<RoomEmittedEvents, RoomEventHandlerMap>; private txnToEvent: Map<string, MatrixEvent> = new Map(); // Pending in-flight requests { string: MatrixEvent } private notificationCounts: NotificationCount = {}; private bumpStamp: number | undefined = undefined; private readonly threadNotifications = new Map<string, NotificationCount>(); public readonly cachedThreadReadReceipts = new Map<string, CachedReceiptStructure[]>(); // Useful to know at what point the current user has started using threads in this room private oldestThreadedReceiptTs = Infinity; /** * A record of the latest unthread receipts per user * This is useful in determining whether a user has read a thread or not */ private unthreadedReceipts = new Map<string, Receipt>(); private readonly timelineSets: EventTimelineSet[]; public readonly polls: Map<string, Poll> = new Map<string, Poll>(); /** * Empty array if the timeline sets have not been initialised. After initialisation: * 0: All threads * 1: Threads the current user has participated in */ public readonly threadsTimelineSets: [] | [EventTimelineSet, EventTimelineSet] = []; // any filtered timeline sets we're maintaining for this room private readonly filteredTimelineSets: Record<string, EventTimelineSet> = {}; // filter_id: timelineSet private timelineNeedsRefresh = false; private readonly pendingEventList?: MatrixEvent[]; // read by megolm via getter; boolean value - null indicates "use global value" private blacklistUnverifiedDevices?: boolean; private selfMembership?: Membership; /** * A `Hero` is a stripped `m.room.member` event which contains the important renderable fields from the event. * * It is used in MSC4186 (Simplified Sliding Sync) as a replacement for the old `summary` field. * * When we are doing old-style (`/v3/sync`) sync, we simulate the SSS behaviour by constructing * a `Hero` object based on the user id we get from the summary. Obviously, in that case, * the `Hero` will lack a `displayName` or `avatarUrl`. */ private heroes: Hero[] | null = null; // flags to stop logspam about missing m.room.create events private getTypeWarning = false; private membersPromise?: Promise<boolean>; // XXX: These should be read-only /** * The human-readable display name for this room. */ public name: string; /** * The un-homoglyphed name for this room. */ public normalizedName: string; /** * Dict of room tags; the keys are the tag name and the values * are any metadata associated with the tag - e.g. `{ "fav" : { order: 1 } }` */ public tags: Record<string, Record<string, any>> = {}; // $tagName: { $metadata: $value } /** * accountData Dict of per-room account_data events; the keys are the * event type and the values are the events. */ public accountData: Map<string, MatrixEvent> = new Map(); // $eventType: $event /** * The room summary. */ public summary: RoomSummary | null = null; /** * oldState The state of the room at the time of the oldest event in the live timeline. * * @deprecated Present for backwards compatibility. * Use getLiveTimeline().getState(EventTimeline.BACKWARDS) instead */ public oldState!: RoomState; /** * currentState The state of the room at the time of the newest event in the timeline. * * @deprecated Present for backwards compatibility. * Use getLiveTimeline().getState(EventTimeline.FORWARDS) instead. */ public currentState!: RoomState; public readonly relations; /** * A collection of events known by the client * This is not a comprehensive list of the threads that exist in this room */ private threads = new Map<string, Thread>(); /** * A mapping of eventId to all visibility changes to apply * to the event, by chronological order, as per * https://github.com/matrix-org/matrix-doc/pull/3531 * * # Invariants * * - within each list, all events are classed by * chronological order; * - all events are events such that * `asVisibilityEvent()` returns a non-null `IVisibilityChange`; * - within each list with key `eventId`, all events * are in relation to `eventId`. * * @experimental */ private visibilityEvents = new Map<string, MatrixEvent[]>(); /** * The latest receipts (synthetic and real) for each user in each thread * (and unthreaded). */ private roomReceipts = new RoomReceipts(this); /** * Stores and tracks sticky events */ private stickyEvents = new RoomStickyEventsStore(); /** * Construct a new Room. * * <p>For a room, we store an ordered sequence of timelines, which may or may not * be continuous. Each timeline lists a series of events, as well as tracking * the room state at the start and the end of the timeline. It also tracks * forward and backward pagination tokens, as well as containing links to the * next timeline in the sequence. * * <p>There is one special timeline - the 'live' timeline, which represents the * timeline to which events are being added in real-time as they are received * from the /sync API. Note that you should not retain references to this * timeline - even if it is the current timeline right now, it may not remain * so if the server gives us a timeline gap in /sync. * * <p>In order that we can find events from their ids later, we also maintain a * map from event_id to timeline and index. * * @param roomId - Required. The ID of this room. * @param client - Required. The client, used to lazy load members. * @param myUserId - Required. The ID of the syncing user. * @param opts - Configuration options */ public constructor( public readonly roomId: string, public readonly client: MatrixClient, public readonly myUserId: string, private readonly opts: IOpts = {}, ) { super(); // In some cases, we add listeners for every displayed Matrix event, so it's // common to have quite a few more than the default limit. this.setMaxListeners(100); this.reEmitter = new TypedReEmitter(this); opts.pendingEventOrdering = opts.pendingEventOrdering || PendingEventOrdering.Chronological; this.name = roomId; this.normalizedName = roomId; this.relations = new RelationsContainer(this.client, this); // Listen to our own receipt event as a more modular way of processing our own // receipts. No need to remove the listener: it's on ourself anyway. this.on(RoomEvent.Receipt, this.onReceipt); this.reEmitter.reEmit(this.stickyEvents, [RoomStickyEventsEvent.Update]); // all our per-room timeline sets. the first one is the unfiltered ones; // the subsequent ones are the filtered ones in no particular order. this.timelineSets = [new EventTimelineSet(this, opts)]; this.reEmitter.reEmit(this.getUnfilteredTimelineSet(), [RoomEvent.Timeline, RoomEvent.TimelineReset]); this.fixUpLegacyTimelineFields(); if (this.opts.pendingEventOrdering === PendingEventOrdering.Detached) { this.pendingEventList = []; this.client.store.getPendingEvents(this.roomId).then((events) => { const mapper = this.client.getEventMapper({ decrypt: false, }); events.forEach(async (serializedEvent: Partial<IEvent>) => { const event = mapper(serializedEvent); await client.decryptEventIfNeeded(event); event.setStatus(EventStatus.NOT_SENT); this.addPendingEvent(event, event.getTxnId()!); }); }); } // awaited by getEncryptionTargetMembers while room members are loading if (!this.opts.lazyLoadMembers) { this.membersPromise = Promise.resolve(false); } else { this.membersPromise = undefined; } } private threadTimelineSetsPromise: Promise<[EventTimelineSet, EventTimelineSet]> | null = null; public async createThreadsTimelineSets(): Promise<[EventTimelineSet, EventTimelineSet] | null> { if (this.threadTimelineSetsPromise) { return this.threadTimelineSetsPromise; } if (this.client?.supportsThreads()) { try { this.threadTimelineSetsPromise = Promise.all([ this.createThreadTimelineSet(), this.createThreadTimelineSet(ThreadFilterType.My), ]); const timelineSets = await this.threadTimelineSetsPromise; this.threadsTimelineSets[0] = timelineSets[0]; this.threadsTimelineSets[1] = timelineSets[1]; return timelineSets; } catch { this.threadTimelineSetsPromise = null; return null; } } return null; } /** * Bulk decrypt critical events in a room * * Critical events represents the minimal set of events to decrypt * for a typical UI to function properly * * - Last event of every room (to generate likely message preview) * - All events up to the read receipt (to calculate an accurate notification count) * * @returns Signals when all events have been decrypted */ public async decryptCriticalEvents(): Promise<void> { if (!this.client.getCrypto()) return; const readReceiptEventId = this.getEventReadUpTo(this.client.getUserId()!, true); const events = this.getLiveTimeline().getEvents(); const readReceiptTimelineIndex = events.findIndex((matrixEvent) => { return matrixEvent.event.event_id === readReceiptEventId; }); const decryptionPromises = events .slice(readReceiptTimelineIndex) .reverse() .map((event) => this.client.decryptEventIfNeeded(event)); await Promise.allSettled(decryptionPromises); } /** * Bulk decrypt events in a room * * @returns Signals when all events have been decrypted */ public async decryptAllEvents(): Promise<void> { if (!this.client.getCrypto()) return; const decryptionPromises = this.getUnfilteredTimelineSet() .getLiveTimeline() .getEvents() .slice(0) // copy before reversing .reverse() .map((event) => this.client.decryptEventIfNeeded(event)); await Promise.allSettled(decryptionPromises); } /** * Gets the creator of the room * @returns The creator of the room, or null if it could not be determined */ public getCreator(): string | null { const createEvent = this.currentState.getStateEvents(EventType.RoomCreate, ""); return createEvent?.getSender() ?? null; } /** * Gets the version of the room * @returns The version of the room */ public getVersion(): string { return this.currentState.getRoomVersion(); } /** * Determines the recommended room version for the room. This returns an * object with 3 properties: `version` as the new version the * room should be upgraded to (may be the same as the current version); * `needsUpgrade` to indicate if the room actually can be * upgraded (ie: does the current version not match?); and `urgent` * to indicate if the new version patches a vulnerability in a previous * version. * @returns * Resolves to the version the room should be upgraded to. */ public async getRecommendedVersion(): Promise<IRecommendedVersion> { let capabilities: Capabilities = {}; try { capabilities = await this.client.getCapabilities(); } catch {} let versionCap = capabilities["m.room_versions"]; if (!versionCap) { versionCap = { default: KNOWN_SAFE_ROOM_VERSION, available: {}, }; for (const safeVer of SAFE_ROOM_VERSIONS) { versionCap.available[safeVer] = RoomVersionStability.Stable; } } let result = this.checkVersionAgainstCapability(versionCap); if (result.urgent && result.needsUpgrade) { // Something doesn't feel right: we shouldn't need to update // because the version we're on should be in the protocol's // namespace. This usually means that the server was updated // before the client was, making us think the newest possible // room version is not stable. As a solution, we'll refresh // the capability we're using to determine this. logger.warn( "Refreshing room version capability because the server looks " + "to be supporting a newer room version we don't know about.", ); try { capabilities = await this.client.fetchCapabilities(); } catch (e) { logger.warn("Failed to refresh room version capabilities", e); } versionCap = capabilities["m.room_versions"]; if (!versionCap) { logger.warn("No room version capability - assuming upgrade required."); return result; } else { result = this.checkVersionAgainstCapability(versionCap); } } return result; } private checkVersionAgainstCapability(versionCap: IRoomVersionsCapability): IRecommendedVersion { const currentVersion = this.getVersion(); logger.log(`[${this.roomId}] Current version: ${currentVersion}`); logger.log(`[${this.roomId}] Version capability: `, versionCap); const result: IRecommendedVersion = { version: currentVersion, needsUpgrade: false, urgent: false, }; // If the room is on the default version then nothing needs to change if (currentVersion === versionCap.default) return result; const stableVersions = Object.keys(versionCap.available).filter((v) => versionCap.available[v] === "stable"); // Check if the room is on an unstable version. We determine urgency based // off the version being in the Matrix spec namespace or not (if the version // is in the current namespace and unstable, the room is probably vulnerable). if (!stableVersions.includes(currentVersion)) { result.version = versionCap.default; result.needsUpgrade = true; result.urgent = !!this.getVersion().match(/^[0-9]+[0-9.]*$/g); if (result.urgent) { logger.warn(`URGENT upgrade required on ${this.roomId}`); } else { logger.warn(`Non-urgent upgrade required on ${this.roomId}`); } return result; } // The room is on a stable, but non-default, version by this point. // No upgrade needed. return result; } /** * Determines whether the given user is permitted to perform a room upgrade * @param userId - The ID of the user to test against * @returns True if the given user is permitted to upgrade the room */ public userMayUpgradeRoom(userId: string): boolean { return this.currentState.maySendStateEvent(EventType.RoomTombstone, userId); } /** * Get the list of pending sent events for this room * * @returns A list of the sent events * waiting for remote echo. * * @throws If `opts.pendingEventOrdering` was not 'detached' */ public getPendingEvents(): MatrixEvent[] { if (!this.pendingEventList) { throw new Error( "Cannot call getPendingEvents with pendingEventOrdering == " + this.opts.pendingEventOrdering, ); } return this.pendingEventList; } /** * Removes a pending event for this room * * @returns True if an element was removed. */ public removePendingEvent(eventId: string): boolean { if (!this.pendingEventList) { throw new Error( "Cannot call removePendingEvent with pendingEventOrdering == " + this.opts.pendingEventOrdering, ); } const removed = removeElement( this.pendingEventList, function (ev) { return ev.getId() == eventId; }, false, ); this.savePendingEvents(); return removed; } /** * Check whether the pending event list contains a given event by ID. * If pending event ordering is not "detached" then this returns false. * * @param eventId - The event ID to check for. */ public hasPendingEvent(eventId: string): boolean { return this.pendingEventList?.some((event) => event.getId() === eventId) ?? false; } /** * Get a specific event from the pending event list, if configured, null otherwise. * * @param eventId - The event ID to check for. */ public getPendingEvent(eventId: string): MatrixEvent | null { return this.pendingEventList?.find((event) => event.getId() === eventId) ?? null; } /** * Get the live unfiltered timeline for this room. * * @returns live timeline */ public getLiveTimeline(): EventTimeline { return this.getUnfilteredTimelineSet().getLiveTimeline(); } /** * The live event timeline for this room, with the oldest event at index 0. * * @deprecated Present for backwards compatibility. * Use getLiveTimeline().getEvents() instead */ public get timeline(): MatrixEvent[] { return this.getLiveTimeline().getEvents(); } /** * Get the timestamp of the last message in the room * * @returns the timestamp of the last message in the room */ public getLastActiveTimestamp(): number { const timeline = this.getLiveTimeline(); const events = timeline.getEvents(); if (events.length) { const lastEvent = events[events.length - 1]; return lastEvent.getTs(); } else { return Number.MIN_SAFE_INTEGER; } } /** * Returns the last live event of this room. * "last" means latest timestamp. * Instead of using timestamps, it would be better to do the comparison based on the order of the homeserver DAG. * Unfortunately, this information is currently not available in the client. * See {@link https://github.com/matrix-org/matrix-js-sdk/issues/3325}. * "live of this room" means from all live timelines: the room and the threads. * * @returns MatrixEvent if there is a last event; else undefined. */ public getLastLiveEvent(): MatrixEvent | undefined { const roomEvents = this.getLiveTimeline().getEvents(); const lastRoomEvent = roomEvents[roomEvents.length - 1] as MatrixEvent | undefined; const lastThread = this.getLastThread(); if (!lastThread) return lastRoomEvent; const lastThreadEvent = lastThread.events[lastThread.events.length - 1]; return (lastRoomEvent?.getTs() ?? 0) > (lastThreadEvent?.getTs() ?? 0) ? lastRoomEvent : lastThreadEvent; } /** * Returns the last thread of this room. * "last" means latest timestamp of the last thread event. * Instead of using timestamps, it would be better to do the comparison based on the order of the homeserver DAG. * Unfortunately, this information is currently not available in the client. * See {@link https://github.com/matrix-org/matrix-js-sdk/issues/3325}. * * @returns the thread with the most recent event in its live time line. undefined if there is no thread. */ public getLastThread(): Thread | undefined { return this.getThreads().reduce<Thread | undefined>((lastThread: Thread | undefined, thread: Thread) => { if (!lastThread) return thread; const threadEvent = thread.events[thread.events.length - 1]; const lastThreadEvent = lastThread.events[lastThread.events.length - 1]; if ((threadEvent?.getTs() ?? 0) >= (lastThreadEvent?.getTs() ?? 0)) { // Last message of current thread is newer → new last thread. // Equal also means newer, because it was added to the thread map later. return thread; } return lastThread; }, undefined); } /** * @returns the membership type (join | leave | invite | knock) for the logged in user */ public getMyMembership(): Membership { return this.selfMembership ?? KnownMembership.Leave; } /** * If this room is a DM we're invited to, * try to find out who invited us * @returns user id of the inviter */ public getDMInviter(): string | undefined { const me = this.getMember(this.myUserId); if (me) { return me.getDMInviter(); } if (this.selfMembership === KnownMembership.Invite) { // fall back to summary information const memberCount = this.getInvitedAndJoinedMemberCount(); if (memberCount === 2) { return this.heroes?.[0]?.userId; } } } /** * Assuming this room is a DM room, tries to guess with which user. * @returns user id of the other member (could be syncing user) */ public guessDMUserId(): string { const me = this.getMember(this.myUserId); if (me) { const inviterId = me.getDMInviter(); if (inviterId) { return inviterId; } } // Remember, we're assuming this room is a DM, so returning the first member we find should be fine if (Array.isArray(this.heroes) && this.heroes.length) { return this.heroes[0].userId; } const members = this.currentState.getMembers(); const anyMember = members.find((m) => m.userId !== this.myUserId); if (anyMember) { return anyMember.userId; } // it really seems like I'm the only user in the room // so I probably created a room with just me in it // and marked it as a DM. Ok then return this.myUserId; } /** * Gets the "functional members" in this room. * * Returns the list of userIDs from the `io.element.functional_members` event. Does not consider the * current membership states of those users. * * @see https://github.com/element-hq/element-meta/blob/develop/spec/functional_members.md. */ private getFunctionalMembers(): string[] { const mFunctionalMembers = this.currentState.getStateEvents(UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name, ""); if (Array.isArray(mFunctionalMembers?.getContent().service_members)) { return mFunctionalMembers!.getContent().service_members; } return []; } public getAvatarFallbackMember(): RoomMember | undefined { const functionalMembers = this.getFunctionalMembers(); // Only generate a fallback avatar if the conversation is with a single specific other user (a "DM"). let nonFunctionalMemberCount = 0; this.getMembers()!.forEach((m) => { if (m.membership !== "join" && m.membership !== "invite") return; if (functionalMembers.includes(m.userId)) return; nonFunctionalMemberCount++; }); if (nonFunctionalMemberCount > 2) return; // Prefer the list of heroes, if present. It should only include the single other user in the DM. const nonFunctionalHeroes = this.heroes?.filter((h) => !functionalMembers.includes(h.userId)); const hasHeroes = Array.isArray(nonFunctionalHeroes) && nonFunctionalHeroes.length; if (hasHeroes) { // use first hero that has a display name or avatar url, or whose user ID // can be looked up as a member of the room for (const hero of nonFunctionalHeroes) { // If the hero was from a legacy sync (`/v3/sync`), we will need to look the user ID up in the room // the display name and avatar URL will not be set. if (!hero.fromMSC4186) { // attempt to look up renderable fields from the m.room.member event if it exists const member = this.getMember(hero.userId); if (member) { return member; } } else { // use the Hero supplied values for the room member. // TODO: It's unfortunate that this function, which clearly only cares about the // avatar url, returns the entire RoomMember event. We need to fake an event // to meet this API shape. const heroMember = new RoomMember(this.roomId, hero.userId); // set the display name and avatar url heroMember.setMembershipEvent( new MatrixEvent({ // ensure it's unique even if we hit the same millisecond event_id: "$" + this.roomId + hero.userId + new Date().getTime(), type: EventType.RoomMember, state_key: hero.userId, content: { displayname: hero.displayName, avatar_url: hero.avatarUrl, }, }), ); return heroMember; } } const availableMember = nonFunctionalHeroes .map((hero) => { return this.getMember(hero.userId); }) .find((member) => !!member); if (availableMember) { return availableMember; } } // Consider *all*, including previous, members, to generate the avatar for DMs where the other user left. // Needed to generate a matching avatar for rooms named "Empty Room (was Alice)". const members = this.getMembers(); const nonFunctionalMembers = members?.filter((m) => !functionalMembers.includes(m.userId)); if (nonFunctionalMembers.length <= 2) { const availableMember = nonFunctionalMembers.find((m) => { return m.userId !== this.myUserId; }); if (availableMember) { return availableMember; } } // If all else failed, but the homeserver gave us heroes that previously could not be found in the room members, // trust and try falling back to a hero, creating a one-off member for it if (hasHeroes) { const availableUser = nonFunctionalHeroes .map((hero) => { return this.client.getUser(hero.userId); }) .find((user) => !!user); if (availableUser) { const member = new RoomMember(this.roomId, availableUser.userId); member.user = availableUser; return member; } } } /** * Sets the membership this room was received as during sync * @param membership - join | leave | invite */ public updateMyMembership(membership: Membership): void { const prevMembership = this.selfMembership; this.selfMembership = membership; if (prevMembership !== membership) { if (membership === KnownMembership.Leave) { this.cleanupAfterLeaving(); } this.emit(RoomEvent.MyMembership, this, membership, prevMembership); } } private async loadMembersFromServer(): Promise<IStateEventWithRoomId[]> { const lastSyncToken = this.client.store.getSyncToken(); const response = await this.client.members( this.roomId, undefined, KnownMembership.Leave, lastSyncToken ?? undefined, ); return response.chunk; } private async loadMembers(): Promise<{ memberEvents: MatrixEvent[]; fromServer: boolean }> { // were the members loaded from the server? let fromServer = false; let rawMembersEvents = await this.client.store.getOutOfBandMembers(this.roomId); // If the room is encrypted, we always fetch members from the server at // least once, in case the latest state wasn't persisted properly. Note // that this function is only called once (unless loading the members // fails), since loadMembersIfNeeded always returns this.membersPromise // if set, which will be the result of the first (successful) call. if (rawMembersEvents === null || this.hasEncryptionStateEvent()) { fromServer = true; rawMembersEvents = await this.loadMembersFromServer(); logger.log(`LL: got ${rawMembersEvents.length} ` + `members from server for room ${this.roomId}`); } const memberEvents = rawMembersEvents.filter(noUnsafeEventProps).map(this.client.getEventMapper()); return { memberEvents, fromServer }; } /** * Check if loading of out-of-band-members has completed * * @returns true if the full membership list of this room has been loaded (including if lazy-loading is disabled). * False if the load is not started or is in progress. */ public membersLoaded(): boolean { if (!this.opts.lazyLoadMembers) { return true; } return this.currentState.outOfBandMembersReady(); } /** * Preloads the member list in case lazy loading * of memberships is in use. Can be called multiple times, * it will only preload once. * @returns when preloading is done and * accessing the members on the room will take * all members in the room into account */ public loadMembersIfNeeded(): Promise<boolean> { if (this.membersPromise) { return this.membersPromise; } // mark the state so that incoming messages while // the request is in flight get marked as superseding // the OOB members this.currentState.markOutOfBandMembersStarted(); const inMemoryUpdate = this.loadMembers() .then((result) => { this.currentState.setOutOfBandMembers(result.memberEvents); // recalculate the room name: it may have been based on members, so may have changed this.recalculate(); return result.fromServer; }) .catch((err) => { // allow retries on fail this.membersPromise = undefined; this.currentState.markOutOfBandMembersFailed(); throw err; }); // update members in storage, but don't wait for it inMemoryUpdate .then((fromServer) => { if (fromServer) { const oobMembers = this.currentState .getMembers() .filter((m) => m.isOutOfBand()) .map((m) => m.events.member?.event as IStateEventWithRoomId); logger.log(`LL: telling store to write ${oobMembers.length}` + ` members for room ${this.roomId}`); const store = this.client.store; return ( store .setOutOfBandMembers(this.roomId, oobMembers) // swallow any IDB error as we don't want to fail // because of this .catch((err) => { logger.log("LL: storing OOB room members failed, oh well", err); }) ); } }) .catch((err) => { // as this is not awaited anywhere, // at least show the error in the console logger.error(err); }); this.membersPromise = inMemoryUpdate; return this.membersPromise; } /** * Removes the lazily loaded members from storage if needed */ public async clearLoadedMembersIfNeeded(): Promise<void> { if (this.opts.lazyLoadMembers && this.membersPromise) { await this.loadMembersIfNeeded(); await this.client.store.clearOutOfBandMembers(this.roomId); this.currentState.clearOutOfBandMembers(); this.membersPromise = undefined; } } /** * called when sync receives this room in the leave section * to do cleanup after leaving a room. Possibly called multiple times. */ private cleanupAfterLeaving(): void { this.clearLoadedMembersIfNeeded().catch((err) => { logger.error(`error after clearing loaded members from ` + `room ${this.roomId} after leaving`); logger.log(err); }); } /** * Empty out the current live timeline and re-request it. This is used when * historical messages are imported into the room via MSC2716 `/batch_send` * because the client may already have that section of the timeline loaded. * We need to force the client to throw away their current timeline so that * when they back paginate over the area again with the historical messages * in between, it grabs the newly imported messages. We can listen for * `UNSTABLE_MSC2716_MARKER`, in order to tell when historical messages are ready * to be discovered in the room and the timeline needs a refresh. The SDK * emits a `RoomEvent.HistoryImportedWithinTimeline` event when we detect a * valid marker and can check the needs refresh status via * `room.getTimelineNeedsRefresh()`. */ public async refreshLiveTimeline(): Promise<void> { const liveTimelineBefore = this.getLiveTimeline(); const forwardPaginationToken = liveTimelineBefore.getPaginationToken(EventTimeline.FORWARDS); const backwardPaginationToken = liveTimelineBefore.getPaginationToken(EventTimeline.BACKWARDS); const eventsBefore = liveTimelineBefore.getEvents(); const mostRecentEventInTimeline = eventsBefore[eventsBefore.length - 1]; logger.log( `[refreshLiveTimeline for ${this.roomId}] at ` + `mostRecentEventInTimeline=${mostRecentEventInTimeline && mostRecentEventInTimeline.getId()} ` + `liveTimelineBefore=${liveTimelineBefore.toString()} ` + `forwardPaginationToken=${forwardPaginationToken} ` + `backwardPaginationToken=${backwardPaginationToken}`, ); // Get the main TimelineSet const timelineSet = this.getUnfilteredTimelineSet(); let newTimeline: EventTimeline | null = null; // If there isn't any event in the timeline, let's go fetch the latest // event and construct a timeline from it. // // This should only really happen if the user ran into an error // with refreshing the timeline before which left them in a blank // timeline from `resetLiveTimeline`. if (!mostRecentEventInTimeline) { newTimeline = await this.client.getLatestTimeline(timelineSet); } else { // Empty out all of `this.timelineSets`. But we also need to keep the // same `timelineSet` references around so the React code updates // properly and doesn't ignore the room events we emit because it checks // that the `timelineSet` references are the same. We need the // `