diff --git a/src/Factory/Api/Mastodon/Notification.php b/src/Factory/Api/Mastodon/Notification.php index 2376e58cc..82ef67d61 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 cf2c75463..39fd6a928 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 2c942d2cb..f4bcb3e40 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 fa93eb521..56b462545 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); + } }