]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/ContactEndpoint.php
Some more API functions moved
[friendica.git] / src / Module / Api / Twitter / ContactEndpoint.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\Twitter;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Profile;
27 use Friendica\Model\User;
28 use Friendica\Module\BaseApi;
29 use Friendica\Model\Contact;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\Strings;
32
33 abstract class ContactEndpoint extends BaseApi
34 {
35         const DEFAULT_COUNT = 20;
36         const MAX_COUNT = 200;
37
38         public static function init(array $parameters = [])
39         {
40                 parent::init($parameters);
41
42                 self::checkAllowedScope(self::SCOPE_READ);
43         }
44
45         /**
46          * Computes the uid from the contact_id + screen_name parameters
47          *
48          * @param int|null $contact_id
49          * @param string   $screen_name
50          * @return int
51          * @throws HTTPException\NotFoundException
52          */
53         protected static function getUid(int $contact_id = null, string $screen_name = null)
54         {
55                 $uid = self::getCurrentUserID();
56
57                 if ($contact_id || $screen_name) {
58                         // screen_name trumps user_id when both are provided
59                         if (!$screen_name) {
60                                 $contact = Contact::getById($contact_id, ['nick', 'url']);
61                                 // We don't have the followers of remote accounts so we check for locality
62                                 if (empty($contact) || !Strings::startsWith($contact['url'], DI::baseUrl()->get())) {
63                                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found'));
64                                 }
65
66                                 $screen_name = $contact['nick'];
67                         }
68
69                         $user = User::getByNickname($screen_name, ['uid']);
70                         if (empty($user)) {
71                                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
72                         }
73
74                         $uid = (int)$user['uid'];
75                 }
76
77                 return $uid;
78         }
79
80         /**
81          * This methods expands the contact ids into full user objects in an existing result set.
82          *
83          * @param mixed $rel A relationship constant or a list of them
84          * @param int   $uid The local user id we query the contacts from
85          * @param int   $cursor
86          * @param int   $count
87          * @param bool  $skip_status
88          * @param bool  $include_user_entities
89          * @return array
90          * @throws HTTPException\InternalServerErrorException
91          * @throws HTTPException\NotFoundException
92          * @throws \ImagickException
93          */
94         protected static function list($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $skip_status = false, bool $include_user_entities = true)
95         {
96                 $return = self::ids($rel, $uid, $cursor, $count);
97
98                 $users = [];
99                 foreach ($return['ids'] as $contactId) {
100                         $users[] = DI::twitterUser()->createFromContactId($contactId, $uid, $skip_status, $include_user_entities);
101                 }
102
103                 unset($return['ids']);
104                 $return['users'] = $users;
105
106                 $return = [
107                         'users' => $users,
108                         'next_cursor' => $return['next_cursor'],
109                         'next_cursor_str' => $return['next_cursor_str'],
110                         'previous_cursor' => $return['previous_cursor'],
111                         'previous_cursor_str' => $return['previous_cursor_str'],
112                         'total_count' => (int)$return['total_count'],
113                 ];
114
115                 return $return;
116         }
117
118         /**
119          * @param mixed $rel A relationship constant or a list of them
120          * @param int   $uid The local user id we query the contacts from
121          * @param int   $cursor
122          * @param int   $count
123          * @param bool  $stringify_ids
124          * @return array
125          * @throws HTTPException\NotFoundException
126          */
127         protected static function ids($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false)
128         {
129                 $hide_friends = false;
130                 if ($uid != self::getCurrentUserID()) {
131                         $profile = Profile::getByUID($uid);
132                         if (empty($profile)) {
133                                 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found'));
134                         }
135
136                         $hide_friends = (bool)$profile['hide-friends'];
137                 }
138
139                 $ids = [];
140                 $next_cursor = 0;
141                 $previous_cursor = 0;
142                 $total_count = 0;
143                 if (!$hide_friends) {
144                         $condition = [
145                                 'rel' => $rel,
146                                 'uid' => $uid,
147                                 'self' => false,
148                                 'deleted' => false,
149                                 'hidden' => false,
150                                 'archive' => false,
151                                 'pending' => false
152                         ];
153
154                         $total_count = (int)DBA::count('contact', $condition);
155
156                         $params = ['limit' => $count, 'order' => ['id' => 'ASC']];
157
158                         if ($cursor !== -1) {
159                                 if ($cursor > 0) {
160                                         $condition = DBA::mergeConditions($condition, ['`id` > ?', $cursor]);
161                                 } else {
162                                         $condition = DBA::mergeConditions($condition, ['`id` < ?', -$cursor]);
163                                         // Previous page case: we want the items closest to cursor but for that we need to reverse the query order
164                                         $params['order']['id'] = 'DESC';
165                                 }
166                         }
167
168                         $contacts = Contact::selectToArray(['id'], $condition, $params);
169
170                         // Previous page case: once we get the relevant items closest to cursor, we need to restore the expected display order
171                         if ($cursor !== -1 && $cursor <= 0) {
172                                 $contacts = array_reverse($contacts);
173                         }
174
175                         // Contains user-specific contact ids
176                         $ids = array_column($contacts, 'id');
177
178                         // Cursor is on the user-specific contact id since it's the sort field
179                         if (count($ids)) {
180                                 $previous_cursor = -$ids[0];
181                                 $next_cursor = (int)$ids[count($ids) -1];
182                         }
183
184                         // No next page
185                         if ($total_count <= count($contacts) || count($contacts) < $count) {
186                                 $next_cursor = 0;
187                         }
188                         // End of results
189                         if ($cursor < 0 && count($contacts) === 0) {
190                                 $next_cursor = -1;
191                         }
192
193                         // No previous page
194                         if ($cursor === -1) {
195                                 $previous_cursor = 0;
196                         }
197
198                         if ($cursor > 0 && count($contacts) === 0) {
199                                 $previous_cursor = -$cursor;
200                         }
201
202                         if ($cursor < 0 && count($contacts) === 0) {
203                                 $next_cursor = -1;
204                         }
205
206                         // Conversion to public contact ids
207                         array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) {
208                                 $cdata = Contact::getPublicAndUserContactID($contactId, $uid);
209                                 if ($stringify_ids) {
210                                         $contactId = (string)$cdata['public'];
211                                 } else {
212                                         $contactId = (int)$cdata['public'];
213                                 }
214                         });
215                 }
216
217                 $return = [
218                         'ids' => $ids,
219                         'next_cursor' => $next_cursor,
220                         'next_cursor_str' => (string)$next_cursor,
221                         'previous_cursor' => $previous_cursor,
222                         'previous_cursor_str' => (string)$previous_cursor,
223                         'total_count' => $total_count,
224                 ];
225
226                 return $return;
227         }
228 }