forked from pupilfirst/pupilfirst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_job.rb
73 lines (64 loc) · 1.71 KB
/
create_job.rb
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
module Notifications
class CreateJob < ApplicationJob
rescue_from ActiveJob::DeserializationError do |_exception|
true # Skip processing if either the actor or the resource have been deleted.
end
def perform(event, actor, resource)
unless Notification.events.include?(event)
raise "Encountered unexpected event #{event}"
end
@event = event
@actor = actor
@resource = resource
return if skip?
users
.where.not(id: actor.id)
.each do |recipient|
I18n.with_locale(recipient.locale) do
notification =
Notification.create!(
actor_id: actor.id,
notifiable: resource,
event: Notification.events[event],
recipient: recipient,
message: message
)
Notifications::DeliverService.new(notification).deliver
end
end
end
private
def skip?
case @event
when :post_created
@resource.archived_at?
when :topic_created
@resource.archived?
end
end
def users
case @event
when :post_created
@resource.topic.users
when :topic_created
@resource.users
end
end
def message
case @event
when :post_created
I18n.t(
'jobs.notifications.create.message.post_created',
user_name: @actor.name,
community_name: @resource.community.name
)
when :topic_created
I18n.t(
'jobs.notifications.create.message.topic_created',
user_name: @actor.name,
community_name: @resource.community.name
)
end
end
end
end