sharkey/src/server/api/endpoints/i/update_widget.ts
Aya Morisawa 125849673a
Use for-of instead of forEach (#3583)
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>
Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
2018-12-11 20:36:55 +09:00

89 lines
1.8 KiB
TypeScript

import $ from 'cafy';
import User from '../../../../models/user';
import { publishMainStream } from '../../../../stream';
import define from '../../define';
export const meta = {
requireCredential: true,
secure: true,
params: {
id: {
validator: $.str
},
data: {
validator: $.obj()
}
}
};
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
if (ps.id == null && ps.data == null) return rej('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;
widget = desktopHome.find((w: any) => w.id == ps.id);
if (widget) {
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;
widget = mobileHome.find((w: any) => w.id == ps.id);
if (widget) {
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) {
widget.data = ps.data;
await User.update(user._id, {
$set: {
'clientSettings.deck': deck
}
});
}
}
//#endregion
if (widget) {
publishMainStream(user._id, 'widgetUpdated', {
id: ps.id, data: ps.data
});
res();
} else {
rej('widget not found');
}
}));