2013-08-19 07:14:26 -04:00
|
|
|
module FlagQuery
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def self.flagged_posts_report(current_user, filter, offset=0, per_page=25)
|
2013-08-19 07:14:26 -04:00
|
|
|
actions = flagged_post_actions(filter)
|
|
|
|
|
2014-02-06 22:11:52 -05:00
|
|
|
guardian = Guardian.new(current_user)
|
|
|
|
|
|
|
|
if !guardian.is_admin?
|
2014-07-28 13:17:37 -04:00
|
|
|
actions = actions.where('category_id in (?)', guardian.allowed_category_ids)
|
2014-02-06 22:11:52 -05:00
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
post_ids = actions.limit(per_page)
|
|
|
|
.offset(offset)
|
|
|
|
.group(:post_id)
|
|
|
|
.order('min(post_actions.created_at) DESC')
|
|
|
|
.pluck(:post_id)
|
|
|
|
.uniq
|
2013-08-19 07:14:26 -04:00
|
|
|
|
|
|
|
return nil if post_ids.blank?
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
posts = SqlBuilder.new("
|
|
|
|
SELECT p.id,
|
|
|
|
p.cooked,
|
|
|
|
p.user_id,
|
|
|
|
p.topic_id,
|
|
|
|
p.post_number,
|
|
|
|
p.hidden,
|
2014-08-18 12:56:39 -04:00
|
|
|
p.deleted_at,
|
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
|
|
|
|
WHERE p.id in (:post_ids)").map_exec(OpenStruct, post_ids: post_ids)
|
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
|
2013-08-19 07:14:26 -04:00
|
|
|
p.excerpt = Post.excerpt(p.cooked)
|
2014-07-28 13:17:37 -04:00
|
|
|
p.delete_field(:cooked)
|
2013-08-19 07:14:26 -04:00
|
|
|
post_lookup[p.id] = p
|
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
post_actions = actions.order('post_actions.created_at DESC')
|
2014-07-28 16:50:49 -04:00
|
|
|
.includes(related_post: { topic: { ordered_posts: :user }})
|
2014-07-28 13:17:37 -04:00
|
|
|
.where(post_id: post_ids)
|
2013-08-19 07:14:26 -04:00
|
|
|
|
|
|
|
post_actions.each do |pa|
|
|
|
|
post = post_lookup[pa.post_id]
|
|
|
|
post.post_actions ||= []
|
2014-07-28 13:17:37 -04:00
|
|
|
# TODO: add serializer so we can skip this
|
|
|
|
action = {
|
|
|
|
id: pa.id,
|
|
|
|
post_id: pa.post_id,
|
|
|
|
user_id: pa.user_id,
|
|
|
|
post_action_type_id: pa.post_action_type_id,
|
|
|
|
created_at: pa.created_at,
|
|
|
|
disposed_by_id: pa.disposed_by_id,
|
|
|
|
disposed_at: pa.disposed_at,
|
|
|
|
disposition: pa.disposition,
|
|
|
|
related_post_id: pa.related_post_id,
|
|
|
|
targets_topic: pa.targets_topic,
|
|
|
|
staff_took_action: pa.staff_took_action
|
|
|
|
}
|
2013-08-19 07:14:26 -04:00
|
|
|
action[:name_key] = PostActionType.types.key(pa.post_action_type_id)
|
2014-07-28 13:17:37 -04:00
|
|
|
|
|
|
|
if pa.related_post && pa.related_post.topic
|
|
|
|
conversation = {}
|
|
|
|
related_topic = pa.related_post.topic
|
2014-07-28 16:50:49 -04:00
|
|
|
if response = related_topic.ordered_posts[0]
|
2014-07-28 13:17:37 -04:00
|
|
|
conversation[:response] = {
|
|
|
|
excerpt: excerpt(response.cooked),
|
|
|
|
user_id: response.user_id
|
|
|
|
}
|
|
|
|
user_ids << response.user_id
|
2014-07-28 16:50:49 -04:00
|
|
|
if reply = related_topic.ordered_posts[1]
|
2014-07-28 13:17:37 -04:00
|
|
|
conversation[:reply] = {
|
|
|
|
excerpt: excerpt(reply.cooked),
|
|
|
|
user_id: reply.user_id
|
|
|
|
}
|
|
|
|
user_ids << reply.user_id
|
|
|
|
conversation[:has_more] = related_topic.posts_count > 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
action.merge!(permalink: related_topic.relative_url, conversation: conversation)
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
2014-07-28 13:17:37 -04:00
|
|
|
|
2013-08-19 07:14:26 -04:00
|
|
|
post.post_actions << action
|
2014-07-28 13:17:37 -04:00
|
|
|
|
|
|
|
user_ids << pa.user_id
|
|
|
|
user_ids << pa.disposed_by_id if pa.disposed_by_id
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
# maintain order
|
|
|
|
posts = post_ids.map { |id| post_lookup[id] }
|
|
|
|
# TODO: add serializer so we can skip this
|
2013-08-19 07:14:26 -04:00
|
|
|
posts.map!(&:marshal_dump)
|
2014-07-28 13:17:37 -04:00
|
|
|
|
|
|
|
[
|
|
|
|
posts,
|
|
|
|
Topic.with_deleted.where(id: topic_ids.to_a).to_a,
|
|
|
|
User.includes(:user_stat).where(id: user_ids.to_a).to_a
|
|
|
|
]
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def self.flagged_post_actions(filter)
|
|
|
|
post_actions = PostAction.flags
|
|
|
|
.joins("INNER JOIN posts ON posts.id = post_actions.post_id")
|
|
|
|
.joins("INNER JOIN topics ON topics.id = posts.topic_id")
|
2014-08-04 16:51:33 -04:00
|
|
|
.joins("LEFT JOIN users ON users.id = posts.user_id")
|
|
|
|
.where("users.id IS NOT NULL")
|
2014-07-28 13:17:37 -04:00
|
|
|
|
|
|
|
if filter == "old"
|
2014-07-30 17:35:42 -04:00
|
|
|
post_actions.where("post_actions.disagreed_at IS NOT NULL OR
|
2014-08-11 05:56:54 -04:00
|
|
|
post_actions.deferred_at IS NOT NULL OR
|
2014-07-30 17:35:42 -04:00
|
|
|
post_actions.agreed_at IS NOT NULL")
|
2014-07-28 13:17:37 -04:00
|
|
|
else
|
|
|
|
post_actions.active
|
|
|
|
.where("posts.deleted_at" => nil)
|
|
|
|
.where("topics.deleted_at" => nil)
|
|
|
|
end
|
2013-08-19 07:14:26 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
end
|
2013-08-19 07:14:26 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
private
|
2013-08-19 07:14:26 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def self.excerpt(cooked)
|
|
|
|
excerpt = Post.excerpt(cooked, 200)
|
|
|
|
# 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.remove
|
|
|
|
end
|
|
|
|
fragment.to_html.strip
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|
2014-07-28 13:17:37 -04:00
|
|
|
|
2013-08-19 07:14:26 -04:00
|
|
|
end
|