FEATURE: View flags grouped by topic

This commit is contained in:
Robin Ward
2017-09-06 10:21:07 -04:00
parent bbbd974487
commit 40eba8cd93
27 changed files with 347 additions and 79 deletions

View File

@ -1,3 +1,5 @@
require 'ostruct'
module FlagQuery
def self.flagged_posts_report(current_user, filter, offset = 0, per_page = 25)
@ -128,6 +130,44 @@ module FlagQuery
end
def self.flagged_topics
results = PostAction
.flags
.active
.includes(post: [:user, :topic])
.order('post_actions.created_at DESC')
ft_by_id = {}
users_by_id = {}
topics_by_id = {}
results.each do |pa|
if pa.post.present? && pa.post.topic.present?
ft = ft_by_id[pa.post.topic.id] ||= OpenStruct.new(
topic: pa.post.topic,
flag_counts: {},
user_ids: [],
last_flag_at: pa.created_at
)
topics_by_id[pa.post.topic.id] = pa.post.topic
ft.flag_counts[pa.post_action_type_id] ||= 0
ft.flag_counts[pa.post_action_type_id] += 1
ft.user_ids << pa.post.user_id
ft.user_ids.uniq!
users_by_id[pa.post.user_id] ||= pa.post.user
end
end
Topic.preload_custom_fields(topics_by_id.values, TopicList.preloaded_custom_fields)
{ flagged_topics: ft_by_id.values, users: users_by_id.values }
end
private
def self.excerpt(cooked)