[ticket/17010] Add new interface and create template data in type

PHPBB3-17010
This commit is contained in:
Marc Alexander 2023-11-05 12:56:39 +01:00
parent fa91bf791f
commit 8d9a7aa62c
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
4 changed files with 89 additions and 34 deletions

View file

@ -14,6 +14,11 @@
/** /**
* @ignore * @ignore
*/ */
use phpbb\controller\helper;
use phpbb\form\form_helper;
use phpbb\notification\method\extended_method_interface;
if (!defined('IN_PHPBB')) if (!defined('IN_PHPBB'))
{ {
exit; exit;
@ -23,17 +28,29 @@ class ucp_notifications
{ {
public $u_action; public $u_action;
private const FORM_TOKEN_NAME = 'ucp_notification';
/** @var helper */
private helper $controller_helper;
/** @var form_helper */
private form_helper $form_helper;
public function main($id, $mode) public function main($id, $mode)
{ {
global $config, $template, $user, $request, $phpbb_container, $phpbb_dispatcher; global $config, $template, $user, $request, $phpbb_container, $phpbb_dispatcher;
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx;
add_form_key('ucp_notification'); add_form_key(self::FORM_TOKEN_NAME);
$start = $request->variable('start', 0); $start = $request->variable('start', 0);
$form_time = $request->variable('form_time', 0); $form_time = $request->variable('form_time', 0);
$form_time = ($form_time <= 0 || $form_time > time()) ? time() : $form_time; $form_time = ($form_time <= 0 || $form_time > time()) ? time() : $form_time;
$this->controller_helper = $phpbb_container->get('controller.helper');
$this->form_helper = $phpbb_container->get('form_helper');
/* @var $phpbb_notifications \phpbb\notification\manager */ /* @var $phpbb_notifications \phpbb\notification\manager */
$phpbb_notifications = $phpbb_container->get('notification_manager'); $phpbb_notifications = $phpbb_container->get('notification_manager');
@ -48,7 +65,7 @@ class ucp_notifications
// Add/remove subscriptions // Add/remove subscriptions
if ($request->is_set_post('submit')) if ($request->is_set_post('submit'))
{ {
if (!check_form_key('ucp_notification')) if (!check_form_key(self::FORM_TOKEN_NAME))
{ {
trigger_error('FORM_INVALID'); trigger_error('FORM_INVALID');
} }
@ -103,15 +120,12 @@ class ucp_notifications
trigger_error($message); trigger_error($message);
} }
$this->output_notification_methods($phpbb_notifications, $template, $user, 'notification_methods'); $this->output_notification_methods($phpbb_notifications, $template, $user);
$this->output_notification_types($subscriptions, $phpbb_notifications, $template, $user, $phpbb_dispatcher, 'notification_types'); $this->output_notification_types($subscriptions, $phpbb_notifications, $template, $user, $phpbb_dispatcher, 'notification_types');
/** @var \phpbb\controller\helper $controller_helper */
$controller_helper = $phpbb_container->get('controller.helper');
$template->assign_vars([ $template->assign_vars([
'T_WEBPUSH_JS_PATH' => $controller_helper->route('phpbb_ucp_push_js_controller'), 'FORM_TOKENS' => $this->form_helper->get_form_tokens(self::FORM_TOKEN_NAME),
]); ]);
$this->tpl_name = 'ucp_notifications_options'; $this->tpl_name = 'ucp_notifications_options';
@ -145,7 +159,7 @@ class ucp_notifications
// Mark specific notifications read // Mark specific notifications read
if ($request->is_set_post('submit')) if ($request->is_set_post('submit'))
{ {
if (!check_form_key('ucp_notification')) if (!check_form_key(self::FORM_TOKEN_NAME))
{ {
trigger_error('FORM_INVALID'); trigger_error('FORM_INVALID');
} }
@ -273,35 +287,18 @@ class ucp_notifications
{ {
$notification_methods = $phpbb_notifications->get_subscription_methods(); $notification_methods = $phpbb_notifications->get_subscription_methods();
if (isset($notification_methods['notification.method.webpush'])) foreach ($notification_methods as $method_data)
{ {
$this->output_webpush_data($template); if ($method_data['method'] instanceof extended_method_interface)
} {
$ucp_template_data = $method_data['method']->get_ucp_template_data($this->controller_helper, $this->form_helper);
$template->assign_vars($ucp_template_data);
}
foreach ($notification_methods as $method => $method_data)
{
$template->assign_block_vars($block, array( $template->assign_block_vars($block, array(
'METHOD' => $method_data['id'], 'METHOD' => $method_data['id'],
'NAME' => $user->lang($method_data['lang']), 'NAME' => $user->lang($method_data['lang']),
)); ));
} }
} }
/**
* Output data for webpush
*
* @param \phpbb\template\template $template
*
* @return void
*/
protected function output_webpush_data(\phpbb\template\template $template): void
{
global $config;
$template->assign_vars([
'NOTIFICATIONS_WEBPUSH_ENABLE' => true, // already checked, otherwise we wouldn't be here
'NOTIFICATIONS_WEBPUSH_VAPID_PUBLIC' => $config['webpush_vapid_public'],
]);
}
} }

View file

@ -0,0 +1,29 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\notification\method;
use phpbb\controller\helper;
use phpbb\form\form_helper;
interface extended_method_interface extends method_interface
{
/**
* Get UCP template data for type
*
* @param helper $controller_helper
* @param form_helper $form_helper
* @return array Template data
*/
public function get_ucp_template_data(helper $controller_helper, form_helper $form_helper): array;
}

View file

@ -15,7 +15,9 @@ namespace phpbb\notification\method;
use Minishlink\WebPush\Subscription; use Minishlink\WebPush\Subscription;
use phpbb\config\config; use phpbb\config\config;
use phpbb\controller\helper;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\form\form_helper;
use phpbb\log\log_interface; use phpbb\log\log_interface;
use phpbb\notification\type\type_interface; use phpbb\notification\type\type_interface;
use phpbb\user; use phpbb\user;
@ -26,7 +28,7 @@ use phpbb\user_loader;
* This class handles sending push messages for notifications * This class handles sending push messages for notifications
*/ */
class webpush extends messenger_base class webpush extends messenger_base implements extended_method_interface
{ {
/** @var config */ /** @var config */
protected $config; protected $config;
@ -320,6 +322,33 @@ class webpush extends messenger_base
return array_intersect_key($data, $row); return array_intersect_key($data, $row);
} }
public function get_ucp_template_data(helper $controller_helper, form_helper $form_helper): array
{
$subscription_map = $this->get_user_subscription_map([$this->user->id()]);
$subscriptions = [];
if (isset($subscription_map[$this->user->id()]))
{
foreach ($subscription_map[$this->user->id()] as $subscription)
{
$subscriptions[] = [
'endpoint' => $subscription['endpoint'],
'expirationTime' => $subscription['expiration_time'],
];
}
}
return [
'NOTIFICATIONS_WEBPUSH_ENABLE' => true,
'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_ucp_push_subscribe_controller'),
'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_ucp_push_unsubscribe_controller'),
'VAPID_PUBLIC_KEY' => $this->config['webpush_vapid_public'],
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_ucp_push_worker_controller'),
'SUBSCRIPTIONS' => $subscriptions,
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\ucp\controller\webpush::FORM_TOKEN_UCP),
];
}
/** /**
* Get subscriptions for notify users * Get subscriptions for notify users
* *
@ -332,7 +361,7 @@ class webpush extends messenger_base
// Get subscriptions for users // Get subscriptions for users
$user_subscription_map = []; $user_subscription_map = [];
$sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth $sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth, expiration_time
FROM ' . $this->push_subscriptions_table . ' FROM ' . $this->push_subscriptions_table . '
WHERE ' . $this->db->sql_in_set('user_id', $notify_users); WHERE ' . $this->db->sql_in_set('user_id', $notify_users);
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);

View file

@ -33,7 +33,7 @@ use Twig\Error\SyntaxError;
class webpush class webpush
{ {
/** @var string UCP form token name */ /** @var string UCP form token name */
private const FORM_TOKEN_UCP = 'ucp_webpush'; public const FORM_TOKEN_UCP = 'ucp_webpush';
/** @var config */ /** @var config */
protected $config; protected $config;