From d53ac36818cc7cef8ba145d1a4608b24de457ec8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 16 Jun 2014 14:49:39 -0400 Subject: [PATCH] FIX: New action arriving in stream via message bus --- .../controllers/preferences/username.js.es6 | 18 +++++++++--------- .../javascripts/discourse/models/user.js | 4 ++-- .../discourse/models/user_stream.js | 9 +++------ .../discourse/views/user/user_stream_view.js | 2 ++ app/models/user_action.rb | 4 ++++ 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/preferences/username.js.es6 b/app/assets/javascripts/discourse/controllers/preferences/username.js.es6 index e75bd1fde..a9a4d44d0 100644 --- a/app/assets/javascripts/discourse/controllers/preferences/username.js.es6 +++ b/app/assets/javascripts/discourse/controllers/preferences/username.js.es6 @@ -21,16 +21,16 @@ export default Discourse.ObjectController.extend({ if( this.get('newUsername') && this.get('newUsername').length < 3 ) { this.set('errorMessage', I18n.t('user.name.too_short')); } else { - var preferencesUsernameController = this; + var self = this; this.set('taken', false); this.set('errorMessage', null); if (this.blank('newUsername')) return; if (this.get('unchanged')) return; Discourse.User.checkUsername(this.get('newUsername'), undefined, this.get('content.id')).then(function(result) { if (result.errors) { - preferencesUsernameController.set('errorMessage', result.errors.join(' ')); + self.set('errorMessage', result.errors.join(' ')); } else if (result.available === false) { - preferencesUsernameController.set('taken', true); + self.set('taken', true); } }); } @@ -43,16 +43,16 @@ export default Discourse.ObjectController.extend({ actions: { changeUsername: function() { - var preferencesUsernameController = this; + var self = this; return bootbox.confirm(I18n.t("user.change_username.confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) { if (result) { - preferencesUsernameController.set('saving', true); - preferencesUsernameController.get('content').changeUsername(preferencesUsernameController.get('newUsername')).then(function() { - Discourse.URL.redirectTo("/users/" + preferencesUsernameController.get('newUsername').toLowerCase() + "/preferences"); + self.set('saving', true); + self.get('content').changeUsername(self.get('newUsername')).then(function() { + Discourse.URL.redirectTo("/users/" + self.get('newUsername').toLowerCase() + "/preferences"); }, function() { // error - preferencesUsernameController.set('error', true); - preferencesUsernameController.set('saving', false); + self.set('error', true); + self.set('saving', false); }); } }); diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 8764f2143..e178c38f7 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -257,8 +257,8 @@ Discourse.User = Discourse.Model.extend({ if (result) { if ((user.get('streamFilter') || result.action_type) !== result.action_type) return; var action = Discourse.UserAction.collapseStream([Discourse.UserAction.create(result)]); - stream.set('itemsLoaded', user.get('itemsLoaded') + 1); - stream.insertAt(0, action[0]); + stream.set('itemsLoaded', stream.get('itemsLoaded') + 1); + stream.get('content').insertAt(0, action[0]); } }); }, diff --git a/app/assets/javascripts/discourse/models/user_stream.js b/app/assets/javascripts/discourse/models/user_stream.js index 0ea56a6d0..27fcc13a3 100644 --- a/app/assets/javascripts/discourse/models/user_stream.js +++ b/app/assets/javascripts/discourse/models/user_stream.js @@ -71,10 +71,6 @@ Discourse.UserStream = Discourse.Model.extend({ url += "&filter=" + this.get('filterParam'); } - var loadingFinished = function() { - userStream.set('loading', false); - }; - return Discourse.ajax(url, {cache: 'false'}).then( function(result) { if (result && result.user_actions) { var copy = Em.A(); @@ -88,8 +84,9 @@ Discourse.UserStream = Discourse.Model.extend({ itemsLoaded: userStream.get('itemsLoaded') + result.user_actions.length }); } - loadingFinished(); - }, loadingFinished); + }).finally(function() { + userStream.set('loading', false); + }); } }); diff --git a/app/assets/javascripts/discourse/views/user/user_stream_view.js b/app/assets/javascripts/discourse/views/user/user_stream_view.js index 7db8799b8..d63d4a314 100644 --- a/app/assets/javascripts/discourse/views/user/user_stream_view.js +++ b/app/assets/javascripts/discourse/views/user/user_stream_view.js @@ -21,6 +21,8 @@ Discourse.UserStreamView = Discourse.View.extend(Discourse.LoadMore, { stream.findItems().then(function() { self.set('loading', false); self.get('eyeline').flushRest(); + }).catch(function() { + // If we were already loading... }); } } diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 366db02cc..07a9261f7 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -39,6 +39,10 @@ class UserAction < ActiveRecord::Base # having strings where you would expect bools class UserActionRow < OpenStruct include ActiveModel::SerializerSupport + + def as_json(options = nil) + @table.as_json(options) + end end def self.last_action_in_topic(user_id, topic_id)