FIX: You can click to see your own PMs from flags

Also refactors post action users to be a new object type since they can
have `post_url` which is not a field of a `User`
This commit is contained in:
Robin Ward 2015-09-30 12:28:13 -04:00
parent 97fbf09259
commit 33e58c0587
10 changed files with 80 additions and 71 deletions

View File

@ -24,9 +24,7 @@ export default Ember.Object.extend({
return "/";
},
pathFor(store, type, findArgs) {
let path = this.basePath(store, type, findArgs) + Ember.String.underscore(store.pluralize(type));
appendQueryParams(path, findArgs) {
if (findArgs) {
if (typeof findArgs === "object") {
const queryString = Object.keys(findArgs)
@ -34,17 +32,21 @@ export default Ember.Object.extend({
.map(k => k + "=" + encodeURIComponent(findArgs[k]));
if (queryString.length) {
path += "?" + queryString.join('&');
return path + "?" + queryString.join('&');
}
} else {
// It's serializable as a string if not an object
path += "/" + findArgs;
return path + "/" + findArgs;
}
}
return path;
},
pathFor(store, type, findArgs) {
let path = this.basePath(store, type, findArgs) + Ember.String.underscore(store.pluralize(type));
return this.appendQueryParams(path, findArgs);
},
findAll(store, type) {
return ajax(this.pathFor(store, type)).catch(rethrow);
},

View File

@ -56,7 +56,7 @@ export default Ember.Component.extend(StringBuffer, {
if (postUrl) { key = key + "_with_url"; }
// TODO postUrl might be uninitialized? pick a good default
buffer.push(" " + I18n.t(key, { icons: iconsHtml, postUrl: postUrl}) + ".");
buffer.push(" " + I18n.t(key, { icons: iconsHtml, postUrl }) + ".");
}
if (users.length === 0) {

View File

@ -5,7 +5,7 @@ export default Ember.Component.extend(StringBuffer, {
renderString(buffer) {
const users = this.get('users');
if (users && users.length > 0) {
if (users && users.get('length') > 0) {
buffer.push("<div class='who-liked'>");
let iconsHtml = "";
users.forEach(function(u) {

View File

@ -111,18 +111,9 @@ export default RestModel.extend({
},
loadUsers(post) {
return Discourse.ajax("/post_actions/users", {
data: { id: post.get('id'), post_action_type_id: this.get('id') }
}).then(function (result) {
const users = [];
result.forEach(function(user) {
if (user.id === Discourse.User.currentProp('id')) {
users.pushObject(Discourse.User.current());
} else {
users.pushObject(Discourse.User.create(user));
}
});
return users;
return this.store.find('post-action-user', {
id: post.get('id'),
post_action_type_id: this.get('id')
});
}
});

View File

@ -208,10 +208,15 @@ const PostView = Discourse.GroupedView.extend(Ember.Evented, {
const likeAction = post.get('likeAction');
if (likeAction && likeAction.get('canToggle')) {
const users = this.get('likedUsers');
if (likeAction.toggle(post) && users.length) {
users.addObject(currentUser);
const store = this.get('controller.store');
const action = store.createRecord('post-action-user',
currentUser.getProperties('id', 'username', 'avatar_template')
);
if (likeAction.toggle(post) && users.get('length')) {
users.addObject(action);
} else {
users.removeObject(currentUser);
users.removeObject(action);
}
}
},
@ -221,7 +226,7 @@ const PostView = Discourse.GroupedView.extend(Ember.Evented, {
const likeAction = post.get('likeAction');
if (likeAction) {
const users = this.get('likedUsers');
if (users.length) {
if (users.get('length')) {
users.clear();
} else {
likeAction.loadUsers(post).then(newUsers => this.set('likedUsers', newUsers));

View File

@ -0,0 +1,22 @@
require_dependency 'discourse'
class PostActionUsersController < ApplicationController
def index
params.require(:post_action_type_id)
params.require(:id)
post_action_type_id = params[:post_action_type_id].to_i
finder = Post.where(id: params[:id].to_i)
finder = finder.with_deleted if guardian.is_staff?
post = finder.first
guardian.ensure_can_see!(post)
guardian.ensure_can_see_post_actors!(post.topic, post_action_type_id)
post_actions = post.post_actions.where(post_action_type_id: post_action_type_id)
.includes(:user)
.order('post_actions.created_at asc')
render_serialized(post_actions.to_a, PostActionUserSerializer, root: 'post_action_users')
end
end

View File

@ -1,8 +1,7 @@
require_dependency 'discourse'
class PostActionsController < ApplicationController
before_filter :ensure_logged_in, except: :users
before_filter :ensure_logged_in
before_filter :fetch_post_from_params
before_filter :fetch_post_action_type_id_from_params
@ -26,16 +25,6 @@ class PostActionsController < ApplicationController
end
end
def users
guardian.ensure_can_see_post_actors!(@post.topic, @post_action_type_id)
post_actions = @post.post_actions.where(post_action_type_id: @post_action_type_id)
.includes(:user)
.order('post_actions.created_at asc')
render_serialized(post_actions.to_a, PostActionUserSerializer)
end
def destroy
post_action = current_user.post_actions.find_by(post_id: params[:id].to_i, post_action_type_id: @post_action_type_id, deleted_at: nil)
raise Discourse::NotFound if post_action.blank?

View File

@ -365,6 +365,7 @@ Discourse::Application.routes.draw do
get "excerpt" => "excerpt#show"
resources :post_action_users
resources :post_actions do
collection do
get "users"

View File

@ -0,0 +1,34 @@
require 'spec_helper'
describe PostActionUsersController do
let!(:post) { Fabricate(:post, user: log_in) }
it 'raises an error without an id' do
expect {
xhr :get, :index, post_action_type_id: PostActionType.types[:like]
}.to raise_error(ActionController::ParameterMissing)
end
it 'raises an error without a post action type' do
expect {
xhr :get, :index, id: post.id
}.to raise_error(ActionController::ParameterMissing)
end
it "fails when the user doesn't have permission to see the post" do
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
xhr :get, :index, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_forbidden
end
it 'raises an error when the post action type cannot be seen' do
Guardian.any_instance.expects(:can_see_post_actors?).with(instance_of(Topic), PostActionType.types[:like]).returns(false)
xhr :get, :index, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_forbidden
end
it 'succeeds' do
xhr :get, :index, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_success
end
end

View File

@ -154,39 +154,4 @@ describe PostActionsController do
end
describe 'users' do
let!(:post) { Fabricate(:post, user: log_in) }
it 'raises an error without an id' do
expect {
xhr :get, :users, post_action_type_id: PostActionType.types[:like]
}.to raise_error(ActionController::ParameterMissing)
end
it 'raises an error without a post action type' do
expect {
xhr :get, :users, id: post.id
}.to raise_error(ActionController::ParameterMissing)
end
it "fails when the user doesn't have permission to see the post" do
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
xhr :get, :users, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_forbidden
end
it 'raises an error when the post action type cannot be seen' do
Guardian.any_instance.expects(:can_see_post_actors?).with(instance_of(Topic), PostActionType.types[:like]).returns(false)
xhr :get, :users, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_forbidden
end
it 'succeeds' do
xhr :get, :users, id: post.id, post_action_type_id: PostActionType.types[:like]
expect(response).to be_success
end
end
end