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.

109 lines
3.4 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. end
  25. end
  26. def log_target_from_history(type, attributes)
  27. case type
  28. when 'CustomEmoji'
  29. attributes['shortcode']
  30. when 'DomainBlock', 'EmailDomainBlock'
  31. link_to attributes['domain'], "https://#{attributes['domain']}"
  32. when 'Status'
  33. tmp_status = Status.new(attributes.except('reblogs_count', 'favourites_count'))
  34. if tmp_status.account
  35. link_to tmp_status.account&.acct || "##{tmp_status.account_id}", admin_account_path(tmp_status.account_id)
  36. else
  37. I18n.t('admin.action_logs.deleted_status')
  38. end
  39. end
  40. end
  41. def relevant_log_changes(log)
  42. if log.target_type == 'CustomEmoji' && [:enable, :disable, :destroy].include?(log.action)
  43. log.recorded_changes.slice('domain')
  44. elsif log.target_type == 'CustomEmoji' && log.action == :update
  45. log.recorded_changes.slice('domain', 'visible_in_picker')
  46. elsif log.target_type == 'User' && [:promote, :demote].include?(log.action)
  47. log.recorded_changes.slice('moderator', 'admin')
  48. elsif log.target_type == 'User' && [:change_email].include?(log.action)
  49. log.recorded_changes.slice('email', 'unconfirmed_email')
  50. elsif log.target_type == 'DomainBlock'
  51. log.recorded_changes.slice('severity', 'reject_media')
  52. elsif log.target_type == 'Status' && log.action == :update
  53. log.recorded_changes.slice('sensitive')
  54. end
  55. end
  56. def log_extra_attributes(hash)
  57. safe_join(hash.to_a.map { |key, value| safe_join([content_tag(:span, key, class: 'diff-key'), '=', log_change(value)]) }, ' ')
  58. end
  59. def log_change(val)
  60. return content_tag(:span, val, class: 'diff-neutral') unless val.is_a?(Array)
  61. safe_join([content_tag(:span, val.first, class: 'diff-old'), content_tag(:span, val.last, class: 'diff-new')], '→')
  62. end
  63. def icon_for_log(log)
  64. case log.target_type
  65. when 'Account', 'User'
  66. 'user'
  67. when 'CustomEmoji'
  68. 'file'
  69. when 'Report'
  70. 'flag'
  71. when 'DomainBlock'
  72. 'lock'
  73. when 'EmailDomainBlock'
  74. 'envelope'
  75. when 'Status'
  76. 'pencil'
  77. end
  78. end
  79. def class_for_log_icon(log)
  80. case log.action
  81. when :enable, :unsuspend, :unsilence, :confirm, :promote, :resolve
  82. 'positive'
  83. when :create
  84. opposite_verbs?(log) ? 'negative' : 'positive'
  85. when :update, :reset_password, :disable_2fa, :memorialize, :change_email
  86. 'neutral'
  87. when :demote, :silence, :disable, :suspend, :remove_avatar, :reopen
  88. 'negative'
  89. when :destroy
  90. opposite_verbs?(log) ? 'positive' : 'negative'
  91. else
  92. ''
  93. end
  94. end
  95. private
  96. def opposite_verbs?(log)
  97. %w(DomainBlock EmailDomainBlock).include?(log.target_type)
  98. end
  99. end