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:
Robin Ward 2018-08-09 10:49:14 -04:00
parent cc90ed3870
commit 5895507153
2 changed files with 36 additions and 1 deletions

View File

@ -2,6 +2,15 @@ require 'ostruct'
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)
opts ||= {}
offset = opts[:offset] || 0
@ -126,10 +135,30 @@ module FlagQuery
user_ids << pa.disposed_by_id if pa.disposed_by_id
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
posts = post_ids.map { |id| post_lookup[id] }
# 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
User.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))

View File

@ -109,6 +109,12 @@ class Plugin::Instance
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)
reloadable_patch do |plugin|
::User.register_plugin_staff_custom_field(field, plugin) # plugin.enabled? is checked at runtime