2020-04-08 12:52:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PublishedPagesController < ApplicationController
|
|
|
|
skip_before_action :preload_json
|
|
|
|
skip_before_action :check_xhr, :verify_authenticity_token, only: [:show]
|
|
|
|
before_action :ensure_publish_enabled
|
2020-06-17 06:42:20 -04:00
|
|
|
before_action :redirect_to_login_if_required, except: [:show]
|
2020-04-08 12:52:36 -04:00
|
|
|
|
|
|
|
def show
|
|
|
|
params.require(:slug)
|
|
|
|
|
|
|
|
pp = PublishedPage.find_by(slug: params[:slug])
|
|
|
|
raise Discourse::NotFound unless pp
|
|
|
|
|
2020-06-17 06:42:20 -04:00
|
|
|
return if enforce_login_required!
|
|
|
|
|
|
|
|
if !pp.public
|
|
|
|
begin
|
|
|
|
guardian.ensure_can_see!(pp.topic)
|
|
|
|
rescue Discourse::InvalidAccess => e
|
|
|
|
return(
|
|
|
|
rescue_discourse_actions(
|
|
|
|
:invalid_access,
|
|
|
|
403,
|
|
|
|
include_ember: false,
|
|
|
|
custom_message: e.custom_message,
|
|
|
|
group: e.group,
|
2023-01-09 07:20:10 -05:00
|
|
|
)
|
2020-06-17 06:42:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
@topic = pp.topic
|
|
|
|
@canonical_url = @topic.url
|
2020-09-20 23:50:39 -04:00
|
|
|
@logo = SiteSetting.logo_small || SiteSetting.logo
|
2020-07-30 11:25:28 -04:00
|
|
|
@site_url = Discourse.base_url
|
|
|
|
@border_color = "#" + ColorScheme.base_colors["tertiary"]
|
2020-04-28 12:24:24 -04:00
|
|
|
|
2022-08-18 12:11:19 -04:00
|
|
|
TopicViewItem.add(pp.topic.id, request.remote_ip, current_user ? current_user.id : nil)
|
|
|
|
|
2020-04-28 12:24:24 -04:00
|
|
|
@body_classes =
|
|
|
|
Set.new(
|
|
|
|
[
|
|
|
|
"published-page",
|
|
|
|
params[:slug],
|
|
|
|
"topic-#{@topic.id}",
|
|
|
|
@topic.tags.pluck(:name),
|
|
|
|
].flatten.compact,
|
|
|
|
)
|
|
|
|
|
|
|
|
@body_classes << @topic.category.slug if @topic.category
|
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
render layout: "publish"
|
|
|
|
end
|
|
|
|
|
|
|
|
def details
|
|
|
|
pp = PublishedPage.find_by(topic: fetch_topic)
|
|
|
|
raise Discourse::NotFound if pp.blank?
|
|
|
|
render_serialized(pp, PublishedPageSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def upsert
|
2020-06-17 06:42:20 -04:00
|
|
|
pp_params = params.require(:published_page)
|
|
|
|
|
|
|
|
result, pp =
|
|
|
|
PublishedPage.publish!(
|
|
|
|
current_user,
|
|
|
|
fetch_topic,
|
|
|
|
pp_params[:slug].strip,
|
|
|
|
pp_params.permit(:public),
|
|
|
|
)
|
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
json_result(pp, serializer: PublishedPageSerializer) { result }
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
PublishedPage.unpublish!(current_user, fetch_topic)
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_slug
|
|
|
|
pp = PublishedPage.new(topic: Topic.new, slug: params[:slug].strip)
|
|
|
|
|
|
|
|
if pp.valid?
|
|
|
|
render json: { valid_slug: true }
|
|
|
|
else
|
|
|
|
render json: { valid_slug: false, reason: pp.errors.full_messages.first }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_topic
|
|
|
|
topic = Topic.find_by(id: params[:topic_id])
|
|
|
|
guardian.ensure_can_publish_page!(topic)
|
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_publish_enabled
|
2022-09-28 19:24:33 -04:00
|
|
|
raise Discourse::NotFound if !SiteSetting.enable_page_publishing? || SiteSetting.secure_uploads
|
2020-04-08 12:52:36 -04:00
|
|
|
end
|
|
|
|
|
2020-06-17 06:42:20 -04:00
|
|
|
def enforce_login_required!
|
|
|
|
if SiteSetting.login_required? && !current_user &&
|
|
|
|
!SiteSetting.show_published_pages_login_required? && redirect_to_login
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 12:52:36 -04:00
|
|
|
end
|