forked from pingcap/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_query.rb
234 lines (191 loc) · 7.26 KB
/
flag_query.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
# frozen_string_literal: true
require 'ostruct'
module FlagQuery
def self.plugin_post_custom_fields
@plugin_post_custom_fields ||= {}
end
# Allow plugins to add custom fields to the flag views
def self.register_plugin_post_custom_field(field, plugin)
plugin_post_custom_fields[field] = plugin
end
def self.flagged_posts_report(current_user, opts = nil)
Discourse.deprecate("FlagQuery is deprecated, use the Reviewable API instead.", since: "2.3.0beta5", drop_from: "2.4")
opts ||= {}
offset = opts[:offset] || 0
per_page = opts[:per_page] || 25
reviewables = ReviewableFlaggedPost.default_visible.viewable_by(current_user, order: 'created_at DESC')
reviewables = reviewables.where(topic_id: opts[:topic_id]) if opts[:topic_id]
reviewables = reviewables.where(target_created_by_id: opts[:user_id]) if opts[:user_id]
reviewables = reviewables.limit(per_page).offset(offset)
if opts[:filter] == 'old'
reviewables = reviewables.where("status <> ?", Reviewable.statuses[:pending])
else
reviewables = reviewables.pending
end
total_rows = reviewables.count
post_ids = reviewables.map(&:target_id).uniq
posts = DB.query(<<~SQL, post_ids: post_ids.blank? ? [0] : post_ids)
SELECT p.id,
p.cooked as excerpt,
p.raw,
p.user_id,
p.topic_id,
p.post_number,
p.reply_count,
p.hidden,
p.deleted_at,
p.user_deleted,
NULL as post_action_ids,
(SELECT created_at FROM post_revisions WHERE post_id = p.id AND user_id = p.user_id ORDER BY created_at DESC LIMIT 1) AS last_revised_at,
(SELECT COUNT(*) FROM post_actions WHERE (disagreed_at IS NOT NULL OR agreed_at IS NOT NULL OR deferred_at IS NOT NULL) AND post_id = p.id) AS previous_flags_count
FROM posts p
WHERE p.id in (:post_ids)
SQL
post_lookup = {}
user_ids = Set.new
topic_ids = Set.new
posts.each do |p|
user_ids << p.user_id
topic_ids << p.topic_id
p.excerpt = Post.excerpt(p.excerpt)
post_lookup[p.id] = p
end
all_post_actions = []
reviewables.each do |r|
post = post_lookup[r.target_id]
post.post_action_ids ||= []
r.reviewable_scores.order('created_at desc').each do |rs|
action = {
id: rs.id,
post_id: post.id,
user_id: rs.user_id,
post_action_type_id: rs.reviewable_score_type,
created_at: rs.created_at,
disposed_by_id: rs.reviewed_by_id,
disposed_at: rs.reviewed_at,
disposition: ReviewableScore.statuses[rs.status],
targets_topic: r.payload['targets_topic'],
staff_took_action: rs.took_action?
}
action[:name_key] = PostActionType.types.key(rs.reviewable_score_type)
if rs.meta_topic.present?
meta_posts = rs.meta_topic.ordered_posts
conversation = {}
if response = meta_posts[0]
action[:related_post_id] = response.id
conversation[:response] = {
excerpt: excerpt(response.cooked),
user_id: response.user_id
}
user_ids << response.user_id
if reply = meta_posts[1]
conversation[:reply] = {
excerpt: excerpt(reply.cooked),
user_id: reply.user_id
}
user_ids << reply.user_id
conversation[:has_more] = rs.meta_topic.posts_count > 2
end
end
action.merge!(permalink: rs.meta_topic.relative_url, conversation: conversation)
end
post.post_action_ids << action[:id]
all_post_actions << action
user_ids << action[:user_id]
user_ids << rs.reviewed_by_id if rs.reviewed_by_id
end
end
post_custom_field_names = []
plugin_post_custom_fields.each do |field, plugin|
post_custom_field_names << field if plugin.enabled?
end
post_custom_fields = Post.custom_fields_for_ids(post_ids, post_custom_field_names)
# maintain order
posts = post_ids.map { |id| post_lookup[id] }
# TODO: add serializer so we can skip this
posts.map! do |post|
result = post.to_h
if cfs = post_custom_fields[post.id]
result[:custom_fields] = cfs
end
result
end
guardian = Guardian.new(current_user)
users = User.includes(:user_stat).where(id: user_ids.to_a).to_a
User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))
[
posts,
Topic.with_deleted.where(id: topic_ids.to_a).to_a,
users,
all_post_actions,
total_rows
]
end
def self.flagged_post_actions(opts = nil)
Discourse.deprecate("FlagQuery is deprecated, please use the Reviewable API instead.", since: "2.3.0beta5", drop_from: "2.4")
opts ||= {}
scores = ReviewableScore.includes(:reviewable).where('reviewables.type' => 'ReviewableFlaggedPost')
scores = scores.where('reviewables.topic_id' => opts[:topic_id]) if opts[:topic_id]
scores = scores.where('reviewables.target_created_by_id' => opts[:user_id]) if opts[:user_id]
if opts[:filter] == 'without_custom'
return scores.where(reviewable_score_type: PostActionType.flag_types_without_custom.values)
end
if opts[:filter] == "old"
scores = scores.where('reviewables.status <> ?', Reviewable.statuses[:pending])
else
scores = scores.where('reviewables.status' => Reviewable.statuses[:pending])
end
scores
end
def self.flagged_topics
Discourse.deprecate("FlagQuery has been deprecated. Please use the Reviewable API instead.", since: "2.3.0beta5", drop_from: "2.4")
params = {
pending: Reviewable.statuses[:pending],
min_score: Reviewable.min_score_for_priority
}
results = DB.query(<<~SQL, params)
SELECT rs.reviewable_score_type,
p.id AS post_id,
r.topic_id,
rs.created_at,
p.user_id
FROM reviewables AS r
INNER JOIN reviewable_scores AS rs ON rs.reviewable_id = r.id
INNER JOIN posts AS p ON p.id = r.target_id
WHERE r.type = 'ReviewableFlaggedPost'
AND r.status = :pending
AND r.score >= :min_score
ORDER BY rs.created_at DESC
SQL
ft_by_id = {}
user_ids = Set.new
results.each do |r|
ft = ft_by_id[r.topic_id] ||= OpenStruct.new(
topic_id: r.topic_id,
flag_counts: {},
user_ids: Set.new,
last_flag_at: r.created_at,
)
ft.flag_counts[r.reviewable_score_type] ||= 0
ft.flag_counts[r.reviewable_score_type] += 1
ft.user_ids << r.user_id
user_ids << r.user_id
end
all_topics = Topic.where(id: ft_by_id.keys).to_a
all_topics.each { |t| ft_by_id[t.id].topic = t }
Topic.preload_custom_fields(all_topics, TopicList.preloaded_custom_fields)
{
flagged_topics: ft_by_id.values,
users: User.where(id: user_ids)
}
end
def self.excerpt(cooked)
excerpt = Post.excerpt(cooked, 200, keep_emoji_images: true)
# remove the first link if it's the first node
fragment = Nokogiri::HTML.fragment(excerpt)
if fragment.children.first == fragment.css("a:first").first && fragment.children.first
fragment.children.first.remove
end
fragment.to_html.strip
end
end