2018-06-06 19:22:45 +09:00
|
|
|
import $ from 'cafy';
|
2018-11-02 13:47:44 +09:00
|
|
|
import User from '../../../../models/user';
|
2019-02-05 14:14:23 +09:00
|
|
|
import { publishMainStream } from '../../../../services/stream';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../define';
|
2018-06-06 19:22:45 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
|
|
|
requireCredential: true,
|
2018-11-02 12:49:08 +09:00
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
id: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
|
|
|
|
|
|
|
data: {
|
|
|
|
validator: $.obj()
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 04:36:44 +09:00
|
|
|
};
|
|
|
|
|
2018-11-02 13:47:44 +09:00
|
|
|
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
2018-11-02 12:49:08 +09:00
|
|
|
if (ps.id == null && ps.data == null) return rej('you need to set id and data params if home param unset');
|
2018-06-06 19:22:45 +09:00
|
|
|
|
|
|
|
let widget;
|
|
|
|
|
|
|
|
//#region Desktop home
|
|
|
|
if (widget == null && user.clientSettings.home) {
|
|
|
|
const desktopHome = user.clientSettings.home;
|
2018-11-02 12:49:08 +09:00
|
|
|
widget = desktopHome.find((w: any) => w.id == ps.id);
|
2018-06-06 19:22:45 +09:00
|
|
|
if (widget) {
|
2018-11-02 12:49:08 +09:00
|
|
|
widget.data = ps.data;
|
2018-06-06 19:22:45 +09:00
|
|
|
|
|
|
|
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 12:49:08 +09:00
|
|
|
widget = mobileHome.find((w: any) => w.id == ps.id);
|
2018-06-06 19:22:45 +09:00
|
|
|
if (widget) {
|
2018-11-02 12:49:08 +09:00
|
|
|
widget.data = ps.data;
|
2018-06-06 19:22:45 +09:00
|
|
|
|
|
|
|
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;
|
2018-12-11 20:36:55 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 19:22:45 +09:00
|
|
|
if (widget) {
|
2018-11-02 12:49:08 +09:00
|
|
|
widget.data = ps.data;
|
2018-06-06 19:22:45 +09:00
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
|
|
|
'clientSettings.deck': deck
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
if (widget) {
|
2018-10-07 11:06:17 +09:00
|
|
|
publishMainStream(user._id, 'widgetUpdated', {
|
2018-11-02 12:49:08 +09:00
|
|
|
id: ps.id, data: ps.data
|
2018-06-06 19:22:45 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
res();
|
|
|
|
} else {
|
|
|
|
rej('widget not found');
|
|
|
|
}
|
2018-11-02 13:47:44 +09:00
|
|
|
}));
|