diff --git a/app/assets/javascripts/discourse/controllers/flag.js.es6 b/app/assets/javascripts/discourse/controllers/flag.js.es6 index 38bacc91a8d..503c6e39989 100644 --- a/app/assets/javascripts/discourse/controllers/flag.js.es6 +++ b/app/assets/javascripts/discourse/controllers/flag.js.es6 @@ -1,16 +1,6 @@ import ModalFunctionality from 'discourse/mixins/modal-functionality'; - import ObjectController from 'discourse/controllers/object'; -/** - This controller supports actions related to flagging - - @class FlagController - @extends ObjectController - @namespace Discourse - @uses ModalFunctionality - @module Discourse -**/ export default ObjectController.extend(ModalFunctionality, { onShow: function() { @@ -91,7 +81,7 @@ export default ObjectController.extend(ModalFunctionality, { if (opts) params = $.extend(params, opts); this.send('hideModal'); - postAction.act(params).then(function() { + postAction.act(params).then(function(result) { self.send('closeModal'); }, function(errors) { self.send('closeModal'); diff --git a/app/assets/javascripts/discourse/models/_post.js b/app/assets/javascripts/discourse/models/_post.js index 6356e6d8dec..984dd507fe8 100644 --- a/app/assets/javascripts/discourse/models/_post.js +++ b/app/assets/javascripts/discourse/models/_post.js @@ -283,34 +283,25 @@ Discourse.Post = Discourse.Model.extend({ @param {Discourse.Post} otherPost The post we're updating from **/ updateFromPost: function(otherPost) { - var post = this; + var self = this; Object.keys(otherPost).forEach(function (key) { - var value = otherPost[key]; - // optimisation - var oldValue = post[key]; + var value = otherPost[key], + oldValue = self[key]; - if(!value) { - value = null; - } - - if(!oldValue) { - oldValue = null; - } + if (!value) { value = null; } + if (!oldValue) { oldValue = null; } var skip = false; - if (typeof value !== "function" && oldValue !== value) { - // wishing for an identity map - if(key === "reply_to_user" && value && oldValue) { + if (key === "reply_to_user" && value && oldValue) { skip = value.username === oldValue.username || Em.get(value, "username") === Em.get(oldValue, "username"); } - if(!skip) { - post.set(key, value); + if (!skip) { + self.set(key, value); } } - }); }, diff --git a/app/assets/javascripts/discourse/models/action_summary.js b/app/assets/javascripts/discourse/models/action_summary.js index 26a649df00f..c6140dc9641 100644 --- a/app/assets/javascripts/discourse/models/action_summary.js +++ b/app/assets/javascripts/discourse/models/action_summary.js @@ -77,7 +77,7 @@ Discourse.ActionSummary = Discourse.Model.extend({ } // Create our post action - var actionSummary = this; + var self = this; return Discourse.ajax("/post_actions", { type: 'POST', @@ -88,8 +88,14 @@ Discourse.ActionSummary = Discourse.Model.extend({ take_action: opts.takeAction, flag_topic: this.get('flagTopic') ? true : false } - }).then(null, function (error) { - actionSummary.removeAction(); + }).then(function(result) { + var post = self.get('post'); + if (post && result && result.id === post.get('id')) { + post.updateFromJson(result); + } + return post; + }).catch(function (error) { + self.removeAction(); var message = $.parseJSON(error.responseText).errors; bootbox.alert(message); }); @@ -100,11 +106,18 @@ Discourse.ActionSummary = Discourse.Model.extend({ this.removeAction(); // Remove our post action + var self = this; return Discourse.ajax("/post_actions/" + (this.get('post.id')), { type: 'DELETE', data: { post_action_type_id: this.get('id') } + }).then(function(result) { + var post = self.get('post'); + if (post && result && result.id === post.get('id')) { + post.updateFromJson(result); + } + return post; }); }, @@ -122,7 +135,7 @@ Discourse.ActionSummary = Discourse.Model.extend({ }, loadUsers: function() { - var actionSummary = this; + var self = this; Discourse.ajax("/post_actions/users", { data: { id: this.get('post.id'), @@ -130,7 +143,7 @@ Discourse.ActionSummary = Discourse.Model.extend({ } }).then(function (result) { var users = Em.A(); - actionSummary.set('users', users); + self.set('users', users); _.each(result,function(user) { if (user.id === Discourse.User.currentProp('id')) { users.pushObject(Discourse.User.current()); diff --git a/app/assets/javascripts/discourse/routes/topic_route.js b/app/assets/javascripts/discourse/routes/topic_route.js index 40a4758fcb5..f95d5710634 100644 --- a/app/assets/javascripts/discourse/routes/topic_route.js +++ b/app/assets/javascripts/discourse/routes/topic_route.js @@ -31,7 +31,6 @@ Discourse.TopicRoute = Discourse.Route.extend({ }, showFlagTopic: function(topic) { - //Discourse.Route.showModal(this, 'flagTopic', topic); Discourse.Route.showModal(this, 'flag', topic); this.controllerFor('flag').setProperties({ selected: null, flagTopic: true }); }, diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 27179e4abba..f75ae63c91d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -348,6 +348,18 @@ class ApplicationController < ActionController::Base protected + def render_post_json(post) + post_serializer = PostSerializer.new(post, scope: guardian, root: false) + post_serializer.add_raw = true + post_serializer.topic_slug = post.topic.slug if post.topic.present? + + counts = PostAction.counts_for([post], current_user) + if counts && counts = counts[post.id] + post_serializer.post_actions = counts + end + render_json_dump(post_serializer) + end + def api_key_valid? request["api_key"] && ApiKey.where(key: request["api_key"]).exists? end diff --git a/app/controllers/post_actions_controller.rb b/app/controllers/post_actions_controller.rb index 68ab3f9597d..cbdb13129ec 100644 --- a/app/controllers/post_actions_controller.rb +++ b/app/controllers/post_actions_controller.rb @@ -21,8 +21,7 @@ class PostActionsController < ApplicationController else # We need to reload or otherwise we are showing the old values on the front end @post.reload - post_serializer = PostSerializer.new(@post, scope: guardian, root: false) - render_json_dump(post_serializer) + render_post_json(@post) end end @@ -42,7 +41,8 @@ class PostActionsController < ApplicationController PostAction.remove_act(current_user, @post, post_action.post_action_type_id) - render nothing: true + @post.reload + render_post_json(@post) end def defer_flags diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 27ec03e85c9..86a57e216a3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -286,18 +286,6 @@ class PostsController < ApplicationController post_revision end - def render_post_json(post) - post_serializer = PostSerializer.new(post, scope: guardian, root: false) - post_serializer.add_raw = true - post_serializer.topic_slug = post.topic.slug if post.topic.present? - - counts = PostAction.counts_for([post], current_user) - if counts && counts = counts[post.id] - post_serializer.post_actions = counts - end - render_json_dump(post_serializer) - end - private def user_posts(user_id, offset=0, limit=60) diff --git a/app/models/post_action.rb b/app/models/post_action.rb index e05ebbaccbb..1a4553d8959 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -233,6 +233,9 @@ class PostAction < ActiveRecord::Base end else post_action = PostAction.where(where_attrs).first + + # after_commit is not called on an `update_all` so do the notify ourselves + post_action.notify_subscribers end # agree with other flags