You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
967 B

  1. # frozen_string_literal: true
  2. class NotifyService < BaseService
  3. def call(recipient, activity)
  4. @recipient = recipient
  5. @activity = activity
  6. @notification = Notification.new(account: @recipient, activity: @activity)
  7. return if blocked?
  8. create_notification
  9. send_email if email_enabled?
  10. end
  11. private
  12. def blocked?
  13. blocked = false
  14. blocked ||= @recipient.id == @notification.from_account.id
  15. blocked ||= @recipient.blocking?(@notification.from_account)
  16. blocked
  17. end
  18. def create_notification
  19. @notification.save!
  20. FeedManager.instance.broadcast(@recipient.id, type: 'notification', message: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
  21. end
  22. def send_email
  23. NotificationMailer.send(@notification.type, @recipient, @notification).deliver_later
  24. end
  25. def email_enabled?
  26. @recipient.user.settings(:notification_emails).send(@notification.type)
  27. end
  28. end