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