]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Notification.php
Merge pull request #10350 from annando/api-notifications
[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\Post;
28 use Friendica\Model\Verb;
29 use Friendica\Protocol\Activity;
30
31 class Notification extends BaseFactory
32 {
33         public function createFromNotificationId(int $id)
34         {
35                 $notification = DBA::selectFirst('notification', [], ['id' => $id]);
36                 if (!DBA::isResult($notification)) {
37                         return null;
38                 }
39                 /*
40                 follow         = Someone followed you
41                 follow_request = Someone requested to follow you
42                 mention        = Someone mentioned you in their status
43                 reblog         = Someone boosted one of your statuses
44                 favourite      = Someone favourited one of your statuses
45                 poll           = A poll you have voted in or created has ended
46                 status         = Someone you enabled notifications for has posted a status
47                 */
48
49                 if (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
50                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
51                         $type = 'reblog';
52                 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
53                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
54                         $type = 'favourite';
55                 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
56                         $type = 'status';
57                 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
58                         Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
59                         Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
60                         $type = 'mention';
61                 } else {
62                         return null;
63                 }
64
65                 $account = DI::mstdnAccount()->createFromContactId($notification['actor-id']);
66
67                 if (!empty($notification['target-uri-id'])) {
68                         try {
69                                 $status = DI::mstdnStatus()->createFromUriId($notification['target-uri-id'], $notification['uid']);
70                         } catch (\Throwable $th) {
71                                 $status = null;
72                         }
73                 } else {
74                         $status = null;
75                 }
76
77                 return new \Friendica\Object\Api\Mastodon\Notification($id, $type, $notification['created'], $account, $status);
78         }
79 }