element-web/src/utils/widgets.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

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);
2018-03-09 09:15:16 +00:00
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)
2018-03-09 09:15:16 +00:00
* @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');
}
2018-02-07 10:05:50 +00:00
const userWidgets = client.getAccountData('m.widgets');
let userWidgetContent = {};
if (userWidgets && userWidgets.getContent()) {
userWidgetContent = userWidgets.getContent();
}
2018-03-09 09:15:16 +00:00
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() {
2018-03-29 16:52:34 +00:00
return Object.values(getUserWidgets());
}
/**
2018-02-25 22:10:38 +00:00
* Get active stickerpicker widgets (stickerpickers are user widgets by nature)
* @return {[object]} Array containing current / active stickerpicker widgets
*/
2018-02-25 22:10:38 +00:00
function getStickerpickerWidgets() {
2018-03-09 09:15:16 +00:00
const widgets = getUserWidgetsArray();
2018-02-25 22:10:38 +00:00
const stickerpickerWidgets = widgets.filter((widget) => widget.type='m.stickerpicker');
return stickerpickerWidgets;
}
2018-02-05 11:49:26 +00:00
/**
2018-02-25 22:10:38 +00:00
* Remove all stickerpicker widgets (stickerpickers are user widgets by nature)
* @return {Promise} Resolves on account data updated
2018-02-05 11:49:26 +00:00
*/
2018-02-25 22:10:38 +00:00
function removeStickerpickerWidgets() {
2018-02-05 11:49:26 +00:00
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]) => {
2018-02-25 22:10:38 +00:00
if (widget.type === 'm.stickerpicker') {
2018-02-05 11:49:26 +00:00
delete userWidgets[key];
}
});
2018-03-29 19:37:24 +00:00
return client.setAccountData('m.widgets', userWidgets);
2018-02-05 11:49:26 +00:00
}
export default {
getWidgets,
getRoomWidgets,
getUserWidgets,
2018-03-09 09:15:16 +00:00
getUserWidgetsArray,
2018-02-25 22:10:38 +00:00
getStickerpickerWidgets,
removeStickerpickerWidgets,
};