Class: Decidim::ModerationTools

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/moderation_tools.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reportable, current_user) ⇒ ModerationTools

Returns a new instance of ModerationTools.



19
20
21
22
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 19

def initialize(reportable, current_user)
  @reportable = reportable
  @current_user = current_user
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



17
18
19
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 17

def current_user
  @current_user
end

#reportableObject (readonly)

Returns the value of attribute reportable.



17
18
19
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 17

def reportable
  @reportable
end

Instance Method Details

#create_report!(options) ⇒ Object

Public: creates a new report for the given resource, having a basic set of options moderation.create_report!(reason: "spam", details: "This is a spam")



45
46
47
48
49
50
51
52
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 45

def create_report!(options)
  options.reverse_merge!(
    moderation:,
    user: @current_user,
    locale: I18n.locale
  )
  Report.create!(options)
end

#hide!Object

Public: hides the resources



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 96

def hide!
  Decidim.traceability.perform_action_without_log!(current_user) do
    @reportable.moderation.update!(hidden_at: Time.current)
    @reportable.try(:touch)
  end

  if @reportable.is_a?(Decidim::Comments::Commentable)
    @reportable.comment_threads.each do |comment|
      Decidim::HideChildResourcesJob.perform_later(comment, current_user.id)
    end
  end

  send_notification_to_author
end

#hide_with_admin_log!Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 82

def hide_with_admin_log!
  Decidim.traceability.perform_action!(
    "hide",
    moderation,
    @current_user,
    extra: {
      reportable_type: @reportable.class.name
    }
  ) do
    hide!
  end
end

#moderationObject

Public: returns the moderation object for the given resource



55
56
57
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 55

def moderation
  @moderation ||= Moderation.find_or_create_by!(reportable: @reportable, participatory_space:)
end

#participatory_spaceObject

Public: fetches the participatory space of the resource's component or from the resource itself



30
31
32
33
34
35
36
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 30

def participatory_space
  @participatory_space ||= if reportable.class.respond_to?(:participatory_space?)
                             reportable
                           else
                             reportable.component&.participatory_space || reportable.try(:participatory_space)
                           end
end

#send_notification_to_authorObject

Public: Broadcasts a notification to the author of the resource that has been hidden



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 60

def send_notification_to_author
  return if affected_users.blank?

  data = if @reportable.moderation.reports.last&.reason == "parent_hidden"
           { event: "decidim.events.reports.parent_hidden", event_class: Decidim::ParentHiddenEvent }
         else
           { event: "decidim.events.reports.resource_hidden", event_class: Decidim::ResourceHiddenEvent }
         end

  data.merge!(
    resource: @reportable,
    extra: {
      report_reasons:,
      force_email: true
    },
    affected_users:,
    force_send: true
  )

  Decidim::EventsManager.publish(**data)
end

#update_report_count!Object

Public: increments the report count for the moderation object associated with resource



25
26
27
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 25

def update_report_count!
  moderation.update!(report_count: moderation.report_count + 1)
end

#update_reported_content!Object

Public: updates the reported content for the moderation object associated with resource



39
40
41
# File 'decidim-core/lib/decidim/moderation_tools.rb', line 39

def update_reported_content!
  moderation.update!(reported_content: reportable.reported_searchable_content_text)
end