]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/ContactEndpoint.php
Implement Twitter contact endpoints
[friendica.git] / src / Module / Api / Twitter / ContactEndpoint.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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                 if (!self::login()) {
43                         throw new HTTPException\UnauthorizedException();
44                 }
45         }
46
47         /**
48          * Computes the uid from the contact_id + screen_name parameters
49          *
50          * @param int|null $contact_id
51          * @param string   $screen_name
52          * @return int
53          * @throws HTTPException\NotFoundException
54          */
55         protected static function getUid(int $contact_id = null, string $screen_name = null)
56         {
57                 $uid = self::$current_user_id;
58
59                 if ($contact_id || $screen_name) {
60                         // screen_name trumps user_id when both are provided
61                         if (!$screen_name) {
62                                 $contact = Contact::getById($contact_id, ['nick', 'url']);
63                                 // We don't have the followers of remote accounts so we check for locality
64                                 if (empty($contact) || !Strings::startsWith($contact['url'], DI::baseUrl()->get())) {
65                                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found'));
66                                 }
67
68                                 $screen_name = $contact['nick'];
69                         }
70
71                         $user = User::getByNickname($screen_name, ['uid']);
72                         if (empty($user)) {
73                                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
74                         }
75
76                         $uid = $user['uid'];
77                 }
78
79                 return $uid;
80         }
81
82         /**
83          * This methods expands the contact ids into full user objects in an existing result set.
84          *
85          * @param mixed $rel A relationship constant or a list of them
86          * @param int   $uid The local user id we query the contacts from
87          * @param int   $cursor
88          * @param int   $count
89          * @param bool  $skip_status
90          * @param bool  $include_user_entities
91          * @return array
92          * @throws HTTPException\InternalServerErrorException
93          * @throws HTTPException\NotFoundException
94          * @throws \ImagickException
95          */
96         protected static function list($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $skip_status = false, bool $include_user_entities = true)
97         {
98                 $return = self::ids($rel, $uid, $cursor, $count);
99
100                 $users = [];
101                 foreach ($return['ids'] as $contactId) {
102                         $users[] = DI::twitterUser()->createFromContactId($contactId, $uid, $skip_status, $include_user_entities);
103                 }
104
105                 unset($return['ids']);
106                 $return['users'] = $users;
107
108                 $return = [
109                         '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'],
115                 ];
116
117
118
119                 return $return;
120         }
121
122         /**
123          * @param mixed $rel A relationship constant or a list of them
124          * @param int   $uid The local user id we query the contacts from
125          * @param int   $cursor
126          * @param int   $count
127          * @param bool  $stringify_ids
128          * @return array
129          * @throws HTTPException\NotFoundException
130          */
131         protected static function ids($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false)
132         {
133                 $hide_friends = false;
134                 if ($uid != self::$current_user_id) {
135                         $profile = Profile::getByUID($uid);
136                         if (empty($profile)) {
137                                 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found'));
138                         }
139
140                         $hide_friends = (bool)$profile['hide-friends'];
141                 }
142
143                 $condition = DBA::collapseCondition([
144                         'rel' => $rel,
145                         'uid' => $uid,
146                         'self' => false,
147                         'deleted' => false,
148                         'hidden' => false,
149                         'archive' => false,
150                         'pending' => false
151                 ]);
152
153                 if ($cursor !== -1) {
154                         $condition[0] .= " AND `id` > ?";
155                         $condition[] = $cursor;
156                 }
157
158                 $ids = [];
159                 $next_cursor = 0;
160                 $previous_cursor = 0;
161                 $total_count = 0;
162                 if (!$hide_friends) {
163                         $total_count = DBA::count('contact', $condition);
164
165                         $contacts = Contact::selectToArray(['id'], $condition, ['limit' => $count, 'order' => ['id']]);
166
167                         // Contains user-specific contact ids
168                         $ids = array_column($contacts, 'id');
169
170                         // Cursor is on the user-specific contact id since it's the sort field
171                         if (count($ids)) {
172                                 $next_cursor = $ids[count($ids) -1];
173                         }
174
175                         // Conversion to public contact ids
176                         array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) {
177                                 $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
178                                 if ($stringify_ids) {
179                                         $contactId = (string)$cdata['public'];
180                                 } else {
181                                         $contactId = (int)$cdata['public'];
182                                 }
183                         });
184
185                         // No next page
186                         if ($total_count <= count($contacts)) {
187                                 $next_cursor = 0;
188                         }
189                 }
190
191                 $return = [
192                         'ids' => $ids,
193                         'next_cursor' => $next_cursor,
194                         'next_cursor_str' => (string)$next_cursor,
195                         'previous_cursor' => $previous_cursor,
196                         'previous_cursor_str' => (string)$previous_cursor,
197                         'total_count' => $total_count,
198                 ];
199
200                 return $return;
201         }
202 }