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