Skip to content

Commit db06ede

Browse files
authored
Merge pull request #119 from iMattPro/fixes
2 parents 0980414 + eb5ea11 commit db06ede

File tree

6 files changed

+55
-43
lines changed

6 files changed

+55
-43
lines changed

language/en/webpushnotifications_module_ucp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Disable to unsubscribe',
4949
'NOTIFY_WEBPUSH_DROPDOWN_TITLE' => 'Visit notifications settings to set your preferred push notifications.',
5050
'NOTIFY_WEBPUSH_DENIED' => 'You have denied notifications from this site. To enable push notifications, allow notifications from this site in your browser settings.',
51-
'NOTIFY_WEBPUSH_DISABLED' => 'Push notifications not supported',
51+
'NOTIFY_WEBPUSH_NOT_SUPPORTED' => 'Push notifications not supported',
5252
'NOTIFY_WEBPUSH_POPUP_TITLE' => 'Allow browser notifications?',
5353
'NOTIFY_WEBPUSH_POPUP_MESSAGE' => 'We would like to send you browser notifications for replies, private messages, and relevant forum activity. Optional — you can manage these settings at any time.',
5454
'NOTIFY_WEBPUSH_POPUP_ALLOW' => 'Allow',

language/ru/webpushnotifications_module_ucp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Отписаться',
4949
'NOTIFY_WEBPUSH_DROPDOWN_TITLE' => 'Посетите настройки уведомлений, чтобы установить предпочтительные типы браузерных уведомлений.',
5050
'NOTIFY_WEBPUSH_DENIED' => 'Вы запретили браузерные уведомления для даного сайта. Для того, чтобы подписаться, необходимо их разрешить в настройках браузера.',
51-
'NOTIFY_WEBPUSH_DISABLED' => 'Не поддерживается',
51+
'NOTIFY_WEBPUSH_NOT_SUPPORTED' => 'Не поддерживается',
5252
'NOTIFY_WEBPUSH_POPUP_TITLE' => 'Включить браузерные уведомления?',
5353
'NOTIFY_WEBPUSH_POPUP_MESSAGE' => 'Браузерные уведомления позволяют быстро получать информацию о новых ответах, личных сообщениях и других активностях на данной конференции. Функцию можно отключить или включить в любое время в настройках уведомлений в Личном разделе.',
5454
'NOTIFY_WEBPUSH_POPUP_ALLOW' => 'Включить',

styles/all/template/event/notification_dropdown_footer_after.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% if NOTIFICATIONS_WEBPUSH_ENABLE and notification_types is not defined %}
22
<div class="wpn-notification-dropdown-footer" title="{{ lang('NOTIFY_WEBPUSH_DROPDOWN_TITLE') }}">
33
<span class="ellipsis-text">{{ lang('NOTIFY_WEBPUSH_ENABLE_SLIDER') ~ lang('COLON') }}</span>
4-
<button id="subscribe_webpush" name="subscribe_webpush" data-l-err="{{ lang('INFORMATION') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED') }}">
4+
<button id="subscribe_webpush" name="subscribe_webpush" data-l-err="{{ lang('INFORMATION')|e('html_attr') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED')|e('html_attr') }}" data-l-unsupported="{{ lang('NOTIFY_WEBPUSH_NOT_SUPPORTED')|e('html_attr') }}">
55
<i class="icon fa-toggle-off fa-fw icon-lightgray" aria-hidden="true" title="{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}"><span class="sr-only">{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}</span></i>
66
</button>
77
<button id="unsubscribe_webpush" name="unsubscribe_webpush" class="hidden">

styles/all/template/ucp_notifications_webpush.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
44
subscribeUrl: '{{ U_WEBPUSH_SUBSCRIBE }}',
55
unsubscribeUrl: '{{ U_WEBPUSH_UNSUBSCRIBE }}',
6-
ajaxErrorTitle: '{{ lang('AJAX_ERROR_TITLE') }}',
6+
ajaxErrorTitle: '{{ lang_js('AJAX_ERROR_TITLE') }}',
77
vapidPublicKey: '{{ VAPID_PUBLIC_KEY }}',
88
formTokens: {
99
creationTime: '{{ WEBPUSH_FORM_TOKENS.creation_time }}',

styles/all/template/webpush.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function PhpbbWebpush() {
9090
}
9191

9292
if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) {
93-
subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg');
93+
subscribeButton.value = subscribeButton.getAttribute('data-l-unsupported');
9494
}
9595
}
9696

@@ -164,8 +164,8 @@ function PhpbbWebpush() {
164164
allowBtn.addEventListener('click', (event) => {
165165
event.stopPropagation();
166166
popup.style.display = 'none';
167-
subscribeButtonHandler({
168-
preventDefault: () => {}
167+
subscribeButtonHandler(event).catch(error => {
168+
console.error('Subscription handler error:', error);
169169
});
170170
});
171171
}
@@ -237,48 +237,60 @@ function PhpbbWebpush() {
237237
async function subscribeButtonHandler(event) {
238238
event.preventDefault();
239239

240-
subscribeButton.addEventListener('click', subscribeButtonHandler);
240+
subscribeButton.removeEventListener('click', subscribeButtonHandler);
241241

242-
// Prevent the user from clicking the subscribe button multiple times.
243-
const result = await Notification.requestPermission();
244-
if (result === 'denied') {
245-
phpbb.alert(subscribeButton.getAttribute('data-l-err'), subscribeButton.getAttribute('data-l-msg'));
246-
return;
247-
}
242+
try {
243+
// Prevent the user from clicking the subscribe button multiple times.
244+
const result = await Notification.requestPermission();
245+
if (result === 'denied') {
246+
phpbb.alert(subscribeButton.getAttribute('data-l-err'), subscribeButton.getAttribute('data-l-msg'));
247+
return;
248+
}
248249

249-
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
250+
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
250251

251-
// We might already have a subscription that is unknown to this instance of phpBB.
252-
// Unsubscribe before trying to subscribe again.
253-
if (typeof registration !== 'undefined') {
254-
const subscribed = await registration.pushManager.getSubscription();
255-
if (subscribed) {
256-
await subscribed.unsubscribe();
252+
// We might already have a subscription that is unknown to this instance of phpBB.
253+
// Unsubscribe before trying to subscribe again.
254+
if (typeof registration !== 'undefined') {
255+
const subscribed = await registration.pushManager.getSubscription();
256+
if (subscribed) {
257+
await subscribed.unsubscribe();
258+
}
257259
}
258-
}
259260

260-
const newSubscription = await registration.pushManager.subscribe({
261-
userVisibleOnly: true,
262-
applicationServerKey: urlB64ToUint8Array(vapidPublicKey),
263-
});
261+
const newSubscription = await registration.pushManager.subscribe({
262+
userVisibleOnly: true,
263+
applicationServerKey: urlB64ToUint8Array(vapidPublicKey),
264+
});
264265

265-
const loadingIndicator = phpbb.loadingIndicator();
266-
fetch(subscribeUrl, {
267-
method: 'POST',
268-
headers: {
269-
'X-Requested-With': 'XMLHttpRequest',
270-
},
271-
body: getFormData(newSubscription),
272-
})
273-
.then(response => {
274-
loadingIndicator.fadeOut(phpbb.alertTime);
275-
return response.json();
266+
const loadingIndicator = phpbb.loadingIndicator();
267+
fetch(subscribeUrl, {
268+
method: 'POST',
269+
headers: {
270+
'X-Requested-With': 'XMLHttpRequest',
271+
},
272+
body: getFormData(newSubscription),
276273
})
277-
.then(handleSubscribe)
278-
.catch(error => {
279-
loadingIndicator.fadeOut(phpbb.alertTime);
280-
phpbb.alert(ajaxErrorTitle, error);
281-
});
274+
.then(response => {
275+
loadingIndicator.fadeOut(phpbb.alertTime);
276+
return response.json();
277+
})
278+
.then(handleSubscribe)
279+
.catch(error => {
280+
loadingIndicator.fadeOut(phpbb.alertTime);
281+
phpbb.alert(ajaxErrorTitle, error);
282+
});
283+
} catch (error) {
284+
promptDenied.set(); // deny the prompt on error to prevent repeated prompting
285+
const popup = document.getElementById('wpn_popup_prompt');
286+
if (popup) {
287+
popup.style.display = 'none';
288+
}
289+
console.error('Push subscription error:', error);
290+
phpbb.alert(subscribeButton.getAttribute('data-l-err'), error.message || subscribeButton.getAttribute('data-l-unsupported'));
291+
} finally {
292+
subscribeButton.addEventListener('click', subscribeButtonHandler);
293+
}
282294
}
283295

284296
/**

styles/prosilver/template/event/ucp_notifications_content_before.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<dl>
66
<dt><label for="subscribe_webpush">{{ lang('NOTIFY_WEBPUSH_NOTIFICATIONS') ~ lang('COLON') }}</label></dt>
77
<dd>
8-
<input id="subscribe_webpush" type="submit" name="subscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_ENABLE') }}" class="wpn button1 button button-form" data-l-err="{{ lang('INFORMATION') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED') }}" data-disabled-msg="{{ lang('NOTIFY_WEBPUSH_DISABLED') }}">
8+
<input id="subscribe_webpush" type="submit" name="subscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_ENABLE') }}" class="wpn button1 button button-form" data-l-err="{{ lang('INFORMATION')|e('html_attr') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED')|e('html_attr') }}" data-l-unsupported="{{ lang('NOTIFY_WEBPUSH_NOT_SUPPORTED')|e('html_attr') }}">
99
<input id="unsubscribe_webpush" type="submit" name="unsubscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_DISABLE') }}" class="wpn button1 button button-form hidden">
1010
<br><span>{{ lang('NOTIFY_WEBPUSH_ENABLE_EXPLAIN') }}</span>
1111
</dd>

0 commit comments

Comments
 (0)