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.

114 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. module Admin::ActionLogsHelper
  3. def log_target(log)
  4. if log.target
  5. linkable_log_target(log.target)
  6. else
  7. log_target_from_history(log.target_type, log.recorded_changes)
  8. end
  9. end
  10. def linkable_log_target(record)
  11. case record.class.name
  12. when 'Account'
  13. link_to record.acct, admin_account_path(record.id)
  14. when 'User'
  15. link_to record.account.acct, admin_account_path(record.account_id)
  16. when 'CustomEmoji'
  17. record.shortcode
  18. when 'Report'
  19. link_to "##{record.id}", admin_report_path(record)
  20. when 'DomainBlock', 'EmailDomainBlock'
  21. link_to record.domain, "https://#{record.domain}"
  22. when 'Status'
  23. link_to record.account.acct, TagManager.instance.url_for(record)
  24. when 'AccountWarning'
  25. link_to record.target_account.acct, admin_account_path(record.target_account_id)
  26. end
  27. end
  28. def log_target_from_history(type, attributes)
  29. case type
  30. when 'CustomEmoji'
  31. attributes['shortcode']
  32. when 'DomainBlock', 'EmailDomainBlock'
  33. link_to attributes['domain'], "https://#{attributes['domain']}"
  34. when 'Status'
  35. tmp_status = Status.new(attributes.except('reblogs_count', 'favourites_count'))
  36. if tmp_status.account
  37. link_to tmp_status.account&.acct || "##{tmp_status.account_id}", admin_account_path(tmp_status.account_id)
  38. else
  39. I18n.t('admin.action_logs.deleted_status')
  40. end
  41. end
  42. end
  43. def relevant_log_changes(log)
  44. if log.target_type == 'CustomEmoji' && [:enable, :disable, :destroy].include?(log.action)
  45. log.recorded_changes.slice('domain')
  46. elsif log.target_type == 'CustomEmoji' && log.action == :update
  47. log.recorded_changes.slice('domain', 'visible_in_picker')
  48. elsif log.target_type == 'User' && [:promote, :demote].include?(log.action)
  49. log.recorded_changes.slice('moderator', 'admin')
  50. elsif log.target_type == 'User' && [:change_email].include?(log.action)
  51. log.recorded_changes.slice('email', 'unconfirmed_email')
  52. elsif log.target_type == 'DomainBlock'
  53. log.recorded_changes.slice('severity', 'reject_media')
  54. elsif log.target_type == 'Status' && log.action == :update
  55. log.recorded_changes.slice('sensitive')
  56. end
  57. end
  58. def log_extra_attributes(hash)
  59. safe_join(hash.to_a.map { |key, value| safe_join([content_tag(:span, key, class: 'diff-key'), '=', log_change(value)]) }, ' ')
  60. end
  61. def log_change(val)
  62. return content_tag(:span, val, class: 'diff-neutral') unless val.is_a?(Array)
  63. safe_join([content_tag(:span, val.first, class: 'diff-old'), content_tag(:span, val.last, class: 'diff-new')], '→')
  64. end
  65. def icon_for_log(log)
  66. case log.target_type
  67. when 'Account', 'User'
  68. 'user'
  69. when 'CustomEmoji'
  70. 'file'
  71. when 'Report'
  72. 'flag'
  73. when 'DomainBlock'
  74. 'lock'
  75. when 'EmailDomainBlock'
  76. 'envelope'
  77. when 'Status'
  78. 'pencil'
  79. when 'AccountWarning'
  80. 'warning'
  81. end
  82. end
  83. def class_for_log_icon(log)
  84. case log.action
  85. when :enable, :unsuspend, :unsilence, :confirm, :promote, :resolve
  86. 'positive'
  87. when :create
  88. opposite_verbs?(log) ? 'negative' : 'positive'
  89. when :update, :reset_password, :disable_2fa, :memorialize, :change_email
  90. 'neutral'
  91. when :demote, :silence, :disable, :suspend, :remove_avatar, :remove_header, :reopen
  92. 'negative'
  93. when :destroy
  94. opposite_verbs?(log) ? 'positive' : 'negative'
  95. else
  96. ''
  97. end
  98. end
  99. private
  100. def opposite_verbs?(log)
  101. %w(DomainBlock EmailDomainBlock AccountWarning).include?(log.target_type)
  102. end
  103. end