forked from discourse/discourse-user-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
343 lines (284 loc) · 8.99 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
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
# frozen_string_literal: true
# name: discourse-user-notes
# about: Gives the ability for staff members to attach notes to users
# version: 0.0.2
# authors: Robin Ward
# url: https://github.com/discourse/discourse-user-notes
enabled_site_setting :user_notes_enabled
register_asset 'stylesheets/user_notes.scss'
register_svg_icon "sticky-note" if respond_to?(:register_svg_icon)
COUNT_FIELD = "user_notes_count"
after_initialize do
require_dependency 'user'
module ::DiscourseUserNotes
class Engine < ::Rails::Engine
engine_name "discourse_user_notes"
isolate_namespace DiscourseUserNotes
end
def self.key_for(user_id)
"notes:#{user_id}"
end
def self.notes_for(user_id)
PluginStore.get('user_notes', key_for(user_id)) || []
end
def self.add_note(user, raw, created_by, opts = nil)
opts ||= {}
notes = notes_for(user.id)
record = {
id: SecureRandom.hex(16),
user_id: user.id,
raw: raw,
created_by: created_by,
created_at: Time.now
}.merge(opts)
notes << record
::PluginStore.set("user_notes", key_for(user.id), notes)
user.custom_fields[COUNT_FIELD] = notes.size
user.save_custom_fields
record
end
def self.remove_note(user, note_id)
notes = notes_for(user.id)
notes.reject! { |n| n[:id] == note_id }
if notes.size > 0
::PluginStore.set("user_notes", key_for(user.id), notes)
else
::PluginStore.remove("user_notes", key_for(user.id))
end
user.custom_fields[COUNT_FIELD] = notes.size
user.save_custom_fields
end
end
require_dependency 'application_serializer'
class ::UserNoteSerializer < ApplicationSerializer
attributes(
:id,
:user_id,
:raw,
:created_by,
:created_at,
:can_delete,
:post_id,
:post_url,
:post_title
)
def id
object[:id]
end
def user_id
object[:user_id]
end
def raw
object[:raw]
end
def created_by
BasicUserSerializer.new(object[:created_by], scope: scope, root: false)
end
def created_at
object[:created_at]
end
def can_delete
scope.can_delete_user_notes?
end
def post_id
object[:post_id]
end
def post_url
url = object[:post].try(:url)
# In case the topic is deleted
if url == "/404"
url = "/t/#{object[:post].topic_id}/#{object[:post].post_number}"
end
"#{Discourse.base_path}#{url}"
end
def post_title
object[:post].try(:title)
end
def topic_id
object[:topic_id]
end
end
require_dependency 'application_controller'
class DiscourseUserNotes::UserNotesController < ::ApplicationController
before_action :ensure_logged_in
before_action :ensure_staff
def index
user = User.where(id: params[:user_id]).first
raise Discourse::NotFound if user.blank?
notes = ::DiscourseUserNotes.notes_for(params[:user_id])
render json: {
extras: { username: user.username },
user_notes: create_json(notes.reverse)
}
end
def create
user = User.where(id: params[:user_note][:user_id]).first
raise Discourse::NotFound if user.blank?
extras = {}
if post_id = params[:user_note][:post_id]
extras[:post_id] = post_id
end
user_note = ::DiscourseUserNotes.add_note(
user,
params[:user_note][:raw],
current_user.id,
extras
)
render json: create_json(user_note)
end
def destroy
user = User.where(id: params[:user_id]).first
raise Discourse::NotFound if user.blank?
raise Discourse::InvalidAccess.new unless guardian.can_delete_user_notes?
::DiscourseUserNotes.remove_note(user, params[:id])
render json: success_json
end
protected
def create_json(obj)
# Avoid n+1
if obj.is_a?(Array)
users_by_id = {}
posts_by_id = {}
User.where(id: obj.map { |o| o[:created_by] }).each do |u|
users_by_id[u.id] = u
end
Post.with_deleted.where(id: obj.map { |o| o[:post_id] }).each do |p|
posts_by_id[p.id] = p
end
obj.each do |o|
o[:created_by] = users_by_id[o[:created_by].to_i]
o[:post] = posts_by_id[o[:post_id].to_i]
end
else
obj[:created_by] = User.where(id: obj[:created_by]).first
obj[:post] = Post.with_deleted.where(id: obj[:post_id]).first
end
serialize_data(obj, ::UserNoteSerializer)
end
end
# TODO Drop after Discourse 2.6.0 release
if respond_to?(:allow_staff_user_custom_field)
allow_staff_user_custom_field(COUNT_FIELD)
else
whitelist_staff_user_custom_field(COUNT_FIELD)
end
add_to_class(Guardian, :can_delete_user_notes?) do
(SiteSetting.user_notes_moderators_delete? && user.staff?) || user.admin?
end
add_to_serializer(:admin_detailed_user, :user_notes_count, false) do
object.custom_fields && object.custom_fields['user_notes_count'].to_i
end
DiscourseUserNotes::Engine.routes.draw do
get '/' => 'user_notes#index'
post '/' => 'user_notes#create'
delete '/:id' => 'user_notes#destroy'
end
Discourse::Application.routes.append do
mount ::DiscourseUserNotes::Engine, at: "/user_notes"
end
add_model_callback(UserWarning, :after_commit, on: :create) do
user = User.find_by_id(self.user_id)
created_by_user = User.find_by_id(self.created_by_id)
warning_topic = Topic.find_by_id(self.topic_id)
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
end
::DiscourseUserNotes.add_note(
user,
raw_note,
Discourse::SYSTEM_USER_ID,
topic_id: self.topic_id
)
end
add_model_callback(UserHistory, :after_commit, on: :create) do
return unless self.action == UserHistory.actions[:suspend_user]
target_user = User.find_by_id(self.target_user_id)
created_by_user = User.find_by_id(self.acting_user_id)
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t("user_notes.user_suspended",
username: created_by_user.username,
suspended_till: I18n.l(target_user.suspended_till, format: :date_only),
reason: self.details
)
end
::DiscourseUserNotes.add_note(
target_user,
raw_note,
Discourse::SYSTEM_USER_ID,
post_id: self.post_id,
topic_id: self.topic_id
)
end
on(:user_silenced) do |details|
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t(
"user_notes.user_silenced",
username: details[:silenced_by]&.username || '',
silenced_till: I18n.l(details[:silenced_till], format: :date_only),
reason: details[:reason]
)
end
note_args = {}
if post = Post.with_deleted.where(id: details[:post_id]).first
note_args = { post_id: post.id, topic_id: post.topic_id }
end
::DiscourseUserNotes.add_note(
details[:user],
raw_note,
Discourse::SYSTEM_USER_ID,
note_args
)
end
if respond_to? :add_report
add_report('user_notes') do |report|
report.modes = [:table]
report.data = []
report.labels = [
{
type: :user,
properties: {
username: :username,
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.user_notes.labels.user")
},
{
type: :user,
properties: {
username: :moderator_username,
id: :moderator_id,
avatar: :moderator_avatar_template,
},
title: I18n.t("reports.user_notes.labels.moderator")
},
{ type: :text, property: :note, title: I18n.t("reports.user_notes.labels.note") }
]
values = []
values = PluginStoreRow.where(plugin_name: 'user_notes')
.where("value::json->0->>'created_at'>=?", report.start_date)
.where("value::json->0->>'created_at'<=?", report.end_date)
.pluck(:value)
values.each do |value|
notes = JSON.parse(value)
notes.each do |note|
data = {}
created_at = Time.parse(note['created_at'])
user = User.find_by(id: note['user_id'])
moderator = User.find_by(id: note['created_by'])
if user && moderator
data[:created_at] = created_at
data[:user_id] = user.id
data[:username] = user.username_lower
data[:user_avatar_template] = User.avatar_template(user.username_lower, user.uploaded_avatar_id)
data[:moderator_id] = moderator.id
data[:moderator_username] = moderator.username_lower
data[:moderator_avatar_template] = User.avatar_template(moderator.username_lower, moderator.uploaded_avatar_id)
data[:note] = note['raw']
report.data << data
end
end
end
end
end
end