diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 4a880a10c29..5051302b81d 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -277,7 +277,9 @@ SQL MessageBus.publish("/user/#{hash[:user_id]}", {user_action_id: action.id, remove: true}) end - update_like_count(hash[:user_id], hash[:action_type], -1) + if !Topic.where(id: hash[:target_topic_id], archetype: Archetype.private_message).exists? + update_like_count(hash[:user_id], hash[:action_type], -1) + end end def self.synchronize_target_topic_ids(post_ids = nil) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index bf12588e709..9b9ef68d54b 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -287,22 +287,26 @@ class PostRevisor .where(action_type: UserAction::WAS_LIKED) .update_all(user_id: new_owner.id) - prev_owner.user_stat.post_count -= 1 - prev_owner.user_stat.topic_count -= 1 if @post.is_first_post? - prev_owner.user_stat.likes_received -= likes - prev_owner.user_stat.update_topic_reply_count + private_message = @post.topic.private_message? + + prev_owner_user_stat = prev_owner.user_stat + prev_owner_user_stat.post_count -= 1 + prev_owner_user_stat.topic_count -= 1 if @post.is_first_post? + prev_owner_user_stat.likes_received -= likes if !private_message + prev_owner_user_stat.update_topic_reply_count if @post.created_at == prev_owner.user_stat.first_post_created_at - prev_owner.user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at) + prev_owner_user_stat.first_post_created_at = prev_owner.posts.order('created_at ASC').first.try(:created_at) end - prev_owner.user_stat.save + prev_owner_user_stat.save! - new_owner.user_stat.post_count += 1 - new_owner.user_stat.topic_count += 1 if @post.is_first_post? - new_owner.user_stat.likes_received += likes - new_owner.user_stat.update_topic_reply_count - new_owner.user_stat.save + new_owner_user_stat = new_owner.user_stat + new_owner_user_stat.post_count += 1 + new_owner_user_stat.topic_count += 1 if @post.is_first_post? + new_owner_user_stat.likes_received += likes if !private_message + new_owner_user_stat.update_topic_reply_count + new_owner_user_stat.save! end end diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb index 5aeaa257816..73ccd9a0679 100644 --- a/spec/models/user_action_spec.rb +++ b/spec/models/user_action_spec.rb @@ -105,7 +105,7 @@ describe UserAction do describe 'when user likes' do - let!(:post) { Fabricate(:post) } + let(:post) { Fabricate(:post) } let(:likee) { post.user } let(:liker) { Fabricate(:coding_horror) } @@ -140,6 +140,23 @@ describe UserAction do expect(liker.user_stat.reload.likes_given).to eq(0) end + context 'private message' do + let(:post) { Fabricate(:private_message_post) } + let(:likee) { post.topic.topic_allowed_users.first.user } + let(:liker) { post.topic.topic_allowed_users.last.user } + + it 'should not increase user stats' do + expect(@liker_action).not_to eq(nil) + expect(liker.user_stat.reload.likes_given).to eq(0) + expect(@likee_action).not_to eq(nil) + expect(likee.user_stat.reload.likes_received).to eq(0) + + PostAction.remove_act(liker, post, PostActionType.types[:like]) + expect(liker.user_stat.reload.likes_given).to eq(0) + expect(likee.user_stat.reload.likes_received).to eq(0) + end + end + end context "liking a private message" do diff --git a/spec/services/post_owner_changer_spec.rb b/spec/services/post_owner_changer_spec.rb index bdbb36541a8..10fb1cebec1 100644 --- a/spec/services/post_owner_changer_spec.rb +++ b/spec/services/post_owner_changer_spec.rb @@ -63,11 +63,21 @@ describe PostOwnerChanger do let(:p2user) { p2.user } before do - topic.user_id = p1user.id - topic.save! + topic.update!(user_id: p1user.id) - p1user.user_stat.update_attributes(topic_count: 1, post_count: 1, first_post_created_at: p1.created_at, topic_reply_count: 0) - p2user.user_stat.update_attributes(topic_count: 0, post_count: 1, first_post_created_at: p2.created_at, topic_reply_count: 1) + p1user.user_stat.update!( + topic_count: 1, + post_count: 1, + first_post_created_at: p1.created_at, + topic_reply_count: 0 + ) + + p2user.user_stat.update!( + topic_count: 0, + post_count: 1, + first_post_created_at: p2.created_at, + topic_reply_count: 1 + ) UserAction.create!( action_type: UserAction::NEW_TOPIC, user_id: p1user.id, acting_user_id: p1user.id, target_post_id: -1, target_topic_id: p1.topic_id, created_at: p1.created_at ) @@ -78,9 +88,19 @@ describe PostOwnerChanger do UserActionCreator.enable end - subject(:change_owners) { described_class.new(post_ids: [p1.id, p2.id], topic_id: topic.id, new_owner: user_a, acting_user: editor).change_owner! } + subject(:change_owners) do + described_class.new( + post_ids: [p1.id, p2.id], + topic_id: topic.id, + new_owner: user_a, + acting_user: editor + ).change_owner! + end it "updates users' topic and post counts" do + PostAction.act(p2user, p1, PostActionType.types[:like]) + expect(p1user.user_stat.reload.likes_received).to eq(1) + change_owners p1user.reload; p2user.reload; user_a.reload @@ -90,11 +110,38 @@ describe PostOwnerChanger do expect(p2user.post_count).to eq(0) expect(user_a.topic_count).to eq(1) expect(user_a.post_count).to eq(2) - expect(p1user.user_stat.first_post_created_at).to eq(nil) - expect(p2user.user_stat.first_post_created_at).to eq(nil) - expect(p1user.user_stat.topic_reply_count).to eq(0) - expect(p2user.user_stat.topic_reply_count).to eq(0) - expect(user_a.user_stat.first_post_created_at).to be_present + + p1_user_stat = p1user.user_stat + + expect(p1_user_stat.first_post_created_at).to eq(nil) + expect(p1_user_stat.topic_reply_count).to eq(0) + expect(p1_user_stat.likes_received).to eq(0) + + p2_user_stat = p2user.user_stat + + expect(p2_user_stat.first_post_created_at).to eq(nil) + expect(p2_user_stat.topic_reply_count).to eq(0) + + user_a_stat = user_a.user_stat + + expect(user_a_stat.first_post_created_at).to be_present + expect(user_a_stat.likes_received).to eq(1) + end + + context 'private message topic' do + let(:topic) { Fabricate(:private_message_topic) } + + it "should update users' counts" do + PostAction.act(p2user, p1, PostActionType.types[:like]) + + change_owners + + expect(p1user.user_stat.likes_received).to eq(0) + + user_a_stat = user_a.user_stat + expect(user_a_stat.first_post_created_at).to be_present + expect(user_a_stat.likes_received).to eq(0) + end end it "updates UserAction records" do