3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Twitter\Followers;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\Model\Contact;
27 use Friendica\Module\Api\Twitter\ContactEndpoint;
28 use Friendica\Module\BaseApi;
31 * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids
33 class Ids extends ContactEndpoint
35 protected function rawContent(array $request = [])
37 self::checkAllowedScope(self::SCOPE_READ);
38 $uid = BaseApi::getCurrentUserID();
40 // Expected value for user_id parameter: public/user contact id
41 $cid = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), $uid);
42 $cursor = $this->getRequestValue($request, 'cursor', -1);
43 $stringify_ids = $this->getRequestValue($request, 'stringify_ids', false);
44 $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT);
47 $since_id = $this->getRequestValue($request, 'since_id', 0, 0);
48 $max_id = $this->getRequestValue($request, 'max_id', 0, 0);
49 $min_id = $this->getRequestValue($request, 'min_id', 0, 0);
51 if ($cid == Contact::getPublicIdByUserId($uid)) {
52 $params = ['order' => ['pid' => true], 'limit' => $count];
54 $condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
56 $total_count = (int)DBA::count('contact', $condition);
58 if (!empty($max_id)) {
59 $condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
62 if (!empty($since_id)) {
63 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
66 if (!empty($min_id)) {
67 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
69 $params['order'] = ['pid'];
74 foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
75 self::setBoundaries($follower['pid']);
76 $ids[] = $follower['pid'];
79 $params = ['order' => ['relation-cid' => true], 'limit' => $count];
81 $condition = ['cid' => $cid, 'follows' => true];
83 $total_count = (int)DBA::count('contact-relation', $condition);
85 if (!empty($max_id)) {
86 $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $max_id]);
89 if (!empty($since_id)) {
90 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $since_id]);
93 if (!empty($min_id)) {
94 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $min_id]);
96 $params['order'] = ['relation-cid'];
101 $followers = DBA::select('contact-relation', ['relation-cid'], $condition, $params);
102 while ($follower = DBA::fetch($followers)) {
103 self::setBoundaries($follower['relation-cid']);
104 $ids[] = $follower['relation-cid'];
106 DBA::close($followers);
109 if (!empty($min_id)) {
110 $ids = array_reverse($ids);
113 $return = self::ids($ids, $total_count, $cursor, $count, $stringify_ids);
115 self::setLinkHeader();
117 System::jsonExit($return);