Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ticket/17314] Parse avatars for push notifications #6614

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpBB/config/default/container/services_notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ services:
- '@log'
- '@user_loader'
- '@user'
- '@path_helper'
- '%core.root_path%'
- '%core.php_ext%'
- '%tables.notification_push%'
Expand Down
52 changes: 49 additions & 3 deletions phpBB/phpbb/notification/method/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpbb\form\form_helper;
use phpbb\log\log_interface;
use phpbb\notification\type\type_interface;
use phpbb\path_helper;
use phpbb\user;
use phpbb\user_loader;

Expand All @@ -42,6 +43,9 @@
/** @var user */
protected $user;

/** @var path_helper */
protected $path_helper;

/** @var string Notification Web Push table */
protected $notification_webpush_table;

Expand All @@ -56,20 +60,22 @@
* @param log_interface $log
* @param user_loader $user_loader
* @param user $user
* @param path_helper $path_helper
* @param string $phpbb_root_path
* @param string $php_ext
* @param string $notification_webpush_table
* @param string $push_subscriptions_table
*/
public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, string $phpbb_root_path,
string $php_ext, string $notification_webpush_table, string $push_subscriptions_table)
public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, path_helper $path_helper,
string $phpbb_root_path, string $php_ext, string $notification_webpush_table, string $push_subscriptions_table)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);

$this->config = $config;
$this->db = $db;
$this->log = $log;
$this->user = $user;
$this->path_helper = $path_helper;
$this->notification_webpush_table = $notification_webpush_table;
$this->push_subscriptions_table = $push_subscriptions_table;
}
Expand Down Expand Up @@ -131,7 +137,7 @@
'title' => strip_tags($notification->get_title()),
'text' => strip_tags($notification->get_reference()),
'url' => htmlspecialchars_decode($notification->get_url()),
'avatar' => $notification->get_avatar(),
'avatar' => $this->prepare_avatar($notification->get_avatar()),

Check failure on line 140 in phpBB/phpbb/notification/method/webpush.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - none

InvalidArgument

phpBB/phpbb/notification/method/webpush.php:140:40: InvalidArgument: Argument 1 of phpbb\notification\method\webpush::prepare_avatar expects string, but array<array-key, mixed> provided (see https://psalm.dev/004)
]),
'notification_time' => time(),
];
Expand Down Expand Up @@ -429,4 +435,44 @@

$this->remove_subscriptions($remove_subscriptions);
}

/**
* Takes an avatar string (usually in full html format already) and extracts the url.
* If the avatar url is a relative path, it's converted to an absolute path.
*
* Converts:
* <img class="avatar" src="./path/to/avatar=123456789.gif" width="123" height="123" alt="User avatar" />
* or <img class="avatar" src="./styles/prosilver/theme/images/no_avatar.gif" data-src="./path/to/avatar=123456789.gif" width="123" height="123" alt="User avatar" />
* into https://myboard.url/path/to/avatar=123456789.gif
*
* @param string $avatar
* @return string Absolute path to avatar image
*/
protected function prepare_avatar(string $avatar): string
{
$pattern = '/src=["\']?([^"\'>]+)["\']?/';

preg_match_all($pattern, $avatar, $matches);

$path = !empty($matches[1]) ? end($matches[1]) : $avatar;

return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1);
}

/**
* Returns the board url (and caches it in the function)
*
* @return string the generated board url
*/
protected function get_board_url(): string
{
static $board_url;

if (empty($board_url))
{
$board_url = generate_board_url() . '/';
}

return $board_url;
}
}
2 changes: 1 addition & 1 deletion phpBB/styles/all/js/push_worker.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ self.addEventListener('push', event => {
const options = {
body: responseBody,
data: response,
icon: response.avatar.src,
icon: response.avatar,
};
self.registration.showNotification(response.heading, options);
});
Expand Down
14 changes: 14 additions & 0 deletions tests/notification/notification_method_webpush_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ protected function setUp(): void
$phpEx
);

$request = new \phpbb_mock_request;
$symfony_request = new \phpbb\symfony_request(
$request
);
$filesystem = new \phpbb\filesystem\filesystem();
$phpbb_path_helper = new \phpbb\path_helper(
$symfony_request,
$request,
$phpbb_root_path,
$phpEx
);

$log_table = 'phpbb_log';
$this->log = new \phpbb\log\log($this->db, $user, $auth, $this->phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, $log_table);

Expand All @@ -139,6 +151,7 @@ protected function setUp(): void
$phpbb_container->set('log', $this->log);
$phpbb_container->set('text_formatter.utils', new \phpbb\textformatter\s9e\utils());
$phpbb_container->set('dispatcher', $this->phpbb_dispatcher);
$phpbb_container->set('path_helper', $phpbb_path_helper);
$phpbb_container->setParameter('core.root_path', $phpbb_root_path);
$phpbb_container->setParameter('core.php_ext', $phpEx);
$phpbb_container->setParameter('tables.notifications', 'phpbb_notifications');
Expand Down Expand Up @@ -177,6 +190,7 @@ protected function setUp(): void
$phpbb_container->get('log'),
$phpbb_container->get('user_loader'),
$phpbb_container->get('user'),
$phpbb_container->get('path_helper'),
$phpbb_root_path,
$phpEx,
$phpbb_container->getParameter('tables.notification_push'),
Expand Down
Loading