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.

85 lines
3.9 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. create_unavailable_domain: { target_type: 'UnavailableDomain', action: 'create' }.freeze,
  19. demote_user: { target_type: 'User', action: 'demote' }.freeze,
  20. destroy_announcement: { target_type: 'Announcement', action: 'destroy' }.freeze,
  21. destroy_custom_emoji: { target_type: 'CustomEmoji', action: 'destroy' }.freeze,
  22. destroy_domain_allow: { target_type: 'DomainAllow', action: 'destroy' }.freeze,
  23. destroy_domain_block: { target_type: 'DomainBlock', action: 'destroy' }.freeze,
  24. destroy_email_domain_block: { target_type: 'EmailDomainBlock', action: 'destroy' }.freeze,
  25. destroy_unavailable_domain: { target_type: 'UnavailableDomain', action: 'destroy' }.freeze,
  26. destroy_status: { target_type: 'Status', action: 'destroy' }.freeze,
  27. disable_2fa_user: { target_type: 'User', action: 'disable' }.freeze,
  28. disable_custom_emoji: { target_type: 'CustomEmoji', action: 'disable' }.freeze,
  29. disable_user: { target_type: 'User', action: 'disable' }.freeze,
  30. enable_custom_emoji: { target_type: 'CustomEmoji', action: 'enable' }.freeze,
  31. enable_user: { target_type: 'User', action: 'enable' }.freeze,
  32. memorialize_account: { target_type: 'Account', action: 'memorialize' }.freeze,
  33. promote_user: { target_type: 'User', action: 'promote' }.freeze,
  34. remove_avatar_user: { target_type: 'User', action: 'remove_avatar' }.freeze,
  35. reopen_report: { target_type: 'Report', action: 'reopen' }.freeze,
  36. reset_password_user: { target_type: 'User', action: 'reset_password' }.freeze,
  37. resolve_report: { target_type: 'Report', action: 'resolve' }.freeze,
  38. sensitive_account: { target_type: 'Account', action: 'sensitive' }.freeze,
  39. silence_account: { target_type: 'Account', action: 'silence' }.freeze,
  40. suspend_account: { target_type: 'Account', action: 'suspend' }.freeze,
  41. unassigned_report: { target_type: 'Report', action: 'unassigned' }.freeze,
  42. unsensitive_account: { target_type: 'Account', action: 'unsensitive' }.freeze,
  43. unsilence_account: { target_type: 'Account', action: 'unsilence' }.freeze,
  44. unsuspend_account: { target_type: 'Account', action: 'unsuspend' }.freeze,
  45. update_announcement: { target_type: 'Announcement', action: 'update' }.freeze,
  46. update_custom_emoji: { target_type: 'CustomEmoji', action: 'update' }.freeze,
  47. update_status: { target_type: 'Status', action: 'update' }.freeze,
  48. }.freeze
  49. attr_reader :params
  50. def initialize(params)
  51. @params = params
  52. end
  53. def results
  54. scope = Admin::ActionLog.includes(:target)
  55. params.each do |key, value|
  56. next if key.to_s == 'page'
  57. scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
  58. end
  59. scope
  60. end
  61. private
  62. def scope_for(key, value)
  63. case key
  64. when 'action_type'
  65. Admin::ActionLog.where(ACTION_TYPE_MAP[value.to_sym])
  66. when 'account_id'
  67. Admin::ActionLog.where(account_id: value)
  68. when 'target_account_id'
  69. account = Account.find(value)
  70. Admin::ActionLog.where(target: [account, account.user].compact)
  71. else
  72. raise "Unknown filter: #{key}"
  73. end
  74. end
  75. end