2017-09-29 13:17:03 -04:00
|
|
|
def print_status_with_label(label, current, max)
|
2017-09-20 09:49:17 -04:00
|
|
|
print "\r%s%9d / %d (%5.1f%%)" % [label, current, max, ((current.to_f / max.to_f) * 100).round(1)]
|
|
|
|
end
|
|
|
|
|
|
|
|
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|
|
|
|
|
topic.update_status("closed", true, Discourse.system_user)
|
2017-09-29 13:17:03 -04:00
|
|
|
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
|
2017-09-29 13:17:03 -04:00
|
|
|
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
|
2018-08-16 13:51:06 -04:00
|
|
|
|
|
|
|
def update_static_page_topic(locale, site_setting_key, title_key, body_key, params = {})
|
|
|
|
topic = Topic.find(SiteSetting.send(site_setting_key))
|
|
|
|
|
|
|
|
if (topic && post = topic.first_post)
|
|
|
|
post.revise(Discourse.system_user,
|
|
|
|
title: I18n.t(title_key, locale: locale),
|
|
|
|
raw: I18n.t(body_key, params.merge(locale: locale)))
|
|
|
|
|
|
|
|
puts "", "Topic for #{site_setting_key} updated"
|
|
|
|
else
|
|
|
|
puts "", "Topic for #{site_setting_key} not found"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Update static topics (ToS, Privacy, Guidelines) with latest translated content"
|
|
|
|
task "topics:update_static", [:locale] => [:environment] do |_, args|
|
|
|
|
locale = args[:locale]&.to_sym
|
|
|
|
|
|
|
|
if locale.blank? || !I18n.locale_available?(locale)
|
|
|
|
puts "ERROR: Expecting rake topics:update_static[locale]"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
update_static_page_topic(locale, "tos_topic_id", "tos_topic.title", "tos_topic.body",
|
2018-11-20 18:18:30 -05:00
|
|
|
company_name: SiteSetting.company_name.presence || "company_name",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
contact_email: SiteSetting.contact_email.presence || "contact_email",
|
|
|
|
governing_law: SiteSetting.governing_law.presence || "governing_law",
|
|
|
|
city_for_disputes: SiteSetting.city_for_disputes.presence || "city_for_disputes")
|
2018-08-16 13:51:06 -04:00
|
|
|
|
2018-11-07 11:59:42 -05:00
|
|
|
update_static_page_topic(locale, "guidelines_topic_id", "guidelines_topic.title", "guidelines_topic.body",
|
|
|
|
base_path: Discourse.base_path)
|
2018-08-16 13:51:06 -04:00
|
|
|
|
|
|
|
update_static_page_topic(locale, "privacy_topic_id", "privacy_topic.title", "privacy_topic.body")
|
|
|
|
end
|