diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 0660f24299b..1a6c5da4270 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -215,6 +215,27 @@ ORDER BY p.created_at desc def self.synchronize_target_topic_ids(post_ids = nil) + # nuke all dupes, using magic + builder = SqlBuilder.new <<SQL +DELETE FROM user_actions USING user_actions ua2 +/*where*/ +SQL + + builder.where <<SQL + user_actions.action_type = ua2.action_type AND + user_actions.user_id = ua2.user_id AND + user_actions.acting_user_id = ua2.acting_user_id AND + user_actions.target_post_id = ua2.target_post_id AND + user_actions.target_post_id > 0 AND + user_actions.id > ua2.id +SQL + + if post_ids + builder.where("user_actions.target_post_id in (:post_ids)", post_ids: post_ids) + end + + builder.exec + builder = SqlBuilder.new("UPDATE user_actions SET target_topic_id = (select topic_id from posts where posts.id = target_post_id) /*where*/") diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb index c09c50e0f8b..e845e854429 100644 --- a/spec/models/user_action_spec.rb +++ b/spec/models/user_action_spec.rb @@ -273,10 +273,16 @@ describe UserAction do target_post_id: post.id, ) - action.reload - action.target_topic_id.should == -1 + UserAction.log_action!( + action_type: UserAction::NEW_PRIVATE_MESSAGE, + user_id: post.user.id, + acting_user_id: post.user.id, + target_topic_id: -2, + target_post_id: post.id, + ) UserAction.ensure_consistency! + action.reload action.target_topic_id.should == post.topic_id