From 19f3cad56c365a6be362aca811faa07cb3a6bdf4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 15 Aug 2021 16:18:25 +0000 Subject: [PATCH] Preparation for creating push notification --- src/Factory/Api/Mastodon/Notification.php | 23 +++-------------- src/Model/Notification.php | 31 +++++++++++++++++++++++ src/Model/Post/UserNotification.php | 19 ++++++++++++-- src/Model/Subscription.php | 23 +++++++++++++++++ 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/src/Factory/Api/Mastodon/Notification.php b/src/Factory/Api/Mastodon/Notification.php index 2376e58cc9..82ef67d618 100644 --- a/src/Factory/Api/Mastodon/Notification.php +++ b/src/Factory/Api/Mastodon/Notification.php @@ -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; } diff --git a/src/Model/Notification.php b/src/Model/Notification.php index cf2c754631..39fd6a9289 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -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; + } } diff --git a/src/Model/Post/UserNotification.php b/src/Model/Post/UserNotification.php index 2c942d2cb9..f4bcb3e402 100644 --- a/src/Model/Post/UserNotification.php +++ b/src/Model/Post/UserNotification.php @@ -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; } /** diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index fa93eb5218..56b4625451 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -23,8 +23,10 @@ * @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); + } }