]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Followers/Ids.php
Merge pull request #12921 from HankG/add-visibility-data-to-mastodon-status
[friendica.git] / src / Module / Api / Twitter / Followers / Ids.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\Twitter\Followers;
23
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;
29
30 /**
31  * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids
32  */
33 class Ids extends ContactEndpoint
34 {
35         protected function rawContent(array $request = [])
36         {
37                 self::checkAllowedScope(self::SCOPE_READ);
38                 $uid = BaseApi::getCurrentUserID();
39
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);
45
46                 // Friendica-specific
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);
50
51                 if ($cid == Contact::getPublicIdByUserId($uid)) {
52                         $params = ['order' => ['pid' => true], 'limit' => $count];
53
54                         $condition = ['uid' => $uid, 'self' => false, 'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
55
56                         $total_count = (int)DBA::count('contact', $condition);
57
58                         if (!empty($max_id)) {
59                                 $condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
60                         }
61
62                         if (!empty($since_id)) {
63                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
64                         }
65
66                         if (!empty($min_id)) {
67                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
68
69                                 $params['order'] = ['pid'];
70                         }
71
72                         $ids = [];
73
74                         foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
75                                 self::setBoundaries($follower['pid']);
76                                 $ids[] = $follower['pid'];
77                         }
78                 } else {
79                         $params = ['order' => ['relation-cid' => true], 'limit' => $count];
80
81                         $condition = ['cid' => $cid, 'follows' => true];
82
83                         $total_count = (int)DBA::count('contact-relation', $condition);
84
85                         if (!empty($max_id)) {
86                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $max_id]);
87                         }
88
89                         if (!empty($since_id)) {
90                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $since_id]);
91                         }
92
93                         if (!empty($min_id)) {
94                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $min_id]);
95
96                                 $params['order'] = ['relation-cid'];
97                         }
98
99                         $ids = [];
100
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'];
105                         }
106                         DBA::close($followers);
107                 }
108
109                 if (!empty($min_id)) {
110                         $ids = array_reverse($ids);
111                 }
112
113                 $return = self::ids($ids, $total_count, $cursor, $count, $stringify_ids);
114
115                 self::setLinkHeader();
116
117                 System::jsonExit($return);
118         }
119 }