1 # frozen_string_literal: true
3 class NotifyService
< BaseService
4 def call(recipient
, activity
)
7 @notification = Notification
.new(account
: @recipient, activity
: @activity)
9 return if recipient
.user
.nil? || blocked
?
12 push_notification!
if @notification.browserable
?
13 push_to_conversation!
if direct_message
?
14 send_email!
if email_enabled
?
15 rescue ActiveRecord
::RecordInvalid
22 FeedManager
.instance
.filter
?(:mentions, @notification.mention
.status
, @recipient.id
)
25 def blocked_favourite
?
34 @recipient.muting_reblogs
?(@notification.from_account
)
37 def blocked_follow_request
?
42 return @following_sender if defined?(@following_sender)
43 @following_sender = @recipient.following
?(@notification.from_account
) || @recipient.requested
?(@notification.from_account
)
46 def optional_non_follower
?
47 @recipient.user
.settings
.interactions
['must_be_follower'] && !
@notification.from_account
.following
?(@recipient)
50 def optional_non_following
?
51 @recipient.user
.settings
.interactions
['must_be_following'] && !following_sender
?
55 @notification.type
== :mention && @notification.target_status
.direct_visibility
?
58 def response_to_recipient
?
59 @notification.target_status
.in_reply_to_account_id
== @recipient.id
&& @notification.target_status
.thread
&.direct_visibility
?
63 @notification.from_account
.local
? && @notification.from_account
.user
.present
? && @notification.from_account
.user
.staff
?
66 def optional_non_following_and_direct
?
68 @recipient.user
.settings
.interactions
['must_be_following_dm'] &&
71 !response_to_recipient
?
75 @notification.from_account
.silenced
? && !following_sender
?
79 @recipient.id
== @notification.from_account
.id
83 @recipient.domain_blocking
?(@notification.from_account
.domain
) && !following_sender
?
87 blocked
= @recipient.suspended
? # Skip if the recipient account is suspended anyway
88 blocked
||= from_self
? # Skip for interactions with self
89 blocked
||= domain_blocking
? # Skip for domain blocked accounts
90 blocked
||= @recipient.blocking
?(@notification.from_account
) # Skip for blocked accounts
91 blocked
||= @recipient.muting_notifications
?(@notification.from_account
)
92 blocked
||= hellbanned
? # Hellban
93 blocked
||= optional_non_follower
? # Options
94 blocked
||= optional_non_following
? # Options
95 blocked
||= optional_non_following_and_direct
? # Options
96 blocked
||= conversation_muted
?
97 blocked
||= send("blocked_#{@notification.type}?") # Type-dependent filters
101 def conversation_muted
?
102 if @notification.target_status
103 @recipient.muting_conversation
?(@notification.target_status
.conversation
)
109 def create_notification!
113 def push_notification!
114 return if @notification.activity
.nil?
116 Redis
.current
.publish("timeline:#{@recipient.id}", Oj
.dump(event
: :notification, payload
: InlineRenderer
.render(@notification, @recipient, :notification)))
117 send_push_notifications!
120 def push_to_conversation!
121 return if @notification.activity
.nil?
122 AccountConversation
.add_status(@recipient, @notification.target_status
)
125 def send_push_notifications!
126 subscriptions_ids
= ::Web::PushSubscription.where(user_id
: @recipient.user
.id
)
127 .select
{ |subscription
| subscription
.pushable
?(@notification) }
130 ::Web::PushNotificationWorker.push_bulk(subscriptions_ids
) do |subscription_id
|
131 [subscription_id
, @notification.id
]
136 return if @notification.activity
.nil?
137 NotificationMailer
.public_send(@notification.type
, @recipient, @notification).deliver_later(wait
: 2.minutes
)
141 @recipient.user
.settings
.notification_emails
[@notification.type
.to_s
]