2020-09-07 15:19:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 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 { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2020-10-19 19:56:41 +00:00
|
|
|
import { IWidget } from "matrix-widget-api";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
2020-09-07 15:19:42 +00:00
|
|
|
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import WidgetEchoStore from "../stores/WidgetEchoStore";
|
2020-09-16 20:35:50 +00:00
|
|
|
import ActiveWidgetStore from "../stores/ActiveWidgetStore";
|
2020-09-07 15:19:42 +00:00
|
|
|
import WidgetUtils from "../utils/WidgetUtils";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { WidgetType } from "../widgets/WidgetType";
|
|
|
|
import { UPDATE_EVENT } from "./AsyncStore";
|
2020-09-07 15:19:42 +00:00
|
|
|
|
|
|
|
interface IState {}
|
|
|
|
|
2020-10-19 19:56:41 +00:00
|
|
|
export interface IApp extends IWidget {
|
2020-09-07 15:19:42 +00:00
|
|
|
roomId: string;
|
|
|
|
eventId: string;
|
|
|
|
// eslint-disable-next-line camelcase
|
2022-08-30 19:13:39 +00:00
|
|
|
avatar_url?: string; // MSC2765 https://github.com/matrix-org/matrix-doc/pull/2765
|
2020-09-07 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IRoomWidgets {
|
|
|
|
widgets: IApp[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO consolidate WidgetEchoStore into this
|
|
|
|
// TODO consolidate ActiveWidgetStore into this
|
2020-09-08 09:19:51 +00:00
|
|
|
export default class WidgetStore extends AsyncStoreWithClient<IState> {
|
2022-08-30 19:13:39 +00:00
|
|
|
private static readonly internalInstance = (() => {
|
|
|
|
const instance = new WidgetStore();
|
|
|
|
instance.start();
|
|
|
|
return instance;
|
|
|
|
})();
|
2020-09-07 15:19:42 +00:00
|
|
|
|
2020-12-02 19:20:59 +00:00
|
|
|
private widgetMap = new Map<string, IApp>(); // Key is widget Unique ID (UID)
|
2020-12-01 20:19:51 +00:00
|
|
|
private roomMap = new Map<string, IRoomWidgets>(); // Key is room ID
|
2020-09-07 15:19:42 +00:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
super(defaultDispatcher, {});
|
|
|
|
|
|
|
|
WidgetEchoStore.on("update", this.onWidgetEchoStoreUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get instance(): WidgetStore {
|
|
|
|
return WidgetStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private initRoom(roomId: string) {
|
|
|
|
if (!this.roomMap.has(roomId)) {
|
|
|
|
this.roomMap.set(roomId, {
|
|
|
|
widgets: [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onReady(): Promise<any> {
|
2022-02-22 12:18:08 +00:00
|
|
|
this.matrixClient.on(ClientEvent.Room, this.onRoom);
|
|
|
|
this.matrixClient.on(RoomStateEvent.Events, this.onRoomStateEvents);
|
2020-09-07 15:19:42 +00:00
|
|
|
this.matrixClient.getRooms().forEach((room: Room) => {
|
|
|
|
this.loadRoomWidgets(room);
|
|
|
|
});
|
2021-01-20 21:44:30 +00:00
|
|
|
this.emit(UPDATE_EVENT, null); // emit for all rooms
|
2020-09-07 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async onNotReady(): Promise<any> {
|
2022-02-22 12:18:08 +00:00
|
|
|
this.matrixClient.off(ClientEvent.Room, this.onRoom);
|
|
|
|
this.matrixClient.off(RoomStateEvent.Events, this.onRoomStateEvents);
|
2020-09-07 15:19:42 +00:00
|
|
|
this.widgetMap = new Map();
|
|
|
|
this.roomMap = new Map();
|
|
|
|
await this.reset({});
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need this, but our contract says we do.
|
|
|
|
protected async onAction(payload: ActionPayload) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 12:15:19 +00:00
|
|
|
private onWidgetEchoStoreUpdate = (roomId: string, widgetId: string) => {
|
2020-09-07 15:19:42 +00:00
|
|
|
this.initRoom(roomId);
|
|
|
|
this.loadRoomWidgets(this.matrixClient.getRoom(roomId));
|
2021-01-20 21:44:30 +00:00
|
|
|
this.emit(UPDATE_EVENT, roomId);
|
2020-09-09 12:15:19 +00:00
|
|
|
};
|
2020-09-07 15:19:42 +00:00
|
|
|
|
|
|
|
private generateApps(room: Room): IApp[] {
|
|
|
|
return WidgetEchoStore.getEchoedRoomWidgets(room.roomId, WidgetUtils.getRoomWidgets(room)).map((ev) => {
|
|
|
|
return WidgetUtils.makeAppConfig(
|
|
|
|
ev.getStateKey(), ev.getContent(), ev.getSender(), ev.getRoomId(), ev.getId(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadRoomWidgets(room: Room) {
|
2020-10-01 11:30:41 +00:00
|
|
|
if (!room) return;
|
2021-01-19 03:26:47 +00:00
|
|
|
const roomInfo = this.roomMap.get(room.roomId) || <IRoomWidgets>{};
|
2020-09-07 15:19:42 +00:00
|
|
|
roomInfo.widgets = [];
|
2020-10-21 15:06:22 +00:00
|
|
|
|
|
|
|
// first clean out old widgets from the map which originate from this room
|
|
|
|
// otherwise we are out of sync with the rest of the app with stale widget events during removal
|
|
|
|
Array.from(this.widgetMap.values()).forEach(app => {
|
2020-12-11 03:57:20 +00:00
|
|
|
if (app.roomId !== room.roomId) return; // skip - wrong room
|
2022-03-15 12:15:26 +00:00
|
|
|
this.widgetMap.delete(WidgetUtils.getWidgetUid(app));
|
2020-10-21 15:06:22 +00:00
|
|
|
});
|
|
|
|
|
2021-01-19 03:26:47 +00:00
|
|
|
let edited = false;
|
2020-09-07 15:19:42 +00:00
|
|
|
this.generateApps(room).forEach(app => {
|
2020-12-01 20:19:51 +00:00
|
|
|
// Sanity check for https://github.com/vector-im/element-web/issues/15705
|
2022-03-15 12:15:26 +00:00
|
|
|
const existingApp = this.widgetMap.get(WidgetUtils.getWidgetUid(app));
|
2020-12-01 20:19:51 +00:00
|
|
|
if (existingApp) {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn(
|
2020-12-01 20:19:51 +00:00
|
|
|
`Possible widget ID conflict for ${app.id} - wants to store in room ${app.roomId} ` +
|
|
|
|
`but is currently stored as ${existingApp.roomId} - letting the want win`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
this.widgetMap.set(WidgetUtils.getWidgetUid(app), app);
|
2020-09-07 15:19:42 +00:00
|
|
|
roomInfo.widgets.push(app);
|
2021-01-19 03:26:47 +00:00
|
|
|
edited = true;
|
2020-09-07 15:19:42 +00:00
|
|
|
});
|
2021-01-19 03:26:47 +00:00
|
|
|
if (edited && !this.roomMap.has(room.roomId)) {
|
|
|
|
this.roomMap.set(room.roomId, roomInfo);
|
|
|
|
}
|
2021-08-12 17:36:57 +00:00
|
|
|
|
|
|
|
// If a persistent widget is active, check to see if it's just been removed.
|
|
|
|
// If it has, it needs to destroyed otherwise unmounting the node won't kill it
|
2021-09-26 17:57:02 +00:00
|
|
|
const persistentWidgetId = ActiveWidgetStore.instance.getPersistentWidgetId();
|
2022-03-15 12:15:26 +00:00
|
|
|
if (
|
|
|
|
persistentWidgetId &&
|
|
|
|
ActiveWidgetStore.instance.getPersistentRoomId() === room.roomId &&
|
|
|
|
!roomInfo.widgets.some(w => w.id === persistentWidgetId)
|
|
|
|
) {
|
|
|
|
logger.log(`Persistent widget ${persistentWidgetId} removed from room ${room.roomId}: destroying.`);
|
|
|
|
ActiveWidgetStore.instance.destroyPersistentWidget(persistentWidgetId, room.roomId);
|
2021-08-12 17:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:19:42 +00:00
|
|
|
this.emit(room.roomId);
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:49:18 +00:00
|
|
|
private onRoom = (room: Room) => {
|
|
|
|
this.initRoom(room.roomId);
|
|
|
|
this.loadRoomWidgets(room);
|
|
|
|
this.emit(UPDATE_EVENT, room.roomId);
|
|
|
|
};
|
|
|
|
|
2020-09-09 12:15:19 +00:00
|
|
|
private onRoomStateEvents = (ev: MatrixEvent) => {
|
2021-01-19 03:26:47 +00:00
|
|
|
if (ev.getType() !== "im.vector.modular.widgets") return; // TODO: Support m.widget too
|
2020-09-07 15:19:42 +00:00
|
|
|
const roomId = ev.getRoomId();
|
|
|
|
this.initRoom(roomId);
|
|
|
|
this.loadRoomWidgets(this.matrixClient.getRoom(roomId));
|
2021-01-20 21:44:30 +00:00
|
|
|
this.emit(UPDATE_EVENT, roomId);
|
2020-09-09 12:15:19 +00:00
|
|
|
};
|
2020-09-07 15:19:42 +00:00
|
|
|
|
2021-01-27 21:40:04 +00:00
|
|
|
public getRoom = (roomId: string, initIfNeeded = false) => {
|
|
|
|
if (initIfNeeded) this.initRoom(roomId); // internally handles "if needed"
|
2020-09-07 15:19:42 +00:00
|
|
|
return this.roomMap.get(roomId);
|
|
|
|
};
|
|
|
|
|
2020-10-15 10:14:48 +00:00
|
|
|
public getApps(roomId: string): IApp[] {
|
|
|
|
const roomInfo = this.getRoom(roomId);
|
|
|
|
return roomInfo?.widgets || [];
|
2020-09-07 15:19:42 +00:00
|
|
|
}
|
2020-09-16 20:35:50 +00:00
|
|
|
|
|
|
|
public doesRoomHaveConference(room: Room): boolean {
|
|
|
|
const roomInfo = this.getRoom(room.roomId);
|
|
|
|
if (!roomInfo) return false;
|
|
|
|
|
|
|
|
const currentWidgets = roomInfo.widgets.filter(w => WidgetType.JITSI.matches(w.type));
|
|
|
|
const hasPendingWidgets = WidgetEchoStore.roomHasPendingWidgetsOfType(room.roomId, [], WidgetType.JITSI);
|
|
|
|
return currentWidgets.length > 0 || hasPendingWidgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isJoinedToConferenceIn(room: Room): boolean {
|
|
|
|
const roomInfo = this.getRoom(room.roomId);
|
|
|
|
if (!roomInfo) return false;
|
|
|
|
|
|
|
|
// A persistent conference widget indicates that we're participating
|
|
|
|
const widgets = roomInfo.widgets.filter(w => WidgetType.JITSI.matches(w.type));
|
2022-03-15 12:15:26 +00:00
|
|
|
return widgets.some(w => ActiveWidgetStore.instance.getWidgetPersistence(w.id, room.roomId));
|
2020-09-16 20:35:50 +00:00
|
|
|
}
|
2020-09-07 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.mxWidgetStore = WidgetStore.instance;
|