|
2 | 2 |
|
3 | 3 | module Jobs
|
4 | 4 | class MarkAsSolution < ::Jobs::Scheduled
|
5 |
| - every 14.days |
| 5 | + every 1.day |
6 | 6 |
|
7 | 7 | def execute(_args = nil)
|
8 |
| - return false unless SiteSetting.solved_enabled |
9 |
| - return false unless SiteSetting.solved_reminders_plugin_enabled |
| 8 | + return false unless SiteSetting.solved_enabled && SiteSetting.solved_reminders_plugin_enabled |
10 | 9 | Rails.logger.warn("Running scheduled job to send notifications to mark a post a solution.")
|
11 | 10 |
|
12 |
| - unsolved_topic_ids = [] |
| 11 | + base_query = |
| 12 | + Topic |
| 13 | + .listable_topics |
| 14 | + .where(closed: false, archived: false, visible: true) |
| 15 | + .where("posts_count > 0") |
| 16 | + .where("last_post_user_id <> user_id") |
| 17 | + .where("last_posted_at > ?", SiteSetting.remind_mark_solution_last_post_age.days.ago) |
| 18 | + .where( |
| 19 | + "NOT EXISTS (SELECT 1 FROM discourse_solved_solved_topics dsst WHERE dsst.topic_id = topics.id)", |
| 20 | + ) |
13 | 21 |
|
14 |
| - if SiteSetting.allow_solved_on_all_topics |
15 |
| - unsolved_topic_ids = unsolved_topic_ids.push(*Topic.pluck(:id)) |
16 |
| - else |
| 22 | + if !SiteSetting.allow_solved_on_all_topics |
17 | 23 | category_ids =
|
18 |
| - CategoryCustomField.where(name: "enable_accepted_answers", value: "true").pluck( |
| 24 | + CategoryCustomField.where(name: "enable_accepted_answers", value: "true").select( |
19 | 25 | :category_id,
|
20 | 26 | )
|
21 | 27 |
|
22 |
| - unsolved_topic_ids = |
23 |
| - unsolved_topic_ids.push(*Topic.where(category_id: category_ids).pluck(:id)) |
24 |
| - |
25 |
| - if SiteSetting.enable_solved_tags.present? |
26 |
| - allowed_tags_ids = Tag.where_name(SiteSetting.enable_solved_tags.split("|")).pluck(:id) |
27 |
| - unsolved_topic_ids = |
28 |
| - unsolved_topic_ids.push( |
29 |
| - *TopicTag.where(tag_id: allowed_tags_ids).distinct.pluck(:topic_id), |
30 |
| - ) |
31 |
| - end |
| 28 | + tag_topic_ids = |
| 29 | + if SiteSetting.enable_solved_tags.present? |
| 30 | + TopicTag.where( |
| 31 | + tag_id: Tag.where_name(SiteSetting.enable_solved_tags.split("|")).select(:id), |
| 32 | + ).select(:topic_id) |
| 33 | + end |
| 34 | + |
| 35 | + base_query = |
| 36 | + if tag_topic_ids |
| 37 | + base_query.where("category_id IN (?) OR id IN (?)", category_ids, tag_topic_ids) |
| 38 | + else |
| 39 | + base_query.where(category_id: category_ids) |
| 40 | + end |
32 | 41 | end
|
33 | 42 |
|
34 |
| - solved_topic_ids = TopicCustomField.where(name: "accepted_answer_post_id").pluck(:topic_id) |
35 |
| - unsolved_topic_ids = unsolved_topic_ids - solved_topic_ids if solved_topic_ids.present? |
| 43 | + cutoff_date = SiteSetting.remind_mark_solution_after_days.days.ago |
36 | 44 |
|
37 |
| - unsolved_topics = |
38 |
| - Topic |
39 |
| - .listable_topics |
40 |
| - .where(id: unsolved_topic_ids) |
41 |
| - .where(closed: false, archived: false, visible: true) |
42 |
| - .where("posts_count > 0") |
43 |
| - .where("last_post_user_id <> user_id") |
44 |
| - .where("last_posted_at > ?", SiteSetting.remind_mark_solution_last_post_age.days.ago) |
45 |
| - |
46 |
| - unsolved_topics.each do |topic| |
47 |
| - if (topic&.last_posted_at || Date.today) < |
48 |
| - SiteSetting.remind_mark_solution_after_days.days.ago |
49 |
| - # create a new reminder PM |
| 45 | + discobot = User.find(-2) |
| 46 | + base_query |
| 47 | + .where("COALESCE(last_posted_at, current_date) < ?", cutoff_date) |
| 48 | + .find_each do |topic| |
50 | 49 | PostCreator.create!(
|
51 |
| - User.find(-2), |
| 50 | + discobot, |
52 | 51 | title: I18n.t("mark_as_solution.title"),
|
53 | 52 | raw: "#{I18n.t("mark_as_solution.message")}\n\n#{topic.url}",
|
54 | 53 | archetype: Archetype.private_message,
|
55 | 54 | target_usernames: [topic.user.username],
|
56 | 55 | skip_validations: true,
|
57 | 56 | )
|
58 | 57 | end
|
59 |
| - end |
60 | 58 | end
|
61 | 59 | end
|
62 | 60 | end
|
0 commit comments