]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Improved handling for undeterminded languages
[friendica.git] / src / Model / Item.php
index 41aae04134374ef0a0a7b504d5f991556f0e1518..a672f5b174791aa7af0d61f6446bd45eb4188812 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,6 +22,9 @@
 namespace Friendica\Model;
 
 use Friendica\Contact\LocalRelationship\Entity\LocalRelationship;
+use Friendica\Content\Image;
+use Friendica\Content\Post\Collection\PostMedias;
+use Friendica\Content\Post\Entity\PostMedia;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
@@ -34,6 +37,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Post\Category;
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Delivery;
@@ -113,7 +117,7 @@ class Item
        const DELIVER_FIELDLIST = [
                'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
                'parent-guid', 'conversation', 'received', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
-               'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
+               'private', 'title', 'body', 'raw-body', 'language', 'location', 'coord', 'app',
                'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                'author-id', 'author-addr', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
@@ -134,7 +138,7 @@ class Item
                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', 'post-reason',
                'private', 'pubmail', 'visible', 'starred',
                'unseen', 'deleted', 'origin', 'mention', 'global', 'network',
-               'title', 'content-warning', 'body', 'location', 'coord', 'app',
+               'title', 'content-warning', 'body', 'language', 'location', 'coord', 'app',
                'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'
@@ -192,6 +196,10 @@ class Item
                        $previous = Post::selectFirst(['edited'], $condition);
                }
 
+               if (!empty($fields['body'])) {
+                       $fields['body'] = self::setHashtags($fields['body']);
+               }
+
                $rows = Post::update($fields, $condition);
                if (is_bool($rows)) {
                        return $rows;
@@ -326,11 +334,11 @@ class Item
         */
        public static function markForDeletionById(int $item_id, int $priority = Worker::PRIORITY_HIGH): bool
        {
-               Logger::info('Mark item for deletion by id', ['id' => $item_id, 'callstack' => System::callstack()]);
+               Logger::info('Mark item for deletion by id', ['id' => $item_id]);
                // locate item to be deleted
                $fields = [
                        'id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri-id', 'origin',
-                       'deleted', 'resource-id', 'event-id',
+                       'thr-parent-id', 'deleted', 'resource-id', 'event-id', 'vid', 'body',
                        'verb', 'object-type', 'object', 'target', 'contact-id', 'psid', 'gravity'
                ];
                $item = Post::selectFirst($fields, ['id' => $item_id]);
@@ -410,6 +418,10 @@ class Item
                DI::notify()->deleteForItem($item['uri-id']);
                DI::notification()->deleteForItem($item['uri-id']);
 
+               if (in_array($item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
+                       Post\Counts::update($item['thr-parent-id'], $item['parent-uri-id'], $item['vid'], $item['verb'], $item['body']);
+               }
+
                Logger::info('Item has been marked for deletion.', ['id' => $item_id]);
 
                return true;
@@ -791,7 +803,7 @@ class Item
                }
 
                if (!DBA::isResult($parent)) {
-                       Logger::notice('item parent was not found - ignoring item', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'callstack' => System::callstack(20)]);
+                       Logger::notice('item parent was not found - ignoring item', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                        return [];
                }
 
@@ -876,6 +888,10 @@ class Item
                        if (is_int($notify) && in_array($notify, Worker::PRIORITIES)) {
                                $priority = $notify;
                        }
+
+                       // Mastodon style API visibility
+                       $copy_permissions = ($item['visibility'] ?? 'private') == 'private';
+                       unset($item['visibility']);
                } else {
                        $item['network'] = trim(($item['network'] ?? '') ?: Protocol::PHANTOM);
                }
@@ -1034,10 +1050,12 @@ class Item
 
                        // Reshares have to keep their permissions to allow groups to work
                        if (!$defined_permissions && (!$item['origin'] || ($item['verb'] != Activity::ANNOUNCE))) {
-                               $item['allow_cid']     = $toplevel_parent['allow_cid'];
-                               $item['allow_gid']     = $toplevel_parent['allow_gid'];
-                               $item['deny_cid']      = $toplevel_parent['deny_cid'];
-                               $item['deny_gid']      = $toplevel_parent['deny_gid'];
+                               // Don't store the permissions on pure AP posts
+                               $store_permissions = ($item['network'] != Protocol::ACTIVITYPUB) || $item['origin'] || !empty($item['diaspora_signed_text']);
+                               $item['allow_cid'] = $store_permissions ? $toplevel_parent['allow_cid'] : '';
+                               $item['allow_gid'] = $store_permissions ? $toplevel_parent['allow_gid'] : '';
+                               $item['deny_cid']  = $store_permissions ? $toplevel_parent['deny_cid'] : '';
+                               $item['deny_gid']  = $store_permissions ? $toplevel_parent['deny_gid'] : '';
                        }
 
                        $parent_origin         = $toplevel_parent['origin'];
@@ -1179,7 +1197,7 @@ class Item
                        if (!empty($quote_id)) {
                                // This is one of these "should not happen" situations.
                                // The protocol implementations should already have done this job.
-                               Logger::notice('Quote-uri-id detected in post', ['id' => $quote_id, 'guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'callstack' => System::callstack(20)]);
+                               Logger::notice('Quote-uri-id detected in post', ['id' => $quote_id, 'guid' => $item['guid'], 'uri-id' => $item['uri-id']]);
                                $item['quote-uri-id'] = $quote_id;
                        }
                }
@@ -1208,8 +1226,6 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
 
-               $item['language'] = self::getLanguage($item);
-
                $notify_type = Delivery::POST;
 
                // Filling item related side tables
@@ -1258,6 +1274,8 @@ class Item
                        }
                }
 
+               $item['language'] = self::getLanguage($item);
+
                $inserted = Post::insert($item['uri-id'], $item);
 
                if ($item['gravity'] == self::GRAVITY_PARENT) {
@@ -1355,6 +1373,9 @@ class Item
 
                if ($notify) {
                        DI::contentItem()->postProcessPost($posted_item);
+                       if ($copy_permissions && ($posted_item['thr-parent-id'] != $posted_item['uri-id']) && ($posted_item['private'] == self::PRIVATE)) {
+                               DI::contentItem()->copyPermissions($posted_item['thr-parent-id'], $posted_item['uri-id'], $posted_item['parent-uri-id']);
+                       }
                } else {
                        Hook::callAll('post_remote_end', $posted_item);
                }
@@ -1401,25 +1422,59 @@ class Item
                }
 
                if (!empty($source) && ($transmit || DI::config()->get('debug', 'store_source'))) {
-                       Post\Activity::insert($item['uri-id'], $source);
+                       Post\Activity::insert($posted_item['uri-id'], $source);
                }
 
                if ($transmit) {
-                       Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
-               }
+                       ActivityPub\Transmitter::storeReceiversForItem($posted_item);
 
-               // Fill the cache with the rendered content.
-               if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) && ($posted_item['uid'] == 0)) {
-                       self::updateDisplayCache($posted_item['uri-id']);
+                       Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
                }
 
                if ($inserted) {
-                       Post\Engagement::storeFromItem($posted_item);
+                       // Fill the cache with the rendered content.
+                       if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT])) {
+                               self::updateDisplayCache($posted_item['uri-id']);
+                       }
+
+                       if (in_array($posted_item['gravity'], [self::GRAVITY_ACTIVITY, self::GRAVITY_COMMENT])) {
+                               Post\Counts::update($posted_item['thr-parent-id'], $posted_item['parent-uri-id'], $posted_item['vid'], $posted_item['verb'], $posted_item['body']);
+                       }
+
+                       $engagement_uri_id = Post\Engagement::storeFromItem($posted_item);
+                       if ($engagement_uri_id) {
+                               self::reshareChannelPost($engagement_uri_id);
+                       }
                }
 
                return $post_user_id;
        }
 
+       private static function reshareChannelPost(int $uri_id)
+       {
+               $item = Post::selectFirst(['id', 'private', 'network', 'language', 'owner-id'], ['uri-id' => $uri_id, 'uid' => 0]);
+               if (empty($item['id'])) {
+                       return;
+               }
+
+               if (($item['private'] != self::PUBLIC) || !in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+                       return;
+               }
+
+               $engagement = DBA::selectFirst('post-engagement', ['searchtext', 'media-type'], ['uri-id' => $uri_id]);
+               if (empty($engagement['searchtext'])) {
+                       return;
+               }
+
+               $language = !empty($item['language']) ? array_key_first(json_decode($item['language'], true)) : '';
+               $tags     = array_column(Tag::getByURIId($uri_id, [Tag::HASHTAG]), 'name');
+
+               foreach (DI::userDefinedChannel()->getMatchingChannelUsers($engagement['searchtext'], $language, $tags, $engagement['media-type'], $item['owner-id']) as $uid) {
+                       Logger::debug('Reshare post', ['uid' => $uid, 'uri-id' => $uri_id, 'language' => $language, 'tags' => $tags, 'searchtext' => $engagement['searchtext'], 'media_type' => $engagement['media-type']]);
+                       self::performActivity($item['id'], 'announce', $uid);
+               }
+       }
+
        /**
         * Fetch the post reason for a given item array
         *
@@ -1471,6 +1526,10 @@ class Item
         */
        private static function setOwnerforResharedItem(array $item)
        {
+               if ($item['uid'] == 0) {
+                       return;
+               }
+
                $parent = Post::selectFirst(
                        ['id', 'causer-id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-reason'],
                        ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]
@@ -1522,7 +1581,25 @@ class Item
                        return;
                }
 
+               $languages = $item['language'] ? array_keys(json_decode($item['language'], true)) : [];
+               
                foreach (Tag::getUIDListByURIId($item['uri-id']) as $uid => $tags) {
+                       if (!empty($languages)) {
+                               $keep = false;
+                               $user_languages = User::getWantedLanguages($uid);
+                               foreach ($user_languages as $language) {
+                                       if (in_array($language, $languages)) {
+                                               $keep = true;
+                                       }
+                               }
+                               if ($keep) {
+                                       Logger::debug('Wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+                               } else {
+                                       Logger::debug('No wanted languages found', ['uid' => $uid, 'user-languages' => $user_languages, 'item-languages' => $languages]);
+                                       continue;
+                               }
+                       }
+
                        $stored = self::storeForUserByUriId($item['uri-id'], $uid, ['post-reason' => self::PR_TAG]);
                        Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
                        foreach ($tags as $tag) {
@@ -1977,19 +2054,31 @@ class Item
         * @return string detected language
         * @throws \Text_LanguageDetect_Exception
         */
-       private static function getLanguage(array $item): string
+       private static function getLanguage(array $item): ?string
        {
                if (!empty($item['language'])) {
                        return $item['language'];
                }
 
-               if (!in_array($item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) || empty($item['body'])) {
-                       return '';
+               $transmitted = [];
+               foreach ($item['transmitted-languages'] ??  [] as $language) {
+                       $transmitted[$language] = 0;
                }
 
-               $languages = self::getLanguageArray(trim($item['title'] . "\n" . $item['body']), 3);
+               $content = trim(($item['title'] ?? '') . ' ' . ($item['content-warning'] ?? '') . ' ' . ($item['body'] ?? ''));
+
+               if (!in_array($item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT]) || empty($content)) {
+                       return !empty($transmitted) ? json_encode($transmitted) : null;
+               }
+
+               $languages = self::getLanguageArray($content, 3, $item['uri-id'], $item['author-id']);
                if (empty($languages)) {
-                       return '';
+                       return !empty($transmitted) ? json_encode($transmitted) : null;
+               }
+
+               if (!empty($transmitted)) {
+                       $languages = array_merge($transmitted, $languages);
+                       arsort($languages);
                }
 
                return json_encode($languages);
@@ -2000,63 +2089,151 @@ class Item
         *
         * @param string  $body
         * @param integer $count
+        * @param integer $uri_id
+        * @param integer $author_id
         * @return array
         */
-       public static function getLanguageArray(string $body, int $count): array
+       public static function getLanguageArray(string $body, int $count, int $uri_id = 0, int $author_id = 0): array
        {
-               // Convert attachments to links
-               $naked_body = BBCode::removeAttachment($body);
-               if (empty($naked_body)) {
-                       return [];
+               $searchtext = BBCode::toSearchText($body, $uri_id);
+
+               if ((count(explode(' ', $searchtext)) < 10) && (mb_strlen($searchtext) < 30) && $author_id) {
+                       $author = Contact::selectFirst(['about'], ['id' => $author_id]);
+                       if (!empty($author['about'])) {
+                               $about = BBCode::toSearchText($author['about'], 0);
+                               Logger::debug('About field added', ['author' => $author_id, 'body' => $searchtext, 'about' => $about]);
+                               $searchtext .= ' ' . $about;
+                       }
                }
 
-               // Remove links and pictures
-               $naked_body = BBCode::removeLinks($naked_body);
+               if (empty($searchtext)) {
+                       return ['un' => 1];
+               }
 
-               // Convert the title and the body to plain text
-               $naked_body = BBCode::toPlaintext($naked_body);
+               $ld = new Language(DI::l10n()->getDetectableLanguages());
 
-               // Remove possibly remaining links
-               $naked_body = preg_replace(Strings::autoLinkRegEx(), '', $naked_body);
+               $result = [];
 
-               if (empty($naked_body)) {
-                       return [];
+               foreach (self::splitByBlocks($searchtext) as $block) {
+                       $languages = $ld->detect($block)->close() ?: [];
+
+                       $data = [
+                               'text'      => $block,
+                               'detected'  => $languages,
+                               'uri-id'    => $uri_id,
+                               'author-id' => $author_id,
+                       ];
+                       Hook::callAll('detect_languages', $data);
+
+                       foreach ($data['detected'] as $language => $quality) {
+                               $result[$language] = max($result[$language] ?? 0, $quality * (strlen($block) / strlen($searchtext)));
+                       }
                }
 
-               $naked_body = self::getDominantLanguage($naked_body);
+               if (empty($result)) {
+                       return ['un' => 1];
+               }
 
-               $availableLanguages = DI::l10n()->getAvailableLanguages(true);
-               $availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
+               $result = self::compactLanguages($result);
 
-               $ld = new Language(array_keys($availableLanguages));
-               return $ld->detect($naked_body)->limit(0, $count)->close() ?: [];
+               arsort($result);
+               return array_slice($result, 0, $count);
        }
 
        /**
-        * Check if latin or non latin are dominant in the body and only return the dominant one
+        * Concert the language code in the detection result to ISO 639-1.
+        * On duplicates the system uses the higher quality value.
+        *
+        * @param array $result
+        * @return array
+        */
+       private static function compactLanguages(array $result): array
+       {
+               $languages = [];
+               foreach ($result as $language => $quality) {
+                       if ($quality == 0) {
+                               continue;
+                       }
+                       $code = DI::l10n()->toISO6391($language);
+                       if (empty($languages[$code]) || ($languages[$code] < $quality)) {
+                               $languages[$code] = $quality;
+                       }
+               }
+               return $languages;
+       }
+
+       /**
+        * Split a string into different unicode blocks
+        * Currently the text is split into the latin and the non latin part.
         *
         * @param string $body
-        * @return string
+        * @return array
         */
-       private static function getDominantLanguage(string $body): string
+       private static function splitByBlocks(string $body): array
        {
-               $latin = '';
-               $non_latin = '';
+               if (!class_exists('IntlChar')) {
+                       return [$body];
+               }
+
+               $blocks         = [];
+               $previous_block = 0;
+
                for ($i = 0; $i < mb_strlen($body); $i++) {
                        $character = mb_substr($body, $i, 1);
-                       $ord = mb_ord($character);
-
-                       // We add the most common characters to both strings.
-                       if (($ord <= 64) || ($ord >= 91 && $ord <= 96) || ($ord >= 123 && $ord <= 191) || in_array($ord, [215, 247]) || ($ord >= 697 && $ord <= 735) || ($ord > 65535)) {
-                               $latin .= $character;
-                               $non_latin .= $character;
-                       } elseif ($ord < 768) {
-                               $latin .= $character;
+                       $previous  = ($i > 0) ? mb_substr($body, $i - 1, 1) : '';
+                       $next      = ($i < mb_strlen($body)) ? mb_substr($body, $i + 1, 1) : '';
+
+                       if (!\IntlChar::isalpha($character)) {
+                               if (($previous != '') && (\IntlChar::isalpha($previous))) {
+                                       $previous_block = self::getBlockCode($previous);
+                               }
+
+                               $block = (($next != '') && \IntlChar::isalpha($next)) ? self::getBlockCode($next) : $previous_block;
+                               $blocks[$block] = ($blocks[$block] ?? '') . $character;
                        } else {
-                               $non_latin .= $character;
+                               $block = self::getBlockCode($character);
+                               $blocks[$block] = ($blocks[$block] ?? '') . $character;
                        }
                }
-               return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+
+               foreach (array_keys($blocks) as $key) {
+                       $blocks[$key] = trim($blocks[$key]);
+                       if (empty($blocks[$key])) {
+                               unset($blocks[$key]);
+                       }
+               }
+
+               return array_values($blocks);
+       }
+
+       /**
+        * returns the block code for the given character
+        *
+        * @param string $character
+        * @return integer 0 = no alpha character (blank, signs, emojis, ...), 1 = latin character, 2 = character in every other language
+        */
+       private static function getBlockCode(string $character): int
+       {
+               if (!\IntlChar::isalpha($character)) {
+                       return 0;
+               }
+               return self::isLatin($character) ? 1 : 2;
+       }
+
+       /**
+        * Checks if the given character is in one of the latin code blocks
+        *
+        * @param string $character
+        * @return boolean
+        */
+       private static function isLatin(string $character): bool
+       {
+               return in_array(\IntlChar::getBlockCode($character), [
+                       \IntlChar::BLOCK_CODE_BASIC_LATIN, \IntlChar::BLOCK_CODE_LATIN_1_SUPPLEMENT,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_A, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_B,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_C, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_D,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_E, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_ADDITIONAL
+               ]);
        }
 
        public static function getLanguageMessage(array $item): string
@@ -2065,7 +2242,20 @@ class Item
 
                $used_languages = '';
                foreach (json_decode($item['language'], true) as $language => $reliability) {
-                       $used_languages .= $iso639->languageByCode1($language) . ' (' . $language . "): " . number_format($reliability, 5) . '\n';
+                       $code = DI::l10n()->toISO6391($language);
+
+                       if ($code == 'un') {
+                               $native = $language = DI::l10n()->t('Undetermined');
+                       } else {
+                               $native   = $iso639->nativeByCode1($code);
+                               $language = $iso639->languageByCode1($code);
+                       }
+
+                       if ($native != $language) {
+                               $used_languages .= DI::l10n()->t('%s (%s - %s): %s', $native, $language, $code, number_format($reliability, 5)) . '\n';
+                       } else {
+                               $used_languages .= DI::l10n()->t('%s (%s): %s', $native, $code, number_format($reliability, 5)) . '\n';
+                       }
                }
                $used_languages = DI::l10n()->t('Detected languages in this post:\n%s', $used_languages);
                return $used_languages;
@@ -2093,12 +2283,12 @@ class Item
 
                $hostPart = $host ?: $parsed['host'] ?? '';
                if (!$hostPart) {
-                       Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed, 'callstack' => System::callstack(10)]);
+                       Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed]);
                }
 
                // Glue it together to be able to make a hash from it
                if (!empty($parsed)) {
-                       $host_id = implode('/', $parsed);
+                       $host_id = implode('/', (array)$parsed);
                } else {
                        $host_id = $uri;
                }
@@ -2443,11 +2633,14 @@ class Item
                        $result = self::insert($datarray2);
                        Logger::info('remote-self post original item', ['contact' => $contact['url'], 'result' => $result, 'item' => $datarray2]);
                } else {
-                       $datarray['private'] = self::PUBLIC;
                        $datarray['app'] = 'Feed';
                        $result = true;
                }
 
+               if ($result) {
+                       unset($datarray['private']);
+               }
+
                return (bool)$result;
        }
 
@@ -3165,15 +3358,15 @@ class Item
                if (!empty($shared_item['uri-id'])) {
                        $shared_uri_id = $shared_item['uri-id'];
                        $shared_links[] = strtolower($shared_item['plink']);
-                       $shared_attachments = Post\Media::splitAttachments($shared_uri_id, [], $shared_item['has-media']);
-                       $shared_links = array_merge($shared_links, array_column($shared_attachments['visual'], 'url'));
-                       $shared_links = array_merge($shared_links, array_column($shared_attachments['link'], 'url'));
-                       $shared_links = array_merge($shared_links, array_column($shared_attachments['additional'], 'url'));
-                       $item['body'] = self::replaceVisualAttachments($shared_attachments, $item['body']);
+                       $sharedSplitAttachments = DI::postMediaRepository()->splitAttachments($shared_uri_id, [], $shared_item['has-media']);
+                       $shared_links = array_merge($shared_links, $sharedSplitAttachments['visual']->column('url'));
+                       $shared_links = array_merge($shared_links, $sharedSplitAttachments['link']->column('url'));
+                       $shared_links = array_merge($shared_links, $sharedSplitAttachments['additional']->column('url'));
+                       $item['body'] = self::replaceVisualAttachments($sharedSplitAttachments['visual'], $item['body']);
                }
 
-               $attachments = Post\Media::splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false);
-               $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? '');
+               $itemSplitAttachments = DI::postMediaRepository()->splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false);
+               $item['body'] = self::replaceVisualAttachments($itemSplitAttachments['visual'], $item['body'] ?? '');
 
                self::putInCache($item);
                $item['body'] = $body;
@@ -3198,7 +3391,7 @@ class Item
                                $filter_reasons[] = DI::l10n()->t('Content warning: %s', $item['content-warning']);
                        }
 
-                       $item['attachments'] = $attachments;
+                       $item['attachments'] = $itemSplitAttachments;
 
                        $hook_data = [
                                'item' => $item,
@@ -3227,11 +3420,11 @@ class Item
                        return $s;
                }
 
-               if (!empty($shared_attachments)) {
-                       $s = self::addGallery($s, $shared_attachments, $item['uri-id']);
-                       $s = self::addVisualAttachments($shared_attachments, $shared_item, $s, true);
-                       $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, $quote_shared_links);
-                       $s = self::addNonVisualAttachments($shared_attachments, $item, $s, true);
+               if (!empty($sharedSplitAttachments)) {
+                       $s = self::addGallery($s, $sharedSplitAttachments['visual']);
+                       $s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true);
+                       $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links);
+                       $s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true);
                        $body = BBCode::removeSharedData($body);
                }
 
@@ -3241,10 +3434,10 @@ class Item
                        $s = substr($s, 0, $pos);
                }
 
-               $s = self::addGallery($s, $attachments, $item['uri-id']);
-               $s = self::addVisualAttachments($attachments, $item, $s, false);
-               $s = self::addLinkAttachment($item['uri-id'], $attachments, $body, $s, false, $shared_links);
-               $s = self::addNonVisualAttachments($attachments, $item, $s, false);
+               $s = self::addGallery($s, $itemSplitAttachments['visual']);
+               $s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false);
+               $s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links);
+               $s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false);
                $s = self::addQuestions($item, $s);
 
                // Map.
@@ -3272,45 +3465,35 @@ class Item
                return $hook_data['html'];
        }
 
-       /**
-        * @param array $images
-        * @return string
-        * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
-        */
-       private static function makeImageGrid(array $images): string
-       {
-               // Image for first column (fc) and second column (sc)
-               $images_fc = [];
-               $images_sc = [];
-
-               for ($i = 0; $i < count($images); $i++) {
-                       ($i % 2 == 0) ? ($images_fc[] = $images[$i]) : ($images_sc[] = $images[$i]);
-               }
-
-               return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image_grid.tpl'), [
-                       'columns' => [
-                               'fc' => $images_fc,
-                               'sc' => $images_sc,
-                       ],
-               ]);
-       }
-
        /**
         * Modify links to pictures to links for the "Fancybox" gallery
         *
-        * @param string $s
-        * @param array $attachments
-        * @param integer $uri_id
+        * @param string     $s
+        * @param PostMedias $PostMedias
         * @return string
         */
-       private static function addGallery(string $s, array $attachments, int $uri_id): string
+       private static function addGallery(string $s, PostMedias $PostMedias): string
        {
-               foreach ($attachments['visual'] as $attachment) {
-                       if (empty($attachment['preview']) || ($attachment['type'] != Post\Media::IMAGE)) {
+               foreach ($PostMedias as $PostMedia) {
+                       if (!$PostMedia->preview || ($PostMedia->type !== Post\Media::IMAGE)) {
                                continue;
                        }
-                       $s = str_replace('<a href="' . $attachment['url'] . '"', '<a data-fancybox="' . $uri_id . '" href="' . $attachment['url'] . '"', $s);
+
+                       if ($PostMedia->hasDimensions()) {
+                               $pattern = '#<a href="' . preg_quote($PostMedia->url) . '">(.*?)"></a>#';
+
+                               $s = preg_replace_callback($pattern, function () use ($PostMedia) {
+                                       return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image/single_with_height_allocation.tpl'), [
+                                               '$image' => $PostMedia,
+                                               '$allocated_height' => $PostMedia->getAllocatedHeight(),
+                                               '$allocated_max_width' => ($PostMedia->previewWidth ?? $PostMedia->width) . 'px',
+                                       ]);
+                               }, $s);
+                       } else {
+                               $s = str_replace('<a href="' . $PostMedia->url . '"', '<a data-fancybox="uri-id-' . $PostMedia->uriId . '" href="' . $PostMedia->url . '"', $s);
+                       }
                }
+
                return $s;
        }
 
@@ -3334,7 +3517,7 @@ class Item
                unset($urlparts['fragment']);
 
                try {
-                       $url = (string)Uri::fromParts($urlparts);
+                       $url = (string)Uri::fromParts((array)$urlparts);
                } catch (\InvalidArgumentException $e) {
                        DI::logger()->notice('Invalid URL', ['$url' => $url, '$urlparts' => $urlparts]);
                        /* See https://github.com/friendica/friendica/issues/12113
@@ -3368,30 +3551,30 @@ class Item
        /**
         * Replace visual attachments in the body
         *
-        * @param array $attachments
-        * @param string $body
+        * @param PostMedias $PostMedias
+        * @param string     $body
         * @return string modified body
         */
-       private static function replaceVisualAttachments(array $attachments, string $body): string
+       private static function replaceVisualAttachments(PostMedias $PostMedias, string $body): string
        {
                DI::profiler()->startRecording('rendering');
 
-               foreach ($attachments['visual'] as $attachment) {
-                       if (!empty($attachment['preview'])) {
-                               if (Network::isLocalLink($attachment['preview'])) {
+               foreach ($PostMedias as $PostMedia) {
+                       if ($PostMedia->preview) {
+                               if (DI::baseUrl()->isLocalUri($PostMedia->preview)) {
                                        continue;
                                }
-                               $proxy   = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_LARGE);
-                               $search  = ['[img=' . $attachment['preview'] . ']', ']' . $attachment['preview'] . '[/img]'];
+                               $proxy   = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE);
+                               $search  = ['[img=' . $PostMedia->preview . ']', ']' . $PostMedia->preview . '[/img]'];
                                $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
 
                                $body = str_replace($search, $replace, $body);
-                       } elseif ($attachment['filetype'] == 'image') {
-                               if (Network::isLocalLink($attachment['url'])) {
+                       } elseif ($PostMedia->mimetype->type == 'image') {
+                               if (DI::baseUrl()->isLocalUri($PostMedia->url)) {
                                        continue;
                                }
-                               $proxy   = Post\Media::getUrlForId($attachment['id']);
-                               $search  = ['[img=' . $attachment['url'] . ']', ']' . $attachment['url'] . '[/img]'];
+                               $proxy   = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE);
+                               $search  = ['[img=' . $PostMedia->url . ']', ']' . $PostMedia->url . '[/img]'];
                                $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
 
                                $body = str_replace($search, $replace, $body);
@@ -3404,29 +3587,31 @@ class Item
        /**
         * Add visual attachments to the content
         *
-        * @param array $attachments
-        * @param array $item
-        * @param string $content
+        * @param PostMedias $PostMedias
+        * @param array      $item
+        * @param string     $content
+        * @param bool       $shared
         * @return string modified content
+        * @throws ServiceUnavailableException
         */
-       private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared): string
+       private static function addVisualAttachments(PostMedias $PostMedias, array $item, string $content, bool $shared): string
        {
                DI::profiler()->startRecording('rendering');
                $leading  = '';
                $trailing = '';
-               $images   = [];
+               $images   = new PostMedias();
 
                // @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty.
-               foreach ($attachments['visual'] as $attachment) {
-                       if (self::containsLink($item['body'], $attachment['preview'] ?? $attachment['url'], $attachment['type'])) {
+               foreach ($PostMedias as $PostMedia) {
+                       if (self::containsLink($item['body'], $PostMedia->preview ?? $PostMedia->url, $PostMedia->type)) {
                                continue;
                        }
 
-                       if ($attachment['filetype'] == 'image') {
-                               $preview_url = Post\Media::getPreviewUrlForId($attachment['id'], ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE);
-                       } elseif (!empty($attachment['preview'])) {
-                               $preview_url = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_LARGE);
+                       if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) {
+                               $preview_size = Proxy::SIZE_MEDIUM;
+                               $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size);
                        } else {
+                               $preview_size = 0;
                                $preview_url = '';
                        }
 
@@ -3434,15 +3619,15 @@ class Item
                                continue;
                        }
 
-                       if (($attachment['filetype'] == 'video')) {
+                       if ($PostMedia->mimetype->type == 'video') {
                                /// @todo Move the template to /content as well
                                $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
                                        '$video' => [
-                                               'id'      => $attachment['id'],
-                                               'src'     => $attachment['url'],
-                                               'name'    => $attachment['name'] ?: $attachment['url'],
+                                               'id'      => $PostMedia->id,
+                                               'src'     => (string)$PostMedia->url,
+                                               'name'    => $PostMedia->name ?: $PostMedia->url,
                                                'preview' => $preview_url,
-                                               'mime'    => $attachment['mimetype'],
+                                               'mime'    => (string)$PostMedia->mimetype,
                                        ],
                                ]);
                                if (($item['post-type'] ?? null) == Item::PT_VIDEO) {
@@ -3450,13 +3635,13 @@ class Item
                                } else {
                                        $trailing .= $media;
                                }
-                       } elseif ($attachment['filetype'] == 'audio') {
+                       } elseif ($PostMedia->mimetype->type == 'audio') {
                                $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/audio.tpl'), [
                                        '$audio' => [
-                                               'id'     => $attachment['id'],
-                                               'src'    => $attachment['url'],
-                                               'name'   => $attachment['name'] ?: $attachment['url'],
-                                               'mime'   => $attachment['mimetype'],
+                                               'id'     => $PostMedia->id,
+                                               'src'    => (string)$PostMedia->url,
+                                               'name'   => $PostMedia->name ?: $PostMedia->url,
+                                               'mime'   => (string)$PostMedia->mimetype,
                                        ],
                                ]);
                                if (($item['post-type'] ?? null) == Item::PT_AUDIO) {
@@ -3464,23 +3649,17 @@ class Item
                                } else {
                                        $trailing .= $media;
                                }
-                       } elseif ($attachment['filetype'] == 'image') {
-                               $src_url = Post\Media::getUrlForId($attachment['id']);
+                       } elseif ($PostMedia->mimetype->type == 'image') {
+                               $src_url = DI::baseUrl() . $PostMedia->getPhotoPath();
                                if (self::containsLink($item['body'], $src_url)) {
                                        continue;
                                }
-                               $images[] = ['src' => $src_url, 'preview' => $preview_url, 'attachment' => $attachment, 'uri_id' => $item['uri-id']];
+
+                               $images[] = $PostMedia->withUrl(new Uri($src_url))->withPreview(new Uri($preview_url), $preview_size);
                        }
                }
 
-               $media = '';
-               if (count($images) > 1) {
-                       $media = self::makeImageGrid($images);
-               } elseif (count($images) == 1) {
-                       $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [
-                               '$image' => $images[0],
-                       ]);
-               }
+               $media = Image::getBodyAttachHtml($images);
 
                // On Diaspora posts the attached pictures are leading
                if ($item['network'] == Protocol::DIASPORA) {
@@ -3509,59 +3688,62 @@ class Item
        /**
         * Add link attachment to the content
         *
-        * @param array  $attachments
-        * @param string $body
-        * @param string $content
-        * @param bool   $shared
-        * @param array  $ignore_links A list of URLs to ignore
+        * @param int          $uriid
+        * @param PostMedias[] $attachments
+        * @param string       $body
+        * @param string       $content
+        * @param bool         $shared
+        * @param array        $ignore_links A list of URLs to ignore
         * @return string modified content
+        * @throws InternalServerErrorException
+        * @throws ServiceUnavailableException
         */
        private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links): string
        {
                DI::profiler()->startRecording('rendering');
                // Don't show a preview when there is a visual attachment (audio or video)
-               $types = array_column($attachments['visual'], 'type');
-               $preview = !in_array(Post\Media::IMAGE, $types) && !in_array(Post\Media::VIDEO, $types);
-
-               if (!empty($attachments['link'])) {
-                       foreach ($attachments['link'] as $link) {
-                               $found = false;
-                               foreach ($ignore_links as $ignore_link) {
-                                       if (Strings::compareLink($link['url'], $ignore_link)) {
-                                               $found = true;
-                                       }
-                               }
-                               // @todo Judge between the links to use the one with most information
-                               if (!$found && (empty($attachment) || !empty($link['author-name']) ||
-                                       (empty($attachment['name']) && !empty($link['name'])) ||
-                                       (empty($attachment['description']) && !empty($link['description'])) ||
-                                       (empty($attachment['preview']) && !empty($link['preview'])))) {
-                                       $attachment = $link;
+               $types = $attachments['visual']->column('type');
+               $preview = !in_array(PostMedia::TYPE_IMAGE, $types) && !in_array(PostMedia::TYPE_VIDEO, $types);
+
+               /** @var ?PostMedia $attachment */
+               $attachment = null;
+               foreach ($attachments['link'] as $PostMedia) {
+                       $found = false;
+                       foreach ($ignore_links as $ignore_link) {
+                               if (Strings::compareLink($PostMedia->url, $ignore_link)) {
+                                       $found = true;
                                }
                        }
+                       // @todo Judge between the links to use the one with most information
+                       if (!$found && (empty($attachment) || $PostMedia->authorName ||
+                               (!$attachment->name && $PostMedia->name) ||
+                               (!$attachment->description && $PostMedia->description) ||
+                               (!$attachment->preview && $PostMedia->preview))) {
+                               $attachment = $PostMedia;
+                       }
                }
 
                if (!empty($attachment)) {
                        $data = [
                                'after' => '',
-                               'author_name' => $attachment['author-name'] ?? '',
-                               'author_url' =>  $attachment['author-url'] ?? '',
-                               'description' => $attachment['description'] ?? '',
+                               'author_name' => $attachment->authorName ?? '',
+                               'author_url' => (string)($attachment->authorUrl ?? ''),
+                               'description' => $attachment->description ?? '',
                                'image' => '',
                                'preview' => '',
-                               'provider_name' => $attachment['publisher-name'] ?? '',
-                               'provider_url' => $attachment['publisher-url'] ?? '',
+                               'provider_name' => $attachment->publisherName ?? '',
+                               'provider_url' => (string)($attachment->publisherUrl ?? ''),
                                'text' => '',
-                               'title' => $attachment['name'] ?? '',
+                               'title' => $attachment->name ?? '',
                                'type' => 'link',
-                               'url' => $attachment['url']
+                               'url' => (string)$attachment->url,
                        ];
 
-                       if ($preview && !empty($attachment['preview'])) {
-                               if ($attachment['preview-width'] >= 500) {
-                                       $data['image'] = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_MEDIUM);
+                       if ($preview && $attachment->preview) {
+                               if ($attachment->previewWidth >= 500) {
+                                       $data['image'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM);
                                } else {
-                                       $data['preview'] = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_MEDIUM);
+                                       $data['preview'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM);
                                }
                        }
 
@@ -3629,19 +3811,21 @@ class Item
        }
 
        /**
-        * Add non visual attachments to the content
+        * Add non-visual attachments to the content
         *
-        * @param array $attachments
-        * @param array $item
-        * @param string $content
+        * @param PostMedias $PostMedias
+        * @param array      $item
+        * @param string     $content
         * @return string modified content
+        * @throws InternalServerErrorException
+        * @throws \ImagickException
         */
-       private static function addNonVisualAttachments(array $attachments, array $item, string $content): string
+       private static function addNonVisualAttachments(PostMedias $PostMedias, array $item, string $content): string
        {
                DI::profiler()->startRecording('rendering');
                $trailing = '';
-               foreach ($attachments['additional'] as $attachment) {
-                       if (strpos($item['body'], $attachment['url'])) {
+               foreach ($PostMedias as $PostMedia) {
+                       if (strpos($item['body'], (string)$PostMedia->url)) {
                                continue;
                        }
 
@@ -3652,16 +3836,16 @@ class Item
                                'url'     => $item['author-link'],
                                'alias'   => $item['author-alias']
                        ];
-                       $the_url = Contact::magicLinkByContact($author, $attachment['url']);
+                       $the_url = Contact::magicLinkByContact($author, $PostMedia->url);
 
-                       $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url']));
+                       $title = Strings::escapeHtml(trim($PostMedia->description ?? '' ?: $PostMedia->url));
 
-                       if (!empty($attachment['size'])) {
-                               $title .= ' ' . $attachment['size'] . ' ' . DI::l10n()->t('bytes');
+                       if ($PostMedia->size) {
+                               $title .= ' ' . $PostMedia->size . ' ' . DI::l10n()->t('bytes');
                        }
 
                        /// @todo Use a template
-                       $icon = '<div class="attachtype icon s22 type-' . $attachment['filetype'] . ' subtype-' . $attachment['subtype'] . '"></div>';
+                       $icon = '<div class="attachtype icon s22 type-' . $PostMedia->mimetype->type . ' subtype-' . $PostMedia->mimetype->subtype . '"></div>';
                        $trailing .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" rel="noopener noreferrer" >' . $icon . '</a>';
                }
 
@@ -3688,16 +3872,16 @@ class Item
                        foreach ($options as $key => $option) {
                                if ($question['voters'] > 0) {
                                        $percent = $option['replies'] / $question['voters'] * 100;
-                                       $options[$key]['vote'] = DI::l10n()->tt('%2$s (%3$d%%, %1$d vote)', '%2$s (%3$d%%, %1$d votes)', $option['replies'], $option['name'], round($percent, 1));
+                                       $options[$key]['vote'] = DI::l10n()->tt('%2$s (%3$d%%, %1$d vote)', '%2$s (%3$d%%, %1$d votes)', $option['replies'] ?? 0, $option['name'], round($percent, 1));
                                } else {
-                                       $options[$key]['vote'] = DI::l10n()->tt('%2$s (%1$d vote)', '%2$s (%1$d votes)', $option['replies'], $option['name']);
+                                       $options[$key]['vote'] = DI::l10n()->tt('%2$s (%1$d vote)', '%2$s (%1$d votes)', $option['replies'] ?? 0, $option['name']);
                                }
                        }
 
                        if (!empty($question['voters']) && !empty($question['endtime'])) {
-                               $summary = DI::l10n()->tt('%d voter. Poll end: %s', '%d voters. Poll end: %s', $question['voters'], Temporal::getRelativeDate($question['endtime']));
+                               $summary = DI::l10n()->tt('%d voter. Poll end: %s', '%d voters. Poll end: %s', $question['voters'] ?? 0, Temporal::getRelativeDate($question['endtime']));
                        } elseif (!empty($question['voters'])) {
-                               $summary = DI::l10n()->tt('%d voter.', '%d voters.', $question['voters']);
+                               $summary = DI::l10n()->tt('%d voter.', '%d voters.', $question['voters'] ?? 0);
                        } elseif (!empty($question['endtime'])) {
                                $summary = DI::l10n()->t('Poll end: %s', Temporal::getRelativeDate($question['endtime']));
                        } else {
@@ -3845,11 +4029,12 @@ class Item
         * Fetches item for given URI or plink
         *
         * @param string $uri
-        * @param integer $uid
+        * @param int    $uid
+        * @param int    $completion
         *
         * @return integer item id
         */
-       public static function fetchByLink(string $uri, int $uid = 0): int
+       public static function fetchByLink(string $uri, int $uid = 0, int $completion = ActivityPub\Receiver::COMPLETION_MANUAL): int
        {
                Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]);
                $item_id = self::searchByLink($uri, $uid);
@@ -3870,7 +4055,7 @@ class Item
                        return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
                }
 
-               $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', ActivityPub\Receiver::COMPLETION_MANUAL, $uid);
+               $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', $completion, $uid);
 
                if ($fetched_uri) {
                        $item_id = self::searchByLink($fetched_uri, $uid);
@@ -3930,7 +4115,7 @@ class Item
                }
 
                $url = $shared['message_id'] ?: $shared['link'];
-               $id = self::fetchByLink($url);
+               $id = self::fetchByLink($url, 0, ActivityPub\Receiver::COMPLETION_ASYNC);
                if (!$id) {
                        Logger::notice('Post could not be fetched.', ['url' => $url, 'uid' => $uid]);
                        return 0;