Class: PushNotificationPusher

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

Constant Summary collapse

CONNECTION_TIMEOUT_SECONDS =
5

Class Method Summary collapse

Class Method Details

.clear_subscriptions(user) ⇒ Object



50
51
52
# File 'app/services/push_notification_pusher.rb', line 50

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

.get_badgeObject



85
86
87
88
89
90
91
# File 'app/services/push_notification_pusher.rb', line 85

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
37
38
39
40
41
42
43
44
# 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] ||
          I18n.t(
            "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}",
            site_title: SiteSetting.title,
            topic: payload[:topic_title],
            username: payload[:username],
          ),
      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],
      hide_when_active: true,
    }

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

  message
end

.subscribe(user, push_params, send_confirmation) ⇒ Object



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

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



46
47
48
# File 'app/services/push_notification_pusher.rb', line 46

def self.subscriptions(user)
  user.push_subscriptions
end

.unsubscribe(user, subscription) ⇒ Object



81
82
83
# File 'app/services/push_notification_pusher.rb', line 81

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