Handle user widgets.

This commit is contained in:
Richard Lewis 2018-01-09 14:37:45 +00:00
parent 60e7646f92
commit 9e9de765dc

View file

@ -335,10 +335,15 @@ function setWidget(event, roomId) {
if (userWidget) { if (userWidget) {
console.warn('Adding user widget'); console.warn('Adding user widget');
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
let userWidgets = client.getAccountData('m.widgets') || {}; // client.setAccountData('m.widgets', {});
const userWidgets = client.getAccountData('m.widgets').getContent() || {};
// Delete existing widget with ID // Delete existing widget with ID
try {
delete userWidgets[widgetId]; delete userWidgets[widgetId];
} catch (e) {
console.error(`$widgetId is non-configurable`);
}
// Add new widget / update // Add new widget / update
if (widgetUrl !== null) { if (widgetUrl !== null) {
@ -346,15 +351,21 @@ function setWidget(event, roomId) {
content: content, content: content,
sender: client.getUserId(), sender: client.getUserId(),
stateKey: widgetId, stateKey: widgetId,
type: 'm.widget', type: 'im.vector.modular.widgets',
id: widgetId,
}; };
} }
client.setAccountData('m.widgets', {widgets: userWidgets}); client.setAccountData('m.widgets', userWidgets);
console.warn(`Set user widgets to:`, client.getAccountData('m.widgets'));
sendResponse(event, { sendResponse(event, {
success: true, success: true,
}); });
} else { // Room widget } else { // Room widget
if (!roomId) {
sendError(event, _t('Missing roomId.'), null);
}
if (widgetUrl === null) { // widget is being deleted if (widgetUrl === null) { // widget is being deleted
content = {}; content = {};
} }
@ -374,6 +385,9 @@ function getWidgets(event, roomId) {
sendError(event, _t('You need to be logged in.')); sendError(event, _t('You need to be logged in.'));
return; return;
} }
let widgetStateEvents = [];
if (roomId) {
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
if (!room) { if (!room) {
sendError(event, _t('This room is not recognised.')); sendError(event, _t('This room is not recognised.'));
@ -381,12 +395,20 @@ function getWidgets(event, roomId) {
} }
const stateEvents = room.currentState.getStateEvents("im.vector.modular.widgets"); const stateEvents = room.currentState.getStateEvents("im.vector.modular.widgets");
// Only return widgets which have required fields // Only return widgets which have required fields
const widgetStateEvents = []; if (room) {
stateEvents.forEach((ev) => { stateEvents.forEach((ev) => {
if (ev.getContent().type && ev.getContent().url) { if (ev.getContent().type && ev.getContent().url) {
widgetStateEvents.push(ev.event); // return the raw event widgetStateEvents.push(ev.event); // return the raw event
} }
}); });
}
}
// Add user widgets (not linked to a specific room)
const userWidgets = client.getAccountData('m.widgets').getContent() || {};
const userWidgetArray = Object.keys(userWidgets).map((key) => userWidgets[key]);
widgetStateEvents = widgetStateEvents.concat(userWidgetArray);
console.warn('Sending user widgets', userWidgetArray);
sendResponse(event, widgetStateEvents); sendResponse(event, widgetStateEvents);
} }
@ -595,6 +617,16 @@ const onMessage = function(event) {
const roomId = event.data.room_id; const roomId = event.data.room_id;
const userId = event.data.user_id; const userId = event.data.user_id;
// These APIs don't require roomId
if (event.data.action === "get_widgets") {
getWidgets(event, roomId);
return;
} else if (event.data.action === "set_widget") {
setWidget(event, roomId);
return;
}
if (!roomId) { if (!roomId) {
sendError(event, _t('Missing room_id in request')); sendError(event, _t('Missing room_id in request'));
return; return;
@ -628,12 +660,6 @@ const onMessage = function(event) {
} else if (event.data.action === "get_membership_count") { } else if (event.data.action === "get_membership_count") {
getMembershipCount(event, roomId); getMembershipCount(event, roomId);
return; return;
} else if (event.data.action === "set_widget") {
setWidget(event, roomId);
return;
} else if (event.data.action === "get_widgets") {
getWidgets(event, roomId);
return;
} else if (event.data.action === "get_room_enc_state") { } else if (event.data.action === "get_room_enc_state") {
getRoomEncState(event, roomId); getRoomEncState(event, roomId);
return; return;