Skip to content

Commit 868fc58

Browse files
committed
Revert "Rever path helper stuff"
This reverts commit f5840f5.
1 parent f5840f5 commit 868fc58

5 files changed

Lines changed: 57 additions & 6 deletions

File tree

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ services:
2828
- '@log'
2929
- '@user_loader'
3030
- '@user'
31+
- '@path_helper'
3132
- '%core.root_path%'
3233
- '%core.php_ext%'
3334
- '%tables.phpbb.wpn.notification_push%'

notification/method/webpush.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpbb\log\log_interface;
1818
use phpbb\notification\method\messenger_base;
1919
use phpbb\notification\type\type_interface;
20+
use phpbb\path_helper;
2021
use phpbb\user;
2122
use phpbb\user_loader;
2223
use phpbb\webpushnotifications\form\form_helper;
@@ -41,6 +42,9 @@ class webpush extends messenger_base implements extended_method_interface
4142
/** @var user */
4243
protected $user;
4344

45+
/** @var path_helper */
46+
protected $path_helper;
47+
4448
/** @var string Notification Web Push table */
4549
protected $notification_webpush_table;
4650

@@ -55,20 +59,22 @@ class webpush extends messenger_base implements extended_method_interface
5559
* @param log_interface $log
5660
* @param user_loader $user_loader
5761
* @param user $user
62+
* @param path_helper $path_helper
5863
* @param string $phpbb_root_path
5964
* @param string $php_ext
6065
* @param string $notification_webpush_table
6166
* @param string $push_subscriptions_table
6267
*/
63-
public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, string $phpbb_root_path,
64-
string $php_ext, string $notification_webpush_table, string $push_subscriptions_table)
68+
public function __construct(config $config, driver_interface $db, log_interface $log, user_loader $user_loader, user $user, path_helper $path_helper,
69+
string $phpbb_root_path, string $php_ext, string $notification_webpush_table, string $push_subscriptions_table)
6570
{
6671
parent::__construct($user_loader, $phpbb_root_path, $php_ext);
6772

6873
$this->config = $config;
6974
$this->db = $db;
7075
$this->log = $log;
7176
$this->user = $user;
77+
$this->path_helper = $path_helper;
7278
$this->notification_webpush_table = $notification_webpush_table;
7379
$this->push_subscriptions_table = $push_subscriptions_table;
7480
}
@@ -430,10 +436,16 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
430436
}
431437

432438
/**
433-
* Takes an avatar string (usually in full html format already) and extracts the src path.
439+
* Takes an avatar string (usually in full html format already) and extracts the url.
440+
* If the avatar url is a relative path, it's converted to an absolute path.
441+
*
442+
* Converts:
443+
* <img class="avatar" src="./path/to/avatar=123456789.gif" width="123" height="123" alt="User avatar" />
444+
* 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" />
445+
* into https://myboard.url/path/to/avatar=123456789.gif
434446
*
435447
* @param string $avatar
436-
* @return array
448+
* @return string Absolute path to avatar image
437449
*/
438450
protected function prepare_avatar($avatar)
439451
{
@@ -443,6 +455,23 @@ protected function prepare_avatar($avatar)
443455

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

446-
return ['src' => preg_replace('#^\./#', '/', $path)];
458+
return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1);
459+
}
460+
461+
/**
462+
* Returns the board url (and caches it in the function)
463+
*
464+
* @return string the generated board url
465+
*/
466+
protected function get_board_url()
467+
{
468+
static $board_url;
469+
470+
if (empty($board_url))
471+
{
472+
$board_url = generate_board_url() . '/';
473+
}
474+
475+
return $board_url;
447476
}
448477
}

styles/all/template/push_worker.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ self.addEventListener('push', event => {
3636
const options = {
3737
body: responseBody,
3838
data: response,
39-
icon: response.avatar.src,
39+
icon: response.avatar,
4040
};
4141
self.registration.showNotification(response.heading, options);
4242
});

tests/event/listener_test.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ protected function setUp(): void
7676
$this->user
7777
);
7878

79+
$path_helper = $this->getMockBuilder('\phpbb\path_helper')
80+
->disableOriginalConstructor()
81+
->setMethods(array())
82+
->getMock();
83+
7984
$this->notification_method_webpush = new \phpbb\webpushnotifications\notification\method\webpush(
8085
$this->config,
8186
$db,
8287
new \phpbb\log\dummy(),
8388
$user_loader,
8489
$this->user,
90+
$path_helper,
8591
$phpbb_root_path,
8692
$phpEx,
8793
'phpbb_wpn_notification_push',

tests/notification/notification_method_webpush_test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ protected function setUp(): void
128128
$phpEx
129129
);
130130

131+
$request = new \phpbb_mock_request;
132+
$symfony_request = new \phpbb\symfony_request(
133+
$request
134+
);
135+
$filesystem = new \phpbb\filesystem\filesystem();
136+
$phpbb_path_helper = new \phpbb\path_helper(
137+
$symfony_request,
138+
$filesystem,
139+
$request,
140+
$phpbb_root_path,
141+
$phpEx
142+
);
143+
131144
$log_table = 'phpbb_log';
132145
$this->log = new \phpbb\log\log($this->db, $user, $auth, $this->phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, $log_table);
133146

@@ -145,6 +158,7 @@ protected function setUp(): void
145158
$phpbb_container->set('log', $this->log);
146159
$phpbb_container->set('text_formatter.utils', new \phpbb\textformatter\s9e\utils());
147160
$phpbb_container->set('dispatcher', $this->phpbb_dispatcher);
161+
$phpbb_container->set('path_helper', $phpbb_path_helper);
148162
$phpbb_container->setParameter('core.root_path', $phpbb_root_path);
149163
$phpbb_container->setParameter('core.php_ext', $phpEx);
150164
$phpbb_container->setParameter('tables.notifications', 'phpbb_notifications');
@@ -160,6 +174,7 @@ protected function setUp(): void
160174
$phpbb_container->get('log'),
161175
$phpbb_container->get('user_loader'),
162176
$phpbb_container->get('user'),
177+
$phpbb_container->get('path_helper'),
163178
$phpbb_root_path,
164179
$phpEx,
165180
$phpbb_container->getParameter('tables.phpbb.wpn.notification_push'),

0 commit comments

Comments
 (0)