]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Notification.php
Space added
[friendica.git] / src / Factory / Api / Mastodon / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Database\Database;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Post;
28 use Friendica\Model\Verb;
29 use Friendica\Protocol\Activity;
30 use Psr\Log\LoggerInterface;
31
32 class Notification extends BaseFactory
33 {
34         /** @var Database */
35         private $dba;
36         /** @var Account */
37         private $mstdnAccountFactory;
38         /** @var Status */
39         private $mstdnStatusFactory;
40
41         public function __construct(LoggerInterface $logger, Database $dba, Account $mstdnAccountFactory, Status $mstdnStatusFactoryFactory)
42         {
43                 parent::__construct($logger);
44                 $this->dba                 = $dba;
45                 $this->mstdnAccountFactory = $mstdnAccountFactory;
46                 $this->mstdnStatusFactory  = $mstdnStatusFactoryFactory;
47         }
48
49         public function createFromNotificationId(int $id)
50         {
51                 $notification = $this->dba->selectFirst('notification', [], ['id' => $id]);
52                 if (!$this->dba->isResult($notification)) {
53                         return null;
54                 }
55                 /*
56                 follow         = Someone followed you
57                 follow_request = Someone requested to follow you
58                 mention        = Someone mentioned you in their status
59                 reblog         = Someone boosted one of your statuses
60                 favourite      = Someone favourited one of your statuses
61                 poll           = A poll you have voted in or created has ended
62                 status         = Someone you enabled notifications for has posted a status
63                 */
64
65                 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
66                         $contact = Contact::getById($notification['actor-id'], ['pending']);
67                         $type    = $contact['pending'] ? $type    = 'follow_request' : 'follow';
68                 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
69                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
70                         $type = 'reblog';
71                 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
72                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
73                         $type = 'favourite';
74                 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
75                         $type = 'status';
76                 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
77                         Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
78                         Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
79                         $type = 'mention';
80                 } else {
81                         return null;
82                 }
83
84                 $account = $this->mstdnAccountFactory->createFromContactId($notification['actor-id'], $notification['uid']);
85
86                 if (!empty($notification['target-uri-id'])) {
87                         try {
88                                 $status = $this->mstdnStatusFactory->createFromUriId($notification['target-uri-id'], $notification['uid']);
89                         } catch (\Throwable $th) {
90                                 $status = null;
91                         }
92                 } else {
93                         $status = null;
94                 }
95
96                 return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['created'], $account, $status);
97         }
98 }