Merge the two different widget utils files

This commit is contained in:
David Baker 2018-06-26 11:52:21 +01:00
parent 069080e7ed
commit 0f2c47937c
5 changed files with 85 additions and 99 deletions

View file

@ -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) {

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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,

View file

@ -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,
};