Move widget utility functions in to their own file.
This commit is contained in:
parent
52f28d09e0
commit
1a994b88f5
2 changed files with 59 additions and 4 deletions
|
@ -235,6 +235,7 @@ const SdkConfig = require('./SdkConfig');
|
||||||
const MatrixClientPeg = require("./MatrixClientPeg");
|
const MatrixClientPeg = require("./MatrixClientPeg");
|
||||||
const MatrixEvent = require("matrix-js-sdk").MatrixEvent;
|
const MatrixEvent = require("matrix-js-sdk").MatrixEvent;
|
||||||
const dis = require("./dispatcher");
|
const dis = require("./dispatcher");
|
||||||
|
const Widgets = require('./utils/widgets');
|
||||||
import { _t } from './languageHandler';
|
import { _t } from './languageHandler';
|
||||||
|
|
||||||
function sendResponse(event, res) {
|
function sendResponse(event, res) {
|
||||||
|
@ -405,10 +406,8 @@ function getWidgets(event, roomId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add user widgets (not linked to a specific room)
|
// Add user widgets (not linked to a specific room)
|
||||||
const userWidgets = client.getAccountData('m.widgets').getContent() || {};
|
const userWidgets = Widgets.getUserWidgets();
|
||||||
const userWidgetArray = Object.keys(userWidgets).map((key) => userWidgets[key]);
|
widgetStateEvents = widgetStateEvents.concat(userWidgets);
|
||||||
widgetStateEvents = widgetStateEvents.concat(userWidgetArray);
|
|
||||||
console.warn('Sending user widgets', userWidgetArray);
|
|
||||||
|
|
||||||
sendResponse(event, widgetStateEvents);
|
sendResponse(event, widgetStateEvents);
|
||||||
}
|
}
|
||||||
|
|
56
src/utils/widgets.js
Normal file
56
src/utils/widgets.js
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
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(getUserWidgets());
|
||||||
|
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]} Array 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').getContent() || {};
|
||||||
|
return Object.keys(userWidgets).map((key) => userWidgets[key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get active stickerpack widgets (stickerpacks are user widgets by nature)
|
||||||
|
* @return {[object]} Array containing current / active stickerpack widgets
|
||||||
|
*/
|
||||||
|
function getStickerpackWidgets() {
|
||||||
|
return getUserWidgets().filter((widget) => widget.type='stickerpack');
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
getWidgets,
|
||||||
|
getRoomWidgets,
|
||||||
|
getUserWidgets,
|
||||||
|
getStickerpackWidgets,
|
||||||
|
};
|
Loading…
Reference in a new issue