闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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