Class: Decidim::ModerationTools

Inherits:
Object
  • Object
show all
Defined in:
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 '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 '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 '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”)



41
42
43
44
45
46
47
48
# File 'lib/decidim/moderation_tools.rb', line 41

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

#hide!Object

Public: hides the resource



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/decidim/moderation_tools.rb', line 71

def hide!
  Decidim.traceability.perform_action!(
    "hide",
    moderation,
    @current_user,
    extra: {
      reportable_type: @reportable.class.name
    }
  ) do
    @reportable.moderation.update!(hidden_at: Time.current)
    @reportable.try(:touch)
  end
end

#moderationObject

Public: returns the moderation object for the given resource



51
52
53
# File 'lib/decidim/moderation_tools.rb', line 51

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
# File 'lib/decidim/moderation_tools.rb', line 30

def participatory_space
  @participatory_space ||= @reportable.component&.participatory_space || @reportable.try(:participatory_space)
end

#send_notification_to_authorObject

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/decidim/moderation_tools.rb', line 56

def send_notification_to_author
  data = {
    event: "decidim.events.reports.resource_hidden",
    event_class: Decidim::ResourceHiddenEvent,
    resource: @reportable,
    extra: {
      report_reasons:
    },
    affected_users: @reportable.try(:authors) || [@reportable.try(:normalized_author)]
  }

  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 '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



35
36
37
# File 'lib/decidim/moderation_tools.rb', line 35

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