2017-09-06 10:21:07 -04:00
|
|
|
require 'ostruct'
|
|
|
|
|
2013-08-19 07:14:26 -04:00
|
|
|
module FlagQuery
|
|
|
|
|
2018-08-09 10:49:14 -04:00
|
|
|
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
|
|
|
|
|
2017-09-08 16:47:49 -04:00
|
|
|
def self.flagged_posts_report(current_user, opts = nil)
|
2019-01-03 12:03:01 -05:00
|
|
|
Discourse.deprecate("FlagQuery is deprecated, use the Reviewable API instead.", since: "2.3.0beta5", drop_from: "2.4")
|
|
|
|
|
2017-09-08 16:47:49 -04:00
|
|
|
opts ||= {}
|
|
|
|
offset = opts[:offset] || 0
|
|
|
|
per_page = opts[:per_page] || 25
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
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)
|
2014-02-06 22:11:52 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
if opts[:filter] == 'old'
|
|
|
|
reviewables = reviewables.where("status <> ?", Reviewable.statuses[:pending])
|
|
|
|
else
|
|
|
|
reviewables = reviewables.pending
|
2014-02-06 22:11:52 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
total_rows = reviewables.count
|
2018-05-07 15:14:18 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
post_ids = reviewables.map(&:target_id).uniq
|
2013-08-19 07:14:26 -04:00
|
|
|
|
2018-06-20 03:48:02 -04:00
|
|
|
posts = DB.query(<<~SQL, post_ids: post_ids)
|
2014-07-28 13:17:37 -04:00
|
|
|
SELECT p.id,
|
2018-06-20 03:48:02 -04:00
|
|
|
p.cooked as excerpt,
|
2018-01-30 16:31:29 -05:00
|
|
|
p.raw,
|
2014-07-28 13:17:37 -04:00
|
|
|
p.user_id,
|
|
|
|
p.topic_id,
|
|
|
|
p.post_number,
|
2018-04-04 12:16:44 -04:00
|
|
|
p.reply_count,
|
2014-07-28 13:17:37 -04:00
|
|
|
p.hidden,
|
2014-08-18 12:56:39 -04:00
|
|
|
p.deleted_at,
|
2014-09-09 14:26:40 -04:00
|
|
|
p.user_deleted,
|
2018-06-20 03:48:02 -04:00
|
|
|
NULL as post_action_ids,
|
2014-09-08 11:53:29 -04:00
|
|
|
(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)::int AS previous_flags_count
|
2014-07-28 13:17:37 -04:00
|
|
|
FROM posts p
|
2018-06-20 03:48:02 -04:00
|
|
|
WHERE p.id in (:post_ids)
|
|
|
|
SQL
|
2013-08-19 07:14:26 -04:00
|
|
|
|
|
|
|
post_lookup = {}
|
2014-07-28 13:17:37 -04:00
|
|
|
user_ids = Set.new
|
|
|
|
topic_ids = Set.new
|
2013-08-19 07:14:26 -04:00
|
|
|
|
|
|
|
posts.each do |p|
|
2014-07-28 13:17:37 -04:00
|
|
|
user_ids << p.user_id
|
|
|
|
topic_ids << p.topic_id
|
2018-06-20 03:48:02 -04:00
|
|
|
p.excerpt = Post.excerpt(p.excerpt)
|
2013-08-19 07:14:26 -04:00
|
|
|
post_lookup[p.id] = p
|
|
|
|
end
|
|
|
|
|
2017-09-11 16:44:20 -04:00
|
|
|
all_post_actions = []
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
2014-07-28 13:17:37 -04:00
|
|
|
}
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
2014-07-28 13:17:37 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
action.merge!(permalink: rs.meta_topic.relative_url, conversation: conversation)
|
|
|
|
end
|
2014-07-28 13:17:37 -04:00
|
|
|
|
2017-09-11 16:44:20 -04:00
|
|
|
post.post_action_ids << action[:id]
|
|
|
|
all_post_actions << action
|
2019-01-03 12:03:01 -05:00
|
|
|
user_ids << action[:user_id]
|
|
|
|
user_ids << rs.reviewed_by_id if rs.reviewed_by_id
|
2017-09-11 16:44:20 -04:00
|
|
|
end
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
|
|
|
|
2018-08-09 10:49:14 -04:00
|
|
|
post_custom_field_names = []
|
|
|
|
plugin_post_custom_fields.each do |field, plugin|
|
|
|
|
post_custom_field_names << field if plugin.enabled?
|
|
|
|
end
|
|
|
|
|
2018-08-21 15:33:15 -04:00
|
|
|
post_custom_fields = Post.custom_fields_for_ids(post_ids, post_custom_field_names)
|
2018-08-09 10:49:14 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
# maintain order
|
|
|
|
posts = post_ids.map { |id| post_lookup[id] }
|
2018-08-09 10:49:14 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
# TODO: add serializer so we can skip this
|
2018-08-09 10:49:14 -04:00
|
|
|
posts.map! do |post|
|
|
|
|
result = post.to_h
|
|
|
|
if cfs = post_custom_fields[post.id]
|
|
|
|
result[:custom_fields] = cfs
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2014-07-28 13:17:37 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
guardian = Guardian.new(current_user)
|
2018-06-13 11:44:13 -04:00
|
|
|
users = User.includes(:user_stat).where(id: user_ids.to_a).to_a
|
|
|
|
User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
[
|
|
|
|
posts,
|
|
|
|
Topic.with_deleted.where(id: topic_ids.to_a).to_a,
|
2018-06-13 11:44:13 -04:00
|
|
|
users,
|
2017-09-12 13:04:53 -04:00
|
|
|
all_post_actions,
|
|
|
|
total_rows
|
2014-07-28 13:17:37 -04:00
|
|
|
]
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
|
|
|
|
2017-09-12 13:04:53 -04:00
|
|
|
def self.flagged_post_actions(opts = nil)
|
2019-01-03 12:03:01 -05:00
|
|
|
Discourse.deprecate("FlagQuery is deprecated, please use the Reviewable API instead.", since: "2.3.0beta5", drop_from: "2.4")
|
2015-02-16 07:03:04 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
opts ||= {}
|
2017-09-08 16:47:49 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
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]
|
2018-01-17 13:03:14 -05:00
|
|
|
|
|
|
|
if opts[:filter] == 'without_custom'
|
2019-01-03 12:03:01 -05:00
|
|
|
return scores.where(reviewable_score_type: PostActionType.flag_types_without_custom.values)
|
2018-01-17 13:03:14 -05:00
|
|
|
end
|
|
|
|
|
2017-09-08 16:47:49 -04:00
|
|
|
if opts[:filter] == "old"
|
2019-01-03 12:03:01 -05:00
|
|
|
scores = scores.where('reviewables.status <> ?', Reviewable.statuses[:pending])
|
2015-02-16 07:03:04 -05:00
|
|
|
else
|
2019-01-03 12:03:01 -05:00
|
|
|
scores = scores.where('reviewables.status' => Reviewable.statuses[:pending])
|
2014-07-28 13:17:37 -04:00
|
|
|
end
|
2013-08-19 07:14:26 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
scores
|
2015-02-16 07:03:04 -05:00
|
|
|
end
|
|
|
|
|
2017-09-06 10:21:07 -04:00
|
|
|
def self.flagged_topics
|
2019-01-03 12:03:01 -05:00
|
|
|
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],
|
2019-05-07 13:25:11 -04:00
|
|
|
min_score: Reviewable.min_score_for_priority
|
2019-01-03 12:03:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
results = DB.query(<<~SQL, params)
|
|
|
|
SELECT rs.reviewable_score_type,
|
|
|
|
p.id AS post_id,
|
|
|
|
r.topic_id,
|
|
|
|
rs.created_at,
|
2018-11-05 14:43:33 -05:00
|
|
|
p.user_id
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
2018-11-05 14:43:33 -05:00
|
|
|
SQL
|
2017-09-06 10:21:07 -04:00
|
|
|
|
|
|
|
ft_by_id = {}
|
2018-11-05 14:43:33 -05:00
|
|
|
user_ids = Set.new
|
2017-09-06 10:21:07 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
results.each do |r|
|
|
|
|
ft = ft_by_id[r.topic_id] ||= OpenStruct.new(
|
|
|
|
topic_id: r.topic_id,
|
2018-11-05 14:43:33 -05:00
|
|
|
flag_counts: {},
|
|
|
|
user_ids: Set.new,
|
2019-01-03 12:03:01 -05:00
|
|
|
last_flag_at: r.created_at,
|
2018-11-05 14:43:33 -05:00
|
|
|
)
|
2017-09-06 10:21:07 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
ft.flag_counts[r.reviewable_score_type] ||= 0
|
|
|
|
ft.flag_counts[r.reviewable_score_type] += 1
|
2017-09-06 10:21:07 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
ft.user_ids << r.user_id
|
|
|
|
user_ids << r.user_id
|
2017-09-06 10:21:07 -04:00
|
|
|
end
|
|
|
|
|
2018-11-05 14:43:33 -05:00
|
|
|
all_topics = Topic.where(id: ft_by_id.keys).to_a
|
|
|
|
all_topics.each { |t| ft_by_id[t.id].topic = t }
|
2018-05-07 15:14:18 -04:00
|
|
|
|
2018-11-05 14:43:33 -05:00
|
|
|
Topic.preload_custom_fields(all_topics, TopicList.preloaded_custom_fields)
|
|
|
|
{
|
2019-01-03 12:03:01 -05:00
|
|
|
flagged_topics: ft_by_id.values,
|
2018-11-05 14:43:33 -05:00
|
|
|
users: User.where(id: user_ids)
|
|
|
|
}
|
2017-09-06 10:21:07 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
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
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
fragment.to_html.strip
|
|
|
|
end
|
2014-07-28 13:17:37 -04:00
|
|
|
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|