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