forked from erickguan/discourse-reply-required
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
executable file
·78 lines (65 loc) · 1.98 KB
/
plugin.rb
File metadata and controls
executable file
·78 lines (65 loc) · 1.98 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# name: Discourse Reply Required
# about: Required reply before sees the attachment
# version: 0.12
# authors: Erick Guan (fantasticfears@gmail.com)
enabled_site_setting :discourse_reply_required_enabled
register_asset "stylesheets/discourse-reply-required.css"
REPLY_REQUIRED_REGEXP = /\[回复可见\]((?:(?!\[回复可见\]|\[\/回复可见\])[\S\s])*)\[\/回复可见\]/i
REPLY_REQUIRED_TOPIC_CUSTOM_FILED_NAME = 'reply-required'
def set_topic_custom_field_reply_required(topic_id, flag)
tf = TopicCustomField.find_or_initialize_by(topic_id: topic_id, name: 'reply-required')
tf.value = flag
tf.save!
end
module ::TopicViewExtension
def is_replied
if @user
Post.where(topic_id: @topic.id, user_id: @user.id)
.limit(1)
.count
else
false
end
end
end
module ::TopicViewSerializerExtension
def details
result = super
result[:reply_required] = false
cf = TopicCustomField.find_by(topic_id: object.topic.id,
name: REPLY_REQUIRED_TOPIC_CUSTOM_FILED_NAME)
if cf && (cf.value == "t" || cf.value == "true") # true
result[:reply_required] = true
result[:is_replied] = object.is_replied
end
result
end
end
after_initialize do
# When create
DiscourseEvent.on(:topic_created) do |topic, opts, user|
# Existing block
if REPLY_REQUIRED_REGEXP.match opts[:raw]
set_topic_custom_field_reply_required(topic.id, true)
else
set_topic_custom_field_reply_required(topic.id, false)
end
end
# When first post edits
DiscourseEvent.on(:validate_post) do |post|
# Existing block
if post.is_first_post?
if REPLY_REQUIRED_REGEXP.match(post.raw)
set_topic_custom_field_reply_required(post.topic.id, true)
else
set_topic_custom_field_reply_required(post.topic.id, false)
end
end
end
TopicView.class_eval do
prepend ::TopicViewExtension
end
TopicViewSerializer.class_eval do
prepend ::TopicViewSerializerExtension
end
end