mizzkey/src/server/api/endpoints/i/update-widget.ts

89 lines
1.8 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
2018-11-02 05:47:44 +01:00
import User from '../../../../models/user';
2019-02-05 06:14:23 +01:00
import { publishMainStream } from '../../../../services/stream';
2018-11-02 05:47:44 +01:00
import define from '../../define';
2018-07-16 21:36:44 +02:00
export const meta = {
requireCredential: true,
2018-11-02 04:49:08 +01:00
secure: true,
params: {
id: {
validator: $.str
},
data: {
validator: $.obj()
}
}
2018-07-16 21:36:44 +02:00
};
export default define(meta, async (ps, user) => {
if (ps.id == null && ps.data == null) throw new Error('you need to set id and data params if home param unset');
let widget;
//#region Desktop home
if (widget == null && user.clientSettings.home) {
const desktopHome = user.clientSettings.home;
2018-11-02 04:49:08 +01:00
widget = desktopHome.find((w: any) => w.id == ps.id);
if (widget) {
2018-11-02 04:49:08 +01:00
widget.data = ps.data;
await User.update(user._id, {
$set: {
'clientSettings.home': desktopHome
}
});
}
}
//#endregion
//#region Mobile home
if (widget == null && user.clientSettings.mobileHome) {
const mobileHome = user.clientSettings.mobileHome;
2018-11-02 04:49:08 +01:00
widget = mobileHome.find((w: any) => w.id == ps.id);
if (widget) {
2018-11-02 04:49:08 +01:00
widget.data = ps.data;
await User.update(user._id, {
$set: {
'clientSettings.mobileHome': mobileHome
}
});
}
}
//#endregion
//#region Deck
if (widget == null && user.clientSettings.deck && user.clientSettings.deck.columns) {
const deck = user.clientSettings.deck;
for (const c of deck.columns.filter((c: any) => c.type == 'widgets')) {
for (const w of c.widgets.filter((w: any) => w.id == ps.id)) {
widget = w;
}
}
if (widget) {
2018-11-02 04:49:08 +01:00
widget.data = ps.data;
await User.update(user._id, {
$set: {
'clientSettings.deck': deck
}
});
}
}
//#endregion
if (widget) {
publishMainStream(user._id, 'widgetUpdated', {
2018-11-02 04:49:08 +01:00
id: ps.id, data: ps.data
});
return;
} else {
throw new Error('widget not found');
}
});