-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathspam_digest_event.rb
More file actions
124 lines (100 loc) · 3.03 KB
/
Copy pathspam_digest_event.rb
File metadata and controls
124 lines (100 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true
module Decidim
module Ai
module SpamDetection
class SpamDigestEvent < Decidim::Events::BaseEvent
include Decidim::Events::EmailEvent
def self.types
[:email, :notification]
end
def resource
return @resource unless @resource.is_a?(Decidim::Organization)
OpenStruct.new(organization: @resource)
end
def email_intro
sanitize(
I18n.t(
"decidim.ai.spam_detection.digest.summary",
count: spam_count,
frequency_label:,
organization: translated_attribute(organization.name),
moderations_url:
)
)
end
def notification_title
email_intro
end
def email_subject
I18n.t(
"decidim.ai.spam_detection.digest.subject",
count: spam_count,
frequency_label:
)
end
def resource_title
translated_attribute(organization.name)
end
def resource_locator
helpers = Decidim::Core::Engine.routes.url_helpers
host = organization.host || Decidim::Organization.first&.host || "localhost"
Class.new do
def initialize(path, url)
@path = path
@url = url
end
def path(_params = nil)
@path
end
def url(_params = nil)
@url
end
def route_name
"organization"
end
end.new(
resource_path,
helpers.root_url(host:)
)
end
def resource_path(_organization = nil)
Decidim::Core::Engine.routes.url_helpers.admin_moderations_path
rescue NoMethodError
Decidim::Core::Engine.routes.url_helpers.root_path
end
def show_extended_information?
false
end
private
def moderations_url
host = organization.host
if host.blank?
return "" unless Rails.env.development? || Rails.env.test?
host = "localhost:3000"
elsif host == "localhost" && (Rails.env.development? || Rails.env.test?)
host = "localhost:3000"
end
protocol = Rails.env.production? ? "https" : "http"
"#{protocol}://#{host}/admin/moderations"
end
def organization
if @resource.is_a?(Decidim::Organization)
@resource
elsif @resource.respond_to?(:organization)
@resource.organization
elsif @resource.respond_to?(:component)
@resource.component.participatory_space.organization
else
Decidim::Organization.first
end
end
def spam_count
extra[:spam_count] || 0
end
def frequency_label
I18n.t("decidim.ai.spam_detection.digest.frequency_label.#{extra[:frequency] || "daily"}")
end
end
end
end
end