Better filtering out private messages on user stream.

This commit is contained in:
Robin Ward 2013-02-15 17:08:28 -05:00
parent fc1c120014
commit b440e30f45
2 changed files with 81 additions and 42 deletions

View file

@ -4,6 +4,7 @@ require_dependency 'sql_builder'
class UserAction < ActiveRecord::Base class UserAction < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :target_post, :class_name => "Post" belongs_to :target_post, :class_name => "Post"
belongs_to :target_topic, :class_name => "Topic"
attr_accessible :acting_user_id, :action_type, :target_topic_id, :target_post_id, :target_user_id, :user_id attr_accessible :acting_user_id, :action_type, :target_topic_id, :target_post_id, :target_user_id, :user_id
validates_presence_of :action_type validates_presence_of :action_type
@ -38,16 +39,18 @@ class UserAction < ActiveRecord::Base
].each_with_index.to_a.flatten] ].each_with_index.to_a.flatten]
def self.stats(user_id, guardian) def self.stats(user_id, guardian)
results = UserAction.select("action_type, COUNT(*) count, '' description") results = UserAction.select("action_type, COUNT(*) count, '' AS description")
.joins(:target_topic)
.where(user_id: user_id) .where(user_id: user_id)
.group('action_type') .group('action_type', 'topics.archetype')
.to_a
# should push this into the sql at some point, but its simple enough for now # should push this into the sql at some point, but its simple enough for now
unless guardian.can_see_private_messages?(user_id) unless guardian.can_see_private_messages?(user_id)
results.reject!{|a| [GOT_PRIVATE_MESSAGE, NEW_PRIVATE_MESSAGE].include?(a.action_type)} results = results.where('topics.archetype <> ?', Archetype::private_message)
end end
results = results.to_a
results.sort!{|a,b| ORDER[a.action_type] <=> ORDER[b.action_type]} results.sort!{|a,b| ORDER[a.action_type] <=> ORDER[b.action_type]}
results.each do |row| results.each do |row|
row.description = self.description(row.action_type, detailed: true) row.description = self.description(row.action_type, detailed: true)
@ -100,7 +103,7 @@ JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id)
end end
if !guardian.can_see_private_messages?(user_id) || ignore_private_messages if !guardian.can_see_private_messages?(user_id) || ignore_private_messages
builder.where("a.action_type not in (#{NEW_PRIVATE_MESSAGE},#{GOT_PRIVATE_MESSAGE})") builder.where("t.archetype != :archetype", archetype: Archetype::private_message)
end end
if action_id if action_id

View file

@ -8,34 +8,39 @@ describe UserAction do
describe 'lists' do describe 'lists' do
before do let(:public_post) { Fabricate(:post) }
a = UserAction.new let(:public_topic) { public_post.topic }
@post = Fabricate(:post) let(:user) { Fabricate(:user) }
@user = Fabricate(:coding_horror)
row = { let(:private_post) { Fabricate(:post) }
let(:private_topic) do
topic = private_post.topic
topic.update_column(:archetype, Archetype::private_message)
topic
end
def log_test_action(opts={})
UserAction.log_action!({
action_type: UserAction::NEW_PRIVATE_MESSAGE, action_type: UserAction::NEW_PRIVATE_MESSAGE,
user_id: @user.id, user_id: user.id,
acting_user_id: @user.id, acting_user_id: user.id,
target_topic_id: @post.topic_id, target_topic_id: private_topic.id,
target_post_id: @post.id, target_post_id: private_post.id,
} }.merge(opts))
end
UserAction.log_action!(row) before do
# Create some test data using a helper
row[:action_type] = UserAction::GOT_PRIVATE_MESSAGE log_test_action
UserAction.log_action!(row) log_test_action(action_type: UserAction::GOT_PRIVATE_MESSAGE)
log_test_action(action_type: UserAction::NEW_TOPIC, target_topic_id: public_topic.id, target_post_id: public_post.id)
row[:action_type] = UserAction::NEW_TOPIC log_test_action(action_type: UserAction::BOOKMARK)
UserAction.log_action!(row)
row[:action_type] = UserAction::BOOKMARK
UserAction.log_action!(row)
end end
describe 'stats' do describe 'stats' do
let :mystats do let :mystats do
UserAction.stats(@user.id,Guardian.new(@user)) UserAction.stats(user.id, Guardian.new(user))
end end
it 'should include non private message events' do it 'should include non private message events' do
@ -43,11 +48,11 @@ describe UserAction do
end end
it 'should exclude private messages for non owners' do it 'should exclude private messages for non owners' do
UserAction.stats(@user.id,Guardian.new).map{|r| r["action_type"].to_i}.should_not include(UserAction::NEW_PRIVATE_MESSAGE) UserAction.stats(user.id,Guardian.new).map{|r| r["action_type"].to_i}.should_not include(UserAction::NEW_PRIVATE_MESSAGE)
end end
it 'should not include got private messages for owners' do it 'should not include got private messages for owners' do
UserAction.stats(@user.id,Guardian.new).map{|r| r["action_type"].to_i}.should_not include(UserAction::GOT_PRIVATE_MESSAGE) UserAction.stats(user.id,Guardian.new).map{|r| r["action_type"].to_i}.should_not include(UserAction::GOT_PRIVATE_MESSAGE)
end end
it 'should include private messages for owners' do it 'should include private messages for owners' do
@ -62,11 +67,11 @@ describe UserAction do
describe 'stream' do describe 'stream' do
it 'should have 1 item for non owners' do it 'should have 1 item for non owners' do
UserAction.stream(user_id: @user.id, guardian: Guardian.new).count.should == 1 UserAction.stream(user_id: user.id, guardian: Guardian.new).count.should == 1
end end
it 'should have bookmarks and pms for owners' do it 'should have bookmarks and pms for owners' do
UserAction.stream(user_id: @user.id, guardian: @user.guardian).count.should == 4 UserAction.stream(user_id: user.id, guardian: user.guardian).count.should == 4
end end
end end
@ -78,13 +83,29 @@ describe UserAction do
end end
describe 'when user likes' do describe 'when user likes' do
let!(:post) { Fabricate(:post) }
let(:likee) { post.user }
let(:liker) { Fabricate(:coding_horror) }
def likee_stream
UserAction.stream(user_id: likee.id, guardian: Guardian.new)
end
before do before do
@post = Fabricate(:post) @old_count = likee_stream.count
@likee = @post.user end
@liker = Fabricate(:coding_horror)
PostAction.act(@liker, @post, PostActionType.Types[:like]) it "creates a new stream entry" do
@liker_action = @liker.user_actions.where(action_type: UserAction::LIKE).first PostAction.act(liker, post, PostActionType.Types[:like])
@likee_action = @likee.user_actions.where(action_type: UserAction::WAS_LIKED).first likee_stream.count.should == @old_count + 1
end
context "successful like" do
before do
PostAction.act(liker, post, PostActionType.Types[:like])
@liker_action = liker.user_actions.where(action_type: UserAction::LIKE).first
@likee_action = likee.user_actions.where(action_type: UserAction::WAS_LIKED).first
end end
it 'should create a like action on the liker' do it 'should create a like action on the liker' do
@ -96,6 +117,21 @@ describe UserAction do
end end
end end
context "liking a private message" do
before do
post.topic.update_column(:archetype, Archetype::private_message)
end
it "doesn't add the entry to the stream" do
PostAction.act(liker, post, PostActionType.Types[:like])
likee_stream.count.should_not == @old_count + 1
end
end
end
describe 'when a user posts a new topic' do describe 'when a user posts a new topic' do
before do before do
@post = Fabricate(:old_post) @post = Fabricate(:old_post)