summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-11-12 03:04:04 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-11-12 03:04:04 +0900
commit10cb2a833273fc84db5ab0525068f93f01472c1e (patch)
tree296a3b03ccdfd9959d6fbd829767b82438356ef1 /src/api
parentv3017 (diff)
downloadsharkey-10cb2a833273fc84db5ab0525068f93f01472c1e.tar.gz
sharkey-10cb2a833273fc84db5ab0525068f93f01472c1e.tar.bz2
sharkey-10cb2a833273fc84db5ab0525068f93f01472c1e.zip
Optimization
Diffstat (limited to 'src/api')
-rw-r--r--src/api/endpoints/i/update_home.ts42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/api/endpoints/i/update_home.ts b/src/api/endpoints/i/update_home.ts
index b9a7642b8e..429e88529a 100644
--- a/src/api/endpoints/i/update_home.ts
+++ b/src/api/endpoints/i/update_home.ts
@@ -15,7 +15,7 @@ import User from '../../models/user';
*/
module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => {
// Get 'home' parameter
- const [home, homeErr] = $(params.home).array().each(
+ const [home, homeErr] = $(params.home).optional.array().each(
$().strict.object()
.have('name', $().string())
.have('id', $().string())
@@ -23,12 +23,38 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
.have('data', $().object())).$;
if (homeErr) return rej('invalid home param');
- await User.update(user._id, {
- $set: {
- 'client_settings.home': home
- }
- });
+ // Get 'id' parameter
+ const [id, idErr] = $(params.id).optional.string().$;
+ if (idErr) return rej('invalid id param');
- // Send response
- res();
+ // Get 'data' parameter
+ const [data, dataErr] = $(params.data).optional.object().$;
+ if (dataErr) return rej('invalid data param');
+
+ if (home) {
+ await User.update(user._id, {
+ $set: {
+ 'client_settings.home': home
+ }
+ });
+
+ res();
+ } else {
+ if (id == null && data == null) return rej('you need to set id and data params if home param unset');
+
+ const _home = user.client_settings.home;
+ const widget = _home.find(w => w.id == id);
+
+ if (widget == null) return rej('widget not found');
+
+ widget.data = data;
+
+ await User.update(user._id, {
+ $set: {
+ 'client_settings.home': _home
+ }
+ });
+
+ res();
+ }
});