]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Lists/Accounts.php
API: added account related endpoints
[friendica.git] / src / Module / Api / Mastodon / Lists / Accounts.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\Module\Api\Mastodon\Lists;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Module\BaseApi;
28
29 /**
30  * @see https://docs.joinmastodon.org/methods/timelines/lists/
31  *
32  * Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
33  */
34 class Accounts extends BaseApi
35 {
36         public static function delete(array $parameters = [])
37         {
38                 self::unsupported('delete');
39         }
40
41         public static function post(array $parameters = [])
42         {
43                 self::unsupported('post');
44         }
45
46         /**
47          * @param array $parameters
48          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49          */
50         public static function rawContent(array $parameters = [])
51         {
52                 self::login();
53                 $uid = self::getCurrentUserID();
54
55                 if (empty($parameters['id'])) {
56                         DI::mstdnError()->RecordNotFound();
57                 }
58
59                 $id = $parameters['id'];
60                 if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
61                         DI::mstdnError()->RecordNotFound();
62                 }
63
64                 // Return results older than this id
65                 $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
66                 // Return results newer than this id
67                 $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
68                 // Maximum number of results to return. Defaults to 20.
69                 $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
70
71
72                 $params = ['order' => ['contact-id' => true], 'limit' => $limit];
73
74                 $condition = ['gid' => $id];
75
76                 if (!empty($max_id)) {
77                         $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $max_id]);
78                 }
79
80                 if (!empty($since_id)) {
81                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $since_id]);
82                 }
83
84                 if (!empty($min_id)) {
85                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $min_id]);
86
87                         $params['order'] = ['contact-id'];
88                 }
89
90                 $members = DBA::select('group_member', ['contact-id'], $condition, $params);
91                 while ($member = DBA::fetch($members)) {
92                         $accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
93                 }
94                 DBA::close($members);
95
96                 if (!empty($min_id)) {
97                         array_reverse($accounts);
98                 }
99
100                 System::jsonExit($accounts);
101         }
102 }