diff --git a/src/api/endpoints/i/update_home.ts b/src/api/endpoints/i/update_home.ts index b9a7642b8..429e88529 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(); + } }); diff --git a/src/web/app/desktop/tags/home-widgets/activity.tag b/src/web/app/desktop/tags/home-widgets/activity.tag index 7bdcd334c..00cf4ea20 100644 --- a/src/web/app/desktop/tags/home-widgets/activity.tag +++ b/src/web/app/desktop/tags/home-widgets/activity.tag @@ -73,10 +73,13 @@ if (this.view == 2) this.view = 0; // Save view state - this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view; this.api('i/update_home', { - home: this.I.client_settings.home + id: this.opts.id, + data: { + view: this.view + } }).then(() => { + this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view; this.I.update(); }); }; diff --git a/src/web/app/desktop/tags/home-widgets/channel.tag b/src/web/app/desktop/tags/home-widgets/channel.tag index 79b57a1e8..2c2b6e7fe 100644 --- a/src/web/app/desktop/tags/home-widgets/channel.tag +++ b/src/web/app/desktop/tags/home-widgets/channel.tag @@ -83,10 +83,13 @@ this.zap(); // Save state - this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.channel = this.channelId; this.api('i/update_home', { - home: this.I.client_settings.home + id: this.opts.id, + data: { + channel: this.channelId + } }).then(() => { + this.I.client_settings.home.find(w => w.id == this.opts.id).data.channel = this.channelId; this.I.update(); }); }; diff --git a/src/web/app/desktop/tags/home-widgets/server.tag b/src/web/app/desktop/tags/home-widgets/server.tag index 235867c38..f4e38aea5 100644 --- a/src/web/app/desktop/tags/home-widgets/server.tag +++ b/src/web/app/desktop/tags/home-widgets/server.tag @@ -81,10 +81,13 @@ if (this.view == 6) this.view = 0; // Save view state - this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view; this.api('i/update_home', { - home: this.I.client_settings.home + id: this.opts.id, + data: { + view: this.view + } }).then(() => { + this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view; this.I.update(); }); }; diff --git a/src/web/app/desktop/tags/home-widgets/slideshow.tag b/src/web/app/desktop/tags/home-widgets/slideshow.tag index aa1e45fad..9d2c06a27 100644 --- a/src/web/app/desktop/tags/home-widgets/slideshow.tag +++ b/src/web/app/desktop/tags/home-widgets/slideshow.tag @@ -88,14 +88,7 @@ if (this.size == 2) this.size = 0; this.applySize(); - - // Save state - this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.size = this.size; - this.api('i/update_home', { - home: this.I.client_settings.home - }).then(() => { - this.I.update(); - }); + this.save(); }; this.change = () => { @@ -147,14 +140,23 @@ i.one('selected', folder => { this.folder = folder ? folder.id : null; this.fetch(); + this.save(); + }); + }; - // Save state - this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.folder = this.folder; - this.api('i/update_home', { - home: this.I.client_settings.home - }).then(() => { - this.I.update(); - }); + this.save = () => { + // Save state + this.api('i/update_home', { + id: this.opts.id, + data: { + folder: this.folder, + size: this.size + } + }).then(() => { + const w = this.I.client_settings.home.find(w => w.id == this.opts.id); + w.data.folder = this.folder; + w.data.size = this.size; + this.I.update(); }); };