X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FSubscription.php;h=88aa820674007a73eedb3228c8ef5e412b0edc25;hb=8e397b5849136b4acee2f67aeb10be0ecb8335af;hp=8d3da36ab342c717fa5122b2707d399cdcb74acb;hpb=74f3cbc3835c742c48723ea7b0360504ccdfebe1;p=friendica.git diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index 8d3da36ab3..88aa820674 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -1,6 +1,6 @@ $applicationid, 'uid' => $uid]); } @@ -60,10 +64,9 @@ class Subscription * @param int $applicationid * @param int $uid * @param array $fields subscription fields - * * @return bool result of update */ - public static function update(int $applicationid, int $uid, array $fields) + public static function update(int $applicationid, int $uid, array $fields): bool { return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]); } @@ -72,10 +75,9 @@ class Subscription * Insert or replace a subscription record * * @param array $fields subscription fields - * * @return bool result of replace */ - public static function replace(array $fields) + public static function replace(array $fields): bool { return DBA::replace('subscription', $fields); } @@ -87,23 +89,71 @@ class Subscription * @param int $uid * @return bool */ - public static function delete(int $applicationid, int $uid) + public static function delete(int $applicationid, int $uid): bool { return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]); } /** - * 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 Notification $Notification + * @return void + */ + public static function pushByNotification(Entity\Notification $notification) + { + $type = NotificationFactory::getType($notification); + + if (DI::notify()->shouldShowOnDesktop($notification, $type)) { + 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(Worker::PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $notification->id); + } + DBA::close($subscriptions); } }