diff --git a/src/CallHandler.js b/src/CallHandler.js index f431589fb2..852827f6fe 100644 --- a/src/CallHandler.js +++ b/src/CallHandler.js @@ -62,7 +62,6 @@ import dis from './dispatcher'; import { showUnknownDeviceDialogForCalls } from './cryptodevices'; import SettingsStore from "./settings/SettingsStore"; import WidgetUtils from './WidgetUtils'; -import { getRoomWidgets } from './utils/widgets'; global.mxCalls = { //room_id: MatrixCall @@ -414,7 +413,7 @@ function _startCallApp(roomId, type) { return; } - const currentJitsiWidgets = getRoomWidgets(room).filter((ev) => { + const currentJitsiWidgets = WidgetUtils.getRoomWidgets(room).filter((ev) => { return ev.getContent().type == 'jitsi'; }); if (currentJitsiWidgets.length > 0) { diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 9d0e274c7b..24c69194f5 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -236,7 +236,6 @@ import SdkConfig from './SdkConfig'; import MatrixClientPeg from './MatrixClientPeg'; import { MatrixEvent } from 'matrix-js-sdk'; import dis from './dispatcher'; -import Widgets from './utils/widgets'; import WidgetUtils from './WidgetUtils'; import RoomViewStore from './stores/RoomViewStore'; import { _t } from './languageHandler'; @@ -375,7 +374,7 @@ function getWidgets(event, roomId) { } // Add user widgets (not linked to a specific room) - const userWidgets = Widgets.getUserWidgetsArray(); + const userWidgets = WidgetUtils.getUserWidgetsArray(); widgetStateEvents = widgetStateEvents.concat(userWidgets); sendResponse(event, widgetStateEvents); diff --git a/src/WidgetUtils.js b/src/WidgetUtils.js index 453038d953..f54419f5e2 100644 --- a/src/WidgetUtils.js +++ b/src/WidgetUtils.js @@ -17,7 +17,6 @@ limitations under the License. import MatrixClientPeg from './MatrixClientPeg'; import SdkConfig from "./SdkConfig"; -import Widgets from './utils/widgets'; import dis from './dispatcher'; import * as url from "url"; @@ -202,7 +201,7 @@ export default class WidgetUtils { }; const client = MatrixClientPeg.get(); - const userWidgets = Widgets.getUserWidgets(); + const userWidgets = WidgetUtils.getUserWidgets(); // Delete existing widget with ID try { @@ -254,4 +253,83 @@ export default class WidgetUtils { return WidgetUtils.waitForRoomWidget(widgetId, roomId, widgetUrl !== null); }); } + + /** + * Get all widgets (user and room) for the current user + * @param {object} room The room to get widgets for + * @return {[object]} Array containing current / active room and user widget state events + */ + static getWidgets(room) { + const widgets = getRoomWidgets(room); + widgets.concat(getUserWidgetsArray()); + return widgets; + } + + /** + * Get room specific widgets + * @param {object} room The room to get widgets force + * @return {[object]} Array containing current / active room widgets + */ + static getRoomWidgets(room) { + const appsStateEvents = room.currentState.getStateEvents('im.vector.modular.widgets'); + if (!appsStateEvents) { + return []; + } + + return appsStateEvents.filter((ev) => { + return ev.getContent().type && ev.getContent().url; + }); + } + + /** + * Get user specific widgets (not linked to a specific room) + * @return {object} Event content object containing current / active user widgets + */ + static getUserWidgets() { + const client = MatrixClientPeg.get(); + if (!client) { + throw new Error('User not logged in'); + } + const userWidgets = client.getAccountData('m.widgets'); + let userWidgetContent = {}; + if (userWidgets && userWidgets.getContent()) { + userWidgetContent = userWidgets.getContent(); + } + return userWidgetContent; + } + + /** + * Get user specific widgets (not linked to a specific room) as an array + * @return {[object]} Array containing current / active user widgets + */ + static getUserWidgetsArray() { + return Object.values(WidgetUtils.getUserWidgets()); + } + + /** + * Get active stickerpicker widgets (stickerpickers are user widgets by nature) + * @return {[object]} Array containing current / active stickerpicker widgets + */ + static getStickerpickerWidgets() { + const widgets = WidgetUtils.getUserWidgetsArray(); + return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker"); + } + + /** + * Remove all stickerpicker widgets (stickerpickers are user widgets by nature) + * @return {Promise} Resolves on account data updated + */ + static removeStickerpickerWidgets() { + const client = MatrixClientPeg.get(); + if (!client) { + throw new Error('User not logged in'); + } + const userWidgets = client.getAccountData('m.widgets').getContent() || {}; + Object.entries(userWidgets).forEach(([key, widget]) => { + if (widget.content && widget.content.type === 'm.stickerpicker') { + delete userWidgets[key]; + } + }); + return client.setAccountData('m.widgets', userWidgets); + } } diff --git a/src/components/views/rooms/Stickerpicker.js b/src/components/views/rooms/Stickerpicker.js index 6152809c1a..992e5b7c53 100644 --- a/src/components/views/rooms/Stickerpicker.js +++ b/src/components/views/rooms/Stickerpicker.js @@ -15,7 +15,6 @@ limitations under the License. */ import React from 'react'; import { _t } from '../../../languageHandler'; -import Widgets from '../../../utils/widgets'; import AppTile from '../elements/AppTile'; import MatrixClientPeg from '../../../MatrixClientPeg'; import Modal from '../../../Modal'; @@ -24,6 +23,7 @@ import SdkConfig from '../../../SdkConfig'; import ScalarAuthClient from '../../../ScalarAuthClient'; import dis from '../../../dispatcher'; import AccessibleButton from '../elements/AccessibleButton'; +import WidgetUtils from '../../../WidgetUtils'; const widgetType = 'm.stickerpicker'; @@ -67,7 +67,7 @@ export default class Stickerpicker extends React.Component { } this.setState({showStickers: false}); - Widgets.removeStickerpickerWidgets().then(() => { + WidgetUtils.removeStickerpickerWidgets().then(() => { this.forceUpdate(); }).catch((e) => { console.error('Failed to remove sticker picker widget', e); @@ -119,7 +119,7 @@ export default class Stickerpicker extends React.Component { } _updateWidget() { - const stickerpickerWidget = Widgets.getStickerpickerWidgets()[0]; + const stickerpickerWidget = WidgetUtils.getStickerpickerWidgets()[0]; this.setState({ stickerpickerWidget, widgetId: stickerpickerWidget ? stickerpickerWidget.id : null, diff --git a/src/utils/widgets.js b/src/utils/widgets.js deleted file mode 100644 index 338df184e2..0000000000 --- a/src/utils/widgets.js +++ /dev/null @@ -1,90 +0,0 @@ -import MatrixClientPeg from '../MatrixClientPeg'; - -/** - * Get all widgets (user and room) for the current user - * @param {object} room The room to get widgets for - * @return {[object]} Array containing current / active room and user widget state events - */ -function getWidgets(room) { - const widgets = getRoomWidgets(room); - widgets.concat(getUserWidgetsArray()); - return widgets; -} - -/** - * Get room specific widgets - * @param {object} room The room to get widgets force - * @return {[object]} Array containing current / active room widgets - */ -function getRoomWidgets(room) { - const appsStateEvents = room.currentState.getStateEvents('im.vector.modular.widgets'); - if (!appsStateEvents) { - return []; - } - - return appsStateEvents.filter((ev) => { - return ev.getContent().type && ev.getContent().url; - }); -} - -/** - * Get user specific widgets (not linked to a specific room) - * @return {object} Event content object containing current / active user widgets - */ -function getUserWidgets() { - const client = MatrixClientPeg.get(); - if (!client) { - throw new Error('User not logged in'); - } - const userWidgets = client.getAccountData('m.widgets'); - let userWidgetContent = {}; - if (userWidgets && userWidgets.getContent()) { - userWidgetContent = userWidgets.getContent(); - } - return userWidgetContent; -} - -/** - * Get user specific widgets (not linked to a specific room) as an array - * @return {[object]} Array containing current / active user widgets - */ -function getUserWidgetsArray() { - return Object.values(getUserWidgets()); -} - -/** - * Get active stickerpicker widgets (stickerpickers are user widgets by nature) - * @return {[object]} Array containing current / active stickerpicker widgets - */ -function getStickerpickerWidgets() { - const widgets = getUserWidgetsArray(); - return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker"); -} - -/** - * Remove all stickerpicker widgets (stickerpickers are user widgets by nature) - * @return {Promise} Resolves on account data updated - */ -function removeStickerpickerWidgets() { - const client = MatrixClientPeg.get(); - if (!client) { - throw new Error('User not logged in'); - } - const userWidgets = client.getAccountData('m.widgets').getContent() || {}; - Object.entries(userWidgets).forEach(([key, widget]) => { - if (widget.content && widget.content.type === 'm.stickerpicker') { - delete userWidgets[key]; - } - }); - return client.setAccountData('m.widgets', userWidgets); -} - - -export default { - getWidgets, - getRoomWidgets, - getUserWidgets, - getUserWidgetsArray, - getStickerpickerWidgets, - removeStickerpickerWidgets, -};