Adding unified streamable notifications
This commit is contained in:
@ -10,7 +10,7 @@ class FavouriteService < BaseService
|
||||
HubPingWorker.perform_async(account.id)
|
||||
|
||||
if status.local?
|
||||
NotificationMailer.favourite(status, account).deliver_later unless status.account.blocking?(account)
|
||||
NotifyService.new.call(status.account, favourite)
|
||||
else
|
||||
NotificationWorker.perform_async(favourite.stream_entry.id, status.account_id)
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ class FollowService < BaseService
|
||||
follow = source_account.follow!(target_account)
|
||||
|
||||
if target_account.local?
|
||||
NotificationMailer.follow(target_account, source_account).deliver_later unless target_account.blocking?(source_account)
|
||||
NotifyService.new.call(target_account, follow)
|
||||
else
|
||||
subscribe_service.call(target_account)
|
||||
NotificationWorker.perform_async(follow.stream_entry.id, target_account.id)
|
||||
|
36
app/services/notify_service.rb
Normal file
36
app/services/notify_service.rb
Normal file
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class NotifyService < BaseService
|
||||
def call(recipient, activity)
|
||||
@recipient = recipient
|
||||
@activity = activity
|
||||
@notification = Notification.new(account: @recipient, activity: @activity)
|
||||
|
||||
return if blocked?
|
||||
|
||||
create_notification
|
||||
send_email if email_enabled?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def blocked?
|
||||
blocked = false
|
||||
blocked ||= @recipient.id == @notification.from_account.id
|
||||
blocked ||= @recipient.blocking?(@notification.from_account)
|
||||
blocked
|
||||
end
|
||||
|
||||
def create_notification
|
||||
@notification.save!
|
||||
FeedManager.instance.broadcast(@recipient.id, type: 'notification', message: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
|
||||
end
|
||||
|
||||
def send_email
|
||||
NotificationMailer.send(@notification.type, @recipient, @notification).deliver_later
|
||||
end
|
||||
|
||||
def email_enabled?
|
||||
@recipient.user.settings(:notification_emails).send(@notification.type)
|
||||
end
|
||||
end
|
@ -150,12 +150,10 @@ class ProcessFeedService < BaseService
|
||||
|
||||
next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
|
||||
|
||||
if mentioned_account.local?
|
||||
# Send notifications
|
||||
NotificationMailer.mention(mentioned_account, parent).deliver_later unless mentioned_account.blocking?(parent.account)
|
||||
end
|
||||
mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
|
||||
|
||||
mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
|
||||
# Notify local user
|
||||
NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
|
||||
|
||||
# So we can skip duplicate mentions
|
||||
processed_account_ids << mentioned_account.id
|
||||
|
@ -65,8 +65,8 @@ class ProcessInteractionService < BaseService
|
||||
end
|
||||
|
||||
def follow!(account, target_account)
|
||||
account.follow!(target_account)
|
||||
NotificationMailer.follow(target_account, account).deliver_later unless target_account.blocking?(account)
|
||||
follow = account.follow!(target_account)
|
||||
NotifyService.new.call(target_account, follow)
|
||||
end
|
||||
|
||||
def unfollow!(account, target_account)
|
||||
@ -83,8 +83,8 @@ class ProcessInteractionService < BaseService
|
||||
|
||||
def favourite!(xml, from_account)
|
||||
current_status = status(xml)
|
||||
current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
|
||||
NotificationMailer.favourite(current_status, from_account).deliver_later unless current_status.account.blocking?(from_account)
|
||||
favourite = current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
|
||||
NotifyService.new.call(current_status.account, favourite)
|
||||
end
|
||||
|
||||
def add_post!(body, account)
|
||||
|
@ -29,7 +29,7 @@ class ProcessMentionsService < BaseService
|
||||
mentioned_account = mention.account
|
||||
|
||||
if mentioned_account.local?
|
||||
NotificationMailer.mention(mentioned_account, status).deliver_later unless mentioned_account.blocking?(status.account)
|
||||
NotifyService.new.call(mentioned_account, mention)
|
||||
else
|
||||
NotificationWorker.perform_async(status.stream_entry.id, mentioned_account.id)
|
||||
end
|
||||
|
@ -11,7 +11,7 @@ class ReblogService < BaseService
|
||||
HubPingWorker.perform_async(account.id)
|
||||
|
||||
if reblogged_status.local?
|
||||
NotificationMailer.reblog(reblogged_status, account).deliver_later unless reblogged_status.account.blocking?(account)
|
||||
NotifyService.new.call(reblogged_status.account, reblog)
|
||||
else
|
||||
NotificationWorker.perform_async(reblog.stream_entry.id, reblogged_status.account_id)
|
||||
end
|
||||
|
Reference in New Issue
Block a user