mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-11 05:48:51 +00:00
[ticket/17010] Handle already existing subscriptions
PHPBB3-17010
This commit is contained in:
parent
5098f315fd
commit
6b00e9fe09
3 changed files with 63 additions and 9 deletions
|
@ -48,7 +48,6 @@ class add_webpush extends migration
|
||||||
'COLUMNS' => [
|
'COLUMNS' => [
|
||||||
'subscription_id' => ['ULINT', null, 'auto_increment'],
|
'subscription_id' => ['ULINT', null, 'auto_increment'],
|
||||||
'user_id' => ['ULINT', 0],
|
'user_id' => ['ULINT', 0],
|
||||||
// 'device_name' => ['VCHAR:64', ''],
|
|
||||||
'endpoint' => ['TEXT', ''],
|
'endpoint' => ['TEXT', ''],
|
||||||
'expiration_time' => ['TIMESTAMP', 0],
|
'expiration_time' => ['TIMESTAMP', 0],
|
||||||
'p256dh' => ['VCHAR', ''],
|
'p256dh' => ['VCHAR', ''],
|
||||||
|
|
|
@ -35,9 +35,6 @@ class webpush
|
||||||
/** @var string UCP form token name */
|
/** @var string UCP form token name */
|
||||||
private const FORM_TOKEN_UCP = 'ucp_webpush';
|
private const FORM_TOKEN_UCP = 'ucp_webpush';
|
||||||
|
|
||||||
/** @var string Push worker form token name */
|
|
||||||
private const FORM_TOKEN_WORKER = 'webpush_worker';
|
|
||||||
|
|
||||||
/** @var config */
|
/** @var config */
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
@ -206,6 +203,7 @@ class webpush
|
||||||
$template_data += [
|
$template_data += [
|
||||||
'VAPID_PUBLIC_KEY' => $this->config['webpush_vapid_public'],
|
'VAPID_PUBLIC_KEY' => $this->config['webpush_vapid_public'],
|
||||||
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_ucp_push_worker_controller'),
|
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_ucp_push_worker_controller'),
|
||||||
|
'SUBSCRIPTIONS' => $this->get_subscriptions(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$content = $this->template->render('webpush.js.twig', $template_data);
|
$content = $this->template->render('webpush.js.twig', $template_data);
|
||||||
|
@ -273,4 +271,26 @@ class webpush
|
||||||
'form_tokens' => $this->form_helper->get_form_tokens(self::FORM_TOKEN_UCP),
|
'form_tokens' => $this->form_helper->get_form_tokens(self::FORM_TOKEN_UCP),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get subscriptions for current user
|
||||||
|
*
|
||||||
|
* @return array Subscriptions for user
|
||||||
|
*/
|
||||||
|
protected function get_subscriptions(): array
|
||||||
|
{
|
||||||
|
$subscriptions = [];
|
||||||
|
|
||||||
|
$sql = 'SELECT endpoint, expiration_time
|
||||||
|
FROM ' . $this->push_subscriptions_table . '
|
||||||
|
WHERE user_id = ' . $this->user->id();
|
||||||
|
$result = $this->db->sql_query($sql);
|
||||||
|
while ($row = $this->db->sql_fetchrow($result))
|
||||||
|
{
|
||||||
|
$subscriptions[] = $row;
|
||||||
|
}
|
||||||
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
|
return $subscriptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,13 @@ function PhpbbWebpush() {
|
||||||
formToken: '{{ FORM_TOKENS.form_token }}'
|
formToken: '{{ FORM_TOKENS.form_token }}'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @type [{endpoint: string, expiration: string}[]] Subscriptions */
|
||||||
|
let subscriptions = [
|
||||||
|
{% for sub in SUBSCRIPTIONS %}
|
||||||
|
{endpoint: '{{ sub.endpoint }}', expiration: '{{ sub.expiration }}' },
|
||||||
|
{% endfor %}
|
||||||
|
];
|
||||||
|
|
||||||
/** @type {string} VAPID public key */
|
/** @type {string} VAPID public key */
|
||||||
const VAPID_PUBLIC_KEY = '{{ VAPID_PUBLIC_KEY }}';
|
const VAPID_PUBLIC_KEY = '{{ VAPID_PUBLIC_KEY }}';
|
||||||
|
|
||||||
|
@ -70,7 +77,7 @@ function PhpbbWebpush() {
|
||||||
|
|
||||||
registration.pushManager.getSubscription()
|
registration.pushManager.getSubscription()
|
||||||
.then((subscribed) => {
|
.then((subscribed) => {
|
||||||
if (subscribed) {
|
if (isValidSubscription(subscribed)) {
|
||||||
setSubscriptionState(true);
|
setSubscriptionState(true);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -78,6 +85,31 @@ function PhpbbWebpush() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether subscription is valid
|
||||||
|
*
|
||||||
|
* @param {PushSubscription} subscription
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
const isValidSubscription = (subscription) => {
|
||||||
|
if (!subscription) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subscription.expirationTime && subscription.expirationTime <= Date.now()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const curSubscription of subscriptions) {
|
||||||
|
if (subscription.endpoint === curSubscription.endpoint) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscription is not in valid subscription list for user
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set subscription state for buttons
|
* Set subscription state for buttons
|
||||||
*
|
*
|
||||||
|
@ -111,16 +143,19 @@ function PhpbbWebpush() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
|
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
|
||||||
|
|
||||||
|
// We might already have a subscription that is unknown to this instance of phpBB.
|
||||||
|
// Unsubscribe before trying to subscribe again.
|
||||||
if (typeof registration !== 'undefined') {
|
if (typeof registration !== 'undefined') {
|
||||||
const subscribed = await registration.pushManager.getSubscription();
|
const subscribed = await registration.pushManager.getSubscription();
|
||||||
if (subscribed) {
|
if (subscribed) {
|
||||||
setSubscriptionState(true);
|
await subscribed.unsubscribe();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const newSubscription = await registration.pushManager.subscribe({
|
|
||||||
|
let newSubscription = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey: urlB64ToUint8Array(VAPID_PUBLIC_KEY)
|
applicationServerKey: urlB64ToUint8Array(VAPID_PUBLIC_KEY),
|
||||||
});
|
});
|
||||||
|
|
||||||
const loadingIndicator = phpbb.loadingIndicator();
|
const loadingIndicator = phpbb.loadingIndicator();
|
||||||
|
|
Loading…
Add table
Reference in a new issue