From fd7fd229322c0128f6b46df181e601e14f5d3da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sun, 25 Aug 2013 17:26:44 +0200 Subject: [PATCH] FIX: changing another user's avatar changes yours --- .../controllers/avatar_selector_controller.js | 32 ------------------- .../javascripts/discourse/models/user.js | 13 +++++++- .../discourse/routes/preferences_routes.js | 30 ++++++++++++++++- .../modal/avatar_selector.js.handlebars | 2 +- app/serializers/current_user_serializer.rb | 10 +----- app/serializers/user_serializer.rb | 10 +++++- 6 files changed, 52 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js index d4aebbc9793..861853ca41c 100644 --- a/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js +++ b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js @@ -8,39 +8,7 @@ @module Discourse **/ Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, { - init: function() { - // copy some data to support the cancel action - this.setProperties(this.get("currentUser").getProperties( - "username", - "has_uploaded_avatar", - "use_uploaded_avatar", - "gravatar_template", - "uploaded_avatar_template" - )); - }, - toggleUseUploadedAvatar: function(toggle) { this.set("use_uploaded_avatar", toggle); - }, - - saveAvatarSelection: function() { - // sends the information to the server if it has changed - if (this.get("use_uploaded_avatar") !== this.get("currentUser.use_uploaded_avatar")) { - var data = { use_uploaded_avatar: this.get("use_uploaded_avatar") }; - Discourse.ajax("/users/" + this.get("currentUser.username") + "/preferences/avatar/toggle", { type: 'PUT', data: data }); - } - // saves the data back to the currentUser object - var currentUser = this.get("currentUser"); - currentUser.setProperties(this.getProperties( - "has_uploaded_avatar", - "use_uploaded_avatar", - "gravatar_template", - "uploaded_avatar_template" - )); - if (this.get("use_uploaded_avatar")) { - currentUser.set("avatar_template", this.get("uploaded_avatar_template")); - } else { - currentUser.set("avatar_template", this.get("gravatar_template")); - } } }); diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index d1b84266173..4abb65886f6 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -139,7 +139,7 @@ Discourse.User = Discourse.Model.extend({ **/ save: function() { var user = this; - return Discourse.ajax("/users/" + this.get('username').toLowerCase(), { + return Discourse.ajax("/users/" + this.get('username_lower'), { data: this.getProperties('auto_track_topics_after_msecs', 'bio_raw', 'website', @@ -260,6 +260,17 @@ Discourse.User = Discourse.Model.extend({ user.setProperties(json.user); return user; }); + }, + + /* + Change avatar selection + + @method toggleAvatarSelection + @returns {Promise} the result of the toggle avatar selection + */ + toggleAvatarSelection: function() { + var data = { use_uploaded_avatar: this.get("use_uploaded_avatar") }; + return Discourse.ajax("/users/" + this.get("username") + "/preferences/avatar/toggle", { type: 'PUT', data: data }); } }); diff --git a/app/assets/javascripts/discourse/routes/preferences_routes.js b/app/assets/javascripts/discourse/routes/preferences_routes.js index 0ddc31a4579..e623cf3c4c5 100644 --- a/app/assets/javascripts/discourse/routes/preferences_routes.js +++ b/app/assets/javascripts/discourse/routes/preferences_routes.js @@ -18,7 +18,35 @@ Discourse.PreferencesRoute = Discourse.RestrictedUserRoute.extend({ events: { showAvatarSelector: function() { Discourse.Route.showModal(this, 'avatarSelector'); - this.controllerFor("avatarSelector").init(); + var user = this.modelFor("user"); + console.log(user); + this.controllerFor("avatarSelector").setProperties(user.getProperties( + "username", + "email", + "has_uploaded_avatar", + "use_uploaded_avatar", + "gravatar_template", + "uploaded_avatar_template" + )); + }, + + saveAvatarSelection: function() { + var user = this.modelFor("user"); + var avatar = this.controllerFor("avatarSelector"); + // sends the information to the server if it has changed + if (avatar.get("use_uploaded_avatar") !== user.get("use_uploaded_avatar")) { user.toggleAvatarSelection(); } + // saves the data back + user.setProperties(avatar.getProperties( + "has_uploaded_avatar", + "use_uploaded_avatar", + "gravatar_template", + "uploaded_avatar_template" + )); + if (avatar.get("use_uploaded_avatar")) { + user.set("avatar_template", avatar.get("uploaded_avatar_template")); + } else { + user.set("avatar_template", avatar.get("gravatar_template")); + } } } }); diff --git a/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars index 8242668af1e..4548c091b79 100644 --- a/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars @@ -2,7 +2,7 @@
- +
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb index 59c28c7f46f..69079b4f369 100644 --- a/app/serializers/current_user_serializer.rb +++ b/app/serializers/current_user_serializer.rb @@ -14,11 +14,7 @@ class CurrentUserSerializer < BasicUserSerializer :external_links_in_new_tab, :dynamic_favicon, :trust_level, - :can_edit, - :use_uploaded_avatar, - :has_uploaded_avatar, - :gravatar_template, - :uploaded_avatar_template + :can_edit def include_site_flagged_posts_count? object.staff? @@ -40,8 +36,4 @@ class CurrentUserSerializer < BasicUserSerializer true end - def gravatar_template - User.gravatar_template(object.email) - end - end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index cda2135ddef..f76744822b8 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -51,7 +51,11 @@ class UserSerializer < BasicUserSerializer :new_topic_duration_minutes, :external_links_in_new_tab, :dynamic_favicon, - :enable_quoting + :enable_quoting, + :use_uploaded_avatar, + :has_uploaded_avatar, + :gravatar_template, + :uploaded_avatar_template def auto_track_topics_after_msecs @@ -78,4 +82,8 @@ class UserSerializer < BasicUserSerializer UserAction.stats(object.id, scope) end + def gravatar_template + User.gravatar_template(object.email) + end + end