Skip to content

Commit 47bd87c

Browse files
authored
Merge pull request #10 from rxu/add-subscription_migrations
Add migration to handle webpush subscriptions on extension purge.
2 parents e31cff3 + 5ecc737 commit 47bd87c

File tree

12 files changed

+281
-76
lines changed

12 files changed

+281
-76
lines changed

acp/wpn_acp_module.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public function main($id, $mode)
101101
public function display_settings()
102102
{
103103
$this->template->assign_vars([
104-
'S_WEBPUSH_ENABLE' => $this->config['webpush_enable'],
105-
'WEBPUSH_VAPID_PUBLIC' => $this->config['webpush_vapid_public'],
106-
'WEBPUSH_VAPID_PRIVATE' => $this->config['webpush_vapid_private'],
104+
'S_WEBPUSH_ENABLE' => $this->config['wpn_webpush_enable'],
105+
'WEBPUSH_VAPID_PUBLIC' => $this->config['wpn_webpush_vapid_public'],
106+
'WEBPUSH_VAPID_PRIVATE' => $this->config['wpn_webpush_vapid_private'],
107107
'U_ACTION' => $this->u_action,
108108
]);
109109
}
@@ -117,12 +117,12 @@ public function save_settings()
117117
{
118118
$config_array = $this->request->variable('config', ['' => ''], true);
119119
$display_settings = [
120-
'webpush_enable' => ['validate' => 'bool'],
121-
'webpush_vapid_public' => ['validate' => 'string:25:255', 'lang' => 'WEBPUSH_VAPID_PUBLIC'],
122-
'webpush_vapid_private'=> ['validate' => 'string:25:255', 'lang' => 'WEBPUSH_VAPID_PRIVATE'],
120+
'wpn_webpush_enable' => ['validate' => 'bool'],
121+
'wpn_webpush_vapid_public' => ['validate' => 'string:25:255', 'lang' => 'WEBPUSH_VAPID_PUBLIC'],
122+
'wpn_webpush_vapid_private'=> ['validate' => 'string:25:255', 'lang' => 'WEBPUSH_VAPID_PRIVATE'],
123123
];
124124

125-
if ($config_array['webpush_enable'])
125+
if ($config_array['wpn_webpush_enable'])
126126
{
127127
// Validate config values
128128
validate_config_vars($display_settings, $config_array, $this->errors);
@@ -140,9 +140,9 @@ public function save_settings()
140140

141141
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CONFIG_WEBPUSH');
142142

143-
$this->config->set('webpush_enable', $config_array['webpush_enable']);
144-
$this->config->set('webpush_vapid_public', $config_array['webpush_vapid_public']);
145-
$this->config->set('webpush_vapid_private', $config_array['webpush_vapid_private']);
143+
$this->config->set('wpn_webpush_enable', $config_array['wpn_webpush_enable']);
144+
$this->config->set('wpn_webpush_vapid_public', $config_array['wpn_webpush_vapid_public']);
145+
$this->config->set('wpn_webpush_vapid_private', $config_array['wpn_webpush_vapid_private']);
146146

147147
trigger_error($this->lang->lang('CONFIG_UPDATED') . adm_back_link($this->u_action), E_USER_NOTICE);
148148
}

adm/style/wpn_acp_settings.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ <h3>{{ lang('WARNING') }}</h3>
1717
<legend>{{ lang('GENERAL_SETTINGS') }}</legend>
1818
<dl>
1919
<dt><label>{{ lang('WEBPUSH_ENABLE') ~ lang('COLON') }}</label><br><span>{{ lang('WEBPUSH_ENABLE_EXPLAIN') }}</span></dt>
20-
<dd><label><input type="radio" class="radio" name="config[webpush_enable]" value="1" {% if S_WEBPUSH_ENABLE %}checked="checked"{% endif %}> {{ lang('YES') }}</label>
21-
<label><input type="radio" class="radio" name="config[webpush_enable]" value="0"{% if not S_WEBPUSH_ENABLE %} checked="checked"{% endif %}> {{ lang('NO') }}</label>
22-
<input class="button2" data-ajax="generate_vapid_keys" type="button" name="config[webpush_enable]" value="{{ lang('WEBPUSH_GENERATE_VAPID_KEYS') }}"></dd>
20+
<dd><label><input type="radio" class="radio" name="config[wpn_webpush_enable]" value="1" {% if S_WEBPUSH_ENABLE %}checked="checked"{% endif %}> {{ lang('YES') }}</label>
21+
<label><input type="radio" class="radio" name="config[wpn_webpush_enable]" value="0"{% if not S_WEBPUSH_ENABLE %} checked="checked"{% endif %}> {{ lang('NO') }}</label>
22+
<input class="button2" data-ajax="generate_vapid_keys" type="button" name="config[wpn_webpush_enable]" value="{{ lang('WEBPUSH_GENERATE_VAPID_KEYS') }}"></dd>
2323
</dl>
2424
<dl>
2525
<dt><label for="webpush_vapid_public">{{ lang('WEBPUSH_VAPID_PUBLIC') ~ lang('COLON') }}</label><br><span>{{ lang('WEBPUSH_VAPID_PUBLIC_EXPLAIN') }}</span></dt>
26-
<dd><label><input id="webpush_vapid_public" type="text" name="config[webpush_vapid_public]" size="25" maxlength="255" value="{{ WEBPUSH_VAPID_PUBLIC }}"></label></dd>
26+
<dd><label><input id="webpush_vapid_public" type="text" name="config[wpn_webpush_vapid_public]" size="25" maxlength="255" value="{{ WEBPUSH_VAPID_PUBLIC }}"></label></dd>
2727
</dl>
2828
<dl>
2929
<dt><label for="webpush_vapid_private">{{ lang('WEBPUSH_VAPID_PRIVATE') ~ lang('COLON') }}</label><br><span>{{ lang('WEBPUSH_VAPID_PRIVATE_EXPLAIN') }}</span></dt>
30-
<dd><label><input id="webpush_vapid_private" type="password" name="config[webpush_vapid_private]" size="25" maxlength="255" autocomplete="off" value="{{ WEBPUSH_VAPID_PRIVATE }}"></label></dd>
30+
<dd><label><input id="webpush_vapid_private" type="password" name="config[wpn_webpush_vapid_private]" size="25" maxlength="255" autocomplete="off" value="{{ WEBPUSH_VAPID_PRIVATE }}"></label></dd>
3131
</dl>
3232
</fieldset>
3333
<fieldset class="submit-buttons">

config/services.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
tags:
2020
- { name: event.listener }
2121

22-
phpbb.wpn.notification.method.webpush:
22+
notification.method.phpbb.wpn.webpush:
2323
class: phpbb\webpushnotifications\notification\method\webpush
2424
shared: false
2525
arguments:
@@ -38,7 +38,6 @@ services:
3838
phpbb.wpn.ucp.controller.webpush:
3939
class: phpbb\webpushnotifications\ucp\controller\webpush
4040
arguments:
41-
- '@config'
4241
- '@controller.helper'
4342
- '@dbal.conn'
4443
- '@phpbb.wpn.form_helper'

event/listener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __construct(controller_helper $controller_helper, form_helper $f
6767
*/
6868
public function load_template_data($event)
6969
{
70-
if ($event['method_data']['id'] == 'phpbb.wpn.notification.method.webpush')
70+
if ($event['method_data']['id'] == 'notification.method.phpbb.wpn.webpush')
7171
{
7272
$template_ary = $event['method_data']['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
7373
$this->template->assign_vars($template_ary);

language/en/webpushnotifications_module_ucp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//
3939

4040
$lang = array_merge($lang, [
41-
'PHPBB_WPN_NOTIFICATION_METHOD_WEBPUSH' => 'Web Push',
41+
'NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH' => 'Web Push',
4242
'NOTIFY_WEBPUSH_ENABLE' => 'Enable receiving Web Push notifications',
4343
'NOTIFY_WEBPUSH_ENABLE_EXPLAIN' => 'Enable receiving browser-based push notifications.<br>The notifications can be turned off at any time in your browser settings, by unsubscribing, or by disabling the push notifications below.',
4444
'NOTIFY_WEBPUSH_SUBSCRIBE' => 'Subscribe',

migrations/add_webpush.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function effectively_installed(): bool
1919
return $this->db_tools->sql_table_exists($this->table_prefix . 'wpn_notification_push');
2020
}
2121

22+
static public function depends_on()
23+
{
24+
return ['\phpbb\webpushnotifications\migrations\handle_subscriptions'];
25+
}
26+
2227
public function update_schema(): array
2328
{
2429
return [
@@ -62,9 +67,9 @@ public function revert_schema(): array
6267
public function update_data(): array
6368
{
6469
return [
65-
['config.add', ['webpush_enable', false]],
66-
['config.add', ['webpush_vapid_public', '']],
67-
['config.add', ['webpush_vapid_private', '']],
70+
['config.add', ['wpn_webpush_enable', false]],
71+
['config.add', ['wpn_webpush_vapid_public', '']],
72+
['config.add', ['wpn_webpush_vapid_private', '']],
6873
['module.add', ['acp', 'ACP_CLIENT_COMMUNICATION', [
6974
'module_basename' => '\phpbb\webpushnotifications\acp\wpn_acp_module',
7075
'module_langname' => 'ACP_WEBPUSH_SETTINGS',
@@ -77,9 +82,9 @@ public function update_data(): array
7782
public function revert_data(): array
7883
{
7984
return [
80-
['config.remove', ['webpush_enable']],
81-
['config.remove', ['webpush_vapid_public']],
82-
['config.remove', ['webpush_vapid_private']],
85+
['config.remove', ['wpn_webpush_enable']],
86+
['config.remove', ['wpn_webpush_vapid_public']],
87+
['config.remove', ['wpn_webpush_vapid_private']],
8388
['module.remove', ['acp', 'ACP_BOARD_CONFIGURATION', 'ACP_WEBPUSH_SETTINGS']]
8489
];
8590
}

migrations/handle_subscriptions.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
*
4+
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2023, phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\webpushnotifications\migrations;
12+
13+
use phpbb\db\migration\migration;
14+
15+
class handle_subscriptions extends migration
16+
{
17+
public function effectively_installed()
18+
{
19+
return !$this->db_tools->sql_table_exists($this->table_prefix . 'wpn_notification_push');
20+
}
21+
22+
public function revert_data(): array
23+
{
24+
return [
25+
['custom', [[$this, 'update_subscriptions']]],
26+
['if', [
27+
($this->db_tools->sql_table_exists($this->table_prefix . 'notification_push')),
28+
['custom', [[$this, 'copy_subscription_tables']]],
29+
]],
30+
['if', [
31+
(isset($this->config['webpush_enable'])),
32+
['config.update', ['webpush_enable', $this->config['wpn_webpush_enable']]],
33+
]],
34+
['if', [
35+
(isset($this->config['webpush_vapid_public']) && empty($this->config['webpush_vapid_public'])),
36+
['config.update', ['webpush_vapid_public', $this->config['wpn_webpush_vapid_public']]],
37+
]],
38+
['if', [
39+
(isset($this->config['webpush_vapid_private']) && empty($this->config['webpush_vapid_private'])),
40+
['config.update', ['webpush_vapid_private', $this->config['wpn_webpush_vapid_private']]],
41+
]],
42+
];
43+
}
44+
45+
/*
46+
* For phpBB 4.0 with core webpush notifications update notification method
47+
* from extension's notification.method.phpbb.wpn.webpush to the core one
48+
* notification.method.webpush otherwise remove notification method from
49+
* user notifications table on the extension purge.
50+
*/
51+
public function update_subscriptions()
52+
{
53+
$user_notifications_table = $this->table_prefix . 'user_notifications';
54+
55+
// Check if webpush notification method exists in phpBB core (as of phpBB 4.0)
56+
$core_webpush_exists = $this->db_tools->sql_table_exists($this->table_prefix . 'notification_push');
57+
58+
/*
59+
* If webpush notification method exists in phpBB core,
60+
* keep all subscriptions by just renaming notification method.
61+
* Otherwise remove all subscriptions
62+
*/
63+
$sql = $core_webpush_exists ?
64+
'UPDATE ' . $user_notifications_table . "
65+
SET method = '" . $this->db->sql_escape('notification.method.webpush') . "'
66+
WHERE method = '" . $this->db->sql_escape('notification.method.phpbb.wpn.webpush') . "'" :
67+
68+
'DELETE FROM ' . $user_notifications_table . "
69+
WHERE method = '" . $this->db->sql_escape('notification.method.phpbb.wpn.webpush') . "'";
70+
71+
$this->db->sql_query($sql);
72+
}
73+
74+
/*
75+
* For phpBB 4.0 with core webpush notifications copy all
76+
* webpush subscriptions data from extension's tables to the core ones
77+
* on the extension purge.
78+
*/
79+
public function copy_subscription_tables()
80+
{
81+
$core_notification_push_table = $this->table_prefix . 'notification_push';
82+
$core_push_subscriptions_table = $this->table_prefix . 'push_subscriptions';
83+
84+
$wpn_notification_push_table = $this->table_prefix . 'wpn_notification_push';
85+
$wpn_push_subscriptions_table = $this->table_prefix . 'wpn_push_subscriptions';
86+
87+
/*
88+
* If webpush notification method exists in phpBB core,
89+
* copy all subscriptions data over the corresponding core tables.
90+
*/
91+
foreach ([
92+
$core_notification_push_table => $wpn_notification_push_table,
93+
$core_push_subscriptions_table => $wpn_push_subscriptions_table
94+
] as $core_table => $ext_table)
95+
{
96+
$sql = 'INSERT INTO ' . $core_table . '
97+
SELECT * FROM ' . $ext_table;
98+
$this->db->sql_query($sql);
99+
}
100+
}
101+
}

notification/method/webpush.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ public function __construct(config $config, driver_interface $db, log_interface
7878
*/
7979
public function get_type(): string
8080
{
81-
return 'phpbb.wpn.notification.method.webpush';
81+
return 'notification.method.phpbb.wpn.webpush';
8282
}
8383

8484
/**
8585
* {@inheritDoc}
8686
*/
8787
public function is_available(type_interface $notification_type = null): bool
8888
{
89-
return parent::is_available($notification_type) && $this->config['webpush_enable']
90-
&& !empty($this->config['webpush_vapid_public']) && !empty($this->config['webpush_vapid_private']);
89+
return parent::is_available($notification_type) && $this->config['wpn_webpush_enable']
90+
&& !empty($this->config['wpn_webpush_vapid_public']) && !empty($this->config['wpn_webpush_vapid_private']);
9191
}
9292

9393
/**
@@ -181,8 +181,8 @@ protected function notify_using_webpush(): void
181181
$auth = [
182182
'VAPID' => [
183183
'subject' => generate_board_url(false),
184-
'publicKey' => $this->config['webpush_vapid_public'],
185-
'privateKey' => $this->config['webpush_vapid_private'],
184+
'publicKey' => $this->config['wpn_webpush_vapid_public'],
185+
'privateKey' => $this->config['wpn_webpush_vapid_private'],
186186
],
187187
];
188188

@@ -341,7 +341,7 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
341341
'NOTIFICATIONS_WEBPUSH_ENABLE' => true,
342342
'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
343343
'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
344-
'VAPID_PUBLIC_KEY' => $this->config['webpush_vapid_public'],
344+
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
345345
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
346346
'SUBSCRIPTIONS' => $subscriptions,
347347
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),

tests/functional/functional_test.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function test_acp_module()
4545
$this->assertContainsLang('WEBPUSH_GENERATE_VAPID_KEYS', $crawler->filter('input[type="button"]')->attr('value'));
4646

4747
$form_data = [
48-
'config[webpush_enable]' => 1,
49-
'config[webpush_vapid_public]' => 'BDnYSJHVZBxq834LqDGr893IfazEez7q-jYH2QBNlT0ji2C9UwGosiqz8Dp_ZN23lqAngBZyRjXVWF4ZLA8X2zI',
50-
'config[webpush_vapid_private]' => 'IE5OYlmfWsMbBU1lzvr0bxrxVAXIteSkAnwGlZIhmRk',
48+
'config[wpn_webpush_enable]' => 1,
49+
'config[wpn_webpush_vapid_public]' => 'BDnYSJHVZBxq834LqDGr893IfazEez7q-jYH2QBNlT0ji2C9UwGosiqz8Dp_ZN23lqAngBZyRjXVWF4ZLA8X2zI',
50+
'config[wpn_webpush_vapid_private]' => 'IE5OYlmfWsMbBU1lzvr0bxrxVAXIteSkAnwGlZIhmRk',
5151
];
5252
$form = $crawler->selectButton('submit')->form($form_data);
5353
$crawler = self::submit($form);
@@ -70,7 +70,6 @@ public function test_ucp_module()
7070
$crawler = self::request('GET', 'ucp.php?i=ucp_notifications&mode=notification_options');
7171

7272
$this->assertContainsLang('NOTIFY_WEBPUSH_ENABLE', $crawler->filter('label[for="subscribe_webpush"]')->text());
73-
$this->assertContainsLang('PHPBB_WPN_NOTIFICATION_METHOD_WEBPUSH', $crawler->filter('th.mark')->eq(2)->text());
73+
$this->assertContainsLang('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH', $crawler->filter('th.mark')->eq(2)->text());
7474
}
75-
7675
}

0 commit comments

Comments
 (0)