2018-06-06 12:22:45 +02:00
|
|
|
import $ from 'cafy';
|
2018-06-18 02:54:53 +02:00
|
|
|
import User, { ILocalUser } from '../../../../models/user';
|
2018-07-07 12:19:00 +02:00
|
|
|
import event from '../../../../stream';
|
2018-06-06 12:22:45 +02:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
|
|
|
requireCredential: true,
|
|
|
|
secure: true
|
|
|
|
};
|
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
2018-06-06 12:22:45 +02:00
|
|
|
// Get 'id' parameter
|
|
|
|
const [id, idErr] = $.str.get(params.id);
|
|
|
|
if (idErr) return rej('invalid id param');
|
|
|
|
|
|
|
|
// Get 'data' parameter
|
2018-07-07 12:19:00 +02:00
|
|
|
const [data, dataErr] = $.obj().get(params.data);
|
2018-06-06 12:22:45 +02:00
|
|
|
if (dataErr) return rej('invalid data param');
|
|
|
|
|
|
|
|
if (id == null && 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;
|
2018-06-18 02:54:53 +02:00
|
|
|
widget = desktopHome.find((w: any) => w.id == id);
|
2018-06-06 12:22:45 +02:00
|
|
|
if (widget) {
|
|
|
|
widget.data = 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-06-18 02:54:53 +02:00
|
|
|
widget = mobileHome.find((w: any) => w.id == id);
|
2018-06-06 12:22:45 +02:00
|
|
|
if (widget) {
|
|
|
|
widget.data = 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;
|
2018-06-18 02:54:53 +02:00
|
|
|
deck.columns.filter((c: any) => c.type == 'widgets').forEach((c: any) => {
|
|
|
|
c.widgets.forEach((w: any) => {
|
2018-06-06 12:22:45 +02:00
|
|
|
if (w.id == id) widget = w;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (widget) {
|
|
|
|
widget.data = data;
|
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
|
|
|
'clientSettings.deck': deck
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
event(user._id, 'widgetUpdated', {
|
|
|
|
id, data
|
|
|
|
});
|
|
|
|
|
|
|
|
res();
|
|
|
|
} else {
|
|
|
|
rej('widget not found');
|
|
|
|
}
|
|
|
|
});
|