mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/17343] Display push notifications in user language
PHPBB-17343
This commit is contained in:
parent
94112979a0
commit
767d9e1198
3 changed files with 52 additions and 13 deletions
|
@ -34,6 +34,8 @@ services:
|
||||||
- '@controller.helper'
|
- '@controller.helper'
|
||||||
- '@dbal.conn'
|
- '@dbal.conn'
|
||||||
- '@form_helper'
|
- '@form_helper'
|
||||||
|
- '@language'
|
||||||
|
- '@notification_manager'
|
||||||
- '@path_helper'
|
- '@path_helper'
|
||||||
- '@request'
|
- '@request'
|
||||||
- '@user'
|
- '@user'
|
||||||
|
|
|
@ -140,13 +140,7 @@ class webpush extends messenger_base implements extended_method_interface
|
||||||
{
|
{
|
||||||
$data = $notification->get_insert_array();
|
$data = $notification->get_insert_array();
|
||||||
$data += [
|
$data += [
|
||||||
'push_data' => json_encode([
|
'push_data' => json_encode(array_merge($notification->get_insert_array(), ['notification_type_name' => $notification->get_type()])),
|
||||||
'heading' => $this->config['sitename'],
|
|
||||||
'title' => strip_tags($notification->get_title()),
|
|
||||||
'text' => strip_tags($notification->get_reference()),
|
|
||||||
'url' => htmlspecialchars_decode($notification->get_url()),
|
|
||||||
'avatar' => $notification->get_avatar(),
|
|
||||||
]),
|
|
||||||
'notification_time' => time(),
|
'notification_time' => time(),
|
||||||
'push_token' => hash('sha256', random_bytes(32))
|
'push_token' => hash('sha256', random_bytes(32))
|
||||||
];
|
];
|
||||||
|
|
|
@ -19,6 +19,8 @@ use phpbb\db\driver\driver_interface;
|
||||||
use phpbb\exception\http_exception;
|
use phpbb\exception\http_exception;
|
||||||
use phpbb\form\form_helper;
|
use phpbb\form\form_helper;
|
||||||
use phpbb\json\sanitizer as json_sanitizer;
|
use phpbb\json\sanitizer as json_sanitizer;
|
||||||
|
use phpbb\language\language;
|
||||||
|
use phpbb\notification\manager;
|
||||||
use phpbb\path_helper;
|
use phpbb\path_helper;
|
||||||
use phpbb\request\request_interface;
|
use phpbb\request\request_interface;
|
||||||
use phpbb\symfony_request;
|
use phpbb\symfony_request;
|
||||||
|
@ -47,6 +49,12 @@ class webpush
|
||||||
/** @var form_helper */
|
/** @var form_helper */
|
||||||
protected $form_helper;
|
protected $form_helper;
|
||||||
|
|
||||||
|
/** @var language */
|
||||||
|
protected $language;
|
||||||
|
|
||||||
|
/** @var manager */
|
||||||
|
protected $notification_manager;
|
||||||
|
|
||||||
/** @var path_helper */
|
/** @var path_helper */
|
||||||
protected $path_helper;
|
protected $path_helper;
|
||||||
|
|
||||||
|
@ -72,6 +80,8 @@ class webpush
|
||||||
* @param controller_helper $controller_helper
|
* @param controller_helper $controller_helper
|
||||||
* @param driver_interface $db
|
* @param driver_interface $db
|
||||||
* @param form_helper $form_helper
|
* @param form_helper $form_helper
|
||||||
|
* @param language $language
|
||||||
|
* @param manager $notification_manager
|
||||||
* @param path_helper $path_helper
|
* @param path_helper $path_helper
|
||||||
* @param request_interface $request
|
* @param request_interface $request
|
||||||
* @param user $user
|
* @param user $user
|
||||||
|
@ -79,13 +89,15 @@ class webpush
|
||||||
* @param string $notification_webpush_table
|
* @param string $notification_webpush_table
|
||||||
* @param string $push_subscriptions_table
|
* @param string $push_subscriptions_table
|
||||||
*/
|
*/
|
||||||
public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
|
public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, language $language, manager $notification_manager,
|
||||||
request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table)
|
path_helper $path_helper, request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->controller_helper = $controller_helper;
|
$this->controller_helper = $controller_helper;
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
$this->form_helper = $form_helper;
|
$this->form_helper = $form_helper;
|
||||||
|
$this->language = $language;
|
||||||
|
$this->notification_manager = $notification_manager;
|
||||||
$this->path_helper = $path_helper;
|
$this->path_helper = $path_helper;
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
|
@ -147,7 +159,7 @@ class webpush
|
||||||
$notification_data = $this->db->sql_fetchfield('push_data');
|
$notification_data = $this->db->sql_fetchfield('push_data');
|
||||||
$this->db->sql_freeresult($result);
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
return $notification_data;
|
return $this->get_notification_data($notification_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -178,23 +190,54 @@ class webpush
|
||||||
$push_token = $notification_row['push_token'];
|
$push_token = $notification_row['push_token'];
|
||||||
|
|
||||||
// Check if passed push token is valid
|
// Check if passed push token is valid
|
||||||
$sql = 'SELECT user_form_salt
|
$sql = 'SELECT user_form_salt, user_lang
|
||||||
FROM ' . USERS_TABLE . '
|
FROM ' . USERS_TABLE . '
|
||||||
WHERE user_id = ' . (int) $user_id;
|
WHERE user_id = ' . (int) $user_id;
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $this->db->sql_query($sql);
|
||||||
$user_form_token = $this->db->sql_fetchfield('user_form_salt');
|
$row = $this->db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
|
$user_form_token = $row['user_form_salt'];
|
||||||
|
$user_lang = $row['user_lang'];
|
||||||
|
|
||||||
$expected_push_token = hash('sha256', $user_form_token . $push_token);
|
$expected_push_token = hash('sha256', $user_form_token . $push_token);
|
||||||
if ($expected_push_token === $token)
|
if ($expected_push_token === $token)
|
||||||
{
|
{
|
||||||
return $notification_data;
|
if ($user_lang !== $this->language->get_used_language())
|
||||||
|
{
|
||||||
|
$this->language->set_user_language($user_lang, true);
|
||||||
|
}
|
||||||
|
return $this->get_notification_data($notification_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new http_exception(Response::HTTP_FORBIDDEN, 'NO_AUTH_OPERATION');
|
throw new http_exception(Response::HTTP_FORBIDDEN, 'NO_AUTH_OPERATION');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function get_notification_data(string $notification_data): string
|
||||||
|
{
|
||||||
|
$row_data = json_decode($notification_data, true);
|
||||||
|
|
||||||
|
// Old notification data is pre-parsed and just needs to be returned
|
||||||
|
if (isset($row_data['heading']))
|
||||||
|
{
|
||||||
|
return $notification_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get notification from row_data
|
||||||
|
$notification = $this->notification_manager->get_item_type_class($row_data['notification_type_name'], $row_data);
|
||||||
|
|
||||||
|
$notification_data = [
|
||||||
|
'heading' => $this->config['sitename'],
|
||||||
|
'title' => strip_tags($notification->get_title()),
|
||||||
|
'text' => strip_tags($notification->get_reference()),
|
||||||
|
'url' => htmlspecialchars_decode($notification->get_url()),
|
||||||
|
'avatar' => $notification->get_avatar(),
|
||||||
|
];
|
||||||
|
|
||||||
|
return json_encode($notification_data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle request to push worker javascript
|
* Handle request to push worker javascript
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue