2020-04-09 00:52:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PublishedPage < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
validates_presence_of :slug
|
|
|
|
validates_uniqueness_of :slug, :topic_id
|
|
|
|
|
|
|
|
validate :slug_format
|
|
|
|
def slug_format
|
|
|
|
if slug !~ /^[a-zA-Z\-\_0-9]+$/
|
|
|
|
errors.add(:slug, I18n.t("publish_page.slug_errors.invalid"))
|
|
|
|
elsif %w[check-slug by-topic].include?(slug)
|
|
|
|
errors.add(:slug, I18n.t("publish_page.slug_errors.unavailable"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
"/pub/#{slug}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
"#{Discourse.base_url}#{path}"
|
|
|
|
end
|
|
|
|
|
2020-06-17 18:42:20 +08:00
|
|
|
def self.publish!(publisher, topic, slug, options = {})
|
2020-04-21 09:50:20 +08:00
|
|
|
pp = nil
|
|
|
|
|
2021-04-21 17:36:32 +08:00
|
|
|
results =
|
|
|
|
transaction do
|
2020-04-09 00:52:36 +08:00
|
|
|
pp = find_or_initialize_by(topic: topic)
|
|
|
|
pp.slug = slug.strip
|
2020-06-17 18:42:20 +08:00
|
|
|
pp.public = options[:public] || false
|
2020-04-09 00:52:36 +08:00
|
|
|
|
|
|
|
if pp.save
|
|
|
|
StaffActionLogger.new(publisher).log_published_page(topic.id, slug)
|
2021-04-21 17:36:32 +08:00
|
|
|
[true, pp]
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
|
2021-04-21 17:36:32 +08:00
|
|
|
results || [false, pp]
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.unpublish!(publisher, topic)
|
|
|
|
if pp = PublishedPage.find_by(topic_id: topic.id)
|
|
|
|
pp.destroy!
|
|
|
|
StaffActionLogger.new(publisher).log_unpublished_page(topic.id, pp.slug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 18:29:39 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: published_pages
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# topic_id :bigint not null
|
|
|
|
# slug :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2020-06-17 18:42:20 +08:00
|
|
|
# public :boolean default(FALSE), not null
|
2020-04-28 18:29:39 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_published_pages_on_slug (slug) UNIQUE
|
|
|
|
# index_published_pages_on_topic_id (topic_id) UNIQUE
|
|
|
|
#
|