action_logs_helper.rb 3.6 KB

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