2018-07-03 10:16:44 +00:00
|
|
|
/*
|
2021-04-06 11:26:50 +00:00
|
|
|
Copyright 2018-2021 The Matrix.org Foundation C.I.C.
|
2018-07-03 10:16:44 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import EventEmitter from "events";
|
|
|
|
import { IWidget } from "matrix-widget-api";
|
2021-06-17 13:24:53 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-06-17 13:24:53 +00:00
|
|
|
import { WidgetType } from "../widgets/WidgetType";
|
2018-07-03 10:16:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Acts as a place to get & set widget state, storing local echo state and
|
|
|
|
* proxying through state from the js-sdk.
|
|
|
|
*/
|
|
|
|
class WidgetEchoStore extends EventEmitter {
|
2021-04-26 13:02:53 +00:00
|
|
|
private roomWidgetEcho: {
|
2021-04-06 11:26:50 +00:00
|
|
|
[roomId: string]: {
|
2021-07-01 22:23:03 +00:00
|
|
|
[widgetId: string]: IWidget;
|
|
|
|
};
|
2021-04-06 11:26:50 +00:00
|
|
|
};
|
|
|
|
|
2018-07-03 10:16:44 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2021-04-26 13:02:53 +00:00
|
|
|
this.roomWidgetEcho = {
|
2018-07-05 18:40:45 +00:00
|
|
|
// Map as below. Object is the content of the widget state event,
|
|
|
|
// so for widgets that have been deleted locally, the object is empty.
|
2018-07-03 10:16:44 +00:00
|
|
|
// roomId: {
|
2021-04-26 14:38:43 +00:00
|
|
|
// widgetId: IWidget
|
2018-07-03 10:16:44 +00:00
|
|
|
// }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-09 22:52:05 +00:00
|
|
|
* Gets the widgets for a room, subtracting those that are pending deletion.
|
2018-07-03 10:16:44 +00:00
|
|
|
* Widgets that are pending addition are not included, since widgets are
|
2022-05-09 22:52:05 +00:00
|
|
|
* represented as MatrixEvents, so to do this we'd have to create fake MatrixEvents,
|
2018-07-03 10:16:44 +00:00
|
|
|
* and we don't really need the actual widget events anyway since we just want to
|
|
|
|
* show a spinner / prevent widgets being added twice.
|
2018-07-03 10:22:19 +00:00
|
|
|
*
|
2021-04-26 14:38:43 +00:00
|
|
|
* @param {string} roomId The ID of the room to get widgets for
|
2018-07-03 10:22:19 +00:00
|
|
|
* @param {MatrixEvent[]} currentRoomWidgets Current widgets for the room
|
|
|
|
* @returns {MatrixEvent[]} List of widgets in the room, minus any pending removal
|
2018-07-03 10:16:44 +00:00
|
|
|
*/
|
2021-04-26 14:38:43 +00:00
|
|
|
getEchoedRoomWidgets(roomId: string, currentRoomWidgets: MatrixEvent[]): MatrixEvent[] {
|
2018-07-03 10:16:44 +00:00
|
|
|
const echoedWidgets = [];
|
|
|
|
|
2021-04-26 13:02:53 +00:00
|
|
|
const roomEchoState = Object.assign({}, this.roomWidgetEcho[roomId]);
|
2018-07-03 10:16:44 +00:00
|
|
|
|
|
|
|
for (const w of currentRoomWidgets) {
|
|
|
|
const widgetId = w.getStateKey();
|
2018-07-05 18:46:37 +00:00
|
|
|
// If there's no echo, or the echo still has a widget present, show the *old* widget
|
2018-07-16 12:18:10 +00:00
|
|
|
// we don't include widgets that have changed for the same reason we don't include new ones,
|
2020-10-21 15:06:22 +00:00
|
|
|
// ie. we'd need to fake matrix events to do so and there's currently no need.
|
2018-07-05 18:46:37 +00:00
|
|
|
if (!roomEchoState[widgetId] || Object.keys(roomEchoState[widgetId]).length !== 0) {
|
2018-07-03 10:16:44 +00:00
|
|
|
echoedWidgets.push(w);
|
|
|
|
}
|
|
|
|
delete roomEchoState[widgetId];
|
|
|
|
}
|
|
|
|
|
|
|
|
return echoedWidgets;
|
|
|
|
}
|
|
|
|
|
2021-04-26 14:38:43 +00:00
|
|
|
roomHasPendingWidgetsOfType(roomId: string, currentRoomWidgets: MatrixEvent[], type?: WidgetType): boolean {
|
2021-04-26 13:02:53 +00:00
|
|
|
const roomEchoState = Object.assign({}, this.roomWidgetEcho[roomId]);
|
2018-07-03 10:16:44 +00:00
|
|
|
|
2018-07-05 18:57:24 +00:00
|
|
|
// any widget IDs that are already in the room are not pending, so
|
|
|
|
// echoes for them don't count as pending.
|
2018-07-03 10:16:44 +00:00
|
|
|
for (const w of currentRoomWidgets) {
|
|
|
|
const widgetId = w.getStateKey();
|
|
|
|
delete roomEchoState[widgetId];
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:57:24 +00:00
|
|
|
// if there's anything left then there are pending widgets.
|
2018-07-03 10:16:44 +00:00
|
|
|
if (type === undefined) {
|
|
|
|
return Object.keys(roomEchoState).length > 0;
|
|
|
|
} else {
|
|
|
|
return Object.values(roomEchoState).some((widget) => {
|
2020-04-09 21:11:57 +00:00
|
|
|
return type.matches(widget.type);
|
2018-07-03 10:16:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 14:38:43 +00:00
|
|
|
roomHasPendingWidgets(roomId: string, currentRoomWidgets: MatrixEvent[]): boolean {
|
2018-07-05 17:43:20 +00:00
|
|
|
return this.roomHasPendingWidgetsOfType(roomId, currentRoomWidgets);
|
2018-07-03 10:16:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:26:50 +00:00
|
|
|
setRoomWidgetEcho(roomId: string, widgetId: string, state: IWidget) {
|
2021-04-26 13:02:53 +00:00
|
|
|
if (this.roomWidgetEcho[roomId] === undefined) this.roomWidgetEcho[roomId] = {};
|
2018-07-03 10:16:44 +00:00
|
|
|
|
2021-04-26 13:02:53 +00:00
|
|
|
this.roomWidgetEcho[roomId][widgetId] = state;
|
2022-12-12 11:24:14 +00:00
|
|
|
this.emit("update", roomId, widgetId);
|
2018-07-03 10:16:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:38:43 +00:00
|
|
|
removeRoomWidgetEcho(roomId: string, widgetId: string) {
|
2021-04-26 13:02:53 +00:00
|
|
|
delete this.roomWidgetEcho[roomId][widgetId];
|
|
|
|
if (Object.keys(this.roomWidgetEcho[roomId]).length === 0) delete this.roomWidgetEcho[roomId];
|
2022-12-12 11:24:14 +00:00
|
|
|
this.emit("update", roomId, widgetId);
|
2018-07-03 10:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 14:57:25 +00:00
|
|
|
let singletonWidgetEchoStore: WidgetEchoStore | null = null;
|
2018-07-03 10:16:44 +00:00
|
|
|
if (!singletonWidgetEchoStore) {
|
|
|
|
singletonWidgetEchoStore = new WidgetEchoStore();
|
|
|
|
}
|
2019-12-20 00:45:24 +00:00
|
|
|
export default singletonWidgetEchoStore;
|