-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathplugin.rb
More file actions
61 lines (52 loc) · 2.2 KB
/
plugin.rb
File metadata and controls
61 lines (52 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
# name: discourse-category-lockdown
# about: Set all topics in a category to redirect, unless part of a specified group
# version: 1.2.1
# authors: Pavilion
# meta_topic_id: 70649
# url: https://github.com/paviliondev/discourse-category-lockdown
enabled_site_setting :category_lockdown_enabled
register_asset "stylesheets/lockdown.scss"
module ::CategoryLockdown
PLUGIN_NAME = "category-lockdown"
end
require_relative "lib/category_lockdown/engine"
after_initialize do
register_html_builder("server:before-head-close-crawler") do |controller|
::CategoryLockdown::CrawlerHtmlBuilder.perform(controller)
end
rescue_from(::CategoryLockdown::NoAccessLocked) do
opts = { include_ember: true }
topic_id = params["topic_id"] || params["id"]
topic_id ||= params["topic"] # if coming from discourse-docs plugin
topic = Topic.find(topic_id.to_i) if topic_id
response = { error: "Payment Required" }
response[:redirect_url] = topic.category.custom_fields["redirect_url"] if topic
response[:redirect_url] ||= SiteSetting.category_lockdown_redirect_url
if request.format.json?
render_json_dump(response, status: 402)
else
redirect_to path(response[:redirect_url]), allow_other_host: true
end
end
::TopicsController.prepend ::CategoryLockdown::TopicsControllerExtension
::TopicView.prepend ::CategoryLockdown::TopicViewExtension
::Guardian.prepend ::CategoryLockdown::PostGuardianExtension
register_category_custom_field_type("redirect_url", :string)
Site.preloaded_category_custom_fields << "redirect_url"
if defined?(register_category_list_preloaded_category_custom_fields)
register_category_list_preloaded_category_custom_fields("redirect_url")
end
::TopicList.preloaded_custom_fields << "lockdown_enabled"
::TopicList.preloaded_custom_fields << "lockdown_allowed_groups"
add_to_serializer(
:basic_category,
:redirect_url,
include_condition: -> { SiteSetting.category_lockdown_enabled },
) { object.custom_fields["redirect_url"] }
add_to_serializer(
:topic_list_item,
:is_locked_down,
include_condition: -> { SiteSetting.category_lockdown_enabled },
) { ::CategoryLockdown.is_locked(scope, object) }
end