]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Friends/Lists.php
API: Use "contact" for own relations
[friendica.git] / src / Module / Api / Twitter / Friends / Lists.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\Twitter\Friends;
23
24 use Friendica\Database\DBA;
25 use Friendica\Model\Contact;
26 use Friendica\Module\Api\Twitter\ContactEndpoint;
27 use Friendica\Module\BaseApi;
28
29 /**
30  * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list
31  */
32 class Lists extends ContactEndpoint
33 {
34         protected function rawContent(array $request = [])
35         {
36                 self::checkAllowedScope(self::SCOPE_READ);
37                 $uid = BaseApi::getCurrentUserID();
38
39                 // Expected value for user_id parameter: public/user contact id
40                 $cid                   = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), $uid);
41                 $cursor                = $this->getRequestValue($request, 'cursor', -1);
42                 $skip_status           = $this->getRequestValue($request, 'skip_status', false);
43                 $include_user_entities = $this->getRequestValue($request, 'include_user_entities', 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, 'rel' => [Contact::SHARING, Contact::FRIEND]];
55         
56                         if (!empty($max_id)) {
57                                 $condition = DBA::mergeConditions($condition, ["`pid` < ?", $max_id]);
58                         }
59         
60                         if (!empty($since_id)) {
61                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $since_id]);
62                         }
63         
64                         if (!empty($min_id)) {
65                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $min_id]);
66         
67                                 $params['order'] = ['pid'];
68                         }
69         
70                         $ids = [];
71         
72                         foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
73                                 self::setBoundaries($follower['pid']);
74                                 $ids[] = $follower['pid'];
75                         }
76                 } else {
77                         $params = ['order' => ['cid' => true], 'limit' => $count];
78         
79                         $condition = ['relation-cid' => $cid, 'follows' => true];
80
81                         $total_count = (int)DBA::count('contact-relation', $condition);
82
83                         if (!empty($max_id)) {
84                                 $condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]);
85                         }
86
87                         if (!empty($since_id)) {
88                                 $condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]);
89                         }
90
91                         if (!empty($min_id)) {
92                                 $condition = DBA::mergeConditions($condition, ["`cid` > ?", $min_id]);
93
94                                 $params['order'] = ['cid'];
95                         }
96
97                         $ids = [];
98
99                         $followers = DBA::select('contact-relation', ['cid'], $condition, $params);
100                         while ($follower = DBA::fetch($followers)) {
101                                 self::setBoundaries($follower['cid']);
102                                 $ids[] = $follower['cid'];
103                         }
104                         DBA::close($followers);
105                 }
106
107                 if (!empty($min_id)) {
108                         $ids = array_reverse($ids);
109                 }
110
111                 $return = self::list($ids, $total_count, $uid, $cursor, $count, $skip_status, $include_user_entities);
112
113                 self::setLinkHeader();
114
115                 $this->response->exit('lists', ['lists' => $return]);
116         }
117 }