You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. class Admin::ActionLogFilter
  3. KEYS = %i(
  4. action_type
  5. account_id
  6. target_account_id
  7. ).freeze
  8. ACTION_TYPE_MAP = {
  9. assigned_to_self_report: { target_type: 'Report', action: 'assigned_to_self' }.freeze,
  10. change_email_user: { target_type: 'User', action: 'change_email' }.freeze,
  11. confirm_user: { target_type: 'User', action: 'confirm' }.freeze,
  12. create_account_warning: { target_type: 'AccountWarning', action: 'create' }.freeze,
  13. create_announcement: { target_type: 'Announcement', action: 'create' }.freeze,
  14. create_custom_emoji: { target_type: 'CustomEmoji', action: 'create' }.freeze,
  15. create_domain_allow: { target_type: 'DomainAllow', action: 'create' }.freeze,
  16. create_domain_block: { target_type: 'DomainBlock', action: 'create' }.freeze,
  17. create_email_domain_block: { target_type: 'EmailDomainBlock', action: 'create' }.freeze,
  18. demote_user: { target_type: 'User', action: 'demote' }.freeze,
  19. destroy_announcement: { target_type: 'Announcement', action: 'destroy' }.freeze,
  20. destroy_custom_emoji: { target_type: 'CustomEmoji', action: 'destroy' }.freeze,
  21. destroy_domain_allow: { target_type: 'DomainAllow', action: 'destroy' }.freeze,
  22. destroy_domain_block: { target_type: 'DomainBlock', action: 'destroy' }.freeze,
  23. destroy_email_domain_block: { target_type: 'EmailDomainBlock', action: 'destroy' }.freeze,
  24. destroy_status: { target_type: 'Status', action: 'destroy' }.freeze,
  25. disable_2fa_user: { target_type: 'User', action: 'disable' }.freeze,
  26. disable_custom_emoji: { target_type: 'CustomEmoji', action: 'disable' }.freeze,
  27. disable_user: { target_type: 'User', action: 'disable' }.freeze,
  28. enable_custom_emoji: { target_type: 'CustomEmoji', action: 'enable' }.freeze,
  29. enable_user: { target_type: 'User', action: 'enable' }.freeze,
  30. memorialize_account: { target_type: 'Account', action: 'memorialize' }.freeze,
  31. promote_user: { target_type: 'User', action: 'promote' }.freeze,
  32. remove_avatar_user: { target_type: 'User', action: 'remove_avatar' }.freeze,
  33. reopen_report: { target_type: 'Report', action: 'reopen' }.freeze,
  34. reset_password_user: { target_type: 'User', action: 'reset_password' }.freeze,
  35. resolve_report: { target_type: 'Report', action: 'resolve' }.freeze,
  36. silence_account: { target_type: 'Account', action: 'silence' }.freeze,
  37. suspend_account: { target_type: 'Account', action: 'suspend' }.freeze,
  38. unassigned_report: { target_type: 'Report', action: 'unassigned' }.freeze,
  39. unsilence_account: { target_type: 'Account', action: 'unsilence' }.freeze,
  40. unsuspend_account: { target_type: 'Account', action: 'unsuspend' }.freeze,
  41. update_announcement: { target_type: 'Announcement', action: 'update' }.freeze,
  42. update_custom_emoji: { target_type: 'CustomEmoji', action: 'update' }.freeze,
  43. update_status: { target_type: 'Status', action: 'update' }.freeze,
  44. }.freeze
  45. attr_reader :params
  46. def initialize(params)
  47. @params = params
  48. end
  49. def results
  50. scope = Admin::ActionLog.includes(:target)
  51. params.each do |key, value|
  52. next if key.to_s == 'page'
  53. scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
  54. end
  55. scope
  56. end
  57. private
  58. def scope_for(key, value)
  59. case key
  60. when 'action_type'
  61. Admin::ActionLog.where(ACTION_TYPE_MAP[value.to_sym])
  62. when 'account_id'
  63. Admin::ActionLog.where(account_id: value)
  64. when 'target_account_id'
  65. account = Account.find(value)
  66. Admin::ActionLog.where(target: [account, account.user].compact)
  67. else
  68. raise "Unknown filter: #{key}"
  69. end
  70. end
  71. end