[ticket/17010] Change how notification data is passed to push notifications JS

PHPBB3-17010
This commit is contained in:
Marc Alexander 2022-10-28 20:55:37 +02:00
parent d4b6ad5620
commit 7092f24645
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 24 additions and 14 deletions

View file

@ -39,6 +39,8 @@ class add_webpush extends migration
'item_id' => ['ULINT', 0], 'item_id' => ['ULINT', 0],
'item_parent_id' => ['ULINT', 0], 'item_parent_id' => ['ULINT', 0],
'user_id' => ['ULINT', 0], 'user_id' => ['ULINT', 0],
'push_data' => ['MTEXT', ''],
'notification_time' => ['TIMESTAMP', 0]
], ],
'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'], 'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'],
], ],
@ -46,11 +48,11 @@ class add_webpush extends migration
'COLUMNS' => [ 'COLUMNS' => [
'subscription_id' => ['ULINT', null, 'auto_increment'], 'subscription_id' => ['ULINT', null, 'auto_increment'],
'user_id' => ['ULINT', 0], 'user_id' => ['ULINT', 0],
'device_name' => ['VCHAR:64', ''], // 'device_name' => ['VCHAR:64', ''],
'endpoint' => ['TEXT', ''], 'endpoint' => ['TEXT', ''],
'expiration_time' => ['TIMESTAMP', 0],
'p256dh' => ['VCHAR', ''], 'p256dh' => ['VCHAR', ''],
'auth' => ['VCHAR', ''], 'auth' => ['VCHAR', ''],
'encoding' => ['VCHAR:32', ''],
], ],
'PRIMARY_KEY' => ['subscription_id', 'user_id'], 'PRIMARY_KEY' => ['subscription_id', 'user_id'],
] ]

View file

@ -115,7 +115,18 @@ class webpush extends \phpbb\notification\method\messenger_base
/** @var type_interface $notification */ /** @var type_interface $notification */
foreach ($this->queue as $notification) foreach ($this->queue as $notification)
{ {
$data = self::clean_data($notification->get_insert_array()); $data = $notification->get_insert_array();
$data += [
'push_data' => json_encode([
'heading' => $this->config['sitename'],
'title' => strip_tags($notification->get_title()),
'text' => strip_tags($notification->get_reference()),
'url' => $notification->get_url(),
'avatar' => $notification->get_avatar(),
]),
'notification_time' => time(),
];
$data = self::clean_data($data);
$insert_buffer->insert($data); $insert_buffer->insert($data);
} }
@ -158,7 +169,7 @@ class webpush extends \phpbb\notification\method\messenger_base
// Get subscriptions for users // Get subscriptions for users
$user_subscription_map = []; $user_subscription_map = [];
$sql = 'SELECT user_id, endpoint, p256dh, auth, encoding $sql = 'SELECT user_id, endpoint, p256dh, auth
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);
@ -200,13 +211,9 @@ class webpush extends \phpbb\notification\method\messenger_base
} }
// add actual web push data // add actual web push data
$data['data'] = [ $data = [
'title' => $this->config['sitename'], 'item_id' => $notification->item_id,
'body' => $notification->get_title(), 'type_id' => $notification->notification_type_id,
'icon' => '', // @todo: to be filled?
'image' => '', // @todo: to be filled?
'url' => $notification->get_url(),
'user_id' => $notification->user_id,
]; ];
$json_data = json_encode($data); $json_data = json_encode($data);
@ -220,7 +227,6 @@ class webpush extends \phpbb\notification\method\messenger_base
'p256dh' => $subscription['p256dh'], 'p256dh' => $subscription['p256dh'],
'auth' => $subscription['auth'], 'auth' => $subscription['auth'],
], ],
'contentEncoding' => !empty($subscription['encoding']) ? $subscription['encoding'] : null,
]); ]);
$web_push->queueNotification($push_subscription, $json_data); $web_push->queueNotification($push_subscription, $json_data);
$number_of_notifications++; $number_of_notifications++;
@ -283,13 +289,15 @@ class webpush extends \phpbb\notification\method\messenger_base
* @param array $data Notification data * @param array $data Notification data
* @return array Cleaned notification data * @return array Cleaned notification data
*/ */
public static function clean_data(array $data) public static function clean_data(array $data): array
{ {
$row = [ $row = [
'notification_type_id' => null, 'notification_type_id' => null,
'item_id' => null, 'item_id' => null,
'item_parent_id' => null, 'item_parent_id' => null,
'user_id' => null, 'user_id' => null,
'push_data' => null,
'notification_time' => null,
]; ];
return array_intersect_key($data, $row); return array_intersect_key($data, $row);

View file

@ -139,7 +139,7 @@ interface type_interface
/** /**
* Get the user's avatar (the user who caused the notification typically) * Get the user's avatar (the user who caused the notification typically)
* *
* @return string * @return array
*/ */
public function get_avatar(); public function get_avatar();