]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/ContactEndpoint.php
Introduce `Response` for Modules to create a testable way for module responses
[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\App;
25 use Friendica\Core\L10n;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Profile;
29 use Friendica\Model\User;
30 use Friendica\Module\BaseApi;
31 use Friendica\Model\Contact;
32 use Friendica\Module\Response;
33 use Friendica\Network\HTTPException;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Strings;
36 use Psr\Log\LoggerInterface;
37
38 abstract class ContactEndpoint extends BaseApi
39 {
40         const DEFAULT_COUNT = 20;
41         const MAX_COUNT = 200;
42
43         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
44         {
45                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
46
47                 self::checkAllowedScope(self::SCOPE_READ);
48         }
49
50         /**
51          * Computes the uid from the contact_id + screen_name parameters
52          *
53          * @param int|null $contact_id
54          * @param string   $screen_name
55          * @return int
56          * @throws HTTPException\NotFoundException
57          */
58         protected static function getUid(int $contact_id = null, string $screen_name = null)
59         {
60                 $uid = self::getCurrentUserID();
61
62                 if ($contact_id || $screen_name) {
63                         // screen_name trumps user_id when both are provided
64                         if (!$screen_name) {
65                                 $contact = Contact::getById($contact_id, ['nick', 'url']);
66                                 // We don't have the followers of remote accounts so we check for locality
67                                 if (empty($contact) || !Strings::startsWith($contact['url'], DI::baseUrl()->get())) {
68                                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found'));
69                                 }
70
71                                 $screen_name = $contact['nick'];
72                         }
73
74                         $user = User::getByNickname($screen_name, ['uid']);
75                         if (empty($user)) {
76                                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
77                         }
78
79                         $uid = (int)$user['uid'];
80                 }
81
82                 return $uid;
83         }
84
85         /**
86          * This methods expands the contact ids into full user objects in an existing result set.
87          *
88          * @param array $ids           List of contact ids
89          * @param int   $total_count   Total list of contacts
90          * @param int   $uid           The local user id we query the contacts from
91          * @param int   $cursor
92          * @param int   $count
93          * @param bool  $skip_status
94          * @param bool  $include_user_entities
95          * @return array
96          * @throws HTTPException\InternalServerErrorException
97          * @throws HTTPException\NotFoundException
98          * @throws \ImagickException
99          */
100         protected static function list(array $ids, int $total_count, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $skip_status = false, bool $include_user_entities = true)
101         {
102                 $return = self::ids($ids, $total_count, $cursor, $count, false);
103
104                 $users = [];
105                 foreach ($return['ids'] as $contactId) {
106                         $users[] = DI::twitterUser()->createFromContactId($contactId, $uid, $skip_status, $include_user_entities);
107                 }
108
109                 unset($return['ids']);
110                 $return['users'] = $users;
111
112                 $return = [
113                         'users' => $users,
114                         'next_cursor' => $return['next_cursor'],
115                         'next_cursor_str' => $return['next_cursor_str'],
116                         'previous_cursor' => $return['previous_cursor'],
117                         'previous_cursor_str' => $return['previous_cursor_str'],
118                         'total_count' => $return['total_count'],
119                 ];
120
121                 return $return;
122         }
123
124         /**
125          * @param array $ids           List of contact ids
126          * @param int   $total_count   Total list of contacts
127          * @param int   $cursor
128          * @param int   $count         Number of elements to return
129          * @param bool  $stringify_ids if "true" then the id is converted to a string
130          * @return array
131          * @throws HTTPException\NotFoundException
132          */
133         protected static function ids(array $ids, int $total_count, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false)
134         {
135                 $next_cursor = 0;
136                 $previous_cursor = 0;
137
138                 // Cursor is on the user-specific contact id since it's the sort field
139                 if (count($ids)) {
140                         $previous_cursor = -$ids[0];
141                         $next_cursor = (int)$ids[count($ids) -1];
142                 }
143
144                 // No next page
145                 if ($total_count <= count($ids) || count($ids) < $count) {
146                         $next_cursor = 0;
147                 }
148                 // End of results
149                 if ($cursor < 0 && count($ids) === 0) {
150                         $next_cursor = -1;
151                 }
152
153                 // No previous page
154                 if ($cursor === -1) {
155                         $previous_cursor = 0;
156                 }
157
158                 if ($cursor > 0 && count($ids) === 0) {
159                         $previous_cursor = -$cursor;
160                 }
161
162                 if ($cursor < 0 && count($ids) === 0) {
163                         $next_cursor = -1;
164                 }
165
166                 if ($stringify_ids) {
167                         array_walk($ids, function (&$contactId) {
168                                 $contactId = (string)$contactId;
169                         });
170                 }
171
172                 $return = [
173                         'ids' => $ids,
174                         'next_cursor' => $next_cursor,
175                         'next_cursor_str' => (string)$next_cursor,
176                         'previous_cursor' => $previous_cursor,
177                         'previous_cursor_str' => (string)$previous_cursor,
178                         'total_count' => $total_count,
179                 ];
180
181                 return $return;
182         }
183 }