]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Notification.php
Merge pull request #10277 from very-ape/authenticate-hook
[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\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Notification as ModelNotification;
29
30 class Notification extends BaseFactory
31 {
32         public function createFromNotifyId(int $id)
33         {
34                 $notification = DBA::selectFirst('notify', [], ['id' => $id]);
35                 if (!DBA::isResult($notification)) {
36                         return null;
37                 }
38
39                 $cid = Contact::getIdForURL($notification['url'], 0, false);
40                 if (empty($cid)) {
41                         return null;
42                 }
43
44                 /*
45                 follow         = Someone followed you
46                 follow_request = Someone requested to follow you
47                 mention        = Someone mentioned you in their status
48                 reblog         = Someone boosted one of your statuses
49                 favourite      = Someone favourited one of your statuses
50                 poll           = A poll you have voted in or created has ended
51                 status         = Someone you enabled notifications for has posted a status
52                 */
53
54                 switch ($notification['type']) {
55                         case ModelNotification\Type::INTRO:
56                                 $type = 'follow_request';
57                                 break;
58
59                         case ModelNotification\Type::WALL:
60                         case ModelNotification\Type::COMMENT:
61                         case ModelNotification\Type::MAIL:
62                         case ModelNotification\Type::TAG_SELF:
63                         case ModelNotification\Type::POKE:
64                                 $type = 'mention';
65                                 break;
66
67                         case ModelNotification\Type::SHARE:
68                                 $type = 'status';
69                                 break;
70
71                         default:
72                                 return null;
73                 }
74
75                 $account = DI::mstdnAccount()->createFromContactId($cid);
76
77                 if (!empty($notification['uri-id'])) {
78                         try {
79                                 $status = DI::mstdnStatus()->createFromUriId($notification['uri-id'], $notification['uid']);
80                         } catch (\Throwable $th) {
81                                 $status = null;
82                         }
83                 } else {
84                         $status = null;
85                 }
86
87                 return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['date'], $account, $status);
88         }
89 }