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