Rejig to support deletions better

This commit is contained in:
Kegan Dougal 2017-06-09 12:34:19 +01:00
parent 6d1d435247
commit b70881f078

View file

@ -263,38 +263,42 @@ function inviteUser(event, roomId, userId) {
} }
function setWidget(event, roomId) { function setWidget(event, roomId) {
// check required fields exist
const widgetId = event.data.widget_id; const widgetId = event.data.widget_id;
const widgetType = event.data.type; const widgetType = event.data.type;
const widgetUrl = event.data.url; const widgetUrl = event.data.url;
if (!widgetId || widgetUrl === undefined || !widgetType) { const widgetName = event.data.name; // optional
sendError(event, _t("Unable to create widget."), new Error("Missing required widget fields.")); const widgetData = event.data.data; // optional
return;
}
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (!client) { if (!client) {
sendError(event, _t('You need to be logged in.')); sendError(event, _t('You need to be logged in.'));
return; return;
} }
// check types of fields // both adding/removing widgets need these checks
const widgetName = event.data.name; // optional if (!widgetId || widgetUrl === undefined) {
const widgetData = event.data.data; // optional sendError(event, _t("Unable to create widget."), new Error("Missing required widget fields."));
if (widgetName !== undefined && typeof widgetName !== 'string') {
sendError(event, _t("Unable to create widget."), new Error("Optional field 'name' must be a string."));
return; return;
} }
if (widgetData !== undefined && !(widgetData instanceof Object)) {
sendError(event, _t("Unable to create widget."), new Error("Optional field 'data' must be an Object.")); if (widgetUrl !== null) { // if url is null it is being deleted, don't need to check name/type/etc
return; // check types of fields
} if (widgetName !== undefined && typeof widgetName !== 'string') {
if (typeof widgetType !== 'string') { sendError(event, _t("Unable to create widget."), new Error("Optional field 'name' must be a string."));
sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string.")); return;
return; }
} if (widgetData !== undefined && !(widgetData instanceof Object)) {
if (widgetUrl !== null && typeof widgetUrl !== 'string') { sendError(event, _t("Unable to create widget."), new Error("Optional field 'data' must be an Object."));
sendError(event, _t("Unable to create widget."), new Error("Field 'url' must be a string or null.")); return;
return; }
if (typeof widgetType !== 'string') {
sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string."));
return;
}
if (typeof widgetUrl !== 'string') {
sendError(event, _t("Unable to create widget."), new Error("Field 'url' must be a string or null."));
return;
}
} }
// TODO: same dance we do for power levels. It'd be nice if the JS SDK had helper methods to do this. // TODO: same dance we do for power levels. It'd be nice if the JS SDK had helper methods to do this.