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