about summary refs log tree commit diff
path: root/app/services/notify_service.rb
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/notify_service.rb
parent3838e6836d47797a4e8ca20afa70eebefb68da26 (diff)
Adding unified streamable notifications
Diffstat (limited to 'app/services/notify_service.rb')
-rw-r--r--app/services/notify_service.rb36
1 files changed, 36 insertions, 0 deletions
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