Skip to content

Commit 12f8a1f

Browse files
committed
Declined state should be stored per device / browser, not globally
1 parent a8ff043 commit 12f8a1f

File tree

9 files changed

+10
-135
lines changed

9 files changed

+10
-135
lines changed

config/wpn_ucp.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ phpbb_webpushnotifications_ucp_push_subscribe_controller:
1313
phpbb_webpushnotifications_ucp_push_unsubscribe_controller:
1414
path: /push/unsubscribe
1515
defaults: { _controller: phpbb.wpn.ucp.controller.webpush:unsubscribe }
16-
17-
phpbb_webpushnotifications_ucp_push_decline_popup_controller:
18-
path: /push/decline-popup
19-
defaults: { _controller: phpbb.wpn.ucp.controller.webpush:decline_popup }

migrations/add_popup_prompt.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,10 @@ public static function depends_on()
2424
return ['\phpbb\webpushnotifications\migrations\add_acp_configs'];
2525
}
2626

27-
public function update_schema(): array
28-
{
29-
return [
30-
'add_columns' => [
31-
$this->table_prefix . 'users' => [
32-
'user_wpn_popup_declined' => ['BOOL', 0],
33-
],
34-
],
35-
];
36-
}
37-
38-
public function revert_schema(): array
39-
{
40-
return [
41-
'drop_columns' => [
42-
$this->table_prefix . 'users' => [
43-
'user_wpn_popup_declined',
44-
],
45-
],
46-
];
47-
}
48-
4927
public function update_data(): array
5028
{
5129
return [
5230
['config.add', ['wpn_webpush_popup_prompt', false]],
5331
];
5432
}
55-
56-
public function revert_data(): array
57-
{
58-
return [
59-
['config.remove', ['wpn_webpush_popup_prompt']],
60-
];
61-
}
6233
}

notification/method/webpush.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,11 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
396396
'NOTIFICATIONS_WEBPUSH_ENABLE' => ($this->config['load_notifications'] && $this->config['allow_board_notifications'] && $this->config['wpn_webpush_dropdown_subscribe']) || stripos($this->user->page['page'], 'notification_options'),
397397
'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
398398
'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
399-
'U_WEBPUSH_DECLINE_POPUP' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_decline_popup_controller'),
400399
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
401400
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
402401
'SUBSCRIPTIONS' => $subscriptions,
403402
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
404-
'S_WEBPUSH_POPUP_PROMPT' => $this->config['wpn_webpush_popup_prompt'] && !$this->user->data['user_wpn_popup_declined'] && $this->user->id() != ANONYMOUS && $this->user->data['user_type'] != USER_IGNORE,
403+
'S_WEBPUSH_POPUP_PROMPT' => $this->config['wpn_webpush_popup_prompt'] && $this->user->id() != ANONYMOUS && $this->user->data['user_type'] != USER_IGNORE,
405404
];
406405
}
407406

styles/all/template/ucp_notifications_webpush.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
44
subscribeUrl: '{{ U_WEBPUSH_SUBSCRIBE }}',
55
unsubscribeUrl: '{{ U_WEBPUSH_UNSUBSCRIBE }}',
6-
declinePopupUrl: '{{ U_WEBPUSH_DECLINE_POPUP }}',
76
ajaxErrorTitle: '{{ lang('AJAX_ERROR_TITLE') }}',
87
vapidPublicKey: '{{ VAPID_PUBLIC_KEY }}',
98
formTokens: {

styles/all/template/webpush.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ function PhpbbWebpush() {
3333
/** @type {HTMLElement} Unsubscribe button */
3434
let unsubscribeButton;
3535

36-
/** @type {string} URL to decline popup */
37-
let declinePopupUrl = '';
36+
3837

3938
/**
4039
* Init function for phpBB Web Push
@@ -44,7 +43,6 @@ function PhpbbWebpush() {
4443
serviceWorkerUrl = options.serviceWorkerUrl;
4544
subscribeUrl = options.subscribeUrl;
4645
unsubscribeUrl = options.unsubscribeUrl;
47-
declinePopupUrl = options.declinePopupUrl;
4846
this.formTokens = options.formTokens;
4947
subscriptions = options.subscriptions;
5048
ajaxErrorTitle = options.ajaxErrorTitle;
@@ -130,6 +128,11 @@ function PhpbbWebpush() {
130128
return;
131129
}
132130

131+
// Check if user declined on this browser
132+
if (localStorage.getItem('wpn_popup_declined') === 'true') {
133+
return;
134+
}
135+
133136
// Check if this browser already has a subscription
134137
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
135138
.then(registration => {
@@ -174,16 +177,10 @@ function PhpbbWebpush() {
174177
}
175178

176179
/**
177-
* Send decline popup request
180+
* Store decline in localStorage
178181
*/
179182
function declinePopup() {
180-
fetch(declinePopupUrl, {
181-
method: 'POST',
182-
headers: {
183-
'X-Requested-With': 'XMLHttpRequest',
184-
},
185-
body: getFormData({}),
186-
}).catch(error => console.info(error));
183+
localStorage.setItem('wpn_popup_declined', 'true');
187184
}
188185

189186
/**
@@ -328,6 +325,7 @@ function PhpbbWebpush() {
328325
if ('form_tokens' in response) {
329326
updateFormTokens(response.form_tokens);
330327
}
328+
localStorage.removeItem('wpn_popup_declined');
331329
const popup = document.getElementById('wpn_popup_prompt');
332330
if (popup) {
333331
popup.style.display = 'none';

tests/controller/controller_webpush_test.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -420,62 +420,6 @@ public function test_sub_unsubscribe_success()
420420
$this->assertEmpty($this->get_subscription_data());
421421
}
422422

423-
public function test_decline_popup_success()
424-
{
425-
$this->form_helper->method('check_form_tokens')->willReturn(true);
426-
$this->request->method('is_ajax')->willReturn(true);
427-
$this->user->data['user_id'] = 2;
428-
$this->user->data['is_bot'] = false;
429-
$this->user->data['user_type'] = USER_NORMAL;
430-
431-
$response = $this->controller->decline_popup();
432-
433-
$this->assertInstanceOf(JsonResponse::class, $response);
434-
$this->assertEquals(['success' => true], json_decode($response->getContent(), true));
435-
436-
$sql = 'SELECT user_wpn_popup_declined FROM phpbb_users WHERE user_id = 2';
437-
$result = $this->db->sql_query($sql);
438-
$declined = $this->db->sql_fetchfield('user_wpn_popup_declined');
439-
$this->db->sql_freeresult($result);
440-
441-
$this->assertEquals(1, $declined);
442-
}
443-
444-
public function test_subscribe_clears_declined_status()
445-
{
446-
$this->form_helper->method('check_form_tokens')->willReturn(true);
447-
$this->request->method('is_ajax')->willReturn(true);
448-
$this->user->data['user_id'] = 2;
449-
$this->user->data['is_bot'] = false;
450-
$this->user->data['user_type'] = USER_NORMAL;
451-
452-
// First decline
453-
$this->controller->decline_popup();
454-
455-
$sql = 'SELECT user_wpn_popup_declined FROM phpbb_users WHERE user_id = 2';
456-
$result = $this->db->sql_query($sql);
457-
$declined = $this->db->sql_fetchfield('user_wpn_popup_declined');
458-
$this->db->sql_freeresult($result);
459-
$this->assertEquals(1, $declined);
460-
461-
// Then subscribe
462-
$symfony_request = $this->createMock(\phpbb\symfony_request::class);
463-
$symfony_request->method('get')->willReturn(json_encode([
464-
'endpoint' => 'test_endpoint',
465-
'expiration_time' => 0,
466-
'keys' => ['p256dh' => 'test_p256dh', 'auth' => 'test_auth']
467-
]));
468-
469-
$this->controller->subscribe($symfony_request);
470-
471-
// Check declined status is cleared
472-
$sql = 'SELECT user_wpn_popup_declined FROM phpbb_users WHERE user_id = 2';
473-
$result = $this->db->sql_query($sql);
474-
$declined = $this->db->sql_fetchfield('user_wpn_popup_declined');
475-
$this->db->sql_freeresult($result);
476-
$this->assertEquals(0, $declined);
477-
}
478-
479423
private function get_subscription_data()
480424
{
481425
$sql = 'SELECT *

tests/controller/fixtures/webpush.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,26 @@
4848
<column>user_permissions</column>
4949
<column>user_sig</column>
5050
<column>user_form_salt</column>
51-
<column>user_wpn_popup_declined</column>
5251
<row>
5352
<value>1</value>
5453
<value>Anonymous</value>
5554
<value></value>
5655
<value></value>
5756
<value></value>
58-
<value>0</value>
5957
</row>
6058
<row>
6159
<value>2</value>
6260
<value>poster</value>
6361
<value></value>
6462
<value></value>
6563
<value>sua0m6f0ektt1g9z</value>
66-
<value>0</value>
6764
</row>
6865
<row>
6966
<value>3</value>
7067
<value>test</value>
7168
<value></value>
7269
<value></value>
7370
<value>abcdef</value>
74-
<value>0</value>
7571
</row>
7672
</table>
7773
</dataset>

tests/functional/functional_test.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,6 @@ public function test_popup_prompt()
160160
$this->assertContainsLang('NOTIFY_WEBPUSH_POPUP_MESSAGE', $crawler->filter('.wpn-popup-message')->text());
161161
$this->assertContainsLang('NOTIFY_WEBPUSH_POPUP_ALLOW', $crawler->filter('#wpn_popup_allow')->text());
162162
$this->assertContainsLang('NOTIFY_WEBPUSH_POPUP_DECLINE', $crawler->filter('#wpn_popup_decline')->text());
163-
164-
// Test decline popup route exists
165-
$this->add_lang('common');
166-
self::request('POST', 'app.php/user/push/decline-popup', [], false);
167-
$response = json_decode(self::get_content(), true);
168-
$this->assertArrayHasKey('success', $response);
169163
}
170164

171165
protected function set_acp_option($option, $value)

ucp/controller/webpush.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,6 @@ public function subscribe(symfony_request $symfony_request): JsonResponse
321321
]);
322322
$this->db->sql_query($sql);
323323

324-
$sql = 'UPDATE ' . USERS_TABLE . '
325-
SET user_wpn_popup_declined = 0
326-
WHERE user_id = ' . (int) $this->user->id();
327-
$this->db->sql_query($sql);
328-
329324
return new JsonResponse([
330325
'success' => true,
331326
'form_tokens' => $this->form_helper->get_form_tokens(self::FORM_TOKEN_UCP),
@@ -357,23 +352,6 @@ public function unsubscribe(symfony_request $symfony_request): JsonResponse
357352
]);
358353
}
359354

360-
/**
361-
* Handle decline popup requests
362-
*
363-
* @return JsonResponse
364-
*/
365-
public function decline_popup(): JsonResponse
366-
{
367-
$this->check_subscribe_requests();
368-
369-
$sql = 'UPDATE ' . USERS_TABLE . '
370-
SET user_wpn_popup_declined = 1
371-
WHERE user_id = ' . (int) $this->user->id();
372-
$this->db->sql_query($sql);
373-
374-
return new JsonResponse(['success' => true]);
375-
}
376-
377355
/**
378356
* Takes an avatar string (usually in full html format already) and extracts the url.
379357
* If the avatar url is a relative path, it's converted to an absolute path.

0 commit comments

Comments
 (0)