Skip to content

A platform for community discussion. Free, open, simple.

DEV: Optimize core user merger (#36455)

Amanda Alves Branquinho15e267f79196
diff --git a/app/models/topic_featured_users.rb b/app/models/topic_featured_users.rb
index 5383ae6655f..8604ace6476 100644
--- a/app/models/topic_featured_users.rb
+++ b/app/models/topic_featured_users.rb
@@ -88,11 +88,17 @@ SQL
private
def update_participant_count
- count =
- topic
- .posts
- .where("NOT hidden AND post_type in (?)", Topic.visible_post_types)
- .count("distinct user_id")
- topic.update_columns(participant_count: count)
+ DB.exec(<<~SQL, topic_id: topic.id)
+ UPDATE topics
+ SET participant_count = (
+ SELECT COUNT(DISTINCT user_id)
+ FROM posts
+ WHERE topic_id = :topic_id
+ AND NOT hidden
+ AND post_type IN (#{Topic.visible_post_types.join(",")})
+ AND deleted_at IS NULL
+ )
+ WHERE id = :topic_id
+ SQL
end
end
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index c9dcc00bddc..a493fc27baa 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -334,7 +334,8 @@ class PostRevisor
QuotedPost.extract_from(@post)
TopicLink.extract_from(@post)
- Topic.reset_highest(@topic.id)
+ # Skip resetting highest post number if only changing post ownership
+ Topic.reset_highest(@topic.id) unless @fields.size == 1 && @fields.has_key?("user_id")
post_process_post
alert_users
@@ -513,7 +514,10 @@ class PostRevisor
@post.link_post_uploads
@post.save_reply_relationships
- @editor.increment_post_edits_count if @post_successfully_saved
+ # we dont want to increment post count on user merge
+ if @post_successfully_saved && @editor.id != Discourse::SYSTEM_USER_ID
+ @editor.increment_post_edits_count
+ end
# post owner changed
if prev_owner && new_owner && prev_owner != new_owner
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index cbac1d300ce..82a35583f96 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -976,6 +976,31 @@ describe PostRevisor do
end
end
+ describe "changing post ownership" do
+ it "does not call Topic.reset_highest when only user_id is changed" do
+ new_owner = Fabricate(:user)
+ Topic.expects(:reset_highest).never
+
+ post_revisor.revise!(admin, user_id: new_owner.id)
+ end
+
+ it "calls Topic.reset_highest when user_id and other fields are changed" do
+ new_owner = Fabricate(:user)
+ Topic.expects(:reset_highest).once
+
+ post_revisor.revise!(admin, user_id: new_owner.id, raw: "updated body")
+ end
+
+ it "does not increment post_edits_count when system user changes ownership" do
+ new_owner = Fabricate(:user)
+ system_user = Discourse.system_user
+
+ expect do post_revisor.revise!(system_user, user_id: new_owner.id) end.not_to change {
+ system_user.user_stat.post_edits_count.to_i
+ }
+ end
+ end
+
it "doesn't strip starting whitespaces" do
post_revisor.revise!(post.user, raw: " <-- whitespaces --> ")
post.reload