3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Twitter;
24 use Friendica\Core\L10n;
25 use Friendica\Database\DBA;
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;
34 abstract class ContactEndpoint extends BaseApi
36 const DEFAULT_COUNT = 20;
37 const MAX_COUNT = 200;
39 public function __construct(L10n $l10n, array $parameters = [])
41 parent::__construct($l10n, $parameters);
43 self::checkAllowedScope(self::SCOPE_READ);
47 * Computes the uid from the contact_id + screen_name parameters
49 * @param int|null $contact_id
50 * @param string $screen_name
52 * @throws HTTPException\NotFoundException
54 protected static function getUid(int $contact_id = null, string $screen_name = null)
56 $uid = self::getCurrentUserID();
58 if ($contact_id || $screen_name) {
59 // screen_name trumps user_id when both are provided
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'));
67 $screen_name = $contact['nick'];
70 $user = User::getByNickname($screen_name, ['uid']);
72 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
75 $uid = (int)$user['uid'];
82 * This methods expands the contact ids into full user objects in an existing result set.
84 * @param array $ids List of contact ids
85 * @param int $total_count Total list of contacts
86 * @param int $uid The local user id we query the contacts from
89 * @param bool $skip_status
90 * @param bool $include_user_entities
92 * @throws HTTPException\InternalServerErrorException
93 * @throws HTTPException\NotFoundException
94 * @throws \ImagickException
96 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)
98 $return = self::ids($ids, $total_count, $cursor, $count, false);
101 foreach ($return['ids'] as $contactId) {
102 $users[] = DI::twitterUser()->createFromContactId($contactId, $uid, $skip_status, $include_user_entities);
105 unset($return['ids']);
106 $return['users'] = $users;
110 'next_cursor' => $return['next_cursor'],
111 'next_cursor_str' => $return['next_cursor_str'],
112 'previous_cursor' => $return['previous_cursor'],
113 'previous_cursor_str' => $return['previous_cursor_str'],
114 'total_count' => $return['total_count'],
121 * @param array $ids List of contact ids
122 * @param int $total_count Total list of contacts
124 * @param int $count Number of elements to return
125 * @param bool $stringify_ids if "true" then the id is converted to a string
127 * @throws HTTPException\NotFoundException
129 protected static function ids(array $ids, int $total_count, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false)
132 $previous_cursor = 0;
134 // Cursor is on the user-specific contact id since it's the sort field
136 $previous_cursor = -$ids[0];
137 $next_cursor = (int)$ids[count($ids) -1];
141 if ($total_count <= count($ids) || count($ids) < $count) {
145 if ($cursor < 0 && count($ids) === 0) {
150 if ($cursor === -1) {
151 $previous_cursor = 0;
154 if ($cursor > 0 && count($ids) === 0) {
155 $previous_cursor = -$cursor;
158 if ($cursor < 0 && count($ids) === 0) {
162 if ($stringify_ids) {
163 array_walk($ids, function (&$contactId) {
164 $contactId = (string)$contactId;
170 'next_cursor' => $next_cursor,
171 'next_cursor_str' => (string)$next_cursor,
172 'previous_cursor' => $previous_cursor,
173 'previous_cursor_str' => (string)$previous_cursor,
174 'total_count' => $total_count,