Class: PushNotificationPusher

Inherits:
Object
  • Object
show all
Defined in:
app/services/push_notification_pusher.rb

Constant Summary collapse

TOKEN_VALID_FOR_SECONDS =
5 * 60
CONNECTION_TIMEOUT_SECONDS =
5
MAX_ERRORS =
3
MIN_ERROR_DURATION =

1 day

86_400

Class Method Summary collapse

Class Method Details

.clear_subscriptions(user) ⇒ Object



68
69
70
# File 'app/services/push_notification_pusher.rb', line 68

def self.clear_subscriptions(user)
  user.push_subscriptions.clear
end

.get_badgeObject



103
104
105
106
107
108
109
# File 'app/services/push_notification_pusher.rb', line 103

def self.get_badge
  if (url = SiteSetting.site_push_notifications_icon_url).present?
    url
  else
    ActionController::Base.helpers.image_url("push-notifications/discourse.png")
  end
end

.push(user, payload) ⇒ Object



7
8
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
# File 'app/services/push_notification_pusher.rb', line 7

def self.push(user, payload)
  message = nil
  I18n.with_locale(user.effective_locale) do
    notification_icon_name = Notification.types[payload[:notification_type]]
    if !File.exist?(
         File.expand_path(
           "../../app/assets/images/push-notifications/#{notification_icon_name}.png",
           __dir__,
         ),
       )
      notification_icon_name = "discourse"
    end
    notification_icon =
      ActionController::Base.helpers.image_url("push-notifications/#{notification_icon_name}.png")

    message = {
      title: payload[:translated_title] || title(payload),
      body: payload[:excerpt],
      badge: get_badge,
      icon: notification_icon,
      tag: payload[:tag] || "#{Discourse.current_hostname}-#{payload[:topic_id]}",
      base_url: Discourse.base_url,
      url: payload[:post_url],
    }

    subscriptions(user).each { |subscription| send_notification(user, subscription, message) }
  end

  message
end

.subscribe(user, push_params, send_confirmation) ⇒ Object



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
# File 'app/services/push_notification_pusher.rb', line 72

def self.subscribe(user, push_params, send_confirmation)
  data = push_params.to_json
  subscriptions = PushSubscription.where(user: user, data: data)
  subscriptions_count = subscriptions.count

  new_subscription =
    if subscriptions_count > 1
      subscriptions.destroy_all
      PushSubscription.create!(user: user, data: data)
    elsif subscriptions_count == 0
      PushSubscription.create!(user: user, data: data)
    end

  if send_confirmation == "true"
    message = {
      title:
        I18n.t("discourse_push_notifications.popup.confirm_title", site_title: SiteSetting.title),
      body: I18n.t("discourse_push_notifications.popup.confirm_body"),
      icon: ActionController::Base.helpers.image_url("push-notifications/check.png"),
      badge: get_badge,
      tag: "#{Discourse.current_hostname}-subscription",
    }

    send_notification(user, new_subscription, message)
  end
end

.subscriptions(user) ⇒ Object



64
65
66
# File 'app/services/push_notification_pusher.rb', line 64

def self.subscriptions(user)
  user.push_subscriptions
end

.title(payload) ⇒ Object



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
# File 'app/services/push_notification_pusher.rb', line 38

def self.title(payload)
  translation_key =
    case payload[:notification_type]
    when Notification.types[:watching_category_or_tag]
      # For watching_category_or_tag, the notification could be for either a new post or new topic.
      # Instead of duplicating translations, we can rely on 'watching_first_post' for new topics,
      # and 'posted' for new posts.
      type = payload[:post_number] == 1 ? "watching_first_post" : "posted"
      "discourse_push_notifications.popup.#{type}"
    else
      "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}"
    end

  # Payload modifier used to adjust arguments to the translation
  payload =
    DiscoursePluginRegistry.apply_modifier(:push_notification_pusher_title_payload, payload)

  I18n.t(
    translation_key,
    site_title: SiteSetting.title,
    topic: payload[:topic_title],
    username: payload[:username],
    group_name: payload[:group_name],
  )
end

.unsubscribe(user, subscription) ⇒ Object



99
100
101
# File 'app/services/push_notification_pusher.rb', line 99

def self.unsubscribe(user, subscription)
  PushSubscription.find_by(user: user, data: subscription.to_json)&.destroy!
end