2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-20 17:01:54 -04:00
|
|
|
class IntroductionUpdater
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_summary
|
|
|
|
summary_from_post(find_welcome_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_summary(new_value)
|
|
|
|
post = find_welcome_post
|
|
|
|
return unless post
|
|
|
|
|
|
|
|
previous_value = summary_from_post(post).strip
|
|
|
|
|
|
|
|
if previous_value != new_value
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
|
|
|
|
remaining = post.raw.split("\n")[1..-1]
|
|
|
|
revisor.revise!(@user, raw: "#{new_value}\n#{remaining.join("\n")}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 12:05:37 -05:00
|
|
|
protected
|
2016-09-20 17:01:54 -04:00
|
|
|
|
|
|
|
def summary_from_post(post)
|
2019-11-14 15:10:51 -05:00
|
|
|
post ? post.raw.split("\n").first : nil
|
2016-09-20 17:01:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_welcome_post
|
2019-03-18 16:09:13 -04:00
|
|
|
topic_id = SiteSetting.welcome_topic_id
|
2018-12-05 12:05:37 -05:00
|
|
|
|
2019-03-18 16:09:13 -04:00
|
|
|
if topic_id <= 0
|
2018-12-05 12:05:37 -05:00
|
|
|
title = I18n.t("discourse_welcome_topic.title")
|
|
|
|
topic_id = find_topic_id(title)
|
2018-08-06 07:27:17 -04:00
|
|
|
end
|
|
|
|
|
2018-12-05 12:05:37 -05:00
|
|
|
if topic_id.blank?
|
|
|
|
title = I18n.t("discourse_welcome_topic.title", locale: :en)
|
|
|
|
topic_id = find_topic_id(title)
|
|
|
|
end
|
|
|
|
|
|
|
|
if topic_id.blank?
|
|
|
|
topic_id = Topic.listable_topics
|
|
|
|
.where(pinned_globally: true)
|
|
|
|
.order(:created_at)
|
|
|
|
.limit(1)
|
|
|
|
.pluck(:id)
|
|
|
|
end
|
2016-09-20 17:01:54 -04:00
|
|
|
|
2018-12-05 12:05:37 -05:00
|
|
|
welcome_topic = Topic.where(id: topic_id).first
|
|
|
|
return nil if welcome_topic.blank?
|
2016-09-20 17:01:54 -04:00
|
|
|
|
2018-12-05 12:05:37 -05:00
|
|
|
welcome_topic.first_post
|
2016-09-20 17:01:54 -04:00
|
|
|
end
|
|
|
|
|
2018-12-05 12:05:37 -05:00
|
|
|
def find_topic_id(topic_title)
|
|
|
|
slug = Slug.for(topic_title, nil)
|
|
|
|
return nil if slug.blank?
|
|
|
|
|
|
|
|
Topic.listable_topics
|
|
|
|
.where(slug: slug)
|
|
|
|
.pluck(:id)
|
|
|
|
end
|
2016-09-20 17:01:54 -04:00
|
|
|
end
|