mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 04:52:45 +08:00
Refactor of PushSubscriptionPusher
.
This commit is contained in:
parent
bbc85258c9
commit
2eb2f273a8
|
@ -33,13 +33,17 @@ class PushNotificationPusher
|
|||
end
|
||||
|
||||
def self.subscribe(user, subscription, send_confirmation)
|
||||
subscriptions = PushSubscription.where(user: user, data: subscription.to_json)
|
||||
if subscriptions.length > 1
|
||||
data = subscription.to_json
|
||||
subscriptions = PushSubscription.where(user: user, data: data)
|
||||
subscriptions_count = subscriptions.count
|
||||
|
||||
if subscriptions_count > 1
|
||||
subscriptions.destroy_all
|
||||
PushSubscription.create user: user, data: subscription.to_json
|
||||
elsif subscriptions.length == 0
|
||||
PushSubscription.create user: user, data: subscription.to_json
|
||||
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",
|
||||
|
@ -55,7 +59,7 @@ class PushNotificationPusher
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
protected
|
||||
|
|
|
@ -61,29 +61,19 @@ describe PushNotificationController do
|
|||
end
|
||||
|
||||
it "should not create duplicate subscriptions" do
|
||||
post '/push_notifications/subscribe.json', params: {
|
||||
username: user.username,
|
||||
subscription: {
|
||||
endpoint: "endpoint",
|
||||
keys: {
|
||||
p256dh: "256dh",
|
||||
auth: "auth"
|
||||
}
|
||||
},
|
||||
send_confirmation: false
|
||||
}
|
||||
|
||||
post '/push_notifications/subscribe.json', params: {
|
||||
username: user.username,
|
||||
subscription: {
|
||||
endpoint: "endpoint",
|
||||
keys: {
|
||||
p256dh: "256dh",
|
||||
auth: "auth"
|
||||
}
|
||||
},
|
||||
send_confirmation: false
|
||||
}
|
||||
2.times do
|
||||
post '/push_notifications/subscribe.json', params: {
|
||||
username: user.username,
|
||||
subscription: {
|
||||
endpoint: "endpoint",
|
||||
keys: {
|
||||
p256dh: "256dh",
|
||||
auth: "auth"
|
||||
}
|
||||
},
|
||||
send_confirmation: false
|
||||
}
|
||||
end
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
expect(user.push_subscriptions.count).to eq(1)
|
||||
|
|
Loading…
Reference in New Issue
Block a user