Handle user widgets.
This commit is contained in:
parent
60e7646f92
commit
9e9de765dc
1 changed files with 48 additions and 22 deletions
|
@ -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
|
||||||
delete userWidgets[widgetId];
|
try {
|
||||||
|
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,19 +385,30 @@ function getWidgets(event, roomId) {
|
||||||
sendError(event, _t('You need to be logged in.'));
|
sendError(event, _t('You need to be logged in.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const room = client.getRoom(roomId);
|
let widgetStateEvents = [];
|
||||||
if (!room) {
|
|
||||||
sendError(event, _t('This room is not recognised.'));
|
if (roomId) {
|
||||||
return;
|
const room = client.getRoom(roomId);
|
||||||
}
|
if (!room) {
|
||||||
const stateEvents = room.currentState.getStateEvents("im.vector.modular.widgets");
|
sendError(event, _t('This room is not recognised.'));
|
||||||
// Only return widgets which have required fields
|
return;
|
||||||
const widgetStateEvents = [];
|
|
||||||
stateEvents.forEach((ev) => {
|
|
||||||
if (ev.getContent().type && ev.getContent().url) {
|
|
||||||
widgetStateEvents.push(ev.event); // return the raw event
|
|
||||||
}
|
}
|
||||||
});
|
const stateEvents = room.currentState.getStateEvents("im.vector.modular.widgets");
|
||||||
|
// Only return widgets which have required fields
|
||||||
|
if (room) {
|
||||||
|
stateEvents.forEach((ev) => {
|
||||||
|
if (ev.getContent().type && ev.getContent().url) {
|
||||||
|
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;
|
||||||
|
|
Loading…
Reference in a new issue