Class: NewsmastMastodon::CustomNotificationService

Inherits:
BaseService
  • Object
show all
Includes:
NonChannelHelper
Defined in:
app/services/newsmast_mastodon/custom_notification_service.rb

Instance Method Summary collapse

Methods included from NonChannelHelper

#is_non_channel?

Instance Method Details

#call(recipient, notification) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/newsmast_mastodon/custom_notification_service.rb', line 9

def call(recipient, notification)
  notification_tokens = NewsmastMastodon::NotificationToken.where(account_id: recipient.id)
  return nil if notification_tokens.empty? || notification_tokens.any? { |token| token.mute }

  body = ''
  destination_id = 0
  reblogged_id = 0
  visibility = ''
  notification_request = nil
  notification_type = notification.type&.to_sym
   = Account.find(notification.).username

  # To skip sending notification when the status is reblogged by the Group channels and local_only is true
  return nil if skip_local_only_notify?(notification)

  case notification_type
  when :status
    body = I18n.t('notification_mailer.status.subject', name: )
    destination_id = Status.find(notification.activity_id).id
  when :update
    body = I18n.t('notification_mailer.update.subject', name: )
    destination_id = Status.find(notification.activity_id).id
  when :reblog
    body = I18n.t('notification_mailer.reblog.subject', name: )
    status = Status.find(notification.activity_id)
    destination_id = status.id
    reblogged_id = status.reblog_of_id
  when :favourite
    body = I18n.t('notification_mailer.favourite.subject', name: )
    favourite = Favourite.find(notification.activity_id)
    destination_id = Status.find(favourite.status_id).id
  when :mention
    mention = Mention.find(notification.activity_id)
    status = Status.find(mention.status_id)
    notification_request = NotificationRequest.find_by(account_id: notification.)
    body = if notification_request.present?
             I18n.t('notification.mention.conversation_request')
           elsif status.visibility == Status.visibilities[:direct]
             I18n.t('notification.mention.direct_message', name: )
           else
             I18n.t('notification_mailer.mention.subject', name: )
           end
    destination_id = status.id
    visibility = status.visibility
  when :poll
    poll = Poll.find(notification.activity_id)
    body = notification. == poll. ? I18n.t('notification.poll.ended_you') : I18n.t('notification.poll.ended_voted')
    destination_id = Status.find(poll.status_id).id
  when :follow
    body = I18n.t('notification_mailer.follow.subject', name: )
    destination_id = notification.
  when :follow_request
    body = I18n.t('notification_mailer.follow_request.subject', name: )
    destination_id = notification.
  when :quote
    body = I18n.t('notification_mailer.quote.subject', name: )
    destination_id = Quote.find(notification.activity_id)&.status_id
  when :quoted_update
    body = I18n.t('notification_mailer.update.subject', name: )
    destination_id = Quote.find(notification.activity_id)&.status_id
  when :'admin.sign_up'
    body = I18n.t('notification_mailer.admin.sign_up.subject', name: )
    destination_id = notification.
  when :'admin.report'
    body = I18n.t('notification_mailer.admin.report.subject', name: )
    destination_id = notification.activity_id
  else
    Rails.logger.warn "[CustomNotificationService] Unhandled notification type: #{notification.type}"
    return nil
  end

  return nil if body.blank?

  data = {
    noti_type: notification_type,
    destination_id: destination_id.to_s,
    reblogged_id: reblogged_id.to_s,
    visibility: visibility,
  }
  data.merge!(conversation_request: 'true') if notification_request.present?

  # ios & android
  ios_android_devices = notification_tokens.where.not(platform_type: 'huawei').pluck(:notification_token)

  app_title = ENV['NOTIFICATION_SENDER_NAME'] || 'Development Patchwork'

  ios_android_devices.each do |device|
    NewsmastMastodon::FirebaseNotificationService.send_notification(device, app_title, body, data)
  end
end