diff --git a/src/api/endpoints/i/appdata/set.js b/src/api/endpoints/i/appdata/set.js index d6506a44fa..947ba0cb42 100644 --- a/src/api/endpoints/i/appdata/set.js +++ b/src/api/endpoints/i/appdata/set.js @@ -5,6 +5,8 @@ */ import Appdata from '../../../models/appdata'; import User from '../../../models/user'; +import serialize from '../../../serializers/user'; +import event from '../../../event'; /** * Set app data @@ -24,30 +26,34 @@ module.exports = (params, user, app, isSecure) => } if (isSecure) { - const set = { + const _user = await User.findOneAndUpdate(user._id, { $set: { data: Object.assign(user.data || {}, JSON.parse(data)) } - }; - await User.update({ _id: user._id }, set); + }); res(204); + + // Publish i updated event + event(user._id, 'i_updated', await serialize(_user, user, { + detail: true, + includeSecrets: true + })); } else { const appdata = await Appdata.findOne({ app_id: app._id, user_id: user._id }); - const set = { - $set: { - data: Object.assign((appdata || {}).data || {}, JSON.parse(data)) - } - }; await Appdata.update({ app_id: app._id, user_id: user._id }, Object.assign({ app_id: app._id, user_id: user._id - }, set), { + }, { + $set: { + data: Object.assign((appdata || {}).data || {}, JSON.parse(data)) + } + }), { upsert: true }); res(204); diff --git a/src/web/app/boot.js b/src/web/app/boot.js index 5f4dc79af8..14cec68704 100644 --- a/src/web/app/boot.js +++ b/src/web/app/boot.js @@ -67,6 +67,11 @@ module.exports = callback => { if (me != null) { riot.observable(me); + me.update = data => { + if (data) Object.assign(me, data); + me.trigger('updated'); + }; + if (me.data.cache) { localStorage.setItem('me', JSON.stringify(me)); diff --git a/src/web/app/common/scripts/i.ls b/src/web/app/common/scripts/i.ls index 888ef194d0..e05a7459ff 100644 --- a/src/web/app/common/scripts/i.ls +++ b/src/web/app/common/scripts/i.ls @@ -9,10 +9,3 @@ module.exports = (me) -> if @SIGNIN @on \mount ~> me.on \updated @update @on \unmount ~> me.off \updated @update - - update-i: (data) -> - if data? - Object.assign me, data - me.trigger \updated - - me: me diff --git a/src/web/app/common/scripts/stream.ls b/src/web/app/common/scripts/stream.ls index 64ae03817a..c2c061603e 100644 --- a/src/web/app/common/scripts/stream.ls +++ b/src/web/app/common/scripts/stream.ls @@ -30,9 +30,7 @@ module.exports = (me) ~> get-state = ~> state - event.on \i_updated (data) ~> - Object.assign me, data - me.trigger \updated + event.on \i_updated me.update { state-ev diff --git a/src/web/app/desktop/tags/donation.tag b/src/web/app/desktop/tags/donation.tag index cd80e53572..c9d88b85b0 100644 --- a/src/web/app/desktop/tags/donation.tag +++ b/src/web/app/desktop/tags/donation.tag @@ -55,11 +55,10 @@ e.stop-propagation! @I.data.no_donation = true + @I.update! @api \i/appdata/set do data: JSON.stringify do no_donation: @I.data.no_donation - .then ~> - @update-i! @unmount! diff --git a/src/web/app/desktop/tags/drive/file-contextmenu.tag b/src/web/app/desktop/tags/drive/file-contextmenu.tag index 400fd7b015..a2c9eb3f4e 100644 --- a/src/web/app/desktop/tags/drive/file-contextmenu.tag +++ b/src/web/app/desktop/tags/drive/file-contextmenu.tag @@ -81,21 +81,15 @@ @set-avatar = ~> @refs.ctx.close! - @update-avatar @I, (i) ~> - @update-i i - , @file + @update-avatar @I, null, @file @set-banner = ~> @refs.ctx.close! - @update-banner @I, (i) ~> - @update-i i - , @file + @update-banner @I, null, @file @set-wallpaper = ~> @refs.ctx.close! - @update-wallpaper @I, (i) ~> - @update-i i - , @file + @update-wallpaper @I, null, @file @add-app = ~> @NotImplementedException! diff --git a/src/web/app/desktop/tags/home-widgets/profile.tag b/src/web/app/desktop/tags/home-widgets/profile.tag index ddf1eae523..19b0fad056 100644 --- a/src/web/app/desktop/tags/home-widgets/profile.tag +++ b/src/web/app/desktop/tags/home-widgets/profile.tag @@ -46,11 +46,9 @@ @mixin \update-banner @set-avatar = ~> - @update-avatar @I, (i) ~> - @update-i i + @update-avatar @I @set-banner = ~> - @update-banner @I, (i) ~> - @update-i i + @update-banner @I diff --git a/src/web/app/desktop/tags/set-avatar-suggestion.tag b/src/web/app/desktop/tags/set-avatar-suggestion.tag index 74462f9e9d..77cba97c33 100644 --- a/src/web/app/desktop/tags/set-avatar-suggestion.tag +++ b/src/web/app/desktop/tags/set-avatar-suggestion.tag @@ -35,8 +35,7 @@ @mixin \update-avatar @set = ~> - @update-avatar @I, (i) ~> - @update-i i + @update-avatar @I @close = (e) ~> e.prevent-default! diff --git a/src/web/app/desktop/tags/set-banner-suggestion.tag b/src/web/app/desktop/tags/set-banner-suggestion.tag index 0b628e7610..0af7319f68 100644 --- a/src/web/app/desktop/tags/set-banner-suggestion.tag +++ b/src/web/app/desktop/tags/set-banner-suggestion.tag @@ -35,8 +35,7 @@ @mixin \update-banner @set = ~> - @update-banner @I, (i) ~> - @update-i i + @update-banner @I @close = (e) ~> e.prevent-default! diff --git a/src/web/app/desktop/tags/settings.tag b/src/web/app/desktop/tags/settings.tag index 0e0ebe7693..a9135dcd69 100644 --- a/src/web/app/desktop/tags/settings.tag +++ b/src/web/app/desktop/tags/settings.tag @@ -250,12 +250,10 @@ @page = page @avatar = ~> - @update-avatar @I, (i) ~> - @update-i i + @update-avatar @I @wallpaper = ~> - @update-wallpaper @I, (i) ~> - @update-i i + @update-wallpaper @I @update-account = ~> @api \i/update do @@ -264,7 +262,6 @@ bio: @refs.account-bio.value birthday: @refs.account-birthday.value .then (i) ~> - @update-i i alert \ok .catch (err) ~> console.error err @@ -274,23 +271,17 @@ @api \i/appdata/set do data: JSON.stringify do cache: @I.data.cache - .then ~> - @update-i! @update-debug = ~> @I.data.debug = !@I.data.debug @api \i/appdata/set do data: JSON.stringify do debug: @I.data.debug - .then ~> - @update-i! @update-nya = ~> @I.data.nya = !@I.data.nya @api \i/appdata/set do data: JSON.stringify do nya: @I.data.nya - .then ~> - @update-i!