]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Notification.php
Merge pull request #12674 from nupplaphil/bug/config_typesafe
[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          *
51          * @return MstdnNotification
52          * @throws UnexpectedNotificationTypeException
53          */
54         public function createFromNotification(Notifications\Entity\Notification $Notification): MstdnNotification
55         {
56                 $type = self::getType($Notification);
57                 if (empty($type)) {
58                         throw new UnexpectedNotificationTypeException();
59                 }
60
61                 $account = $this->mstdnAccountFactory->createFromContactId($Notification->actorId, $Notification->uid);
62
63                 if ($Notification->targetUriId) {
64                         try {
65                                 $status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid);
66                         } catch (\Throwable $th) {
67                                 $status = null;
68                         }
69                 } else {
70                         $status = null;
71                 }
72
73                 return new MstdnNotification($Notification->id, $type, $Notification->created, $account, $status, $Notification->dismissed);
74         }
75
76         /**
77          * Computes the Mastodon notification type from the given local notification
78          *
79          * @param Entity\Notification $Notification
80          * @return string
81          * @throws \Exception
82          */
83         public static function getType(Entity\Notification $Notification): string
84         {
85                 if (($Notification->verb == Activity::FOLLOW) && ($Notification->type === Post\UserNotification::TYPE_NONE)) {
86                         $contact = Contact::getById($Notification->actorId, ['pending', 'uri-id', 'uid']);
87                         if (($contact['uid'] == 0) && !empty($contact['uri-id'])) {
88                                 $contact = Contact::selectFirst(['pending'], ['uri-id' => $contact['uri-id'], 'uid' => $Notification->uid]);
89                         }
90
91                         if (!isset($contact['pending'])) {
92                                 return '';
93                         }
94
95                         $type = $contact['pending'] ? MstdnNotification::TYPE_INTRODUCTION : MstdnNotification::TYPE_FOLLOW;
96                 } elseif (($Notification->verb == Activity::ANNOUNCE) &&
97                         in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
98                         $type = MstdnNotification::TYPE_RESHARE;
99                 } elseif (in_array($Notification->verb, [Activity::LIKE, Activity::DISLIKE]) &&
100                         in_array($Notification->type, [Post\UserNotification::TYPE_DIRECT_COMMENT, Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT])) {
101                         $type = MstdnNotification::TYPE_LIKE;
102                 } elseif ($Notification->type === Post\UserNotification::TYPE_SHARED) {
103                         $type = MstdnNotification::TYPE_POST;
104                 } elseif (in_array($Notification->type, [
105                         Post\UserNotification::TYPE_EXPLICIT_TAGGED,
106                         Post\UserNotification::TYPE_IMPLICIT_TAGGED,
107                         Post\UserNotification::TYPE_DIRECT_COMMENT,
108                         Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT,
109                         Post\UserNotification::TYPE_THREAD_COMMENT
110                 ])) {
111                         $type = MstdnNotification::TYPE_MENTION;
112                 } else {
113                         return '';
114                 }
115
116                 return $type;
117         }
118 }