]> git.mxchange.org Git - friendica.git/commitdiff
Preparation for creating push notification
authorMichael <heluecht@pirati.ca>
Sun, 15 Aug 2021 16:18:25 +0000 (16:18 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 15 Aug 2021 16:18:25 +0000 (16:18 +0000)
src/Factory/Api/Mastodon/Notification.php
src/Model/Notification.php
src/Model/Post/UserNotification.php
src/Model/Subscription.php

index 2376e58cc923433040ae4ec7e34453f1b56df1d2..82ef67d618924c51be6a0cf1851ba5529938cf96 100644 (file)
@@ -23,10 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Database\Database;
-use Friendica\Model\Contact;
-use Friendica\Model\Post;
-use Friendica\Model\Verb;
-use Friendica\Protocol\Activity;
+use Friendica\Model\Notification as ModelNotification;
 use Psr\Log\LoggerInterface;
 
 class Notification extends BaseFactory
@@ -62,22 +59,8 @@ class Notification extends BaseFactory
                status         = Someone you enabled notifications for has posted a status
                */
 
-               if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
-                       $contact = Contact::getById($notification['actor-id'], ['pending']);
-                       $type    = $contact['pending'] ? $type    = 'follow_request' : 'follow';
-               } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
-                       in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
-                       $type = 'reblog';
-               } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
-                       in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
-                       $type = 'favourite';
-               } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
-                       $type = 'status';
-               } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
-                       Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
-                       Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
-                       $type = 'mention';
-               } else {
+               $type = ModelNotification::getType($notification);
+               if (empty($type)) {
                        return null;
                }
 
index cf2c754631c6760c0a8fcf72a9c88efe08ecf9a1..39fd6a92890b544581c4339d52ab19c81f4ad7e9 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\BaseModel;
 use Friendica\Content\Text\BBCode;
 use Friendica\Database\Database;
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Protocol\Activity;
 use Psr\Log\LoggerInterface;
 
 /**
@@ -123,4 +124,34 @@ class Notification extends BaseModel
 
                return $message;
        }
+
+       /**
+        * Fetch the notification type for the given notification
+        *
+        * @param array $notification 
+        * @return string
+        */
+       public static function getType(array $notification): string
+       {
+               if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
+                       $contact = Contact::getById($notification['actor-id'], ['pending']);
+                       $contact['pending'] ? $type = 'follow_request' : 'follow';
+               } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
+                       in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
+                       $type = 'reblog';
+               } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
+                       in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
+                       $type = 'favourite';
+               } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
+                       $type = 'status';
+               } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
+                       Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
+                       Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
+                       $type = 'mention';
+               } else {
+                       return '';
+               }
+
+               return $type;
+       }
 }
index 2c942d2cb944ee4cc4668cfc20f110faf80b4d84..f4bcb3e402d17a3f48da8b1a7e51c54fcff28710 100644 (file)
@@ -30,6 +30,7 @@ use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Post;
+use Friendica\Model\Subscription;
 use Friendica\Util\Strings;
 use Friendica\Model\Tag;
 use Friendica\Protocol\Activity;
@@ -297,7 +298,14 @@ class UserNotification
                        $fields['target-uri-id'] = $item['uri-id'];
                }
 
-               return DBA::insert('notification', $fields, Database::INSERT_IGNORE);
+               $ret = DBA::insert('notification', $fields, Database::INSERT_IGNORE);
+               if ($ret) {
+                       $id = DBA::lastInsertId();
+                       if (!empty($id)) {
+                               Subscription::pushByNotificationId($id);
+                       }
+               }
+               return $ret;
        }
 
        /**
@@ -318,7 +326,14 @@ class UserNotification
                        'created' => DateTimeFormat::utcNow(),
                ];
 
-               return DBA::insert('notification', $fields, Database::INSERT_IGNORE);
+               $ret = DBA::insert('notification', $fields, Database::INSERT_IGNORE);
+               if ($ret) {
+                       $id = DBA::lastInsertId();
+                       if (!empty($id)) {
+                               Subscription::pushByNotificationId($id);
+                       }
+               }
+               return $ret;
        }
 
        /**
index fa93eb52187c34bfaa7cbd325c1d6d59a6e5a08b..56b46254510ab5e93ac2438c23c739d57de45a25 100644 (file)
   * @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\Database\DBA;
 use Friendica\DI;
 use Friendica\Util\Crypto;
@@ -110,4 +112,25 @@ class Subscription
                }
                return $keypair['vapid-public'];
        }
+
+       /**
+        * Prepare push notification
+        *
+        * @param int $nid 
+        * @return void 
+        */
+       public static function pushByNotificationId(int $nid)
+       {
+               $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
+               $type = Notification::getType($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]);
+               }
+               DBA::close($subscriptions);
+       }
 }