forked from worldismine/PM-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
executable file
·98 lines (78 loc) · 3.87 KB
/
plugin.rb
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
# name: discourse-pm-scanner
# authors: Muhlis Budi Cahyono ([email protected]) and [email protected]
# version: 3.0.1
# url: https://github.com/worldismine/PM-Scanner
enabled_site_setting :pm_scanner_enabled
after_initialize {
register_svg_icon("exclamation")
add_model_callback(::ChatMessage, :after_save) {
begin
return unless SiteSetting.pm_scanner_enabled
return unless self.chat_channel.chatable_type == "DirectMessage"
admin_ids = Group[:admins].users.pluck(:id)
return unless (ChatChannelMembershipsQuery.call(self.chat_channel).pluck(:user_id) & admin_ids).count
keywords = SiteSetting.pm_scanner_keywords.to_s.split(",").map{ |k| Regexp.escape(k.strip) }
regexp = Regexp.new(keywords.join("|"), Regexp::IGNORECASE)
match_data = self.message.match(regexp) # nil or MatchData
creator = self.user
return unless match_data && creator && !creator.admin
messages = self.chat_channel.chat_messages.where("id <= #{self.id}").order(created_at: :desc).limit(10)
body = "[Open chat](/chat/channel/#{self.chat_channel.id}/open)\n\n"
body += "|User|Date/Time|Message|\n|---|---|---|\n"
messages.reverse_each do |msg|
body += "|#{msg.user.username}|[date=#{msg.created_at.strftime('%Y-%m-%d')} time=#{msg.created_at.strftime('%H:%M:%S')} timezone=Etc/UTC]|#{msg.message.gsub("\n", " ").truncate(50)}|\n"
end
title = "#{match_data.to_s} found in direct chat message sent by #{self.user.username}"
create_args = {
archetype: Archetype.private_message,
title: "PM Scanner: " + title.truncate(SiteSetting.max_topic_title_length, separator: /\s/),
raw: ERB::Util.html_escape(body),
target_group_names: [Group[:admins].name]
}
post = PostCreator.create!(Discourse.system_user, create_args)
return unless post
admin_ids.each do |adm_id| # notify ONLY human admins
notif_payload = {topic_id: post.topic.id, user_id: adm_id, post_number: post.post_number, notification_type: Notification.types[:custom]}
if Notification.where(notif_payload).first.blank?
Notification.create(notif_payload.merge(data: {message: "pm_scanner.notification.found", display_username: match_data.to_s, topic_title: title}.to_json))
end
end
rescue
end
}
add_model_callback(:post, :after_save) {
if SiteSetting.pm_scanner_enabled
keywords = SiteSetting.pm_scanner_keywords.to_s.split(",").map{ |k| Regexp.escape(k.strip) }
if !keywords.blank?
post_topic = self.topic
if post_topic.private_message?
regexp = Regexp.new(keywords.join("|"), Regexp::IGNORECASE)
match_data = self.raw.match(regexp) # nil or MatchData
creator = self.user
if match_data && creator && !creator.admin
admin_ids = User.where("id > ?", 0).where(admin: true).pluck(:id)
user_ids = post_topic.topic_allowed_users.pluck(:user_id)
if (admin_ids & user_ids).empty? # if admins are not in the conversation
admin_ids.each do |adm_id| # notify ONLY human admins
notif_payload = {topic_id: post_topic.id, user_id: adm_id, post_number: self.post_number, notification_type: Notification.types[:custom]}
if Notification.where(notif_payload).first.blank?
Notification.create(notif_payload.merge(data: {message: "pm_scanner.notification.found", display_username: match_data.to_s, topic_title: post_topic.title}.to_json))
end
end
end
end
end
end
end
}
# admins need to be able to see the direct message chats
module ::PMScannerDirectMessage
def user_can_access?(user)
return true if SiteSetting.pm_scanner_enabled && user.admin?
super(user)
end
end
class ::DirectMessage
prepend PMScannerDirectMessage
end
}