3 * @copyright Copyright (C) 2010-2022, 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\Contact;
25 use Friendica\Content\Widget;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Database\Database;
31 use Friendica\Model\Contact;
32 use Friendica\Model\User;
33 use Friendica\Module\Response;
34 use Friendica\Navigation\SystemMessages;
35 use Friendica\Util\Profiler;
36 use Friendica\Util\Strings;
37 use Psr\Log\LoggerInterface;
39 class Unfollow extends \Friendica\BaseModule
41 /** @var IHandleUserSessions */
44 /** @var SystemMessages */
45 private $systemMessages;
53 public function __construct(App\Page $page, Database $database, SystemMessages $systemMessages, IHandleUserSessions $userSession, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
55 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57 $this->userSession = $userSession;
58 $this->systemMessages = $systemMessages;
59 $this->database = $database;
63 protected function post(array $request = [])
65 if (!$this->userSession->getLocalUserId()) {
66 $this->systemMessages->addNotice($this->t('Permission denied.'));
67 $this->baseUrl->redirect('login');
70 $url = trim($request['url'] ?? '');
75 protected function content(array $request = []): string
77 $base_return_path = 'contact';
79 if (!$this->userSession->getLocalUserId()) {
80 $this->systemMessages->addNotice($this->t('Permission denied.'));
81 $this->baseUrl->redirect('login');
84 $uid = $this->userSession->getLocalUserId();
85 $url = trim($request['url']);
89 AND (`rel` = ? OR `rel` = ?)
90 AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
91 $this->userSession->getLocalUserId(),
92 Contact::SHARING, Contact::FRIEND,
93 Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
96 $contact = $this->database->selectFirst('contact', ['url', 'id', 'uid', 'network', 'addr', 'name'], $condition);
97 if (!$this->database->isResult($contact)) {
98 $this->systemMessages->addNotice($this->t("You aren't following this contact."));
99 $this->baseUrl->redirect($base_return_path);
102 if (!Protocol::supportsFollow($contact['network'])) {
103 $this->systemMessages->addNotice($this->t('Unfollowing is currently not supported by your network.'));
104 $this->baseUrl->redirect($base_return_path . '/' . $contact['id']);
107 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
109 $self = $this->database->selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
111 if (!$this->database->isResult($self)) {
112 $this->systemMessages->addNotice($this->t('Permission denied.'));
113 $this->baseUrl->redirect($base_return_path);
116 if (!empty($request['auto'])) {
117 $this->process($contact['url']);
120 $o = Renderer::replaceMacros($tpl, [
121 '$header' => $this->t('Disconnect/Unfollow'),
123 '$your_address' => $this->t('Your Identity Address:'),
124 '$invite_desc' => '',
125 '$submit' => $this->t('Submit Request'),
126 '$cancel' => $this->t('Cancel'),
127 '$url' => $contact['url'],
128 '$zrl' => Contact::magicLinkByContact($contact),
129 '$url_label' => $this->t('Profile URL'),
130 '$myaddr' => $self['url'],
131 '$action' => $this->baseUrl . '/contact/unfollow',
133 '$keywords_label' => ''
136 $this->page['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
138 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Status Messages and Posts')]);
140 // Show last public posts
141 $o .= Contact::getPostsFromUrl($contact['url']);
146 private function process(string $url): void
148 $base_return_path = 'contact';
150 $uid = $this->userSession->getLocalUserId();
152 $owner = User::getOwnerDataById($uid);
154 throw new \Friendica\Network\HTTPException\NotFoundException();
159 AND (`rel` = ? OR `rel` = ?)
160 AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
161 $uid, Contact::SHARING, Contact::FRIEND,
162 Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
164 $contact = $this->database->selectFirst('contact', [], $condition);
166 if (!$this->database->isResult($contact)) {
167 $this->systemMessages->addNotice($this->t("You aren't following this contact."));
168 $this->baseUrl->redirect($base_return_path);
171 $return_path = $base_return_path . '/' . $contact['id'];
174 Contact::unfollow($contact);
175 $notice_message = $this->t('Contact was successfully unfollowed');
176 } catch (\Exception $e) {
177 $this->logger->error($e->getMessage(), ['contact' => $contact]);
178 $notice_message = $this->t('Unable to unfollow this contact, please contact your administrator');
181 $this->systemMessages->addNotice($notice_message);
182 $this->baseUrl->redirect($return_path);