From 5da436a285b37af7a7bd34a5230f1e5e579f8a46 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 21 Jan 2017 21:08:40 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=AA=E3=82=93=E3=81=8B=E3=82=82=E3=81=86?= =?UTF-8?q?=E3=82=81=E3=81=A3=E3=81=A1=E3=82=83=E5=A4=89=E3=81=88=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/endpoints/i/appdata/set.js | 24 ++++++++++++------- src/web/app/boot.js | 5 ++++ src/web/app/common/scripts/i.ls | 7 ------ src/web/app/common/scripts/stream.ls | 4 +--- src/web/app/desktop/tags/donation.tag | 3 +-- .../desktop/tags/drive/file-contextmenu.tag | 12 +++------- .../app/desktop/tags/home-widgets/profile.tag | 6 ++--- .../desktop/tags/set-avatar-suggestion.tag | 3 +-- .../desktop/tags/set-banner-suggestion.tag | 3 +-- src/web/app/desktop/tags/settings.tag | 13 ++-------- 10 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/api/endpoints/i/appdata/set.js b/src/api/endpoints/i/appdata/set.js index d6506a44f..947ba0cb4 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 5f4dc79af..14cec6870 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 888ef194d..e05a7459f 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 64ae03817..c2c061603 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 cd80e5357..c9d88b85b 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 400fd7b01..a2c9eb3f4 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 ddf1eae52..19b0fad05 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 74462f9e9..77cba97c3 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 0b628e761..0af7319f6 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 0e0ebe769..a9135dcd6 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!