mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Refactor of PushSubscriptionPusher
.
This commit is contained in:
parent
bbc85258c9
commit
2eb2f273a8
@ -33,13 +33,17 @@ class PushNotificationPusher
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.subscribe(user, subscription, send_confirmation)
|
def self.subscribe(user, subscription, send_confirmation)
|
||||||
subscriptions = PushSubscription.where(user: user, data: subscription.to_json)
|
data = subscription.to_json
|
||||||
if subscriptions.length > 1
|
subscriptions = PushSubscription.where(user: user, data: data)
|
||||||
|
subscriptions_count = subscriptions.count
|
||||||
|
|
||||||
|
if subscriptions_count > 1
|
||||||
subscriptions.destroy_all
|
subscriptions.destroy_all
|
||||||
PushSubscription.create user: user, data: subscription.to_json
|
PushSubscription.create!(user: user, data: data)
|
||||||
elsif subscriptions.length == 0
|
elsif subscriptions_count == 0
|
||||||
PushSubscription.create user: user, data: subscription.to_json
|
PushSubscription.create!(user: user, data: data)
|
||||||
end
|
end
|
||||||
|
|
||||||
if send_confirmation == "true"
|
if send_confirmation == "true"
|
||||||
message = {
|
message = {
|
||||||
title: I18n.t("discourse_push_notifications.popup.confirm_title",
|
title: I18n.t("discourse_push_notifications.popup.confirm_title",
|
||||||
@ -55,7 +59,7 @@ class PushNotificationPusher
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.unsubscribe(user, subscription)
|
def self.unsubscribe(user, subscription)
|
||||||
PushSubscription.find_by(user: user, data: subscription.to_json)&.destroy
|
PushSubscription.find_by(user: user, data: subscription.to_json)&.destroy!
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
@ -61,6 +61,7 @@ describe PushNotificationController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "should not create duplicate subscriptions" do
|
it "should not create duplicate subscriptions" do
|
||||||
|
2.times do
|
||||||
post '/push_notifications/subscribe.json', params: {
|
post '/push_notifications/subscribe.json', params: {
|
||||||
username: user.username,
|
username: user.username,
|
||||||
subscription: {
|
subscription: {
|
||||||
@ -72,18 +73,7 @@ describe PushNotificationController do
|
|||||||
},
|
},
|
||||||
send_confirmation: false
|
send_confirmation: false
|
||||||
}
|
}
|
||||||
|
end
|
||||||
post '/push_notifications/subscribe.json', params: {
|
|
||||||
username: user.username,
|
|
||||||
subscription: {
|
|
||||||
endpoint: "endpoint",
|
|
||||||
keys: {
|
|
||||||
p256dh: "256dh",
|
|
||||||
auth: "auth"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
send_confirmation: false
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
expect(user.push_subscriptions.count).to eq(1)
|
expect(user.push_subscriptions.count).to eq(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user