This commit is contained in:
syuilo
2018-11-02 12:49:08 +09:00
parent befc35a3ac
commit a7e6b766be
26 changed files with 434 additions and 354 deletions

View File

@ -1,31 +1,38 @@
import $ from 'cafy';
import User, { ILocalUser } from '../../../../models/user';
import { publishMainStream } from '../../../../stream';
import getParams from '../../get-params';
export const meta = {
requireCredential: true,
secure: true
secure: true,
params: {
id: {
validator: $.str
},
data: {
validator: $.obj()
}
}
};
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'id' parameter
const [id, idErr] = $.str.get(params.id);
if (idErr) return rej('invalid id param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Get 'data' parameter
const [data, dataErr] = $.obj().get(params.data);
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');
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 == id);
widget = desktopHome.find((w: any) => w.id == ps.id);
if (widget) {
widget.data = data;
widget.data = ps.data;
await User.update(user._id, {
$set: {
@ -39,9 +46,9 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res,
//#region Mobile home
if (widget == null && user.clientSettings.mobileHome) {
const mobileHome = user.clientSettings.mobileHome;
widget = mobileHome.find((w: any) => w.id == id);
widget = mobileHome.find((w: any) => w.id == ps.id);
if (widget) {
widget.data = data;
widget.data = ps.data;
await User.update(user._id, {
$set: {
@ -57,11 +64,11 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res,
const deck = user.clientSettings.deck;
deck.columns.filter((c: any) => c.type == 'widgets').forEach((c: any) => {
c.widgets.forEach((w: any) => {
if (w.id == id) widget = w;
if (w.id == ps.id) widget = w;
});
});
if (widget) {
widget.data = data;
widget.data = ps.data;
await User.update(user._id, {
$set: {
@ -74,7 +81,7 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res,
if (widget) {
publishMainStream(user._id, 'widgetUpdated', {
id, data
id: ps.id, data: ps.data
});
res();