mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 07:11:34 +08:00
FEATURE: flag dispositions normalization
All flags should end up in one of the three dispositions - Agree - Disagree - Defer In the administration area, the *active* flags section displays 4 buttons - Agree (hide post + send PM) - Disagree - Defer - Delete Clicking "Delete" will open a modal that offer to - Delete Post & Defer Flags - Delete Post & Agree with Flags - Delete Spammer (if available) When the flag has a list associated, the list will now display 1 response and 1 reply and a "show more..." link if there are more in the conversation. Replying to the conversation will NOT give a disposition. Moderators must click the buttons that does that. If someone clicks one buttons, this will add a default moderator message from that moderator saying what happened. The *old* flags section now displays the proper dispositions and is super duper fast (no more N+9999 queries). FIX: the old list includes deleted topics FIX: the lists now properly display the topic states (deleted, closed, archived, hidden, PM) FIX: flagging a topic that you've already flagged the first post
This commit is contained in:
@ -1,105 +1,139 @@
|
||||
module FlagQuery
|
||||
def self.flagged_posts_report(current_user, filter, offset = 0, per_page = 25)
|
||||
|
||||
def self.flagged_posts_report(current_user, filter, offset=0, per_page=25)
|
||||
actions = flagged_post_actions(filter)
|
||||
|
||||
guardian = Guardian.new(current_user)
|
||||
|
||||
if !guardian.is_admin?
|
||||
actions = actions.joins(:post => :topic)
|
||||
.where('category_id in (?)', guardian.allowed_category_ids)
|
||||
actions = actions.where('category_id in (?)', guardian.allowed_category_ids)
|
||||
end
|
||||
|
||||
post_ids = actions
|
||||
.limit(per_page)
|
||||
.offset(offset)
|
||||
.group(:post_id)
|
||||
.order('min(post_actions.created_at) DESC')
|
||||
.pluck(:post_id).uniq
|
||||
post_ids = actions.limit(per_page)
|
||||
.offset(offset)
|
||||
.group(:post_id)
|
||||
.order('min(post_actions.created_at) DESC')
|
||||
.pluck(:post_id)
|
||||
.uniq
|
||||
|
||||
return nil if post_ids.blank?
|
||||
|
||||
actions = actions
|
||||
.order('post_actions.created_at DESC')
|
||||
.includes({:related_post => :topic})
|
||||
|
||||
posts = SqlBuilder.new("SELECT p.id, t.title, p.cooked, p.user_id,
|
||||
p.topic_id, p.post_number, p.hidden, t.visible topic_visible,
|
||||
p.deleted_at, t.deleted_at topic_deleted_at
|
||||
FROM posts p
|
||||
JOIN topics t ON t.id = p.topic_id
|
||||
WHERE p.id in (:post_ids)").map_exec(OpenStruct, post_ids: post_ids)
|
||||
posts = SqlBuilder.new("
|
||||
SELECT p.id,
|
||||
p.cooked,
|
||||
p.user_id,
|
||||
p.topic_id,
|
||||
p.post_number,
|
||||
p.hidden,
|
||||
p.deleted_at
|
||||
FROM posts p
|
||||
WHERE p.id in (:post_ids)").map_exec(OpenStruct, post_ids: post_ids)
|
||||
|
||||
post_lookup = {}
|
||||
users = Set.new
|
||||
user_ids = Set.new
|
||||
topic_ids = Set.new
|
||||
|
||||
posts.each do |p|
|
||||
users << p.user_id
|
||||
user_ids << p.user_id
|
||||
topic_ids << p.topic_id
|
||||
p.excerpt = Post.excerpt(p.cooked)
|
||||
p.topic_slug = Slug.for(p.title)
|
||||
p.delete_field(:cooked)
|
||||
post_lookup[p.id] = p
|
||||
end
|
||||
|
||||
# maintain order
|
||||
posts = post_ids.map{|id| post_lookup[id]}
|
||||
|
||||
post_actions = actions.where(:post_id => post_ids)
|
||||
post_actions = actions.order('post_actions.created_at DESC')
|
||||
.includes(related_post: { topic: { posts: :user }})
|
||||
.where(post_id: post_ids)
|
||||
|
||||
post_actions.each do |pa|
|
||||
post = post_lookup[pa.post_id]
|
||||
post.post_actions ||= []
|
||||
action = pa.attributes
|
||||
# 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
|
||||
}
|
||||
action[:name_key] = PostActionType.types.key(pa.post_action_type_id)
|
||||
if (pa.related_post && pa.related_post.topic)
|
||||
action.merge!(topic_id: pa.related_post.topic_id,
|
||||
slug: pa.related_post.topic.slug,
|
||||
permalink: pa.related_post.topic.url)
|
||||
|
||||
if pa.related_post && pa.related_post.topic
|
||||
conversation = {}
|
||||
related_topic = pa.related_post.topic
|
||||
if response = related_topic.posts[0]
|
||||
conversation[:response] = {
|
||||
excerpt: excerpt(response.cooked),
|
||||
user_id: response.user_id
|
||||
}
|
||||
user_ids << response.user_id
|
||||
if reply = related_topic.posts[1]
|
||||
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)
|
||||
end
|
||||
|
||||
post.post_actions << action
|
||||
users << pa.user_id
|
||||
users << pa.deleted_by_id if pa.deleted_by_id
|
||||
|
||||
user_ids << pa.user_id
|
||||
user_ids << pa.disposed_by_id if pa.disposed_by_id
|
||||
end
|
||||
|
||||
# TODO add serializer so we can skip this
|
||||
# maintain order
|
||||
posts = post_ids.map { |id| post_lookup[id] }
|
||||
# TODO: add serializer so we can skip this
|
||||
posts.map!(&:marshal_dump)
|
||||
[posts, User.where(id: users.to_a).to_a]
|
||||
|
||||
[
|
||||
posts,
|
||||
Topic.with_deleted.where(id: topic_ids.to_a).to_a,
|
||||
User.includes(:user_stat).where(id: user_ids.to_a).to_a
|
||||
]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def self.flagged_post_ids(filter, offset, limit)
|
||||
<<SQL
|
||||
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")
|
||||
|
||||
SELECT p.id from posts p
|
||||
JOIN topics t ON t.id = p.topic_id
|
||||
WHERE p.id IN (
|
||||
SELECT post_id from post_actions
|
||||
WHERE
|
||||
)
|
||||
/*offset*/
|
||||
/*limit*/
|
||||
if filter == "old"
|
||||
post_actions.with_deleted
|
||||
.where("post_actions.deleted_at IS NOT NULL OR
|
||||
post_actions.defered_at IS NOT NULL OR
|
||||
post_actions.agreed_at IS NOT NULL")
|
||||
else
|
||||
post_actions.active
|
||||
.where("posts.deleted_at" => nil)
|
||||
.where("topics.deleted_at" => nil)
|
||||
end
|
||||
|
||||
SQL
|
||||
end
|
||||
|
||||
def self.flagged_post_actions(filter)
|
||||
post_actions = PostAction
|
||||
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
||||
.joins(:post => :topic)
|
||||
|
||||
if filter == 'old'
|
||||
post_actions
|
||||
.with_deleted
|
||||
.where('post_actions.deleted_at IS NOT NULL OR
|
||||
defer = true OR
|
||||
topics.deleted_at IS NOT NULL OR
|
||||
posts.deleted_at IS NOT NULL')
|
||||
else
|
||||
post_actions
|
||||
.where('defer IS NULL OR
|
||||
defer = false')
|
||||
.where('posts.deleted_at IS NULL AND
|
||||
topics.deleted_at IS NULL')
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user