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.

83 lines
3.7 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. sensitive_account: { target_type: 'Account', action: 'sensitive' }.freeze,
  37. silence_account: { target_type: 'Account', action: 'silence' }.freeze,
  38. suspend_account: { target_type: 'Account', action: 'suspend' }.freeze,
  39. unassigned_report: { target_type: 'Report', action: 'unassigned' }.freeze,
  40. unsensitive_account: { target_type: 'Account', action: 'unsensitive' }.freeze,
  41. unsilence_account: { target_type: 'Account', action: 'unsilence' }.freeze,
  42. unsuspend_account: { target_type: 'Account', action: 'unsuspend' }.freeze,
  43. update_announcement: { target_type: 'Announcement', action: 'update' }.freeze,
  44. update_custom_emoji: { target_type: 'CustomEmoji', action: 'update' }.freeze,
  45. update_status: { target_type: 'Status', action: 'update' }.freeze,
  46. }.freeze
  47. attr_reader :params
  48. def initialize(params)
  49. @params = params
  50. end
  51. def results
  52. scope = Admin::ActionLog.includes(:target)
  53. params.each do |key, value|
  54. next if key.to_s == 'page'
  55. scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
  56. end
  57. scope
  58. end
  59. private
  60. def scope_for(key, value)
  61. case key
  62. when 'action_type'
  63. Admin::ActionLog.where(ACTION_TYPE_MAP[value.to_sym])
  64. when 'account_id'
  65. Admin::ActionLog.where(account_id: value)
  66. when 'target_account_id'
  67. account = Account.find(value)
  68. Admin::ActionLog.where(target: [account, account.user].compact)
  69. else
  70. raise "Unknown filter: #{key}"
  71. end
  72. end
  73. end