Repository navigation
Expand file tree
/
Copy pathplugin.rb
More file actions
356 lines (296 loc) · 12 KB
/
Copy pathplugin.rb
File metadata and controls
356 lines (296 loc) · 12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# frozen_string_literal: true
# name: discourse-custom-webhook
# about: Sends webhook notifications to custom endpoints when new posts are created
# version: 2.0.0
# authors: choo121600
# url: https://github.com/AirflowKorea/discourse-custom-webhook
# required_version: 3.4.0
enabled_site_setting :custom_webhook_enabled
register_asset "stylesheets/custom-webhook-admin.scss"
module ::DiscourseCustomWebhook
PLUGIN_NAME = "discourse-custom-webhook"
end
after_initialize do
# ========== MODELS ==========
class ::DiscourseCustomWebhook::Channel < ActiveRecord::Base
self.table_name = "custom_webhook_channels"
has_many :rules,
class_name: "DiscourseCustomWebhook::Rule",
foreign_key: :custom_webhook_channel_id,
dependent: :destroy
validates :name, presence: true, length: { maximum: 255 }
validates :webhook_url, presence: true
validates :excerpt_length, numericality: { greater_than_or_equal_to: 100, less_than_or_equal_to: 2000 }
scope :enabled, -> { where(enabled: true) }
end
class ::DiscourseCustomWebhook::Rule < ActiveRecord::Base
self.table_name = "custom_webhook_rules"
belongs_to :channel,
class_name: "DiscourseCustomWebhook::Channel",
foreign_key: :custom_webhook_channel_id
enum :filter, { watch: 0, follow: 1, mute: 2 }
validates :filter, presence: true
scope :ordered, -> { order(priority: :desc, created_at: :asc) }
def tags
value = read_attribute(:tags)
return [] if value.blank?
JSON.parse(value) rescue []
end
def tags=(value)
write_attribute(:tags, Array(value).compact_blank.to_json)
end
def matches_post?(post)
return false if mute?
return false unless matches_filter?(post)
return false unless matches_category?(post)
return false unless matches_tags?(post)
true
end
private
def matches_filter?(post)
case filter
when "watch" then true
when "follow" then post.is_first_post?
when "mute" then false
else true
end
end
def matches_category?(post)
return true if category_id.blank?
topic_category_id = post.topic&.category_id
return false if topic_category_id.blank?
category = Category.find_by(id: topic_category_id)
return false unless category
category_id == topic_category_id || category_id == category.parent_category_id
end
def matches_tags?(post)
tag_list = tags
return true if tag_list.blank? || tag_list.empty?
post_tags = post.topic&.tags&.pluck(:name) || []
(tag_list & post_tags).any?
end
end
# ========== SERIALIZERS ==========
class ::DiscourseCustomWebhook::RuleSerializer < ApplicationSerializer
attributes :id, :custom_webhook_channel_id, :filter, :category_id, :category_name, :tags, :priority, :created_at
def category_name
return nil if object.category_id.blank?
Category.find_by(id: object.category_id)&.name
end
end
class ::DiscourseCustomWebhook::ChannelSerializer < ApplicationSerializer
attributes :id, :name, :webhook_url, :webhook_secret, :message_content, :excerpt_length, :enabled, :created_at, :updated_at
has_many :rules, serializer: ::DiscourseCustomWebhook::RuleSerializer, embed: :objects
def webhook_secret
object.webhook_secret.present? ? "••••••••" : nil
end
def rules
object.rules.ordered
end
end
# ========== HELPER METHODS ==========
module ::DiscourseCustomWebhook
def self.ensure_protocol(url)
return nil if url.blank?
url.start_with?("//") ? "https:#{url}" : url
end
def self.formatted_display_name(user)
if user.name.present? && user.name != user.username
"@#{user.username} (#{user.name})"
else
"@#{user.username}"
end
end
def self.build_payload(channel, post)
topic = post.topic
user = post.user
category = topic.category
title_parts = [topic.title]
if category.present?
category_name = category.parent_category.present? ? "#{category.parent_category.name}/#{category.name}" : category.name
title_parts << "[#{category_name}]"
end
tags = topic.tags.pluck(:name)
title_parts << tags.join(", ") if tags.present?
title = title_parts.join(" ")
embed_color = category&.color.present? ? category.color.to_i(16) : 5793266
{
content: channel.message_content.presence || "",
embeds: [{
title: title,
color: embed_color,
description: post.excerpt(channel.excerpt_length || 400, keep_newlines: true, keep_emoji_images: false) || "",
url: post.full_url,
author: {
name: formatted_display_name(user),
url: "#{Discourse.base_url}/u/#{user.username}",
icon_url: ensure_protocol(user.small_avatar_url)
}
}]
}
end
def self.send_to_channel(channel, post)
payload = build_payload(channel, post)
uri = URI.parse(channel.webhook_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.open_timeout = 5
http.read_timeout = 10
request = Net::HTTP::Post.new(uri.request_uri)
request["Content-Type"] = "application/json"
request["X-Webhook-Secret"] = channel.webhook_secret if channel.webhook_secret.present?
request.body = payload.to_json
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
Rails.logger.warn("[CustomWebhook] Channel '#{channel.name}' failed: #{response.code}")
raise "HTTP #{response.code}"
end
response
end
def self.send_notification(post)
return unless SiteSetting.custom_webhook_enabled
return if post.post_type != Post.types[:regular]
return if post.topic.blank? || post.topic.archetype == Archetype.private_message
Channel.enabled.includes(:rules).find_each do |channel|
matching_rule = channel.rules.ordered.find { |rule| rule.matches_post?(post) }
next unless matching_rule || channel.rules.empty?
Thread.new do
send_to_channel(channel, post)
rescue => e
Rails.logger.error("[CustomWebhook] Error: #{e.message}")
end
end
end
end
# ========== CONTROLLERS ==========
class ::DiscourseCustomWebhook::ChannelsController < ::Admin::AdminController
requires_plugin DiscourseCustomWebhook::PLUGIN_NAME
def index
channels = DiscourseCustomWebhook::Channel.includes(:rules).order(:name)
render json: { channels: channels.map { |c| DiscourseCustomWebhook::ChannelSerializer.new(c, root: false) } }
end
def create
channel = DiscourseCustomWebhook::Channel.new(channel_params)
if channel.save
render json: { channel: DiscourseCustomWebhook::ChannelSerializer.new(channel, root: false) }
else
render json: { errors: channel.errors.full_messages }, status: :unprocessable_entity
end
end
def update
channel = DiscourseCustomWebhook::Channel.find(params[:id])
update_params = channel_params
update_params = update_params.except(:webhook_secret) if update_params[:webhook_secret] == "••••••••"
if channel.update(update_params)
render json: { channel: DiscourseCustomWebhook::ChannelSerializer.new(channel, root: false) }
else
render json: { errors: channel.errors.full_messages }, status: :unprocessable_entity
end
end
def destroy
DiscourseCustomWebhook::Channel.find(params[:id]).destroy!
render json: success_json
end
def test
channel = DiscourseCustomWebhook::Channel.find(params[:id])
# Find post from topic_id or use latest
post = nil
if params[:topic_id].present?
topic = Topic.find_by(id: params[:topic_id])
if topic.nil?
render json: { error: I18n.t("custom_webhook.test.topic_not_found") }, status: :unprocessable_entity
return
end
post = topic.posts.where(post_type: Post.types[:regular]).order(:post_number).first
else
post = Post.where(post_type: Post.types[:regular])
.joins(:topic)
.where.not(topics: { archetype: Archetype.private_message })
.order("posts.created_at DESC")
.first
end
if post.nil?
render json: { error: I18n.t("custom_webhook.test.no_posts") }, status: :unprocessable_entity
return
end
begin
DiscourseCustomWebhook.send_to_channel(channel, post)
render json: { success: true, topic_title: post.topic.title }
rescue => e
render json: { error: e.message }, status: :unprocessable_entity
end
end
private
def channel_params
params.require(:channel).permit(:name, :webhook_url, :webhook_secret, :message_content, :excerpt_length, :enabled)
end
end
class ::DiscourseCustomWebhook::RulesController < ::Admin::AdminController
requires_plugin DiscourseCustomWebhook::PLUGIN_NAME
def create
channel = DiscourseCustomWebhook::Channel.find(params[:channel_id])
rule = channel.rules.new(rule_params)
if rule.save
render json: { rule: DiscourseCustomWebhook::RuleSerializer.new(rule, root: false) }
else
render json: { errors: rule.errors.full_messages }, status: :unprocessable_entity
end
end
def update
channel = DiscourseCustomWebhook::Channel.find(params[:channel_id])
rule = channel.rules.find(params[:id])
if rule.update(rule_params)
render json: { rule: DiscourseCustomWebhook::RuleSerializer.new(rule, root: false) }
else
render json: { errors: rule.errors.full_messages }, status: :unprocessable_entity
end
end
def destroy
channel = DiscourseCustomWebhook::Channel.find(params[:channel_id])
channel.rules.find(params[:id]).destroy!
render json: success_json
end
private
def rule_params
permitted = params.require(:rule).permit(:filter, :category_id, :priority, tags: [])
permitted[:tags] = Array(permitted[:tags]).compact_blank
permitted
end
end
# ========== ROUTES ==========
Discourse::Application.routes.append do
scope "/admin/plugins/custom-webhook", constraints: StaffConstraint.new do
get "/channels" => "discourse_custom_webhook/channels#index", defaults: { format: :json }
post "/channels" => "discourse_custom_webhook/channels#create", defaults: { format: :json }
put "/channels/:id" => "discourse_custom_webhook/channels#update", defaults: { format: :json }
delete "/channels/:id" => "discourse_custom_webhook/channels#destroy", defaults: { format: :json }
post "/channels/:id/test" => "discourse_custom_webhook/channels#test", defaults: { format: :json }
post "/channels/:channel_id/rules" => "discourse_custom_webhook/rules#create", defaults: { format: :json }
put "/channels/:channel_id/rules/:id" => "discourse_custom_webhook/rules#update", defaults: { format: :json }
delete "/channels/:channel_id/rules/:id" => "discourse_custom_webhook/rules#destroy", defaults: { format: :json }
end
get "/admin/plugins/custom-webhook" => "admin/plugins#index", constraints: StaffConstraint.new
get "/admin/plugins/custom-webhook/*path" => "admin/plugins#index", constraints: StaffConstraint.new
end
add_admin_route "custom_webhook.title", "custom-webhook"
# ========== EVENT HANDLER ==========
on(:post_created) do |post, opts, user|
delay = SiteSetting.custom_webhook_delay_seconds.to_i
if delay > 0
Jobs.enqueue_in(delay.seconds, :custom_webhook_notify, post_id: post.id)
else
DiscourseCustomWebhook.send_notification(post)
end
end
# ========== BACKGROUND JOB ==========
module ::Jobs
class CustomWebhookNotify < ::Jobs::Base
def execute(args)
return unless args[:post_id]
post = Post.find_by(id: args[:post_id])
return unless post
DiscourseCustomWebhook.send_notification(post)
end
end
end
end