]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Notification.php
Adding and removing of pictures via API is now possible
[friendica.git] / src / Factory / Api / Mastodon / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
24 use Friendica\BaseFactory;
25 use Friendica\Model\Contact;
26 use Friendica\Navigation\Notifications;
27 use Friendica\Navigation\Notifications\Exception\UnexpectedNotificationTypeException;
28 use Friendica\Object\Api\Mastodon\Notification as MstdnNotification;
29 use Friendica\Protocol\Activity;
30 use Psr\Log\LoggerInterface;
31 use Friendica\Navigation\Notifications\Entity;
32 use Friendica\Model\Post;
33
34 class Notification extends BaseFactory
35 {
36         /** @var Account */
37         private $mstdnAccountFactory;
38         /** @var Status */
39         private $mstdnStatusFactory;
40
41         public function __construct(LoggerInterface $logger, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
42         {
43                 parent::__construct($logger);
44                 $this->mstdnAccountFactory = $mstdnAccountFactory;
45                 $this->mstdnStatusFactory  = $mstdnStatusFactoryFactory;
46         }
47
48         /**
49          * @param Notifications\Entity\Notification $Notification
50          * @param bool $display_quote Display quoted posts
51          *
52          * @return MstdnNotification
53          * @throws UnexpectedNotificationTypeException
54          */
55         public function createFromNotification(Notifications\Entity\Notification $Notification, bool $display_quotes): MstdnNotification
56         {
57                 $type = self::getType($Notification);
58                 if (empty($type)) {
59                         throw new UnexpectedNotificationTypeException();
60                 }
61
62                 $account = $this->mstdnAccountFactory->createFromContactId($Notification->actorId, $Notification->uid);
63
64                 if ($Notification->targetUriId) {
65                         try {
66                                 $status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid, $display_quotes);
67                         } catch (\Exception $exception) {
68                                 $status = null;
69                         }
70                 } else {
71                         $status = null;
72                 }
73
74                 return new MstdnNotification($Notification->id, $type, $Notification->created, $account, $status, $Notification->dismissed);
75         }
76
77         /**
78          * Computes the Mastodon notification type from the given local notification
79          *
80          * @param Entity\Notification $Notification
81          * @return string
82          * @throws \Exception
83          */
84         public static function getType(Entity\Notification $Notification): string
85         {
86                 if (($Notification->verb == Activity::FOLLOW) && ($Notification->type === Post\UserNotification::TYPE_NONE)) {
87                         $contact = Contact::getById($Notification->actorId, ['pending', 'uri-id', 'uid']);
88                         if (($contact['uid'] == 0) && !empty($contact['uri-id'])) {
89                                 $contact = Contact::selectFirst(['pending'], ['uri-id' => $contact['uri-id'], 'uid' => $Notification->uid]);
90                         }
91
92                         if (!isset($contact['pending'])) {
93                                 return '';
94                         }
95
96                         $type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
97                 } elseif (($Notification->verb == Activity::ANNOUNCE) &&
98                         in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
99                         $type = MstdnNotification::TYPE_RESHARE;
100                 } elseif (in_array($Notification->verb, [Activity::LIKE, Activity::DISLIKE]) &&
101                         in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
102                         $type = MstdnNotification::TYPE_LIKE;
103                 } elseif ($Notification->type === Post\UserNotification::TYPE_SHARED) {
104                         $type = MstdnNotification::TYPE_POST;
105                 } elseif (in_array($Notification->type, [
106                         Post\UserNotification::TYPE_EXPLICIT_TAGGED,
107                         Post\UserNotification::TYPE_IMPLICIT_TAGGED,
108                         Post\UserNotification::TYPE_DIRECT_COMMENT,
109                         Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
110                         Post\UserNotification::TYPE_THREAD_COMMENT
111                 ])) {
112                         $type = MstdnNotification::TYPE_MENTION;
113                 } else {
114                         return '';
115                 }
116
117                 return $type;
118         }
119 }