"network/group" fragments are removed
[friendica.git/.git] / src / Model / Contact.php
index d519fe9..3b09991 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2023, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -22,7 +22,9 @@
 namespace Friendica\Model;
 
 use Friendica\Contact\Avatar;
+use Friendica\Contact\Header;
 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
+use Friendica\Contact\LocalRelationship\Entity\LocalRelationship;
 use Friendica\Content\Conversation as ConversationContent;
 use Friendica\Content\Pager;
 use Friendica\Content\Text\HTML;
@@ -111,10 +113,14 @@ class Contact
         * @}
         */
 
-       const MIRROR_DEACTIVATED = 0;
-       const MIRROR_FORWARDED = 1; // Deprecated, now does the same like MIRROR_OWN_POST
-       const MIRROR_OWN_POST = 2;
-       const MIRROR_NATIVE_RESHARE = 3;
+       /** @deprecated Use Entity\LocalRelationship::MIRROR_DEACTIVATED instead */
+       const MIRROR_DEACTIVATED = LocalRelationship::MIRROR_DEACTIVATED;
+       /** @deprecated Now does the same as MIRROR_OWN_POST */
+       const MIRROR_FORWARDED = 1;
+       /** @deprecated Use Entity\LocalRelationship::MIRROR_OWN_POST instead */
+       const MIRROR_OWN_POST = LocalRelationship::MIRROR_OWN_POST;
+       /** @deprecated Use Entity\LocalRelationship::MIRROR_NATIVE_RESHARE instead */
+       const MIRROR_NATIVE_RESHARE = LocalRelationship::MIRROR_NATIVE_RESHARE;
 
        /**
         * @param array $fields    Array of selected fields, empty for all
@@ -166,6 +172,11 @@ class Contact
                return DBA::selectFirst('account-view', $fields, $condition, $params);
        }
 
+       public static function selectFirstAccountUser(array $fields = [], array $condition = [], array $params = [])
+       {
+               return DBA::selectFirst('account-user-view', $fields, $condition, $params);
+       }
+
        /**
         * Insert a row into the contact table
         * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
@@ -216,6 +227,11 @@ class Contact
 
                Contact\User::insertForContactArray($contact);
 
+               if ((empty($contact['baseurl']) || empty($contact['gsid'])) && Probe::isProbable($contact['network'])) {
+                       Logger::debug('Update missing baseurl', ['id' => $contact['id'], 'url' => $contact['url'], 'callstack' => System::callstack(4, 0, true)]);
+                       UpdateContact::add(['priority' => Worker::PRIORITY_MEDIUM, 'dont_fork' => true], $contact['id']);
+               }
+
                return $contact['id'];
        }
 
@@ -522,6 +538,17 @@ class Contact
                return self::isSharing($cid, $uid, $strict);
        }
 
+       /**
+        * Checks if the provided public contact id has got followers on this system
+        *
+        * @param integer $cid
+        * @return boolean
+        */
+       public static function hasFollowers(int $cid): bool
+       {
+               return DBA::exists('account-user-view', ["`pid` = ? AND `uid` != ? AND `rel` IN (?, ?)", $cid, 0, self::SHARING, self::FRIEND]);
+       }
+
        /**
         * Get the basepath for a given contact link
         *
@@ -728,7 +755,7 @@ class Contact
                $user = DBA::selectFirst(
                        'user',
                        ['uid', 'username', 'nickname', 'pubkey', 'prvkey'],
-                       ['uid' => $uid, 'account_expired' => false]
+                       ['uid' => $uid, 'account_removed' => false, 'account_expired' => false]
                );
                if (!DBA::isResult($user)) {
                        return false;
@@ -757,6 +784,7 @@ class Contact
                        'name-date'   => DateTimeFormat::utcNow(),
                        'uri-date'    => DateTimeFormat::utcNow(),
                        'avatar-date' => DateTimeFormat::utcNow(),
+                       'baseurl'     => DI::baseUrl(),
                        'closeness'   => 0
                ];
 
@@ -782,25 +810,25 @@ class Contact
        /**
         * Updates the self-contact for the provided user id
         *
-        * @param int   $uid
-        * @param bool  $update_avatar Force the avatar update
+        * @param int  $uid
+        * @param bool $update_avatar Force the avatar update
         * @return bool "true" if updated
-        * @throws HTTPException\InternalServerErrorException
+        * @throws \Exception
         */
        public static function updateSelfFromUserID(int $uid, bool $update_avatar = false): bool
        {
                $fields = [
                        'id', 'uri-id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', 'manually-approve',
                        'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
-                       'photo', 'thumb', 'micro', 'header', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'
+                       'photo', 'thumb', 'micro', 'header', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network', 'baseurl', 'gsid'
                ];
                $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
                if (!DBA::isResult($self)) {
                        return false;
                }
 
-               $fields = ['uid', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
-               $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
+               $fields = ['uid', 'username', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
+               $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]);
                if (!DBA::isResult($user)) {
                        return false;
                }
@@ -814,11 +842,10 @@ class Contact
                        return false;
                }
 
-               $file_suffix = 'jpg';
                $url = DI::baseUrl() . '/profile/' . $user['nickname'];
 
                $fields = [
-                       'name'         => $profile['name'],
+                       'name'         => $user['username'],
                        'nick'         => $user['nickname'],
                        'avatar-date'  => $self['avatar-date'],
                        'location'     => Profile::formatLocation($profile),
@@ -841,24 +868,17 @@ class Contact
                        'confirm'      => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
                ];
 
-
                $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
                if (DBA::isResult($avatar)) {
                        if ($update_avatar) {
                                $fields['avatar-date'] = DateTimeFormat::utcNow();
                        }
 
-                       // Creating the path to the avatar, beginning with the file suffix
-                       $types = Images::supportedTypes();
-                       if (isset($types[$avatar['type']])) {
-                               $file_suffix = $types[$avatar['type']];
-                       }
-
                        // We are adding a timestamp value so that other systems won't use cached content
                        $timestamp = strtotime($fields['avatar-date']);
 
                        $prefix = DI::baseUrl() . '/photo/' . $avatar['resource-id'] . '-';
-                       $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
+                       $suffix = Images::getExtensionByMimeType($avatar['type']) . '?ts=' . $timestamp;
 
                        $fields['photo'] = $prefix . '4' . $suffix;
                        $fields['thumb'] = $prefix . '5' . $suffix;
@@ -876,6 +896,8 @@ class Contact
                $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
                $fields['unsearchable'] = !$profile['net-publish'];
                $fields['manually-approve'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
+               $fields['baseurl'] = DI::baseUrl();
+               $fields['gsid'] = GServer::getID($fields['baseurl'], true);
 
                $update = false;
 
@@ -1060,7 +1082,7 @@ class Contact
                                return;
                        }
                } elseif (!isset($contact['url'])) {
-                       Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
+                       Logger::info('Empty contact', ['contact' => $contact]);
                }
 
                Logger::info('Contact is marked for archival', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
@@ -1156,6 +1178,7 @@ class Contact
                }
 
                $pm_url      = '';
+               $mention_url = '';
                $status_link = '';
                $photos_link = '';
 
@@ -1177,7 +1200,17 @@ class Contact
                }
 
                $contact_url = 'contact/' . $contact['id'];
-               $posts_link = 'contact/' . $contact['id'] . '/conversations';
+
+               if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
+                       $mention_label = DI::l10n()->t('Post to group');
+                       $mention_url = 'compose/0?body=!' . $contact['addr'];
+                       $network_label = DI::l10n()->t('View group');
+               } else {
+                       $mention_label = DI::l10n()->t('Mention');
+                       $mention_url = 'compose/0?body=@' . $contact['addr'];
+                       $network_label = DI::l10n()->t('Network Posts');
+               }
+               $network_url = 'contact/' . $contact['id'] . '/conversations';
 
                $follow_link   = '';
                $unfollow_link = '';
@@ -1193,24 +1226,28 @@ class Contact
                 * Menu array:
                 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
                 */
+
+
                if (empty($contact['uid'])) {
                        $menu = [
                                'profile'  => [DI::l10n()->t('View Profile'), $profile_link, true],
-                               'network'  => [DI::l10n()->t('Network Posts'), $posts_link, false],
+                               'network'  => [$network_label, $network_url, false],
                                'edit'     => [DI::l10n()->t('View Contact'), $contact_url, false],
                                'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link, true],
                                'unfollow' => [DI::l10n()->t('Unfollow'), $unfollow_link, true],
+                               'mention'  => [$mention_label, $mention_url, false],
                        ];
                } else {
                        $menu = [
                                'status'   => [DI::l10n()->t('View Status'), $status_link, true],
                                'profile'  => [DI::l10n()->t('View Profile'), $profile_link, true],
                                'photos'   => [DI::l10n()->t('View Photos'), $photos_link, true],
-                               'network'  => [DI::l10n()->t('Network Posts'), $posts_link, false],
+                               'network'  => [$network_label, $network_url, false],
                                'edit'     => [DI::l10n()->t('View Contact'), $contact_url, false],
                                'pm'       => [DI::l10n()->t('Send PM'), $pm_url, false],
                                'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link, true],
                                'unfollow' => [DI::l10n()->t('Unfollow'), $unfollow_link, true],
+                               'mention'  => [$mention_label, $mention_url, false],
                        ];
 
                        if (!empty($contact['pending'])) {
@@ -1329,7 +1366,7 @@ class Contact
                        }
 
                        if (DBA::isResult($personal_contact)) {
-                               Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
+                               Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact]);
                                $data = $personal_contact;
                                $data['photo'] = $personal_contact['avatar'];
                                $data['account-type'] = $personal_contact['contact-type'];
@@ -1341,7 +1378,7 @@ class Contact
                }
 
                if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
-                       Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
+                       Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update]);
                        return 0;
                }
 
@@ -1367,6 +1404,7 @@ class Contact
                        $fields = [
                                'uid'       => $uid,
                                'url'       => $data['url'],
+                               'baseurl'   => $data['baseurl'] ?? '',
                                'nurl'      => Strings::normaliseLink($data['url']),
                                'network'   => $data['network'],
                                'created'   => DateTimeFormat::utcNow(),
@@ -1528,23 +1566,20 @@ class Contact
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromUrl(string $contact_url, int $uid, bool $only_media = false): string
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $uid, $only_media);
        }
 
        /**
         * Returns posts from a given contact id
         *
         * @param int  $cid         Contact ID
-        * @param bool $thread_mode
-        * @param int  $update      Update mode
-        * @param int  $parent      Item parent ID for the update mode
         * @param bool $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromId(int $cid, int $uid, bool $only_media = false, string $last_created = null): string
        {
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
@@ -1552,32 +1587,17 @@ class Contact
                }
 
                if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
-                       $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
                } else {
-                       $sql = "`uid` = ?";
+                       $condition = ["`uid` = ?", $uid];
                }
 
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
-               if ($thread_mode) {
-                       $condition = [
-                               "((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA, DI::userSession()->getLocalUserId()
-                       ];
-               } else {
-                       $condition = [
-                               "`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()
-                       ];
-               }
+               $condition = DBA::mergeConditions($condition, ["`$contact_field` = ? AND `gravity` IN (?, ?)", $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]);
 
-               if (!empty($parent)) {
-                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
-               } else {
-                       $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
-                       if (!empty($last_received)) {
-                               $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
-                       }
+               if (!empty($last_created)) {
+                       $condition = DBA::mergeConditions($condition, ["`created` < ?", $last_created]);
                }
 
                if ($only_media) {
@@ -1588,66 +1608,108 @@ class Contact
                }
 
                if (DI::mode()->isMobile()) {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_mobile_network',
-                               DI::config()->get('system', 'itemspage_network_mobile')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
                } else {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_network',
-                               DI::config()->get('system', 'itemspage_network')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
                }
 
                $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
 
-               $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
+               $params = ['order' => ['created' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                        $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
                        $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
                } else {
                        $o = '';
                }
 
-               if ($thread_mode) {
-                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented'];
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
+               $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
+               $items = Post::toArray(Post::selectForUser($uid, $fields, $condition, $params));
 
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
-                                       $items = array_merge($items, $pinned);
-                               }
-                       }
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS);
 
-                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $o .= HTML::scrollLoader();
                } else {
-                       $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $condition = [
-                                               "`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
-                                               $cdata['public'], Post\Collection::FEATURED
-                                       ];
-                                       $pinned = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-                                       $items = array_merge($pinned, $items);
-                               }
-                       }
+                       $o .= $pager->renderMinimal(count($items));
+               }
+
+               return $o;
+       }
+
+       /**
+        * Returns threads from a given contact id
+        *
+        * @param int  $cid         Contact ID
+        * @param int  $update      Update mode
+        * @param int  $parent      Item parent ID for the update mode
+        * @return string posts in HTML
+        * @throws \Exception
+        */
+       public static function getThreadsFromId(int $cid, int $uid, int $update = 0, int $parent = 0, string $last_created = ''): string
+       {
+               $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
+               if (!DBA::isResult($contact)) {
+                       return '';
+               }
+
+               if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
+               } else {
+                       $condition = ["`uid` = ?", $uid];
+               }
+
+               if (!empty($parent)) {
+                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
+               } elseif (!empty($last_created)) {
+                       $condition = DBA::mergeConditions($condition, ["`created` < ?", $last_created]);
+               }
+
+               $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
+
+               if (DI::mode()->isMobile()) {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
+               } else {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
+               }
+
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
+
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+                       $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+               } else {
+                       $o = '';
+               }
+
+               $condition1 = DBA::mergeConditions($condition, ["`$contact_field` = ? AND `gravity` = ?", $cid, Item::GRAVITY_PARENT]);
+
+               $condition2 = DBA::mergeConditions($condition, [
+                       "`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`",
+                       $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA
+               ]);
+
+               $sql1 = "SELECT `uri-id`, `created` FROM `post-thread-user-view` WHERE " . array_shift($condition1);
+               $sql2 = "SELECT `thr-parent-id` AS `uri-id`, `created` FROM `post-user-view` WHERE " . array_shift($condition2);
+
+               $union = array_merge($condition1, $condition2);
+               $sql = $sql1 . " UNION " . $sql2;
+
+               $sql .= " ORDER BY `created` DESC LIMIT ?, ?";
+               $union = array_merge($union, [$pager->getStart(), $pager->getItemsPerPage()]);
+               $items = Post::toArray(DBA::p($sql, $union));
 
-                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACT_POSTS, $update);
+               if (empty($last_created) && ($pager->getStart() == 0)) {
+                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'created'];
+                       $pinned = Post\Collection::selectToArrayForContact($cid, Post\Collection::FEATURED, $fields);
+                       $items = array_merge($items, $pinned);
                }
 
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_created', $uid);
+
                if (!$update) {
-                       if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+                       if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                                $o .= HTML::scrollLoader();
                        } else {
                                $o .= $pager->renderMinimal(count($items));
@@ -1680,6 +1742,10 @@ class Contact
                                $account_type = DI::l10n()->t("Group");
                                break;
 
+                       case self::TYPE_RELAY:
+                               $account_type = DI::l10n()->t("Relay");
+                               break;
+
                        default:
                                $account_type = "";
                                break;
@@ -1895,13 +1961,7 @@ class Contact
                switch ($platform) {
                        case 'friendica':
                        case 'friendika':
-                               /**
-                                * Picture credits
-                                * @author  Lostinlight <https://mastodon.xyz/@lightone>
-                                * @license CC0 https://creativecommons.org/share-your-work/public-domain/cc0/
-                                * @link    https://gitlab.com/lostinlight/per_aspera_ad_astra/-/blob/master/friendica-404/friendica-promo-bubbles.jpg
-                                */
-                               $header = DI::baseUrl() . '/images/friendica-banner.jpg';
+                               $header = DI::baseUrl() . (new Header(DI::config()))->getMastodonBannerPath();
                                break;
                        case 'diaspora':
                                /**
@@ -2244,9 +2304,9 @@ class Contact
                                        try {
                                                $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
 
-                                               $img_str = $fetchResult->getBody();
-                                               if (!empty($img_str)) {
-                                                       $image = new Image($img_str, Images::getMimeTypeByData($img_str));
+                                               $img_str = $fetchResult->getBodyString();
+                                               if ($fetchResult->isSuccess() && !empty($img_str)) {
+                                                       $image = new Image($img_str, $fetchResult->getContentType(), $avatar);
                                                        if ($image->isValid()) {
                                                                $update_fields['blurhash'] = $image->getBlurHash();
                                                        } else {
@@ -2544,7 +2604,7 @@ class Contact
                        Worker::add(Worker::PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
                }
                DBA::close($duplicates);
-               Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl, 'callstack' => System::callstack(20)]);
+               Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl]);
                return true;
        }
 
@@ -2567,7 +2627,7 @@ class Contact
 
                $stamp = (float)microtime(true);
                self::updateFromProbe($id);
-               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url'], 'callstack' => System::callstack(20)]);
+               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url']]);
                return true;
        }
 
@@ -2807,7 +2867,7 @@ class Contact
                        }
 
                        $ret['last-item'] = Probe::getLastUpdate($ret);
-                       Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
+                       Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item']]);
                }
 
                $update = false;
@@ -3038,6 +3098,13 @@ class Contact
                        $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
                }
 
+               if (($protocol == Protocol::ACTIVITYPUB) && ($uid != 0)) {
+                       if (APContact::isRelay(APContact::getByURL($ret['url']))) {
+                               $result['message'] = DI::l10n()->t('This seems to be a relay account. They can\'t be followed by users.');
+                               return $result;
+                       }
+               }
+
                // do we have enough information?
                if (empty($protocol) || ($protocol == Protocol::PHANTOM) || (empty($ret['url']) && empty($ret['addr']))) {
                        $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . '<br />';
@@ -3182,7 +3249,7 @@ class Contact
                        return false;
                }
 
-               $fields = ['id', 'url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
+               $fields = ['id', 'url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked', 'baseurl'];
                $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
                if (!DBA::isResult($pub_contact)) {
                        // Should never happen
@@ -3253,6 +3320,7 @@ class Contact
                                'created'  => DateTimeFormat::utcNow(),
                                'url'      => $url,
                                'nurl'     => Strings::normaliseLink($url),
+                               'baseurl'  => $pub_contact['baseurl'] ?? '',
                                'name'     => $name,
                                'nick'     => $nick,
                                'network'  => $network,
@@ -3337,7 +3405,7 @@ class Contact
                } elseif (!empty($contact['id'])) {
                        self::remove($contact['id']);
                } else {
-                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
+                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact]);
                        return;
                }
 
@@ -3433,6 +3501,21 @@ class Contact
                return array_column($contacts, 'id');
        }
 
+       /**
+        * Return the link to the profile
+        *
+        * @param array $contact
+        * @return string
+        */
+       public static function getProfileLink(array $contact): string
+       {
+               if (!empty($contact['alias']) && Network::isValidHttpUrl($contact['alias']) && (($contact['network'] ?? '') != Protocol::DFRN)) {
+                       return $contact['alias'];
+               } else {
+                       return $contact['url'];
+               }
+       }
+
        /**
         * Returns a magic link to authenticate remote visitors
         *
@@ -3491,7 +3574,7 @@ class Contact
         */
        public static function magicLinkByContact(array $contact, string $url = ''): string
        {
-               $destination = $url ?: (!Network::isValidHttpUrl($contact['url']) && !empty($contact['alias']) && Network::isValidHttpUrl($contact['alias']) ? $contact['alias'] : $contact['url']);
+               $destination = $url ?: self::getProfileLink($contact);
 
                if (!DI::userSession()->isAuthenticated()) {
                        return $destination;
@@ -3596,7 +3679,7 @@ class Contact
                ];
 
                if (!$show_blocked) {
-                       $condition['server-blocked'] = true;
+                       $condition['server-blocked'] = false;
                }
 
                if ($uid == 0) {