]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Contact/Profile.php
Merge pull request #13337 from annando/block-chatgpt
[friendica.git] / src / Module / Contact / Profile.php
index 8ab8dae60e98a4e4906b1b0eae23c70768565d48..090f7cd86a21c1e4011b3d0e212e9c00261aeb7c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -35,8 +35,9 @@ use Friendica\Core\L10n;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\Contact;
-use Friendica\Model\Group;
+use Friendica\Model\Circle;
 use Friendica\Module;
 use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
@@ -73,7 +74,7 @@ class Profile extends BaseModule
 
        protected function post(array $request = [])
        {
-               if (!local_user()) {
+               if (!DI::userSession()->getLocalUserId()) {
                        return;
                }
 
@@ -81,7 +82,7 @@ class Profile extends BaseModule
 
                // Backward compatibility: The update still needs a user-specific contact ID
                // Change to user-contact table check by version 2022.03
-               $cdata = Contact::getPublicAndUserContactID($contact_id, local_user());
+               $cdata = Contact::getPublicAndUserContactID($contact_id, DI::userSession()->getLocalUserId());
                if (empty($cdata['user']) || !DBA::exists('contact', ['id' => $cdata['user'], 'deleted' => false])) {
                        return;
                }
@@ -123,20 +124,20 @@ class Profile extends BaseModule
                        $fields['info'] = $_POST['info'];
                }
 
-               if (!Contact::update($fields, ['id' => $cdata['user'], 'uid' => local_user()])) {
-                       notice($this->t('Failed to update contact record.'));
+               if (!Contact::update($fields, ['id' => $cdata['user'], 'uid' => DI::userSession()->getLocalUserId()])) {
+                       DI::sysmsg()->addNotice($this->t('Failed to update contact record.'));
                }
        }
 
        protected function content(array $request = []): string
        {
-               if (!local_user()) {
+               if (!DI::userSession()->getLocalUserId()) {
                        return Module\Security\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 = Contact::getPublicAndUserContactID(intval($this->parameters['id']), local_user());
+               $data = Contact::getPublicAndUserContactID(intval($this->parameters['id']), DI::userSession()->getLocalUserId());
                if (empty($data)) {
                        throw new HTTPException\NotFoundException($this->t('Contact not found.'));
                }
@@ -151,7 +152,7 @@ class Profile extends BaseModule
                        throw new HTTPException\NotFoundException($this->t('Contact not found.'));
                }
 
-               $localRelationship = $this->localRelationship->getForUserContact(local_user(), $contact['id']);
+               $localRelationship = $this->localRelationship->getForUserContact(DI::userSession()->getLocalUserId(), $contact['id']);
 
                if ($localRelationship->rel === Contact::SELF) {
                        $this->baseUrl->redirect('profile/' . $contact['nick'] . '/profile');
@@ -165,53 +166,69 @@ class Profile extends BaseModule
                                Module\Contact::updateContactFromPoll($contact['id']);
                        }
 
-                       if ($cmd === 'updateprofile' && $localRelationship->rel !== Contact::NOTHING) {
+                       if ($cmd === 'updateprofile') {
                                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);
+                                       Contact\User::setBlocked($contact['id'], DI::userSession()->getLocalUserId(), false);
 
                                        $message = $this->t('Contact has been unblocked');
                                } else {
                                        // @TODO Backward compatibility, replace with $localRelationship->block()
-                                       Contact\User::setBlocked($contact['id'], local_user(), true);
+                                       Contact\User::setBlocked($contact['id'], DI::userSession()->getLocalUserId(), true);
                                        $message = $this->t('Contact has been blocked');
                                }
 
                                // @TODO: add $this->localRelationship->save($localRelationship);
-                               info($message);
+                               DI::sysmsg()->addInfo($message);
                        }
 
                        if ($cmd === 'ignore') {
                                if ($localRelationship->ignored) {
                                        // @TODO Backward compatibility, replace with $localRelationship->unblock()
-                                       Contact\User::setIgnored($contact['id'], local_user(), false);
+                                       Contact\User::setIgnored($contact['id'], DI::userSession()->getLocalUserId(), false);
 
                                        $message = $this->t('Contact has been unignored');
                                } else {
                                        // @TODO Backward compatibility, replace with $localRelationship->block()
-                                       Contact\User::setIgnored($contact['id'], local_user(), true);
+                                       Contact\User::setIgnored($contact['id'], DI::userSession()->getLocalUserId(), true);
                                        $message = $this->t('Contact has been ignored');
                                }
 
                                // @TODO: add $this->localRelationship->save($localRelationship);
-                               info($message);
+                               DI::sysmsg()->addInfo($message);
+                       }
+
+                       if ($cmd === 'collapse') {
+                               if ($localRelationship->collapsed) {
+                                       // @TODO Backward compatibility, replace with $localRelationship->unblock()
+                                       Contact\User::setCollapsed($contact['id'], DI::userSession()->getLocalUserId(), false);
+
+                                       $message = $this->t('Contact has been uncollapsed');
+                               } else {
+                                       // @TODO Backward compatibility, replace with $localRelationship->block()
+                                       Contact\User::setCollapsed($contact['id'], DI::userSession()->getLocalUserId(), true);
+                                       $message = $this->t('Contact has been collapsed');
+                               }
+
+                               // @TODO: add $this->localRelationship->save($localRelationship);
+                               DI::sysmsg()->addInfo($message);
                        }
 
                        $this->baseUrl->redirect('contact/' . $contact['id']);
                }
 
                $vcard_widget  = Widget\VCard::getHTML($contact);
-               $groups_widget = '';
+               $circles_widget = '';
 
                if (!in_array($localRelationship->rel, [Contact::NOTHING, Contact::SELF])) {
-                       $groups_widget = Group::sidebarWidget('contact', 'group', 'full', 'everyone', $data['user']);
+                       $circles_widget = Circle::sidebarWidget('contact', 'circle', 'full', 'everyone', $data['user']);
                }
 
-               $this->page['aside'] .= $vcard_widget . $groups_widget;
+               $this->page['aside'] .= $vcard_widget . $circles_widget;
 
                $o = '';
                Nav::setSelected('contact');
@@ -219,12 +236,8 @@ class Profile extends BaseModule
                $_SESSION['return_path'] = $this->args->getQueryString();
 
                $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
-                       '$baseurl' => $this->baseUrl->get(true),
                ]);
 
-               $contact['blocked']  = Contact\User::isBlocked($contact['id'], local_user());
-               $contact['readonly'] = Contact\User::isIgnored($contact['id'], local_user());
-
                switch ($localRelationship->rel) {
                        case Contact::FRIEND:   $relation_text = $this->t('You are mutual friends with %s', $contact['name']); break;
                        case Contact::FOLLOWER: $relation_text = $this->t('You are sharing with %s', $contact['name']); break;
@@ -233,12 +246,12 @@ class Profile extends BaseModule
                                $relation_text = '';
                }
 
-               if (!in_array($contact['network'], array_merge(Protocol::FEDERATED, [Protocol::TWITTER]))) {
+               if (!Protocol::supportsFollow($contact['network'])) {
                        $relation_text = '';
                }
 
                $url = Contact::magicLinkByContact($contact);
-               if (strpos($url, 'redir/') === 0) {
+               if (strpos($url, 'contact/redir/') === 0) {
                        $sparkle = ' class="sparkle" ';
                } else {
                        $sparkle = '';
@@ -270,10 +283,10 @@ class Profile extends BaseModule
                                $localRelationship->fetchFurtherInformation,
                                $this->t('Fetch information like preview pictures, title and teaser from the feed item. You can activate this if the feed doesn\'t contain much text. Keywords are taken from the meta header in the feed item and are posted as hash tags.'),
                                [
-                                       '0' => $this->t('Disabled'),
-                                       '1' => $this->t('Fetch information'),
-                                       '3' => $this->t('Fetch keywords'),
-                                       '2' => $this->t('Fetch information and keywords')
+                                       Entity\LocalRelationship::FFI_NONE        => $this->t('Disabled'),
+                                       Entity\LocalRelationship::FFI_INFORMATION => $this->t('Fetch information'),
+                                       Entity\LocalRelationship::FFI_KEYWORD     => $this->t('Fetch keywords'),
+                                       Entity\LocalRelationship::FFI_BOTH        => $this->t('Fetch information and keywords')
                                ]
                        ];
                }
@@ -284,7 +297,6 @@ class Profile extends BaseModule
                if ($contact['network'] == Protocol::FEED) {
                        $remote_self_options = [
                                Contact::MIRROR_DEACTIVATED => $this->t('No mirroring'),
-                               Contact::MIRROR_FORWARDED   => $this->t('Mirror as forwarded posting'),
                                Contact::MIRROR_OWN_POST    => $this->t('Mirror as my own posting')
                        ];
                } elseif ($contact['network'] == Protocol::ACTIVITYPUB) {
@@ -327,7 +339,7 @@ class Profile extends BaseModule
                        '$submit'                    => $this->t('Submit'),
                        '$lbl_info1'                 => $lbl_info1,
                        '$lbl_info2'                 => $this->t('Their personal note'),
-                       '$reason'                    => trim($contact['reason']),
+                       '$reason'                    => trim($contact['reason'] ?? ''),
                        '$infedit'                   => $this->t('Edit contact notes'),
                        '$common_link'               => 'contact/' . $contact['id'] . '/contacts/common',
                        '$relation_text'             => $relation_text,
@@ -345,15 +357,13 @@ class Profile extends BaseModule
                        '$last_update'               => $last_update,
                        '$udnow'                     => $this->t('Update now'),
                        '$contact_id'                => $contact['id'],
-                       '$block_text'                => ($contact['blocked'] ? $this->t('Unblock') : $this->t('Block')),
-                       '$ignore_text'               => ($contact['readonly'] ? $this->t('Unignore') : $this->t('Ignore')),
+                       '$pending'                   => $localRelationship->pending   ? $this->t('Awaiting connection acknowledge') : '',
+                       '$blocked'                   => $localRelationship->blocked   ? $this->t('Currently blocked') : '',
+                       '$ignored'                   => $localRelationship->ignored   ? $this->t('Currently ignored') : '',
+                       '$collapsed'                 => $localRelationship->collapsed ? $this->t('Currently collapsed') : '',
+                       '$archived'                  => ($contact['archive'] ? $this->t('Currently archived') : ''),
                        '$insecure'                  => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
-                       '$info'                      => $localRelationship->info,
                        '$cinfo'                     => ['info', '', $localRelationship->info, ''],
-                       '$blocked'                   => ($contact['blocked'] ? $this->t('Currently blocked') : ''),
-                       '$ignored'                   => ($contact['readonly'] ? $this->t('Currently ignored') : ''),
-                       '$archived'                  => ($contact['archive'] ? $this->t('Currently archived') : ''),
-                       '$pending'                   => ($contact['pending'] ? $this->t('Awaiting connection acknowledge') : ''),
                        '$hidden'                    => ['hidden', $this->t('Hide this contact from others'), $localRelationship->hidden, $this->t('Replies/likes to your public posts <strong>may</strong> still be visible')],
                        '$notify_new_posts'          => ['notify_new_posts', $this->t('Notification for new posts'), ($localRelationship->notifyNewPosts), $this->t('Send a notification of every new post of this contact')],
                        '$fetch_further_information' => $fetch_further_information,
@@ -384,7 +394,7 @@ class Profile extends BaseModule
                        '$remote_self'               => [
                                'remote_self',
                                $this->t('Mirror postings from this contact'),
-                               $localRelationship->isRemoteSelf,
+                               $localRelationship->remoteSelf,
                                $this->t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
                                $remote_self_options
                        ],
@@ -414,6 +424,24 @@ class Profile extends BaseModule
 
                $formSecurityToken = self::getFormSecurityToken('contact_action');
 
+               if ($localRelationship->rel & Contact::SHARING) {
+                       $contact_actions['unfollow'] = [
+                               'label' => $this->t('Unfollow'),
+                               'url'   => 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1',
+                               'title' => '',
+                               'sel'   => '',
+                               'id'    => 'unfollow',
+                       ];
+               } else {
+                       $contact_actions['follow'] = [
+                               'label' => $this->t('Follow'),
+                               'url'   => 'contact/follow?url=' . urlencode($contact['url']) . '&auto=1',
+                               'title' => '',
+                               'sel'   => '',
+                               'id'    => 'follow',
+                       ];
+               }
+
                // Provide friend suggestion only for Friendica contacts
                if ($contact['network'] === Protocol::DFRN) {
                        $contact_actions['suggest'] = [
@@ -435,7 +463,7 @@ class Profile extends BaseModule
                        ];
                }
 
-               if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
+               if (Protocol::supportsProbe($contact['network'])) {
                        $contact_actions['updateprofile'] = [
                                'label' => $this->t('Refetch contact data'),
                                'url'   => 'contact/' . $contact['id'] . '/updateprofile?t=' . $formSecurityToken,
@@ -461,6 +489,14 @@ class Profile extends BaseModule
                        'id'    => 'toggle-ignore',
                ];
 
+               $contact_actions['collapse'] = [
+                       'label' => $localRelationship->collapsed ? $this->t('Uncollapse') : $this->t('Collapse'),
+                       'url'   => 'contact/' . $contact['id'] . '/collapse?t=' . $formSecurityToken,
+                       'title' => $this->t('Toggle Collapsed status'),
+                       'sel'   => $localRelationship->collapsed ? 'active' : '',
+                       'id'    => 'toggle-collapse',
+               ];
+
                if (Protocol::supportsRevokeFollow($contact['network']) && in_array($localRelationship->rel, [Contact::FOLLOWER, Contact::FRIEND])) {
                        $contact_actions['revoke_follow'] = [
                                'label' => $this->t('Revoke Follow'),
@@ -475,13 +511,16 @@ class Profile extends BaseModule
        }
 
        /**
+        * Updates contact from probing
+        *
         * @param int $contact_id Id of the contact with uid != 0
+        * @return void
         * @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]);
+               $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => [0, DI::userSession()->getLocalUserId()], 'deleted' => false]);
                if (!DBA::isResult($contact)) {
                        return;
                }