2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-19 04:09:13 +08:00
|
|
|
module SeedData
|
|
|
|
class Topics
|
|
|
|
def self.with_default_locale
|
|
|
|
SeedData::Topics.new(SiteSetting.default_locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(locale)
|
|
|
|
@locale = locale
|
|
|
|
end
|
|
|
|
|
2023-05-25 04:05:36 +08:00
|
|
|
def create(site_setting_names: nil, include_welcome_topics: true, include_legal_topics: false)
|
2019-03-19 04:09:13 +08:00
|
|
|
I18n.with_locale(@locale) do
|
2023-05-25 04:05:36 +08:00
|
|
|
topics(
|
|
|
|
site_setting_names: site_setting_names,
|
|
|
|
include_welcome_topics: include_welcome_topics,
|
|
|
|
include_legal_topics: include_legal_topics || SiteSetting.company_name.present?,
|
|
|
|
).each { |params| create_topic(**params) }
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(site_setting_names: nil, skip_changed: false)
|
|
|
|
I18n.with_locale(@locale) do
|
2023-05-25 04:05:36 +08:00
|
|
|
topics(site_setting_names: site_setting_names).each do |params|
|
|
|
|
update_topic(**params.except(:category, :after_create), skip_changed: skip_changed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(site_setting_names: nil, skip_changed: false)
|
|
|
|
I18n.with_locale(@locale) do
|
|
|
|
topics(site_setting_names: site_setting_names).each do |params|
|
|
|
|
delete_topic(**params.slice(:site_setting_name), skip_changed: skip_changed)
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reseed_options
|
|
|
|
I18n.with_locale(@locale) do
|
|
|
|
topics
|
|
|
|
.map do |params|
|
|
|
|
post = find_post(params[:site_setting_name])
|
|
|
|
next unless post
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2019-03-19 04:09:13 +08:00
|
|
|
{ id: params[:site_setting_name], name: post.topic.title, selected: unchanged?(post) }
|
|
|
|
end
|
|
|
|
.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-05-25 04:05:36 +08:00
|
|
|
def topics(site_setting_names: nil, include_welcome_topics: true, include_legal_topics: true)
|
2019-03-19 04:09:13 +08:00
|
|
|
staff_category = Category.find_by(id: SiteSetting.staff_category_id)
|
2024-02-01 04:33:09 +08:00
|
|
|
feedback_category = Category.find_by(id: SiteSetting.meta_category_id)
|
|
|
|
feedback_category_hashtag =
|
|
|
|
feedback_category ? "##{feedback_category.slug}" : "#site-feedback"
|
2019-03-19 04:09:13 +08:00
|
|
|
|
2023-05-25 04:05:36 +08:00
|
|
|
topics = []
|
|
|
|
|
|
|
|
# Terms of Service
|
|
|
|
if include_legal_topics
|
|
|
|
topics << {
|
2019-03-19 04:09:13 +08:00
|
|
|
site_setting_name: "tos_topic_id",
|
|
|
|
title: I18n.t("tos_topic.title"),
|
|
|
|
raw:
|
|
|
|
I18n.t(
|
|
|
|
"tos_topic.body",
|
|
|
|
company_name: setting_value("company_name"),
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
contact_email: setting_value("contact_email"),
|
|
|
|
governing_law: setting_value("governing_law"),
|
|
|
|
city_for_disputes: setting_value("city_for_disputes"),
|
|
|
|
),
|
|
|
|
category: staff_category,
|
|
|
|
static_first_reply: true,
|
2023-05-25 04:05:36 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# FAQ/Guidelines
|
|
|
|
topics << {
|
|
|
|
site_setting_name: "guidelines_topic_id",
|
|
|
|
title: I18n.t("guidelines_topic.title"),
|
2024-02-01 04:33:09 +08:00
|
|
|
raw:
|
|
|
|
I18n.t(
|
|
|
|
"guidelines_topic.body",
|
|
|
|
base_path: Discourse.base_path,
|
2024-02-01 08:40:30 +08:00
|
|
|
feedback_category: feedback_category_hashtag,
|
2024-02-01 04:33:09 +08:00
|
|
|
),
|
2023-05-25 04:05:36 +08:00
|
|
|
category: staff_category,
|
|
|
|
static_first_reply: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Privacy Policy
|
|
|
|
if include_legal_topics
|
|
|
|
topics << {
|
2019-03-19 04:09:13 +08:00
|
|
|
site_setting_name: "privacy_topic_id",
|
|
|
|
title: I18n.t("privacy_topic.title"),
|
|
|
|
raw: I18n.t("privacy_topic.body"),
|
|
|
|
category: staff_category,
|
|
|
|
static_first_reply: true,
|
2023-05-25 04:05:36 +08:00
|
|
|
}
|
|
|
|
end
|
2019-03-19 04:09:13 +08:00
|
|
|
|
|
|
|
if include_welcome_topics
|
|
|
|
# Welcome Topic
|
2022-08-30 03:05:41 +08:00
|
|
|
if general_category = Category.find_by(id: SiteSetting.general_category_id)
|
2023-06-07 03:13:28 +08:00
|
|
|
site_info_quote =
|
|
|
|
if SiteSetting.title.present? && SiteSetting.site_description.present?
|
|
|
|
<<~RAW
|
|
|
|
> ## #{SiteSetting.title}
|
|
|
|
>
|
|
|
|
> #{SiteSetting.site_description}
|
|
|
|
RAW
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2019-03-19 04:09:13 +08:00
|
|
|
topics << {
|
2022-08-30 03:05:41 +08:00
|
|
|
site_setting_name: "welcome_topic_id",
|
2023-05-12 22:09:40 +08:00
|
|
|
title: I18n.t("discourse_welcome_topic.title", site_title: SiteSetting.title),
|
|
|
|
raw:
|
|
|
|
I18n.t(
|
|
|
|
"discourse_welcome_topic.body",
|
|
|
|
base_path: Discourse.base_path,
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
site_description: SiteSetting.site_description,
|
2023-06-07 03:13:28 +08:00
|
|
|
site_info_quote: site_info_quote,
|
2023-08-01 19:53:23 +08:00
|
|
|
feedback_category: feedback_category_hashtag,
|
2023-05-12 22:09:40 +08:00
|
|
|
),
|
2022-08-30 03:05:41 +08:00
|
|
|
category: general_category,
|
|
|
|
after_create: proc { |post| post.topic.update_pinned(true, true) },
|
2019-03-19 04:09:13 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Admin Quick Start Guide
|
|
|
|
topics << {
|
|
|
|
site_setting_name: "admin_quick_start_topic_id",
|
|
|
|
title:
|
|
|
|
DiscoursePluginRegistry.seed_data["admin_quick_start_title"] ||
|
|
|
|
I18n.t("admin_quick_start_title"),
|
|
|
|
raw: admin_quick_start_raw,
|
|
|
|
category: staff_category,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if site_setting_names
|
|
|
|
topics.select! { |t| site_setting_names.include?(t[:site_setting_name]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
topics
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_topic(
|
|
|
|
site_setting_name:,
|
|
|
|
title:,
|
|
|
|
raw:,
|
|
|
|
category: nil,
|
|
|
|
static_first_reply: false,
|
|
|
|
after_create: nil
|
|
|
|
)
|
2019-05-07 09:00:09 +08:00
|
|
|
topic_id = SiteSetting.get(site_setting_name)
|
2019-03-19 04:09:13 +08:00
|
|
|
return if topic_id > 0 || Topic.find_by(id: topic_id)
|
|
|
|
|
|
|
|
post =
|
|
|
|
PostCreator.create!(
|
|
|
|
Discourse.system_user,
|
|
|
|
title: title,
|
|
|
|
raw: raw,
|
2020-11-20 00:31:21 +08:00
|
|
|
skip_jobs: true,
|
2019-03-19 04:09:13 +08:00
|
|
|
skip_validations: true,
|
2019-05-25 01:16:19 +08:00
|
|
|
category: category&.id,
|
2019-03-19 04:09:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if static_first_reply
|
|
|
|
PostCreator.create!(
|
|
|
|
Discourse.system_user,
|
|
|
|
raw: first_reply_raw(title),
|
2020-11-20 00:31:21 +08:00
|
|
|
skip_jobs: true,
|
2019-03-19 04:09:13 +08:00
|
|
|
skip_validations: true,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
after_create&.call(post)
|
|
|
|
|
2019-05-07 09:00:09 +08:00
|
|
|
SiteSetting.set(site_setting_name, post.topic_id)
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_topic(site_setting_name:, title:, raw:, static_first_reply: false, skip_changed:)
|
2023-05-25 04:05:36 +08:00
|
|
|
post = find_post(site_setting_name, deleted: true)
|
2019-03-19 04:09:13 +08:00
|
|
|
return if !post
|
|
|
|
|
|
|
|
if !skip_changed || unchanged?(post)
|
2023-05-25 04:05:36 +08:00
|
|
|
if post.trashed?
|
|
|
|
PostDestroyer.new(Discourse.system_user, post).recover
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
post.revise(Discourse.system_user, { title: title, raw: raw }, skip_validations: true)
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if static_first_reply && (reply = first_reply(post)) && (!skip_changed || unchanged?(reply))
|
2023-05-25 04:05:36 +08:00
|
|
|
reply.revise(Discourse.system_user, { raw: first_reply_raw(title) }, skip_validations: true)
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-25 04:05:36 +08:00
|
|
|
def delete_topic(site_setting_name:, skip_changed:)
|
|
|
|
post = find_post(site_setting_name)
|
|
|
|
return if !post
|
|
|
|
|
|
|
|
PostDestroyer.new(Discourse.system_user, post).destroy if !skip_changed || unchanged?(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_post(site_setting_name, deleted: false)
|
2019-05-07 09:00:09 +08:00
|
|
|
topic_id = SiteSetting.get(site_setting_name)
|
2023-05-25 04:05:36 +08:00
|
|
|
return if topic_id < 1
|
|
|
|
|
|
|
|
posts = Post.where(topic_id: topic_id, post_number: 1)
|
|
|
|
posts = posts.with_deleted if deleted
|
|
|
|
posts.first
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unchanged?(post)
|
2023-05-25 04:05:36 +08:00
|
|
|
post.last_editor_id == Discourse::SYSTEM_USER_ID &&
|
|
|
|
(!post.deleted_by_id || post.deleted_by_id == Discourse::SYSTEM_USER_ID)
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def setting_value(site_setting_key)
|
2019-05-07 09:00:09 +08:00
|
|
|
SiteSetting.get(site_setting_key).presence || "<ins>#{site_setting_key}</ins>"
|
2019-03-19 04:09:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def first_reply(post)
|
|
|
|
Post.find_by(topic_id: post.topic_id, post_number: 2, user_id: Discourse::SYSTEM_USER_ID)
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_reply_raw(topic_title)
|
|
|
|
I18n.t("static_topic_first_reply", page_name: topic_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_quick_start_raw
|
|
|
|
quick_start_filename = DiscoursePluginRegistry.seed_data["admin_quick_start_filename"]
|
|
|
|
|
|
|
|
if !quick_start_filename || !File.exist?(quick_start_filename)
|
|
|
|
# TODO Make the quick start guide translatable
|
|
|
|
quick_start_filename = File.join(Rails.root, "docs", "ADMIN-QUICK-START-GUIDE.md")
|
|
|
|
end
|
|
|
|
|
|
|
|
File.read(quick_start_filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|