]> git.mxchange.org Git - friendica.git/commitdiff
Create Contact\Unfollow module class
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 30 Oct 2022 19:51:41 +0000 (15:51 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 31 Oct 2022 19:31:44 +0000 (15:31 -0400)
src/Content/Widget/VCard.php
src/Model/Contact.php
src/Model/Profile.php
src/Module/Contact/Unfollow.php [new file with mode: 0644]
static/routes.config.php

index 9794270e2685cb9348578840f930c2eef5befb56..1edbf63c8c17e9af3a7e7b4eeb73d0d120a7594d 100644 (file)
@@ -83,7 +83,7 @@ class VCard
 
                        if (empty($contact['self']) && Protocol::supportsFollow($contact['network'])) {
                                if (in_array($rel, [Contact::SHARING, Contact::FRIEND])) {
-                                       $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
+                                       $unfollow_link = 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1';
                                } elseif (!$pending) {
                                        $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1';
                                }
index 91e89b2f2f558f52be9e2241854e3ee47eed8649..0ae7003f05df1666d7b8bf2506db484bf952d31c 100644 (file)
@@ -1176,7 +1176,7 @@ class Contact
                $unfollow_link = '';
                if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
                        if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
-                               $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
+                               $unfollow_link = 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1';
                        } elseif(!$contact['pending']) {
                                $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1';
                        }
index 8538bfde2c9b766919fb9898a7b5c5bca33d8645..b73b235385047b59d3e41343ac087097403457cb 100644 (file)
@@ -339,9 +339,9 @@ class Profile
                                }
                        } else {
                                if ($visitor_is_following) {
-                                       $unfollow_link = $visitor_base_path . '/unfollow?url=' . urlencode($profile_url) . '&auto=1';
+                                       $unfollow_link = $visitor_base_path . '/contact/unfollow?url=' . urlencode($profile_url) . '&auto=1';
                                } else {
-                                       $follow_link =  $visitor_base_path .'/follow?url=' . urlencode($profile_url) . '&auto=1';
+                                       $follow_link =  $visitor_base_path . '/follow?url=' . urlencode($profile_url) . '&auto=1';
                                }
                        }
 
diff --git a/src/Module/Contact/Unfollow.php b/src/Module/Contact/Unfollow.php
new file mode 100644 (file)
index 0000000..913638a
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Contact;
+
+use Friendica\App;
+use Friendica\Content\Widget;
+use Friendica\Core\L10n;
+use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Database\Database;
+use Friendica\Model\Contact;
+use Friendica\Model\User;
+use Friendica\Module\Response;
+use Friendica\Navigation\SystemMessages;
+use Friendica\Util\Profiler;
+use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
+
+class Unfollow extends \Friendica\BaseModule
+{
+       /** @var IHandleUserSessions */
+       private $userSession;
+
+       /** @var SystemMessages */
+       private $systemMessages;
+
+       /** @var Database */
+       private $database;
+
+       /** @var App\Page */
+       private $page;
+
+       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 = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->userSession    = $userSession;
+               $this->systemMessages = $systemMessages;
+               $this->database       = $database;
+               $this->page           = $page;
+       }
+
+       protected function post(array $request = [])
+       {
+               if (!$this->userSession->getLocalUserId()) {
+                       $this->systemMessages->addNotice($this->t('Permission denied.'));
+                       $this->baseUrl->redirect('login');
+               }
+
+               $url = trim($request['url'] ?? '');
+
+               $this->process($url);
+       }
+
+       protected function content(array $request = []): string
+       {
+               $base_return_path = 'contact';
+
+               if (!$this->userSession->getLocalUserId()) {
+                       $this->systemMessages->addNotice($this->t('Permission denied.'));
+                       $this->baseUrl->redirect('login');
+               }
+
+               $uid = $this->userSession->getLocalUserId();
+               $url = trim($request['url']);
+
+               $condition = [
+                       "`uid` = ?
+                       AND (`rel` = ? OR `rel` = ?)
+                       AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
+                       $this->userSession->getLocalUserId(),
+                       Contact::SHARING, Contact::FRIEND,
+                       Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
+               ];
+
+               $contact = $this->database->selectFirst('contact', ['url', 'id', 'uid', 'network', 'addr', 'name'], $condition);
+               if (!$this->database->isResult($contact)) {
+                       $this->systemMessages->addNotice($this->t("You aren't following this contact."));
+                       $this->baseUrl->redirect($base_return_path);
+               }
+
+               if (!Protocol::supportsFollow($contact['network'])) {
+                       $this->systemMessages->addNotice($this->t('Unfollowing is currently not supported by your network.'));
+                       $this->baseUrl->redirect($base_return_path . '/' . $contact['id']);
+               }
+
+               $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
+
+               $self = $this->database->selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
+
+               if (!$this->database->isResult($self)) {
+                       $this->systemMessages->addNotice($this->t('Permission denied.'));
+                       $this->baseUrl->redirect($base_return_path);
+               }
+
+               if (!empty($request['auto'])) {
+                       $this->process($contact['url']);
+               }
+
+               $o = Renderer::replaceMacros($tpl, [
+                       '$header'         => $this->t('Disconnect/Unfollow'),
+                       '$page_desc'      => '',
+                       '$your_address'   => $this->t('Your Identity Address:'),
+                       '$invite_desc'    => '',
+                       '$submit'         => $this->t('Submit Request'),
+                       '$cancel'         => $this->t('Cancel'),
+                       '$url'            => $contact['url'],
+                       '$zrl'            => Contact::magicLinkByContact($contact),
+                       '$url_label'      => $this->t('Profile URL'),
+                       '$myaddr'         => $self['url'],
+                       '$action'         => $this->baseUrl . '/contact/unfollow',
+                       '$keywords'       => '',
+                       '$keywords_label' => ''
+               ]);
+
+               $this->page['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
+
+               $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Status Messages and Posts')]);
+
+               // Show last public posts
+               $o .= Contact::getPostsFromUrl($contact['url']);
+
+               return $o;
+       }
+
+       private function process(string $url): void
+       {
+               $base_return_path = 'contact';
+
+               $uid = $this->userSession->getLocalUserId();
+
+               $owner = User::getOwnerDataById($uid);
+               if (!$owner) {
+                       throw new \Friendica\Network\HTTPException\NotFoundException();
+               }
+
+               $condition = [
+                       "`uid` = ?
+                       AND (`rel` = ? OR `rel` = ?)
+                       AND (`nurl` = ? OR `alias` = ? OR `alias` = ?)",
+                       $uid, Contact::SHARING, Contact::FRIEND,
+                       Strings::normaliseLink($url), Strings::normaliseLink($url), $url,
+               ];
+               $contact = $this->database->selectFirst('contact', [], $condition);
+
+               if (!$this->database->isResult($contact)) {
+                       $this->systemMessages->addNotice($this->t("You aren't following this contact."));
+                       $this->baseUrl->redirect($base_return_path);
+               }
+
+               $return_path = $base_return_path . '/' . $contact['id'];
+
+               try {
+                       Contact::unfollow($contact);
+                       $notice_message = $this->t('Contact was successfully unfollowed');
+               } catch (\Exception $e) {
+                       $this->logger->error($e->getMessage(), ['contact' => $contact]);
+                       $notice_message = $this->t('Unable to unfollow this contact, please contact your administrator');
+               }
+
+               $this->systemMessages->addNotice($notice_message);
+               $this->baseUrl->redirect($return_path);
+       }
+}
index c5685acea8d4aa88fba79225e9a69045b5cf7891..62742b6593e7a940e1946d53f3c4ebf3bf3321ca 100644 (file)
@@ -387,6 +387,7 @@ return [
                '/hidden'                     => [Module\Contact::class,           [R::GET]],
                '/ignored'                    => [Module\Contact::class,           [R::GET]],
                '/hovercard'                  => [Module\Contact\Hovercard::class, [R::GET]],
+               '/unfollow'                   => [Module\Contact\Unfollow::class,  [R::GET, R::POST]],
        ],
 
        '/credits'                  => [Module\Credits::class,          [R::GET]],