]> git.mxchange.org Git - friendica.git/commitdiff
Create constants for Mastodon notification types
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 18 Sep 2021 03:15:23 +0000 (23:15 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 2 Oct 2021 22:15:44 +0000 (18:15 -0400)
src/Factory/Api/Mastodon/Notification.php
src/Model/Notification.php
src/Model/Subscription.php
src/Module/Api/Mastodon/Notifications.php
src/Module/Api/Mastodon/PushSubscription.php
src/Object/Api/Mastodon/Notification.php
src/Object/Api/Mastodon/Subscription.php

index 82ef67d618924c51be6a0cf1851ba5529938cf96..03d102b503446ff7f5ce8c01aa6a012762982f9d 100644 (file)
@@ -49,15 +49,6 @@ class Notification extends BaseFactory
                if (!$this->dba->isResult($notification)) {
                        return null;
                }
-               /*
-               follow         = Someone followed you
-               follow_request = Someone requested to follow you
-               mention        = Someone mentioned you in their status
-               reblog         = Someone boosted one of your statuses
-               favourite      = Someone favourited one of your statuses
-               poll           = A poll you have voted in or created has ended
-               status         = Someone you enabled notifications for has posted a status
-               */
 
                $type = ModelNotification::getType($notification);
                if (empty($type)) {
index 8411ed77db95b6d7104b240ae970e73a3ce72714..284b3e7a831e2528521c97ac22490753989afe7d 100644 (file)
@@ -28,6 +28,7 @@ use Friendica\Core\Logger;
 use Friendica\Database\Database;
 use Friendica\DI;
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Object\Api\Mastodon\Notification as MstdnNotification;
 use Friendica\Protocol\Activity;
 use Psr\Log\LoggerInterface;
 
@@ -46,15 +47,15 @@ class Notification extends BaseModel
        {
                if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::TYPE_NONE)) {
                        $contact = Contact::getById($notification['actor-id'], ['pending']);
-                       $type = $contact['pending'] ? 'follow_request' : 'follow';
+                       $type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
                } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
                        in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
-                       $type = 'reblog';
+                       $type = MstdnNotification::TYPE_RESHARE;
                } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
                        in_array($notification['type'], [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
-                       $type = 'favourite';
+                       $type = MstdnNotification::TYPE_LIKE;
                } elseif ($notification['type'] == Post\UserNotification::TYPE_SHARED) {
-                       $type = 'status';
+                       $type = MstdnNotification::TYPE_POST;
                } elseif (in_array($notification['type'], [
                        Post\UserNotification::TYPE_EXPLICIT_TAGGED,
             Post\UserNotification::TYPE_IMPLICIT_TAGGED,
@@ -62,7 +63,7 @@ class Notification extends BaseModel
                        Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
                        Post\UserNotification::TYPE_THREAD_COMMENT
                ])) {
-                       $type = 'mention';
+                       $type = MstdnNotification::TYPE_MENTION;
                } else {
                        return '';
                }
index 59e71307f5ffb4092bf67fbe132ec823e65f63bc..f9498af986a5b5fb2205c84b76bc8c3c5d2968b3 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Object\Api\Mastodon\Notification;
 use Minishlink\WebPush\VAPID;
 
 class Subscription
@@ -142,8 +143,8 @@ class Subscription
                $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
 
                $type = Notification::getType($notification);
+               $desktop_notification = !in_array($type, [Notification::TYPE_RESHARE, Notification::TYPE_LIKE]);
 
-               $desktop_notification = !in_array($type, ['reblog', 'favourite']);
 
                if (DI::pConfig()->get($notification['uid'], 'system', 'notify_like') && ($type == 'favourite')) {
                        $desktop_notification = true;
index 763a768f5829a0a7d10d6652c056b5bf04a1d596..a04a9ae69ac1e56a00d088c457ff31c4f27addc4 100644 (file)
@@ -74,19 +74,19 @@ class Notifications extends BaseApi
                        }
                }
 
-               if (in_array('follow_request', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition,
                                ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))",
                                Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
                }
 
-               if (in_array('follow', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition,
                                ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))",
                                Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]);
                }
 
-               if (in_array('favourite', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_LIKE, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition, [
                                "(NOT `vid` IN (?, ?) OR NOT `type` IN (?, ?))",
                                Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE),
@@ -94,7 +94,7 @@ class Notifications extends BaseApi
                        ]);
                }
 
-               if (in_array('reblog', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_RESHARE, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition, [
                                "(NOT `vid` IN (?) OR NOT `type` IN (?, ?))",
                                Verb::getID(Activity::ANNOUNCE),
@@ -102,7 +102,7 @@ class Notifications extends BaseApi
                        ]);
                }
 
-               if (in_array('mention', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_MENTION, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition, [
                                "(NOT `vid` IN (?) OR NOT `type` IN (?, ?, ?, ?, ?))",
                                Verb::getID(Activity::POST), Post\UserNotification::TYPE_EXPLICIT_TAGGED,
@@ -110,7 +110,7 @@ class Notifications extends BaseApi
                                Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT, Post\UserNotification::TYPE_THREAD_COMMENT]);
                }
 
-               if (in_array('status', $request['exclude_types'])) {
+               if (in_array(Notification::TYPE_POST, $request['exclude_types'])) {
                        $condition = DBA::mergeConditions($condition, ["(NOT `vid` IN (?) OR NOT `type` IN (?))",
                                Verb::getID(Activity::POST), Post\UserNotification::TYPE_SHARED]);
                }
index 411d5391f523411096eed2c59351820d05559006..3bc77aeb6fde1268b2cd33a41fc0142a077b2083 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Model\Subscription;
 use Friendica\Module\BaseApi;
+use Friendica\Object\Api\Mastodon\Notification;
 
 /**
  * @see https://docs.joinmastodon.org/methods/notifications/push/
@@ -44,18 +45,18 @@ class PushSubscription extends BaseApi
                ]);
 
                $subscription = [
-                       'application-id' => $application['id'],
-                       'uid'            => $uid,
-                       'endpoint'       => $request['subscription']['endpoint'] ?? '',
-                       'pubkey'         => $request['subscription']['keys']['p256dh'] ?? '',
-                       'secret'         => $request['subscription']['keys']['auth'] ?? '',
-                       'follow'         => $request['data']['alerts']['follow'] ?? false,
-                       'favourite'      => $request['data']['alerts']['favourite'] ?? false,
-                       'reblog'         => $request['data']['alerts']['reblog'] ?? false,
-                       'mention'        => $request['data']['alerts']['mention'] ?? false,
-                       'poll'           => $request['data']['alerts']['poll'] ?? false,
-                       'follow_request' => $request['data']['alerts']['follow_request'] ?? false,
-                       'status'         => $request['data']['alerts']['status'] ?? false,
+                       'application-id'                => $application['id'],
+                       'uid'                           => $uid,
+                       'endpoint'                      => $request['subscription']['endpoint'] ?? '',
+                       'pubkey'                        => $request['subscription']['keys']['p256dh'] ?? '',
+                       'secret'                        => $request['subscription']['keys']['auth'] ?? '',
+                       Notification::TYPE_FOLLOW       => $request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false,
+                       Notification::TYPE_LIKE         => $request['data']['alerts'][Notification::TYPE_LIKE] ?? false,
+                       Notification::TYPE_RESHARE      => $request['data']['alerts'][Notification::TYPE_RESHARE] ?? false,
+                       Notification::TYPE_MENTION      => $request['data']['alerts'][Notification::TYPE_MENTION] ?? false,
+                       Notification::TYPE_POLL         => $request['data']['alerts'][Notification::TYPE_POLL] ?? false,
+                       Notification::TYPE_INTRODUCTION => $request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false,
+                       Notification::TYPE_POST         => $request['data']['alerts'][Notification::TYPE_POST] ?? false,
                ];
 
                $ret = Subscription::replace($subscription);
@@ -82,13 +83,13 @@ class PushSubscription extends BaseApi
                }
 
                $fields = [
-                       'follow'         => $request['data']['alerts']['follow'] ?? false,
-                       'favourite'      => $request['data']['alerts']['favourite'] ?? false,
-                       'reblog'         => $request['data']['alerts']['reblog'] ?? false,
-                       'mention'        => $request['data']['alerts']['mention'] ?? false,
-                       'poll'           => $request['data']['alerts']['poll'] ?? false,
-                       'follow_request' => $request['data']['alerts']['follow_request'] ?? false,
-                       'status'         => $request['data']['alerts']['status'] ?? false,
+                       Notification::TYPE_FOLLOW       => $request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false,
+                       Notification::TYPE_LIKE         => $request['data']['alerts'][Notification::TYPE_LIKE] ?? false,
+                       Notification::TYPE_RESHARE      => $request['data']['alerts'][Notification::TYPE_RESHARE] ?? false,
+                       Notification::TYPE_MENTION      => $request['data']['alerts'][Notification::TYPE_MENTION] ?? false,
+                       Notification::TYPE_POLL         => $request['data']['alerts'][Notification::TYPE_POLL] ?? false,
+                       Notification::TYPE_INTRODUCTION => $request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false,
+                       Notification::TYPE_POST         => $request['data']['alerts'][Notification::TYPE_POST] ?? false,
                ];
 
                $ret = Subscription::update($application['id'], $uid, $fields);
index 5f39c503e245e1a100c278250f47b1a88de758e9..d660baa083727016cc07afe1bb3d6fe2049c3612 100644 (file)
@@ -33,9 +33,26 @@ use Friendica\Util\DateTimeFormat;
  */
 class Notification extends BaseDataTransferObject
 {
+       /* From the Mastodon documentation:
+        * - follow         = Someone followed you
+        * - follow_request = Someone requested to follow you
+        * - mention        = Someone mentioned you in their status
+        * - reblog         = Someone boosted one of your statuses
+        * - favourite      = Someone favourited one of your statuses
+        * - poll           = A poll you have voted in or created has ended
+        * - status         = Someone you enabled notifications for has posted a status
+        */
+       public const TYPE_FOLLOW       = 'follow';
+       public const TYPE_INTRODUCTION = 'follow_request';
+       public const TYPE_MENTION      = 'mention';
+       public const TYPE_RESHARE      = 'reblog';
+       public const TYPE_LIKE         = 'favourite';
+       public const TYPE_POLL         = 'poll';
+       public const TYPE_POST         = 'status';
+
        /** @var string */
        protected $id;
-       /** @var string (Enumerable oneOf) */
+       /** @var string One of the TYPE_* constant values */
        protected $type;
        /** @var string (Datetime) */
        protected $created_at;
index dcd0a6982fafa4c4563f2620de7826bada0491b9..ddc6f7d6ebbcbdcdae06388493e921fe95f3ff2c 100644 (file)
@@ -42,20 +42,19 @@ class Subscription extends BaseDataTransferObject
        /**
         * Creates a subscription record from an item record.
         *
-        * @param array   $subscription
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @param array  $subscription
+        * @param string $vapid
         */
        public function __construct(array $subscription, string $vapid)
        {
                $this->id       = (string)$subscription['id'];
                $this->endpoint = $subscription['endpoint'];
                $this->alerts   = [
-                       'follow'    => $subscription['follow'],
-                       'favourite' => $subscription['favourite'],
-                       'reblog'    => $subscription['reblog'],
-                       'mention'   => $subscription['mention'],
-                       'mention'   => $subscription['mention'],
-                       'poll'      => $subscription['poll'],
+                       Notification::TYPE_FOLLOW  => $subscription[Notification::TYPE_FOLLOW],
+                       Notification::TYPE_LIKE    => $subscription[Notification::TYPE_LIKE],
+                       Notification::TYPE_RESHARE => $subscription[Notification::TYPE_RESHARE],
+                       Notification::TYPE_MENTION => $subscription[Notification::TYPE_MENTION],
+                       Notification::TYPE_POLL    => $subscription[Notification::TYPE_POLL],
                ];
 
                $this->server_key = $vapid;