]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Friendships/Show.php
Merge pull request #11141 from urbalazs/language-names
[friendica.git] / src / Module / Api / Twitter / Friendships / Show.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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($request['source_screen_name'] ?? '', '', $request['source_id'] ?? 0, $uid);
42
43                 $target_cid = BaseApi::getContactIDForSearchterm($request['target_screen_name'] ?? '', '', $request['target_id'] ?? 0, $uid);
44
45                 $source = Contact::getById($source_cid);
46                 if (empty($source)) {
47                         throw new NotFoundException('Source not found');
48                 }
49
50                 $target = Contact::getById($target_cid);
51                 if (empty($source)) {
52                         throw new NotFoundException('Target not found');
53                 }
54
55                 $follower  = false;
56                 $following = false;
57
58                 if ($source_cid == Contact::getPublicIdByUserId($uid)) {
59                         $cdata = Contact::getPublicAndUserContactID($target_cid, $uid);
60                         if (!empty($cdata['user'])) {
61                                 $usercontact = Contact::getById($cdata['user'], ['rel']);
62                                 switch ($usercontact['rel'] ?? Contact::NOTHING) {
63                                         case Contact::FOLLOWER:
64                                                 $follower  = true;
65                                                 $following = false;
66                                                 break;
67
68                                         case Contact::SHARING:
69                                                 $follower  = false;
70                                                 $following = true;
71                                                 break;
72
73                                         case Contact::FRIEND:
74                                                 $follower  = true;
75                                                 $following = true;
76                                                 break;
77                                 }
78                         }
79                 } else {
80                         $follower  = DBA::exists('contact-relation', ['cid' => $source_cid, 'relation-cid' => $target_cid, 'follows' => true]);
81                         $following = DBA::exists('contact-relation', ['relation-cid' => $source_cid, 'cid' => $target_cid, 'follows' => true]);
82                 }
83
84                 $relationship = [
85                         'relationship' => [
86                                 'source' => [
87                                         'id'                    => $source['id'],
88                                         'id_str'                => (string)$source['id'],
89                                         'screen_name'           => $source['nick'],
90                                         'following'             => $following,
91                                         'followed_by'           => $follower,
92                                         'live_following'        => false,
93                                         'following_received'    => null,
94                                         'following_requested'   => null,
95                                         'notifications_enabled' => null,
96                                         'can_dm'                => $following && $follower,
97                                         'blocking'              => null,
98                                         'blocked_by'            => null,
99                                         'muting'                => null,
100                                         'want_retweets'         => null,
101                                         'all_replies'           => null,
102                                         'marked_spam'           => null
103                                 ],
104                                 'target' => [
105                                         'id'                  => $target['id'],
106                                         'id_str'              => (string)$target['id'],
107                                         'screen_name'         => $target['nick'],
108                                         'following'           => $follower,
109                                         'followed_by'         => $following,
110                                         'following_received'  => null,
111                                         'following_requested' => null
112                                 ]
113                         ]
114                 ];
115
116                 DI::apiResponse()->exit('relationship', ['relationship' => $relationship], $this->parameters['extension'] ?? null);
117         }
118 }