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.

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