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