3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Twitter\Friendships;
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;
41 * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy.html
43 class Destroy extends ContactEndpoint
45 /** @var TwitterUser */
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 = [])
50 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52 $this->twitterUser = $twitterUser;
55 protected function post(array $request = [])
57 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
58 $uid = BaseApi::getCurrentUserID();
60 $owner = User::getOwnerDataById($uid);
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');
66 $contact_id = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), 0);
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');
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');
80 $contact = Contact::getById($cdata['user']);
81 $user = $this->twitterUser->createFromContactId($contact_id, $uid, true)->toArray();
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');
90 $this->response->exit('friendships', ['user' => $user], $this->parameters['extension'] ?? null);