1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-23 02:58:30 +09:00
misskey/src/server/api/endpoints/i/update_widget.ts

92 lines
1.9 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
2018-06-18 09:54:53 +09:00
import User, { ILocalUser } from '../../../../models/user';
import { publishMainStream } from '../../../../stream';
2018-11-02 12:49:08 +09:00
import getParams from '../../get-params';
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-07-06 02:58:29 +09:00
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
2018-11-02 12:49:08 +09:00
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
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');
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);
if (widget) {
2018-11-02 12:49:08 +09: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 12:49:08 +09:00
widget = mobileHome.find((w: any) => w.id == ps.id);
if (widget) {
2018-11-02 12:49:08 +09: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;
2018-06-18 09:54:53 +09:00
deck.columns.filter((c: any) => c.type == 'widgets').forEach((c: any) => {
c.widgets.forEach((w: any) => {
2018-11-02 12:49:08 +09:00
if (w.id == ps.id) widget = w;
});
});
if (widget) {
2018-11-02 12:49:08 +09:00
widget.data = ps.data;
await User.update(user._id, {
$set: {
'clientSettings.deck': deck
}
});
}
}
//#endregion
if (widget) {
publishMainStream(user._id, 'widgetUpdated', {
2018-11-02 12:49:08 +09:00
id: ps.id, data: ps.data
});
res();
} else {
rej('widget not found');
}
});