mirror of
https://github.com/discourse/discourse.git
synced 2025-03-06 11:19:51 +00:00
FEATURE: Ability for plugins to whitelist custom fields for flags
You can now call `whitelist_flag_post_custom_field` from your plugins and those custom fields will be available on the flagged posts area of the admin section.
This commit is contained in:
parent
cc90ed3870
commit
5895507153
@ -2,6 +2,15 @@ require 'ostruct'
|
|||||||
|
|
||||||
module FlagQuery
|
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)
|
def self.flagged_posts_report(current_user, opts = nil)
|
||||||
opts ||= {}
|
opts ||= {}
|
||||||
offset = opts[:offset] || 0
|
offset = opts[:offset] || 0
|
||||||
@ -126,10 +135,30 @@ module FlagQuery
|
|||||||
user_ids << pa.disposed_by_id if pa.disposed_by_id
|
user_ids << pa.disposed_by_id if pa.disposed_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 = {}
|
||||||
|
if post_custom_field_names.present?
|
||||||
|
PostCustomField.where(post_id: post_ids, name: post_custom_field_names).each do |f|
|
||||||
|
post_custom_fields[f.post_id] ||= {}
|
||||||
|
post_custom_fields[f.post_id][f.name] = f.value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# maintain order
|
# maintain order
|
||||||
posts = post_ids.map { |id| post_lookup[id] }
|
posts = post_ids.map { |id| post_lookup[id] }
|
||||||
|
|
||||||
# TODO: add serializer so we can skip this
|
# TODO: add serializer so we can skip this
|
||||||
posts.map!(&:to_h)
|
posts.map! do |post|
|
||||||
|
result = post.to_h
|
||||||
|
if cfs = post_custom_fields[post.id]
|
||||||
|
result[:custom_fields] = cfs
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
users = User.includes(:user_stat).where(id: user_ids.to_a).to_a
|
users = User.includes(:user_stat).where(id: user_ids.to_a).to_a
|
||||||
User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))
|
User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))
|
||||||
|
@ -109,6 +109,12 @@ class Plugin::Instance
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def whitelist_flag_post_custom_field(field)
|
||||||
|
reloadable_patch do |plugin|
|
||||||
|
::FlagQuery.register_plugin_post_custom_field(field, plugin) # plugin.enabled? is checked at runtime
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def whitelist_staff_user_custom_field(field)
|
def whitelist_staff_user_custom_field(field)
|
||||||
reloadable_patch do |plugin|
|
reloadable_patch do |plugin|
|
||||||
::User.register_plugin_staff_custom_field(field, plugin) # plugin.enabled? is checked at runtime
|
::User.register_plugin_staff_custom_field(field, plugin) # plugin.enabled? is checked at runtime
|
||||||
|
Loading…
x
Reference in New Issue
Block a user