2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
require "rake_helpers"
|
2017-09-20 09:49:17 -04:00
|
|
|
|
|
|
|
def close_old_topics(category)
|
|
|
|
topics = Topic.where(closed: false, category_id: category.id)
|
|
|
|
|
|
|
|
if category.auto_close_based_on_last_post
|
|
|
|
topics = topics.where("last_posted_at < ?", category.auto_close_hours.hours.ago)
|
|
|
|
else
|
|
|
|
topics = topics.where("created_at < ?", category.auto_close_hours.hours.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
topics_closed = 0
|
|
|
|
total = topics.count
|
|
|
|
|
2017-09-20 17:18:53 -04:00
|
|
|
if total == 0
|
|
|
|
print " all old topics are closed"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-09-20 09:49:17 -04:00
|
|
|
topics.find_each do |topic|
|
2023-08-15 21:20:47 -04:00
|
|
|
topic.update_status("closed", true, Discourse.system_user, { silent: true })
|
2019-11-17 20:25:42 -05:00
|
|
|
RakeHelpers.print_status_with_label(" closing old topics: ", topics_closed += 1, total)
|
2017-09-20 09:49:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_auto_close(category)
|
|
|
|
topics =
|
|
|
|
Topic.where(closed: false, category_id: category.id).where(<<-SQL, TopicTimer.types[:close])
|
|
|
|
NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM topic_timers
|
|
|
|
WHERE topic_timers.topic_id = topics.id
|
|
|
|
AND topic_timers.status_type = ?
|
2017-09-20 17:18:53 -04:00
|
|
|
AND topic_timers.deleted_at IS NULL
|
2017-09-20 09:49:17 -04:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
topics_closed = 0
|
|
|
|
total = topics.count
|
|
|
|
|
2017-09-20 17:18:53 -04:00
|
|
|
if total == 0
|
|
|
|
print " all topics have auto-close applied"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-09-20 09:49:17 -04:00
|
|
|
topics.find_each do |topic|
|
|
|
|
topic.inherit_auto_close_from_category
|
2019-11-17 20:25:42 -05:00
|
|
|
RakeHelpers.print_status_with_label(
|
|
|
|
" applying auto-close to topics: ",
|
|
|
|
topics_closed += 1,
|
|
|
|
total,
|
|
|
|
)
|
2017-09-20 09:49:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task "topics:apply_autoclose" => :environment do
|
|
|
|
categories = Category.where("auto_close_hours > 0")
|
|
|
|
|
|
|
|
categories.find_each do |category|
|
|
|
|
puts "", "Applying auto-close to category '#{category.name}' ..."
|
|
|
|
close_old_topics(category)
|
2017-09-20 17:18:53 -04:00
|
|
|
puts ""
|
2017-09-20 09:49:17 -04:00
|
|
|
apply_auto_close(category)
|
2017-09-20 17:18:53 -04:00
|
|
|
puts ""
|
2017-09-20 09:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done"
|
|
|
|
end
|
2019-05-30 11:48:16 -04:00
|
|
|
|
|
|
|
task "topics:watch_all_replied_topics" => :environment do
|
|
|
|
puts "Setting all topics to Watching on which a user has posted at least once..."
|
|
|
|
topics = Topic.where("archetype != ?", Archetype.private_message)
|
|
|
|
total = topics.count
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
topics.find_each do |t|
|
|
|
|
t
|
|
|
|
.topic_users
|
|
|
|
.where(posted: true)
|
|
|
|
.find_each do |tp|
|
|
|
|
tp.update!(
|
|
|
|
notification_level: TopicUser.notification_levels[:watching],
|
|
|
|
notifications_reason_id: TopicUser.notification_reasons[:created_post],
|
|
|
|
)
|
|
|
|
end
|
2019-11-17 20:25:42 -05:00
|
|
|
RakeHelpers.print_status(count += 1, total)
|
2019-05-30 11:48:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done"
|
|
|
|
end
|
|
|
|
|
2019-07-12 15:09:57 -04:00
|
|
|
task "topics:update_fancy_titles" => :environment do
|
|
|
|
if !SiteSetting.title_fancy_entities?
|
|
|
|
puts "fancy topic titles are disabled"
|
2021-11-10 10:53:55 -05:00
|
|
|
next
|
2019-07-12 15:09:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
DB.exec("UPDATE topics SET fancy_title = NULL")
|
|
|
|
|
|
|
|
total = Topic.count
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
Topic.find_each do |topic|
|
|
|
|
topic.fancy_title
|
2019-11-17 20:25:42 -05:00
|
|
|
RakeHelpers.print_status(count += 1, total)
|
2019-07-12 15:09:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done"
|
|
|
|
end
|