From 204b077353fbea2696421afc41bfdf76b996477d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 11 Nov 2021 09:43:38 -0500 Subject: [PATCH] Move contact profile GET actions to separate module class --- src/Module/Contact.php | 143 +++------------------------------ src/Module/Contact/Profile.php | 63 +++++++++++++++ static/routes.config.php | 7 +- 3 files changed, 76 insertions(+), 137 deletions(-) diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 8969b1610..2db4af5e9 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -25,9 +25,7 @@ use Friendica\BaseModule; use Friendica\Content\ContactSelector; use Friendica\Content\Nav; use Friendica\Content\Pager; -use Friendica\Content\Text\BBCode; use Friendica\Content\Widget; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\Core\Theme; @@ -37,10 +35,7 @@ use Friendica\DI; use Friendica\Model; use Friendica\Model\User; use Friendica\Module\Security\Login; -use Friendica\Network\HTTPException\BadRequestException; use Friendica\Network\HTTPException\NotFoundException; -use Friendica\Util\DateTimeFormat; -use Friendica\Util\Strings; /** * Manages and show Contacts and their content @@ -116,7 +111,7 @@ class Contact extends BaseModule * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function updateContactFromPoll(int $contact_id) + public static function updateContactFromPoll(int $contact_id) { $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]); if (!DBA::isResult($contact)) { @@ -137,22 +132,6 @@ class Contact extends BaseModule } } - /** - * @param int $contact_id Id of the contact with uid != 0 - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - * @throws \ImagickException - */ - private static function updateContactFromProbe(int $contact_id) - { - $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]); - if (!DBA::isResult($contact)) { - return; - } - - // Update the entry in the contact table - Model\Contact::updateFromProbe($contact_id); - } - /** * Toggles the blocked status of a contact identified by id. * @@ -199,64 +178,19 @@ class Contact extends BaseModule $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css')); $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css')); - $contact = null; - // @TODO: Replace with parameter from router - 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 - $data = Model\Contact::getPublicAndUserContactID($contact_id, local_user()); - if (!empty($data['user']) && ($contact_id == $data['public'])) { - $contact_id = $data['user']; - } - - if (!empty($data)) { - $contact = DBA::selectFirst('contact', [], [ - 'id' => $contact_id, - 'uid' => [0, local_user()], - 'deleted' => false - ]); - - // Don't display contacts that are about to be deleted - if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) { - $contact = false; - } - } - } - - if (DBA::isResult($contact)) { - if ($contact['self']) { - DI::baseUrl()->redirect('profile/' . $contact['nick'] . '/profile'); - } - - $vcard_widget = Widget\VCard::getHTML($contact); - - $findpeople_widget = ''; - $follow_widget = ''; - $account_widget = ''; - $networks_widget = ''; - $rel_widget = ''; - - if ($contact['uid'] != 0) { - $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id); - } else { - $groups_widget = ''; - } + $vcard_widget = ''; + $findpeople_widget = Widget::findPeople(); + if (isset($_GET['add'])) { + $follow_widget = Widget::follow($_GET['add']); } else { - $vcard_widget = ''; - $findpeople_widget = Widget::findPeople(); - if (isset($_GET['add'])) { - $follow_widget = Widget::follow($_GET['add']); - } else { - $follow_widget = Widget::follow(); - } - - $account_widget = Widget::accounttypes($_SERVER['REQUEST_URI'], $accounttype); - $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets); - $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel); - $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group); + $follow_widget = Widget::follow(); } + $account_widget = Widget::accounttypes($_SERVER['REQUEST_URI'], $accounttype); + $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets); + $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel); + $groups_widget = Widget::groups($_SERVER['REQUEST_URI'], $group); + DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $account_widget . $groups_widget . $networks_widget . $rel_widget; $tpl = Renderer::getMarkupTemplate('contacts-head.tpl'); @@ -267,61 +201,6 @@ class Contact extends BaseModule $o = ''; Nav::setSelected('contact'); - if (DI::args()->getArgc() == 3) { - $contact_id = intval(DI::args()->getArgv()[1]); - if (!$contact_id) { - throw new BadRequestException(); - } - - // @TODO: Replace with parameter from router - $cmd = DI::args()->getArgv()[2]; - - $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]); - if (!DBA::isResult($orig_record)) { - throw new NotFoundException(DI::l10n()->t('Contact not found')); - } - - self::checkFormSecurityTokenRedirectOnError('contact/' . $contact_id, 'contact_action', 't'); - - $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user()); - if (empty($cdata)) { - throw new NotFoundException(DI::l10n()->t('Contact not found')); - } - - if ($cmd === 'update' && $cdata['user']) { - self::updateContactFromPoll($cdata['user']); - } - - if ($cmd === 'updateprofile' && $cdata['user']) { - self::updateContactFromProbe($cdata['user']); - } - - if ($cmd === 'block') { - if (public_contact() === $cdata['public']) { - throw new BadRequestException(DI::l10n()->t('You can\'t block yourself')); - } - - self::toggleBlockContact($cdata['public'], local_user()); - - $blocked = Model\Contact\User::isBlocked($contact_id, local_user()); - info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked'))); - } - - if ($cmd === 'ignore') { - if (public_contact() === $cdata['public']) { - throw new BadRequestException(DI::l10n()->t('You can\'t ignore yourself')); - } - - self::toggleIgnoreContact($cdata['public']); - - $ignored = Model\Contact\User::isIgnored($cdata['public'], local_user()); - info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored'))); - } - - DI::baseUrl()->redirect('contact/' . $contact_id); - // NOTREACHED - } - $_SESSION['return_path'] = DI::args()->getQueryString(); $sql_values = [local_user()]; diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php index 95d4a59f9..1af171872 100644 --- a/src/Module/Contact/Profile.php +++ b/src/Module/Contact/Profile.php @@ -164,6 +164,53 @@ class Profile extends BaseModule $this->baseUrl->redirect('profile/' . $contact['nick'] . '/profile'); } + if (isset($parameters['action'])) { + self::checkFormSecurityTokenRedirectOnError('contact/' . $contact['id'], 'contact_action', 't'); + + $cmd = $parameters['action']; + if ($cmd === 'update' && $localRelationship->rel !== Contact::NOTHING) { + Module\Contact::updateContactFromPoll($contact['id']); + } + + if ($cmd === 'updateprofile' && $localRelationship->rel !== Contact::NOTHING) { + self::updateContactFromProbe($contact['id']); + } + + if ($cmd === 'block') { + if ($localRelationship->blocked) { + // @TODO Backward compatibility, replace with $localRelationship->unblock() + Contact\User::setBlocked($contact['id'], local_user(), false); + + $message = $this->t('Contact has been unblocked'); + } else { + // @TODO Backward compatibility, replace with $localRelationship->block() + Contact\User::setBlocked($contact['id'], local_user(), true); + $message = $this->t('Contact has been blocked'); + } + + // @TODO: add $this->localRelationship->save($localRelationship); + info($message); + } + + if ($cmd === 'ignore') { + if ($localRelationship->ignored) { + // @TODO Backward compatibility, replace with $localRelationship->unblock() + Contact\User::setIgnored($contact['id'], local_user(), false); + + $message = $this->t('Contact has been unignored'); + } else { + // @TODO Backward compatibility, replace with $localRelationship->block() + Contact\User::setIgnored($contact['id'], local_user(), true); + $message = $this->t('Contact has been ignored'); + } + + // @TODO: add $this->localRelationship->save($localRelationship); + info($message); + } + + $this->baseUrl->redirect('contact/' . $contact['id']); + } + $vcard_widget = Widget\VCard::getHTML($contact); $groups_widget = ''; @@ -433,4 +480,20 @@ class Profile extends BaseModule return $contact_actions; } + + /** + * @param int $contact_id Id of the contact with uid != 0 + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + private static function updateContactFromProbe(int $contact_id) + { + $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]); + if (!DBA::isResult($contact)) { + return; + } + + // Update the entry in the contact table + Contact::updateFromProbe($contact_id); + } } diff --git a/static/routes.config.php b/static/routes.config.php index 3fa38a385..efedbb334 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -330,18 +330,15 @@ return [ '/contact' => [ '[/]' => [Module\Contact::class, [R::GET]], '/{id:\d+}[/]' => [Module\Contact\Profile::class, [R::GET, R::POST]], - '/{id:\d+}/archive' => [Module\Contact::class, [R::GET]], + '/{id:\d+}/{action:block|ignore|update|updateprofile}' + => [Module\Contact\Profile::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\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]], '/{id:\d+}/poke' => [Module\Contact\Poke::class, [R::GET, R::POST]], '/{id:\d+}/posts' => [Module\Contact\Posts::class, [R::GET]], '/{id:\d+}/revoke' => [Module\Contact\Revoke::class, [R::GET, R::POST]], - '/{id:\d+}/update' => [Module\Contact::class, [R::GET]], - '/{id:\d+}/updateprofile' => [Module\Contact::class, [R::GET]], '/archived' => [Module\Contact::class, [R::GET]], '/batch' => [Module\Contact::class, [R::GET, R::POST]], '/pending' => [Module\Contact::class, [R::GET]],