]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Friendships/Destroy.php
spelling: days
[friendica.git] / src / Module / Api / Twitter / Friendships / Destroy.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 Exception;
25 use Friendica\App;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Logger;
28 use Friendica\Factory\Api\Twitter\User as TwitterUser;
29 use Friendica\Model\Contact;
30 use Friendica\Model\User;
31 use Friendica\Module\Api\ApiResponse;
32 use Friendica\Module\Api\Twitter\ContactEndpoint;
33 use Friendica\Module\BaseApi;
34 use Friendica\Network\HTTPException;
35 use Friendica\Util\Profiler;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Unfollow Contact
40  *
41  * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html
42  */
43 class Destroy extends ContactEndpoint
44 {
45         /** @var TwitterUser */
46         private $twitterUser;
47
48         public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, TwitterUser $twitterUser, array $server, array $parameters = [])
49         {
50                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
51
52                 $this->twitterUser = $twitterUser;
53         }
54
55         protected function post(array $request = [])
56         {
57                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
58                 $uid = BaseApi::getCurrentUserID();
59
60                 $owner = User::getOwnerDataById($uid);
61                 if (!$owner) {
62                         Logger::notice(BaseApi::LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]);
63                         throw new HTTPException\NotFoundException('Error Processing Request');
64                 }
65
66                 $contact_id = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), 0);
67
68                 if (empty($contact_id)) {
69                         Logger::notice(BaseApi::LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']);
70                         throw new HTTPException\BadRequestException('no user_id specified');
71                 }
72
73                 // Get Contact by given id
74                 $cdata = Contact::getPublicAndUserContactID($contact_id, $uid);
75                 if (!empty($cdata['user'])) {
76                         Logger::notice(BaseApi::LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']);
77                         throw new HTTPException\NotFoundException('Not following Contact');
78                 }
79
80                 $contact = Contact::getById($cdata['user']);
81                 $user    = $this->twitterUser->createFromContactId($contact_id, $uid, true)->toArray();
82
83                 try {
84                         Contact::unfollow($contact);
85                 } catch (Exception $e) {
86                         Logger::error(BaseApi::LOG_PREFIX . $e->getMessage(), ['contact' => $contact]);
87                         throw new HTTPException\InternalServerErrorException('Unable to unfollow this contact, please contact your administrator');
88                 }
89
90                 $this->response->exit('friendships', ['user' => $user], $this->parameters['extension'] ?? null);
91         }
92 }