]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Friendships/Show.php
spelling: days
[friendica.git] / src / Module / Api / Twitter / Friendships / Show.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\Friendships;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Module\Api\Twitter\ContactEndpoint;
28 use Friendica\Module\BaseApi;
29 use Friendica\Network\HTTPException\NotFoundException;
30
31 /**
32  * @see https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-friendships-show
33  */
34 class Show extends ContactEndpoint
35 {
36         protected function rawContent(array $request = [])
37         {
38                 self::checkAllowedScope(self::SCOPE_READ);
39                 $uid = BaseApi::getCurrentUserID();
40
41                 $source_cid = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'source_screen_name', ''), '', $this->getRequestValue($request, 'source_id', 0), $uid);
42                 $target_cid = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'target_screen_name', ''), '', $this->getRequestValue($request, 'target_id', 0), $uid);
43
44                 $source = Contact::getById($source_cid);
45                 if (empty($source)) {
46                         throw new NotFoundException('Source not found');
47                 }
48
49                 $target = Contact::getById($target_cid);
50                 if (empty($source)) {
51                         throw new NotFoundException('Target not found');
52                 }
53
54                 $follower  = false;
55                 $following = false;
56
57                 if ($source_cid == Contact::getPublicIdByUserId($uid)) {
58                         $cdata = Contact::getPublicAndUserContactID($target_cid, $uid);
59                         if (!empty($cdata['user'])) {
60                                 $usercontact = Contact::getById($cdata['user'], ['rel']);
61                                 switch ($usercontact['rel'] ?? Contact::NOTHING) {
62                                         case Contact::FOLLOWER:
63                                                 $follower  = true;
64                                                 $following = false;
65                                                 break;
66
67                                         case Contact::SHARING:
68                                                 $follower  = false;
69                                                 $following = true;
70                                                 break;
71
72                                         case Contact::FRIEND:
73                                                 $follower  = true;
74                                                 $following = true;
75                                                 break;
76                                 }
77                         }
78                 } else {
79                         $follower  = DBA::exists('contact-relation', ['cid' => $source_cid, 'relation-cid' => $target_cid, 'follows' => true]);
80                         $following = DBA::exists('contact-relation', ['relation-cid' => $source_cid, 'cid' => $target_cid, 'follows' => true]);
81                 }
82
83                 $relationship = [
84                         'relationship' => [
85                                 'source' => [
86                                         'id'                    => $source['id'],
87                                         'id_str'                => (string)$source['id'],
88                                         'screen_name'           => $source['nick'],
89                                         'following'             => $following,
90                                         'followed_by'           => $follower,
91                                         'live_following'        => false,
92                                         'following_received'    => null,
93                                         'following_requested'   => null,
94                                         'notifications_enabled' => null,
95                                         'can_dm'                => $following && $follower,
96                                         'blocking'              => null,
97                                         'blocked_by'            => null,
98                                         'muting'                => null,
99                                         'want_retweets'         => null,
100                                         'all_replies'           => null,
101                                         'marked_spam'           => null
102                                 ],
103                                 'target' => [
104                                         'id'                  => $target['id'],
105                                         'id_str'              => (string)$target['id'],
106                                         'screen_name'         => $target['nick'],
107                                         'following'           => $follower,
108                                         'followed_by'         => $following,
109                                         'following_received'  => null,
110                                         'following_requested' => null
111                                 ]
112                         ]
113                 ];
114
115                 DI::apiResponse()->exit('relationship', ['relationship' => $relationship], $this->parameters['extension'] ?? null);
116         }
117 }