Skip to content

Commit e952d79

Browse files
committed
Progressive Web App compatibility
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 16e7012 commit e952d79

6 files changed

Lines changed: 112 additions & 0 deletions

File tree

config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
phpbb_webpushnotifications_ucp_routing:
22
resource: wpn_ucp.yml
33
prefix: /user
4+
5+
phpbb_webpushnotifications_manifest_controller:
6+
path: /manifest
7+
defaults: { _controller: phpbb.wpn.controller.manifest:handle }

config/services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ services:
5252
- '%tables.phpbb.wpn.notification_push%'
5353
- '%tables.phpbb.wpn.push_subscriptions%'
5454

55+
phpbb.wpn.controller.manifest:
56+
class: phpbb\webpushnotifications\controller\manifest
57+
arguments:
58+
- '@config'
59+
- '@language'
60+
- '@path_helper'
61+
- '@user'

controller/manifest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
*
4+
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2024, phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\webpushnotifications\controller;
12+
13+
use phpbb\config\config;
14+
use phpbb\exception\http_exception;
15+
use phpbb\language\language;
16+
use phpbb\path_helper;
17+
use phpbb\user;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
19+
use Symfony\Component\HttpFoundation\Response;
20+
21+
class manifest
22+
{
23+
/** @var config */
24+
protected $config;
25+
26+
/** @var language */
27+
protected $language;
28+
29+
/** @var path_helper */
30+
protected $path_helper;
31+
32+
/** @var user */
33+
protected $user;
34+
35+
/**
36+
* Constructor for webpush controller
37+
*
38+
* @param config $config
39+
* @param path_helper $path_helper
40+
* @param language $language
41+
* @param user $user
42+
*/
43+
public function __construct(config $config, language $language, path_helper $path_helper, user $user)
44+
{
45+
$this->config = $config;
46+
$this->path_helper = $path_helper;
47+
$this->language = $language;
48+
$this->user = $user;
49+
}
50+
51+
/**
52+
* Handle creation of a manifest json file for progressive web-app support
53+
*
54+
* @return JsonResponse
55+
*/
56+
public function handle(): JsonResponse
57+
{
58+
if ($this->user->data['is_bot'] || $this->user->data['user_type'] == USER_INACTIVE)
59+
{
60+
throw new http_exception(Response::HTTP_FORBIDDEN, 'Forbidden');
61+
}
62+
63+
$root_path = $this->path_helper->get_web_root_path();
64+
65+
$manifest = [
66+
'name' => $this->config['sitename'],
67+
'short_name' => substr($this->config['sitename'], 0, 12), // TODO need an ACP option for short name
68+
'display' => 'standalone',
69+
'orientation' => 'portrait',
70+
'dir' => $this->language->lang('DIRECTION'),
71+
'start_url' => $root_path,
72+
'scope' => $root_path
73+
];
74+
75+
// TODO add support for icons
76+
if (isset($this->config['wpn_app_icon_small'], $this->config['wpn_app_icon_large']))
77+
{
78+
$manifest['icons'] = [
79+
[
80+
'src' => $root_path . 'images/icons/' . $this->config['wpn_app_icon_small'],
81+
'sizes' => '192x192',
82+
'type' => 'image/png'
83+
],
84+
[
85+
'src' => $root_path . 'images/icons/' . $this->config['wpn_app_icon_large'],
86+
'sizes' => '512x512',
87+
'type' => 'image/png'
88+
]
89+
];
90+
}
91+
92+
return new JsonResponse($manifest);
93+
}
94+
}

notification/method/webpush.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
386386
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
387387
'SUBSCRIPTIONS' => $subscriptions,
388388
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
389+
'U_MANIFEST_URL' => $controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
389390
];
390391
}
391392

styles/all/template/ucp_notifications_webpush.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<meta name="apple-mobile-web-app-capable" content="yes">
2+
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
3+
<meta name="apple-mobile-web-app-title" content="{{ SITENAME }}">
4+
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
5+
16
<script>
27
phpbbWebpushOptions = {
38
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',

tests/event/listener_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public function test_get_ucp_template_data($user_id, $method_data, $subscription
218218
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
219219
'SUBSCRIPTIONS' => $subscriptions,
220220
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
221+
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
221222
]
222223
);
223224

0 commit comments

Comments
 (0)