From b710d42832579c3d1543f42e4bbf0307e610a4f4 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 30 Sep 2020 16:12:00 -0600 Subject: [PATCH] Fix stickerpicker to use new messaging --- src/components/views/rooms/Stickerpicker.js | 9 ++- src/stores/widgets/StopGapWidget.ts | 68 +++++++++++++++++++-- src/stores/widgets/WidgetMessagingStore.ts | 11 ++++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/components/views/rooms/Stickerpicker.js b/src/components/views/rooms/Stickerpicker.js index 548e1d02bb..039d2571f4 100644 --- a/src/components/views/rooms/Stickerpicker.js +++ b/src/components/views/rooms/Stickerpicker.js @@ -30,6 +30,7 @@ import {ContextMenu} from "../../structures/ContextMenu"; import {WidgetType} from "../../../widgets/WidgetType"; import AccessibleTooltipButton from "../elements/AccessibleTooltipButton"; import {Action} from "../../../dispatcher/actions"; +import {WidgetMessagingStore} from "../../../stores/widgets/WidgetMessagingStore"; // This should be below the dialog level (4000), but above the rest of the UI (1000-2000). // We sit in a context menu, so this should be given to the context menu. @@ -213,9 +214,11 @@ export default class Stickerpicker extends React.Component { _sendVisibilityToWidget(visible) { if (!this.state.stickerpickerWidget) return; // TODO: [TravisR] Fix this - const widgetMessaging = ActiveWidgetStore.getWidgetMessaging(this.state.stickerpickerWidget.id); - if (widgetMessaging && visible !== this._prevSentVisibility) { - widgetMessaging.sendVisibility(visible); + const messaging = WidgetMessagingStore.instance.getMessagingForId(this.state.stickerpickerWidget.id); + if (messaging && visible !== this._prevSentVisibility) { + messaging.updateVisibility(visible).catch(err => { + console.error("Error updating widget visibility: ", err); + }); this._prevSentVisibility = visible; } } diff --git a/src/stores/widgets/StopGapWidget.ts b/src/stores/widgets/StopGapWidget.ts index 4b6ce70a6e..073073abec 100644 --- a/src/stores/widgets/StopGapWidget.ts +++ b/src/stores/widgets/StopGapWidget.ts @@ -16,12 +16,12 @@ import {Room} from "matrix-js-sdk/src/models/room"; import { - ClientWidgetApi, + ClientWidgetApi, IStickerActionRequest, IStickyActionRequest, - IWidget, + IWidget, IWidgetApiRequest, IWidgetApiRequestEmptyData, IWidgetData, - Widget + Widget, WidgetApiFromWidgetAction } from "matrix-widget-api"; import { StopGapWidgetDriver } from "./StopGapWidgetDriver"; import { EventEmitter } from "events"; @@ -35,6 +35,9 @@ import SettingsStore from "../../settings/SettingsStore"; import { WidgetType } from "../../widgets/WidgetType"; import { Capability } from "../../widgets/WidgetApi"; import ActiveWidgetStore from "../ActiveWidgetStore"; +import { objectShallowClone } from "../../utils/objects"; +import defaultDispatcher from "../../dispatcher/dispatcher"; +import dis from "../../dispatcher/dispatcher"; // TODO: Destroy all of this code @@ -88,7 +91,15 @@ export class StopGapWidget extends EventEmitter { constructor(private appTileProps: IAppTileProps) { super(); - this.mockWidget = new ElementWidget(appTileProps.app); + let app = appTileProps.app; + + // Backwards compatibility: not all old widgets have a creatorUserId + if (!app.creatorUserId) { + app = objectShallowClone(app); // clone to prevent accidental mutation + app.creatorUserId = MatrixClientPeg.get().getUserId(); + } + + this.mockWidget = new ElementWidget(app); } public get widgetApi(): ClientWidgetApi { @@ -166,6 +177,55 @@ export class StopGapWidget extends EventEmitter { } }, ); + } else if (WidgetType.STICKERPICKER.matches(this.mockWidget.type)) { + this.messaging.addEventListener("action:integration_manager_open", + (ev: CustomEvent) => { + // Acknowledge first + ev.preventDefault(); + this.messaging.transport.reply(ev.detail, {}); + + // First close the stickerpicker + defaultDispatcher.dispatch({action: "stickerpicker_close"}); + + // Now open the integration manager + // TODO: Spec this interaction. + const data = ev.detail.data; + const integType = data?.integType + const integId = data?.integId; + + // TODO: Open the right integration manager for the widget + if (SettingsStore.getValue("feature_many_integration_managers")) { + IntegrationManagers.sharedInstance().openAll( + MatrixClientPeg.get().getRoom(RoomViewStore.getRoomId()), + `type_${integType}`, + integId, + ); + } else { + IntegrationManagers.sharedInstance().getPrimaryManager().open( + MatrixClientPeg.get().getRoom(RoomViewStore.getRoomId()), + `type_${integType}`, + integId, + ); + } + }, + ); + + // TODO: Replace this event listener with appropriate driver functionality once the API + // establishes a sane way to send events back and forth. + this.messaging.addEventListener(`action:${WidgetApiFromWidgetAction.SendSticker}`, + (ev: CustomEvent) => { + // Acknowledge first + ev.preventDefault(); + this.messaging.transport.reply(ev.detail, {}); + + // Send the sticker + defaultDispatcher.dispatch({ + action: 'm.sticker', + data: ev.detail.data, + widgetId: this.mockWidget.id, + }); + }, + ); } } diff --git a/src/stores/widgets/WidgetMessagingStore.ts b/src/stores/widgets/WidgetMessagingStore.ts index 34fc2c4e04..828465ce84 100644 --- a/src/stores/widgets/WidgetMessagingStore.ts +++ b/src/stores/widgets/WidgetMessagingStore.ts @@ -28,6 +28,7 @@ import { EnhancedMap } from "../../utils/maps"; export class WidgetMessagingStore extends AsyncStoreWithClient { private static internalInstance = new WidgetMessagingStore(); + // TODO: Fix uniqueness problem (widget IDs are not unique across the whole app) private widgetMap = new EnhancedMap(); // public constructor() { @@ -59,4 +60,14 @@ export class WidgetMessagingStore extends AsyncStoreWithClient { public getMessaging(widget: Widget): ClientWidgetApi { return this.widgetMap.get(widget.id); } + + /** + * Gets the widget messaging class for a given widget ID. + * @param {string} widgetId The widget ID. + * @returns {ClientWidgetApi} The widget API, or a falsey value if not found. + * @deprecated Widget IDs are not globally unique. + */ + public getMessagingForId(widgetId: string): ClientWidgetApi { + return this.widgetMap.get(widgetId); + } }