]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses/Context.php
Add to Mastodon Status/Context filter for ignored and blocked user's comments
[friendica.git] / src / Module / Api / Mastodon / Statuses / Context.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Api\Mastodon\Statuses;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Item;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
30
31 /**
32  * @see https://docs.joinmastodon.org/methods/statuses/
33  */
34 class Context extends BaseApi
35 {
36         /**
37          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38          */
39         protected function rawContent(array $request = [])
40         {
41                 $uid = self::getCurrentUserID();
42
43                 if (empty($this->parameters['id'])) {
44                         DI::mstdnError()->UnprocessableEntity();
45                 }
46
47                 $request = $this->getRequest([
48                         'max_id'   => 0,  // Return results older than this id
49                         'since_id' => 0,  // Return results newer than this id
50                         'min_id'   => 0,  // Return results immediately newer than this id
51                         'limit'    => 40, // Maximum number of results to return. Defaults to 40.
52                 ], $request);
53
54                 $id = $this->parameters['id'];
55
56                 $parents  = [];
57                 $children = [];
58
59                 $parent = Post::selectFirst(['parent-uri-id'], ['uri-id' => $id]);
60                 if (DBA::isResult($parent)) {
61                         $params    = ['order' => ['uri-id' => true]];
62                         $condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]];
63
64                         if (!empty($request['max_id'])) {
65                                 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
66                         }
67         
68                         if (!empty($request['since_id'])) {
69                                 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
70                         }
71         
72                         if (!empty($request['min_id'])) {
73                                 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
74                                 $params['order'] = ['uri-id'];
75                         }
76
77                         if (!empty($uid)) {
78                                 $condition = DBA::mergeConditions(
79                                         $condition,
80                                         ["NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]
81                                 );
82                         }
83         
84                         $posts = Post::selectPosts(['uri-id', 'thr-parent-id'], $condition, $params);
85                         while ($post = Post::fetch($posts)) {
86                                 if ($post['uri-id'] == $post['thr-parent-id']) {
87                                         continue;
88                                 }
89                                 self::setBoundaries($post['uri-id']);
90
91                                 $parents[$post['uri-id']] = $post['thr-parent-id'];
92
93                                 $children[$post['thr-parent-id']][] = $post['uri-id'];
94                         }
95                         DBA::close($posts);
96
97                         self::setLinkHeader();
98                 } else {
99                         $parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]);
100                         if (DBA::isResult($parent)) {
101                                 $posts = DBA::select('mail', ['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']]);
102                                 while ($post = DBA::fetch($posts)) {
103                                         if ($post['uri-id'] == $post['thr-parent-id']) {
104                                                 continue;
105                                         }
106                                         $parents[$post['uri-id']] = $post['thr-parent-id'];
107
108                                         $children[$post['thr-parent-id']][] = $post['uri-id'];
109                                 }
110                                 DBA::close($posts);
111                         } else {
112                                 DI::mstdnError()->RecordNotFound();
113                         }
114                 }
115
116                 $statuses = ['ancestors' => [], 'descendants' => []];
117
118                 $ancestors = self::getParents($id, $parents);
119
120                 asort($ancestors);
121
122                 $display_quotes = self::appSupportsQuotes();
123
124                 foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) {
125                         $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid, $display_quotes);
126                 }
127
128                 $descendants = self::getChildren($id, $children);
129
130                 asort($descendants);
131
132                 foreach (array_slice($descendants, 0, $request['limit']) as $descendant) {
133                         $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid, $display_quotes);
134                 }
135
136                 System::jsonExit($statuses);
137         }
138
139         private static function getParents(int $id, array $parents, array $list = [])
140         {
141                 if (!empty($parents[$id])) {
142                         $list[] = $parents[$id];
143
144                         $list = self::getParents($parents[$id], $parents, $list);
145                 }
146                 return $list;
147         }
148
149         private static function getChildren(int $id, array $children, array $list = [])
150         {
151                 if (!empty($children[$id])) {
152                         foreach ($children[$id] as $child) {
153                                 $list[] = $child;
154
155                                 $list = self::getChildren($child, $children, $list);
156                         }
157                 }
158                 return $list;
159         }
160 }