Skip to content

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

FIX: Do not count deleted posts or topics in Rewind score calculation (#37434)

Martin Brennand8ee96b33530
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/writing_analysis.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/writing_analysis.rb
index 9d96dd507f8..f5810d06176 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/writing_analysis.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/writing_analysis.rb
@@ -18,18 +18,24 @@ module DiscourseRewind
total_words =
DB.query_single(<<~SQL, user_id: user.id, date_start: date.first, date_end: date.last)
- SELECT SUM(word_count) FROM posts
- WHERE user_id = :user_id
- AND created_at BETWEEN :date_start AND :date_end
- AND deleted_at IS NULL
+ SELECT SUM(p.word_count)
+ FROM posts p
+ INNER JOIN topics t ON t.id = p.topic_id
+ WHERE p.user_id = :user_id
+ AND p.created_at BETWEEN :date_start AND :date_end
+ AND p.deleted_at IS NULL
+ AND t.deleted_at IS NULL
SQL
post_count =
DB.query_single(<<~SQL, user_id: user.id, date_start: date.first, date_end: date.last)
- SELECT COUNT(*) FROM posts
- WHERE user_id = :user_id
- AND created_at BETWEEN :date_start AND :date_end
- AND deleted_at IS NULL
+ SELECT COUNT(*)
+ FROM posts p
+ INNER JOIN topics t ON t.id = p.topic_id
+ WHERE p.user_id = :user_id
+ AND p.created_at BETWEEN :date_start AND :date_end
+ AND p.deleted_at IS NULL
+ AND t.deleted_at IS NULL
SQL
average_post_length =
@@ -55,8 +61,11 @@ module DiscourseRewind
p.word_count,
regexp_replace(p.cooked, '<[^>]+>', ' ', 'g') AS plain
FROM posts p
+ INNER JOIN topics t ON t.id = p.topic_id
WHERE p.user_id = :user_id
AND p.created_at BETWEEN :start AND :end
+ AND p.deleted_at IS NULL
+ AND t.deleted_at IS NULL
),
metrics AS (
SELECT
diff --git a/plugins/discourse-rewind/spec/actions/writing_analysis_spec.rb b/plugins/discourse-rewind/spec/actions/writing_analysis_spec.rb
index ceb62dfc6db..f4178ed51d9 100644
--- a/plugins/discourse-rewind/spec/actions/writing_analysis_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/writing_analysis_spec.rb
@@ -140,6 +140,46 @@ RSpec.describe DiscourseRewind::Action::WritingAnalysis do
expect(result[:data][:total_words]).to eq(total_words)
end
+
+ it "does not include deleted posts in readability score calculation" do
+ result_with_deletion = call_report
+
+ post1.recover!
+ result_without_deletion = call_report
+
+ expect(result_with_deletion[:data][:readability_score]).not_to eq(
+ result_without_deletion[:data][:readability_score],
+ )
+ end
+ end
+
+ context "when a post's topic is deleted" do
+ before { post1.topic.trash!(Discourse.system_user) }
+
+ it "does not include deleted topic posts in total posts" do
+ result = call_report
+
+ expect(result[:data][:total_posts]).to eq(2)
+ end
+
+ it "does not include deleted topic posts in total words" do
+ result = call_report
+
+ total_words = [post2, post3].sum { |p| p.reload.word_count }
+
+ expect(result[:data][:total_words]).to eq(total_words)
+ end
+
+ it "does not include deleted topic posts in readability score calculation" do
+ result_with_deletion = call_report
+
+ post1.topic.recover!
+ result_without_deletion = call_report
+
+ expect(result_with_deletion[:data][:readability_score]).not_to eq(
+ result_without_deletion[:data][:readability_score],
+ )
+ end
end
context "when posts are from another user" do