FIX: Do not update user stats like counts for private messages.

This commit is contained in:
Guo Xiang Tan 2017-01-16 10:18:10 +08:00
parent ed5fa20b0c
commit e3b6f9b8ae
4 changed files with 93 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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