]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses/Context.php
Merge branch 'develop' into rewrite/gravity-constants
[friendica.git] / src / Module / Api / Mastodon / Statuses / Context.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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                         'limit'    => 40, // Maximum number of results to return. Defaults to 40.
49                 ], $request);
50
51                 $id = $this->parameters['id'];
52
53                 $parents  = [];
54                 $children = [];
55
56                 $parent = Post::selectFirst(['parent-uri-id'], ['uri-id' => $id]);
57                 if (DBA::isResult($parent)) {
58                         $posts = Post::selectPosts(['uri-id', 'thr-parent-id'],
59                                 ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]]);
60                         while ($post = Post::fetch($posts)) {
61                                 if ($post['uri-id'] == $post['thr-parent-id']) {
62                                         continue;
63                                 }
64                                 $parents[$post['uri-id']] = $post['thr-parent-id'];
65
66                                 $children[$post['thr-parent-id']][] = $post['uri-id'];
67                         }
68                         DBA::close($posts);
69                 } else {
70                         $parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]);
71                         if (DBA::isResult($parent)) {
72                                 $posts = DBA::select('mail', ['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']]);
73                                 while ($post = DBA::fetch($posts)) {
74                                         if ($post['uri-id'] == $post['thr-parent-id']) {
75                                                 continue;
76                                         }
77                                         $parents[$post['uri-id']] = $post['thr-parent-id'];
78
79                                         $children[$post['thr-parent-id']][] = $post['uri-id'];
80                                 }
81                                 DBA::close($posts);
82                         } else {
83                                 DI::mstdnError()->RecordNotFound();
84                         }
85                 }
86
87                 $statuses = ['ancestors' => [], 'descendants' => []];
88
89                 $ancestors = self::getParents($id, $parents);
90
91                 asort($ancestors);
92
93                 foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) {
94                         $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid);;
95                 }
96
97                 $descendants = self::getChildren($id, $children);
98
99                 asort($descendants);
100
101                 foreach (array_slice($descendants, 0, $request['limit']) as $descendant) {
102                         $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid);
103                 }
104
105                 System::jsonExit($statuses);
106         }
107
108         private static function getParents(int $id, array $parents, array $list = [])
109         {
110                 if (!empty($parents[$id])) {
111                         $list[] = $parents[$id];
112
113                         $list = self::getParents($parents[$id], $parents, $list);
114                 }
115                 return $list;
116         }
117
118         private static function getChildren(int $id, array $children, array $list = [])
119         {
120                 if (!empty($children[$id])) {
121                         foreach ($children[$id] as $child) {
122                                 $list[] = $child;
123
124                                 $list = self::getChildren($child, $children, $list);
125                         }
126                 }
127                 return $list;
128         }
129 }