]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Subscription.php
Use central function to fetch the global directory
[friendica.git] / src / Model / Subscription.php
index 7401eb927343bb728438b1228f0a6c5b074c4c00..0d8f346d18281dfd80035b4b1c0c14c2f7fe2768 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
  *
  */
 
- /**
-  * @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 Friendica\Navigation\Notifications\Entity;
+use Friendica\Object\Api\Mastodon\Notification;
+use Minishlink\WebPush\VAPID;
 
 class Subscription
 {
@@ -99,18 +97,40 @@ 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-public'];
+               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'];
        }
 
        /**
@@ -119,17 +139,32 @@ class Subscription
         * @param int $nid
         * @return void
         */
-       public static function pushByNotificationId(int $nid)
+       public static function pushByNotification(Entity\Notification $Notification)
        {
-               $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
-               $type = Notification::getType($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]);
+               $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);
        }