2024-04-03 23:20:43 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
class DiscourseAutomation::StalledTopicFinder
|
2024-04-03 23:20:43 +08:00
|
|
|
def self.call(stalled_date, tags: nil, categories: nil)
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT t.id
|
|
|
|
FROM topics t
|
|
|
|
SQL
|
|
|
|
|
2024-08-06 04:26:12 +08:00
|
|
|
sql += <<~SQL if tags.present?
|
2024-04-03 23:20:43 +08:00
|
|
|
JOIN topic_tags ON topic_tags.topic_id = t.id
|
|
|
|
JOIN tags
|
|
|
|
ON tags.name IN (:tags)
|
|
|
|
AND tags.id = topic_tags.tag_id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
sql += <<~SQL
|
|
|
|
WHERE t.deleted_at IS NULL
|
|
|
|
AND t.posts_count > 0
|
|
|
|
AND t.archetype != 'private_message'
|
|
|
|
AND NOT t.closed
|
|
|
|
AND NOT t.archived
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT p.id
|
|
|
|
FROM posts p
|
|
|
|
WHERE t.id = p.topic_id
|
|
|
|
AND p.deleted_at IS NULL
|
|
|
|
AND t.user_id = p.user_id
|
|
|
|
AND p.created_at > :stalled_date
|
|
|
|
LIMIT 1
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2024-08-06 04:26:12 +08:00
|
|
|
sql += <<~SQL if categories.present?
|
2024-04-03 23:20:43 +08:00
|
|
|
AND t.category_id IN (:categories)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
sql += <<~SQL
|
|
|
|
LIMIT 250
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.query(sql, categories: categories, tags: tags, stalled_date: stalled_date)
|
|
|
|
end
|
|
|
|
end
|