FIX: Don't show wrong flag choices after undo

This commit is contained in:
Robin Ward 2014-09-02 17:37:19 -04:00
parent defe1dd86f
commit b04a52676e
8 changed files with 45 additions and 49 deletions

View File

@ -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');

View File

@ -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);
}
}
});
},

View File

@ -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());

View File

@ -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 });
},

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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