about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-11-20 00:33:02 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-11-20 19:39:58 +0100
commitda2ef4d676ff71e6ab3edf8d1a7cee8bf6b6d353 (patch)
treef73fa34a3323a70d5dcba360f781bce5325e3ed1 /app/services
parent3838e6836d47797a4e8ca20afa70eebefb68da26 (diff)
Adding unified streamable notifications
Diffstat (limited to 'app/services')
-rw-r--r--app/services/favourite_service.rb2
-rw-r--r--app/services/follow_service.rb2
-rw-r--r--app/services/notify_service.rb36
-rw-r--r--app/services/process_feed_service.rb8
-rw-r--r--app/services/process_interaction_service.rb8
-rw-r--r--app/services/process_mentions_service.rb2
-rw-r--r--app/services/reblog_service.rb2
7 files changed, 47 insertions, 13 deletions
diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb
index 9c6f12478..781b03b40 100644
--- a/app/services/favourite_service.rb
+++ b/app/services/favourite_service.rb
@@ -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
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index 3b97840cb..cdae254bf 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -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)
diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb
new file mode 100644
index 000000000..a51c5b959
--- /dev/null
+++ b/app/services/notify_service.rb
@@ -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
diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb
index 8daea1675..4b2fe3799 100644
--- a/app/services/process_feed_service.rb
+++ b/app/services/process_feed_service.rb
@@ -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
diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb
index ecd3c2b2c..e7bb3c73b 100644
--- a/app/services/process_interaction_service.rb
+++ b/app/services/process_interaction_service.rb
@@ -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)
diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb
index fd5a02ffe..98e48299e 100644
--- a/app/services/process_mentions_service.rb
+++ b/app/services/process_mentions_service.rb
@@ -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
diff --git a/app/services/reblog_service.rb b/app/services/reblog_service.rb
index 884d911a4..6543d4ae7 100644
--- a/app/services/reblog_service.rb
+++ b/app/services/reblog_service.rb
@@ -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