Skip to content

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

FIX: Missing `t.deleted_at IS NULL` filter exposes deleted topics (#37718)

Joffrey JAFFEUX7e980df9d40f
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
index 45aa2824645..2e1f54264c0 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
@@ -64,6 +64,7 @@ module DiscourseAi
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
AND p.deleted_at IS NULL
+ AND t.deleted_at IS NULL
ORDER BY p.created_at DESC
LIMIT :limit OFFSET :offset
SQL
diff --git a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
index 2f4e98e6265..c6dda7bc488 100644
--- a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
@@ -35,5 +35,26 @@ RSpec.describe DiscourseAi::Sentiment::SentimentController do
expect(post["sentiment"]).to match(/positive|negative|neutral/)
end
end
+
+ it "excludes posts from soft-deleted topics" do
+ deleted_topic = Fabricate(:topic, category: category, deleted_at: Time.current)
+ deleted_topic_post = Fabricate(:post, user: admin, topic: deleted_topic)
+ Fabricate(:sentiment_classification, target: deleted_topic_post)
+
+ live_topic_post = Fabricate(:post, user: admin, topic: topic)
+ Fabricate(:sentiment_classification, target: live_topic_post)
+
+ get "/discourse-ai/sentiment/posts",
+ params: {
+ group_by: "category",
+ group_value: category.name,
+ }
+
+ expect(response).to be_successful
+
+ post_ids = JSON.parse(response.body)["posts"].map { |p| p["post_id"] }
+ expect(post_ids).to include(live_topic_post.id)
+ expect(post_ids).not_to include(deleted_topic_post.id)
+ end
end
end