summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/i/update_widget.ts
blob: da96ec6fc18c546e4d74b76069f1ac4c423810d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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');
	}
}));