Merge pull request #10602 from annando/push
Transmit push subscriptions
This commit is contained in:
commit
e205bd450e
7 changed files with 843 additions and 22 deletions
|
@ -36,6 +36,6 @@ class Subscription extends BaseFactory
|
|||
public function createForApplicationIdAndUserId(int $applicationid, int $uid): \Friendica\Object\Api\Mastodon\Subscription
|
||||
{
|
||||
$subscription = DBA::selectFirst('subscription', [], ['application-id' => $applicationid, 'uid' => $uid]);
|
||||
return new \Friendica\Object\Api\Mastodon\Subscription($subscription, ModelSubscription::getVapidKey());
|
||||
return new \Friendica\Object\Api\Mastodon\Subscription($subscription, ModelSubscription::getPublicVapidKey());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,17 +19,13 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see https://github.com/web-push-libs/web-push-php
|
||||
* Possibly we should simply use this.
|
||||
*/
|
||||
|
||||
namespace Friendica\Model;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\Worker;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Util\Crypto;
|
||||
use Minishlink\WebPush\VAPID;
|
||||
|
||||
class Subscription
|
||||
{
|
||||
|
@ -99,18 +95,40 @@ class Subscription
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetch a VAPID key
|
||||
* Fetch a VAPID keypair
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function getKeyPair(): array
|
||||
{
|
||||
$keypair = DI::config()->get('system', 'ec_keypair');
|
||||
if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
|
||||
$keypair = VAPID::createVapidKeys();
|
||||
DI::config()->set('system', 'ec_keypair', $keypair);
|
||||
}
|
||||
return $keypair;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the public VAPID key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getVapidKey(): string
|
||||
public static function getPublicVapidKey(): string
|
||||
{
|
||||
$keypair = DI::config()->get('system', 'ec_keypair');
|
||||
if (empty($keypair)) {
|
||||
$keypair = Crypto::newECKeypair();
|
||||
DI::config()->set('system', 'ec_keypair', $keypair);
|
||||
}
|
||||
return $keypair['vapid-public'];
|
||||
$keypair = self::getKeyPair();
|
||||
return $keypair['publicKey'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the public VAPID key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getPrivateVapidKey(): string
|
||||
{
|
||||
$keypair = self::getKeyPair();
|
||||
return $keypair['privateKey'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,6 +149,7 @@ class Subscription
|
|||
$subscriptions = DBA::select('subscription', [], ['uid' => $notification['uid'], $type => true]);
|
||||
while ($subscription = DBA::fetch($subscriptions)) {
|
||||
Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
|
||||
Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $nid);
|
||||
}
|
||||
DBA::close($subscriptions);
|
||||
}
|
||||
|
|
100
src/Worker/PushSubscription.php
Normal file
100
src/Worker/PushSubscription.php
Normal file
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Worker;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Subscription as ModelSubscription;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Minishlink\WebPush\WebPush;
|
||||
use Minishlink\WebPush\Subscription;
|
||||
|
||||
class PushSubscription
|
||||
{
|
||||
public static function execute(int $sid, int $nid)
|
||||
{
|
||||
Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
|
||||
|
||||
$subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
|
||||
if (empty($subscription)) {
|
||||
Logger::info('Subscription not found', ['subscription' => $sid]);
|
||||
return;
|
||||
}
|
||||
|
||||
$notification = DBA::selectFirst('notification', [], ['id' => $nid]);
|
||||
if (empty($notification)) {
|
||||
Logger::info('Notification not found', ['notification' => $nid]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($notification['uri-id'])) {
|
||||
$notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
|
||||
}
|
||||
|
||||
if (!empty($notification['actor-id'])) {
|
||||
$actor = Contact::getById($notification['actor-id']);
|
||||
}
|
||||
|
||||
$push = [
|
||||
'subscription' => Subscription::create([
|
||||
'endpoint' => $subscription['endpoint'],
|
||||
'publicKey' => $subscription['pubkey'],
|
||||
'authToken' => $subscription['secret'],
|
||||
]),
|
||||
// @todo Check if we are supposed to transmit a payload at all
|
||||
'payload' => json_encode([
|
||||
'title' => 'Friendica',
|
||||
'body' => $notify['msg'] ?? '',
|
||||
'icon' => $actor['thumb'] ?? '',
|
||||
'image' => '',
|
||||
'badge' => DI::baseUrl()->get() . '/images/friendica-192.png',
|
||||
'tag' => $notification['parent-uri-id'] ?? '',
|
||||
'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
|
||||
]),
|
||||
];
|
||||
|
||||
$auth = [
|
||||
'VAPID' => [
|
||||
'subject' => DI::baseUrl()->getHostname(),
|
||||
'publicKey' => ModelSubscription::getPublicVapidKey(),
|
||||
'privateKey' => ModelSubscription::getPrivateVapidKey(),
|
||||
],
|
||||
];
|
||||
|
||||
$webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
|
||||
|
||||
$report = $webPush->sendOneNotification(
|
||||
$push['subscription'],
|
||||
$push['payload']
|
||||
);
|
||||
|
||||
$endpoint = $report->getRequest()->getUri()->__toString();
|
||||
|
||||
if ($report->isSuccess()) {
|
||||
Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]);
|
||||
} else {
|
||||
Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue