more fixes for post ownership change and user deletion

This commit is contained in:
Neil Lalonde 2015-03-11 15:54:11 -04:00
parent 5de563fd2f
commit dece5a351a
3 changed files with 37 additions and 15 deletions

View File

@ -16,8 +16,9 @@ class PostOwnerChanger
@topic.user = @new_owner if post.is_first_post?
post.set_owner(@new_owner, @acting_user)
end
end
@topic.update_statistics
@topic.update_statistics
@new_owner.user_stat.update(first_post_created_at: @new_owner.posts(true).order('created_at ASC').first.try(:created_at))
end
end
end

View File

@ -200,19 +200,13 @@ class PostRevisor
end
def update_post
prev_owner, new_owner = nil, nil
if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id
if prev_owner = User.find_by_id(@post.user_id)
prev_owner.user_stat.update_attributes({post_count: prev_owner.post_count - 1}.merge(
@post.is_first_post? ? {topic_count: prev_owner.topic_count - 1} : {}
))
end
if new_owner = User.find_by_id(@fields["user_id"])
new_owner.user_stat.update_attributes({post_count: new_owner.post_count + 1}.merge(
@post.is_first_post? ? {topic_count: new_owner.topic_count + 1} : {}
))
end
prev_owner = User.find_by_id(@post.user_id)
new_owner = User.find_by_id(@fields["user_id"])
UserAction.where( target_post_id: @post.id,
user_id: @post.user_id,
user_id: prev_owner.id,
action_type: [UserAction::NEW_TOPIC, UserAction::REPLY, UserAction::RESPONSE] )
.find_each { |ua| ua.destroy }
# UserActionObserver will create new UserAction records for the new owner
@ -232,6 +226,28 @@ class PostRevisor
@post_successfully_saved = @post.save(validate: @validate_post)
@post.save_reply_relationships
# post owner changed
if prev_owner && new_owner && prev_owner != new_owner
likes = UserAction.where( target_post_id: @post.id,
user_id: prev_owner.id,
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
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)
end
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
end
end
def self_edit?

View File

@ -43,8 +43,8 @@ describe PostOwnerChanger do
topic.user_id = p1user.id
topic.save!
p1user.user_stat.update_attributes(topic_count: 1, post_count: 1)
p2user.user_stat.update_attributes(topic_count: 0, post_count: 1)
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)
UserAction.create!( action_type: UserAction::NEW_TOPIC, user_id: p1user.id, acting_user_id: p1user.id,
target_post_id: p1.id, target_topic_id: p1.topic_id, created_at: p1.created_at )
@ -66,6 +66,11 @@ describe PostOwnerChanger do
p2user.post_count.should == 0
user_a.topic_count.should == 1
user_a.post_count.should == 2
p1user.user_stat.first_post_created_at.should == nil
p2user.user_stat.first_post_created_at.should == nil
p1user.user_stat.topic_reply_count.should == 0
p2user.user_stat.topic_reply_count.should == 0
[p1.created_at, p2.created_at].should include(user_a.user_stat.first_post_created_at)
end
it "updates UserAction records" do