X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FSubscription.php;h=31048d963f3350977bfb5a7018adc4f942e4c053;hb=41062eb7e4036946711afefd529de5c9a89b7b9d;hp=8d3da36ab342c717fa5122b2707d399cdcb74acb;hpb=74f3cbc3835c742c48723ea7b0360504ccdfebe1;p=friendica.git diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index 8d3da36ab3..31048d963f 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -21,9 +21,13 @@ namespace Friendica\Model; +use Friendica\Core\Logger; +use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Util\Crypto; +use Friendica\Navigation\Notifications\Entity; +use Friendica\Object\Api\Mastodon\Notification; +use Minishlink\WebPush\VAPID; class Subscription { @@ -93,17 +97,75 @@ class Subscription } /** - * Fetch a VAPID key + * Fetch a VAPID keypair * - * @return string + * @return array */ - public static function getVapidKey(): string + private static function getKeyPair(): array { $keypair = DI::config()->get('system', 'ec_keypair'); - if (empty($keypair)) { - $keypair = Crypto::newECKeypair(); + if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) { + $keypair = VAPID::createVapidKeys(); DI::config()->set('system', 'ec_keypair', $keypair); } - return $keypair['vapid']; + return $keypair; + } + + /** + * Fetch the public VAPID key + * + * @return string + */ + public static function getPublicVapidKey(): string + { + $keypair = self::getKeyPair(); + return $keypair['publicKey']; + } + + /** + * Fetch the public VAPID key + * + * @return string + */ + public static function getPrivateVapidKey(): string + { + $keypair = self::getKeyPair(); + return $keypair['privateKey']; + } + + /** + * Prepare push notification + * + * @param int $nid + * @return void + */ + public static function pushByNotification(Entity\Notification $Notification) + { + $type = \Friendica\Factory\Api\Mastodon\Notification::getType($Notification); + + $desktop_notification = !in_array($type, [Notification::TYPE_RESHARE, Notification::TYPE_LIKE]); + + if (DI::pConfig()->get($Notification->uid, 'system', 'notify_like') && ($type == Notification::TYPE_LIKE)) { + $desktop_notification = true; + } + + if (DI::pConfig()->get($Notification->uid, 'system', 'notify_announce') && ($type == Notification::TYPE_RESHARE)) { + $desktop_notification = true; + } + + if ($desktop_notification) { + DI::notify()->createFromNotification($Notification); + } + + if (empty($type)) { + return; + } + + $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'], $Notification->id); + } + DBA::close($subscriptions); } }