From 05a56b25a9e77cb50f656251ac8561751982578f Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 31 Jan 2015 23:42:20 +1100 Subject: [PATCH] FIX: setting custom avatar would not work a lot of the time Due to internal structure we were often caching "redirect" images. --- .../discourse/components/avatar-uploader.js.es6 | 8 +++++--- app/assets/javascripts/discourse/models/user.js | 5 ++++- .../discourse/routes/preferences.js.es6 | 14 ++++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/components/avatar-uploader.js.es6 b/app/assets/javascripts/discourse/components/avatar-uploader.js.es6 index f711802a4..b67da9c65 100644 --- a/app/assets/javascripts/discourse/components/avatar-uploader.js.es6 +++ b/app/assets/javascripts/discourse/components/avatar-uploader.js.es6 @@ -14,9 +14,6 @@ export default Em.Component.extend(UploadMixin, { uploadDone: function(data) { var self = this; - // indicates the users is using an uploaded avatar - this.set("custom_avatar_upload_id", data.result.upload_id); - // display a warning whenever the image is not a square this.set("imageIsNotASquare", data.result.width !== data.result.height); @@ -26,6 +23,11 @@ export default Em.Component.extend(UploadMixin, { // this will also capture the first frame of animated avatars when they're not allowed Discourse.Utilities.cropAvatar(data.result.url, data.files[0].type).then(function(avatarTemplate) { self.set("uploadedAvatarTemplate", avatarTemplate); + + // indicates the users is using an uploaded avatar (must happen after cropping, otherwise + // we will attempt to load an invalid avatar and cache a redirect to old one, uploadedAvatarTemplate + // trumps over custom avatar upload id) + self.set("custom_avatar_upload_id", data.result.upload_id); }); // the upload is now done diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index fde769225..c650d1704 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -351,10 +351,13 @@ Discourse.User = Discourse.Model.extend({ Change avatar selection */ pickAvatar: function(uploadId) { - this.set("uploaded_avatar_id", uploadId); + var self = this; + return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/avatar/pick", { type: 'PUT', data: { upload_id: uploadId } + }).then(function(){ + self.set('uploaded_avatar_id', uploadId); }); }, diff --git a/app/assets/javascripts/discourse/routes/preferences.js.es6 b/app/assets/javascripts/discourse/routes/preferences.js.es6 index 6ad9efb52..3f28b1b40 100644 --- a/app/assets/javascripts/discourse/routes/preferences.js.es6 +++ b/app/assets/javascripts/discourse/routes/preferences.js.es6 @@ -45,15 +45,17 @@ export default RestrictedUserRoute.extend(ShowFooter, { // sends the information to the server if it has changed if (avatarSelector.get('selectedUploadId') !== user.get('uploaded_avatar_id')) { - user.pickAvatar(avatarSelector.get('selectedUploadId')); + user.pickAvatar(avatarSelector.get('selectedUploadId')) + .then(function(){ + user.setProperties(avatarSelector.getProperties( + 'system_avatar_upload_id', + 'gravatar_avatar_upload_id', + 'custom_avatar_upload_id' + )); + }); } // saves the data back - user.setProperties(avatarSelector.getProperties( - 'system_avatar_upload_id', - 'gravatar_avatar_upload_id', - 'custom_avatar_upload_id' - )); avatarSelector.send('closeModal'); },