From 197c77f43dcbe08743366ed35a83e5affd284872 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 8 Nov 2021 06:24:39 -0500 Subject: [PATCH] Move contact conversation to its own module class --- mod/update_contact.php | 25 +++--- src/Module/Contact.php | 52 +----------- src/Module/Contact/Conversations.php | 116 +++++++++++++++++++++++++++ static/routes.config.php | 2 +- 4 files changed, 133 insertions(+), 62 deletions(-) create mode 100644 src/Module/Contact/Conversations.php diff --git a/mod/update_contact.php b/mod/update_contact.php index 7dccb103d1..8eeb9facf1 100644 --- a/mod/update_contact.php +++ b/mod/update_contact.php @@ -23,22 +23,25 @@ use Friendica\App; use Friendica\Core\System; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Post; -use Friendica\Module\Contact; +use Friendica\Model\Contact; function update_contact_content(App $a) { - if (!empty(DI::args()->getArgv()[1]) && (!empty($_GET['force']) || !DI::pConfig()->get(local_user(), 'system', 'no_auto_update'))) { - if (!empty($_GET['item'])) { - $item = Post::selectFirst(['parent'], ['id' => $_GET['item']]); - $parentid = $item['parent'] ?? 0; - } else { - $parentid = 0; + if (!empty(DI::args()->get(1)) && (!empty($_GET['force']) || !DI::pConfig()->get(local_user(), 'system', 'no_auto_update'))) { + $contact = Contact::getById(DI::args()->get(1), ['id', 'deleted']); + if (DBA::isResult($contact) && empty($contact['deleted'])) { + DI::page()['aside'] = ''; + + if (!empty($_GET['item'])) { + $item = Post::selectFirst(['parent'], ['id' => $_GET['item']]); + } + + $text = Contact::getPostsFromId($contact['id'], true, true, $item['parent'] ?? 0); } - $text = Contact::getConversationsHMTL($a, DI::args()->getArgv()[1], true, $parentid); - } else { - $text = ''; } - System::htmlUpdateExit($text); + + System::htmlUpdateExit($text ?? ''); } diff --git a/src/Module/Contact.php b/src/Module/Contact.php index fc518d1045..1bc277f2ef 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -232,8 +232,6 @@ class Contact extends BaseModule return Login::form($_SERVER['REQUEST_URI']); } - $a = DI::app(); - $search = trim($_GET['search'] ?? ''); $nets = trim($_GET['nets'] ?? ''); $rel = trim($_GET['rel'] ?? ''); @@ -251,9 +249,7 @@ class Contact extends BaseModule $contact = null; // @TODO: Replace with parameter from router - if (DI::args()->getArgc() == 2 && intval(DI::args()->getArgv()[1]) - || DI::args()->getArgc() == 3 && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['conversations']) - ) { + if (DI::args()->getArgc() == 2 && intval(DI::args()->getArgv()[1])) { $contact_id = intval(DI::args()->getArgv()[1]); // Ensure to use the user contact when the public contact was provided @@ -278,12 +274,7 @@ class Contact extends BaseModule if (DBA::isResult($contact)) { if ($contact['self']) { - // @TODO: Replace with parameter from router - if ((DI::args()->getArgc() == 3) && intval(DI::args()->getArgv()[1]) && in_array(DI::args()->getArgv()[2], ['conversations'])) { - DI::baseUrl()->redirect('profile/' . $contact['nick']); - } else { - DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile'); - } + DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile'); } $vcard_widget = Widget\VCard::getHTML($contact); @@ -338,10 +329,6 @@ class Contact extends BaseModule throw new NotFoundException(DI::l10n()->t('Contact not found')); } - if ($cmd === 'conversations') { - return self::getConversationsHMTL($a, $contact_id, $update); - } - self::checkFormSecurityTokenRedirectOnError('contact/' . $contact_id, 'contact_action', 't'); $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user()); @@ -866,41 +853,6 @@ class Contact extends BaseModule return $tab_str; } - public static function getConversationsHMTL($a, $contact_id, $update, $parent = 0) - { - $o = ''; - - if (!$update) { - // We need the editor here to be able to reshare an item. - if (local_user()) { - $o = DI::conversation()->statusEditor([], 0, true); - } - } - - $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]); - - if (!$update) { - $o .= self::getTabsHTML($contact, self::TAB_CONVERSATIONS); - } - - if (DBA::isResult($contact)) { - if (!$update) { - $profiledata = Model\Contact::getByURLForUser($contact['url'], local_user()); - DI::page()['aside'] = Widget\VCard::getHTML($profiledata); - } else { - DI::page()['aside'] = ''; - } - - if ($contact['uid'] == 0) { - $o .= Model\Contact::getPostsFromId($contact['id'], true, $update, $parent); - } else { - $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update, $parent); - } - } - - return $o; - } - /** * Return the fields for the contact template * diff --git a/src/Module/Contact/Conversations.php b/src/Module/Contact/Conversations.php new file mode 100644 index 0000000000..2c1cf91d6b --- /dev/null +++ b/src/Module/Contact/Conversations.php @@ -0,0 +1,116 @@ +. + * + */ + +namespace Friendica\Module\Contact; + +use Friendica\App; +use Friendica\BaseModule; +use Friendica\Contact\LocalRelationship\Repository\LocalRelationship; +use Friendica\Content\Conversation; +use Friendica\Content\Nav; +use Friendica\Content\Widget; +use Friendica\Core\L10n; +use Friendica\Core\Protocol; +use Friendica\Core\Theme; +use Friendica\Model; +use Friendica\Module\Contact; +use Friendica\Module\Security\Login; +use Friendica\Network\HTTPException\NotFoundException; + +/** + * Manages and show Contacts and their content + */ +class Conversations extends BaseModule +{ + /** + * @var App\Page + */ + private $page; + /** + * @var Conversation + */ + private $conversation; + /** + * @var App\BaseURL + */ + private $baseUrl; + /** + * @var LocalRelationship + */ + private $localRelationship; + + public function __construct(L10n $l10n, LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Page $page, Conversation $conversation, array $parameters = []) + { + parent::__construct($l10n, $parameters); + + $this->page = $page; + $this->conversation = $conversation; + $this->baseUrl = $baseUrl; + $this->localRelationship = $localRelationship; + } + + public function content(): string + { + if (!local_user()) { + return Login::form($_SERVER['REQUEST_URI']); + } + + // Backward compatibility: Ensure to use the public contact when the user contact is provided + // Remove by version 2022.03 + $data = Model\Contact::getPublicAndUserContactID(intval($this->parameters['id']), local_user()); + if (empty($data)) { + throw new NotFoundException($this->t('Contact not found.')); + } + + $contact = Model\Contact::getById($data['public']); + if (empty($contact)) { + throw new NotFoundException($this->t('Contact not found.')); + } + + // Don't display contacts that are about to be deleted + if (!empty($contact['deleted']) || !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) { + throw new NotFoundException($this->t('Contact not found.')); + } + + $localRelationship = $this->localRelationship->getForUserContact(local_user(), $contact['id']); + if ($localRelationship->rel === Model\Contact::SELF) { + $this->baseUrl->redirect('profile/' . $contact['nick']); + } + + // Load necessary libraries for the status editor + $this->page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js')); + $this->page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js')); + $this->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css')); + $this->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css')); + + $this->page['aside'] .= Widget\VCard::getHTML($contact); + + Nav::setSelected('contact'); + + // We need the editor here to be able to reshare an item. + $o = $this->conversation->statusEditor([], 0, true); + + $o .= Contact::getTabsHTML($contact, Contact::TAB_CONVERSATIONS); + $o .= Model\Contact::getPostsFromId($contact['id'], true); + + return $o; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 36df142a64..2c68940068 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -333,7 +333,7 @@ return [ '/{id:\d+}/archive' => [Module\Contact::class, [R::GET]], '/{id:\d+}/advanced' => [Module\Contact\Advanced::class, [R::GET, R::POST]], '/{id:\d+}/block' => [Module\Contact::class, [R::GET]], - '/{id:\d+}/conversations' => [Module\Contact::class, [R::GET]], + '/{id:\d+}/conversations' => [Module\Contact\Conversations::class, [R::GET]], '/{id:\d+}/contacts[/{type}]' => [Module\Contact\Contacts::class, [R::GET]], '/{id:\d+}/ignore' => [Module\Contact::class, [R::GET]], '/{id:\d+}/media' => [Module\Contact\Media::class, [R::GET]],