]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge remote-tracking branch 'upstream/2022.09-rc' into language
[friendica.git] / src / Model / Item.php
index 10b14a4b466251c8c1e9a5154ea0e0ce8f32997b..78bc8c64d18b26410117ba65d0546db74ebb32e8 100644 (file)
@@ -33,7 +33,6 @@ use Friendica\Model\Tag;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
@@ -44,6 +43,7 @@ use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 use Friendica\Util\Temporal;
 use Friendica\Worker\Delivery;
+use GuzzleHttp\Psr7\Uri;
 use LanguageDetection\Language;
 
 class Item
@@ -74,6 +74,12 @@ class Item
        const PR_GLOBAL = 73;
        const PR_RELAY = 74;
        const PR_FETCHED = 75;
+       const PR_COMPLETION = 76;
+       const PR_DIRECT = 77;
+       const PR_ACTIVITY = 78;
+       const PR_DISTRIBUTE = 79;
+       const PR_PUSHED = 80;
+       const PR_LOCAL = 81;
 
        // system.accept_only_sharer setting values
        const COMPLETION_NONE    = 1;
@@ -83,7 +89,7 @@ class Item
        // Field list that is used to display the items
        const DISPLAY_FIELDLIST = [
                'uid', 'id', 'parent', 'guid', 'network', 'gravity',
-               'uri-id', 'uri', 'thr-parent-id', 'thr-parent', 'parent-uri-id', 'parent-uri',
+               'uri-id', 'uri', 'thr-parent-id', 'thr-parent', 'parent-uri-id', 'parent-uri', 'conversation',
                'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
                'wall', 'private', 'starred', 'origin', 'parent-origin', 'title', 'body', 'language',
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
@@ -103,7 +109,7 @@ class Item
 
        // Field list that is used to deliver items via the protocols
        const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
-                       'parent-guid', 'received', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
+                       'parent-guid', 'conversation', 'received', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
                        'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
                        'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
@@ -116,7 +122,7 @@ class Item
 
        // All fields in the item table
        const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
-                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid',
+                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'conversation', 'vid',
                        'contact-id', 'wall', 'gravity', 'extid', 'psid',
                        'created', 'edited', 'commented', 'received', 'changed', 'verb',
                        'postopts', 'plink', 'resource-id', 'event-id', 'inform',
@@ -273,7 +279,7 @@ class Item
                        if ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } elseif ($item['uid'] != 0) {
-                               Logger::notice('Wrong ownership. Not deleting item', ['id' => $item['id']]);
+                               Logger::warning('Wrong ownership. Not deleting item', ['id' => $item['id']]);
                        }
                }
                DBA::close($items);
@@ -343,7 +349,7 @@ class Item
 
                Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
 
-               if (!Post::exists(['`uri-id` = ? AND `uid` != 0 AND NOT `deleted`', $item['uri-id']])) {
+               if (!Post::exists(["`uri-id` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri-id']])) {
                        self::markForDeletion(['uri-id' => $item['uri-id'], 'uid' => 0, 'deleted' => false], $priority);
                }
 
@@ -441,21 +447,46 @@ class Item
         */
        private static function contactId(array $item): int
        {
-               if (!empty($item['contact-id']) && DBA::exists('contact', ['self' => true, 'id' => $item['contact-id']])) {
-                       return $item['contact-id'];
-               } elseif (($item['gravity'] == GRAVITY_PARENT) && !empty($item['uid']) && !empty($item['contact-id']) && Contact::isSharing($item['contact-id'], $item['uid'])) {
-                       return $item['contact-id'];
-               } elseif (!empty($item['uid']) && !Contact::isSharing($item['author-id'], $item['uid'])) {
+               if ($item['uid'] == 0) {
                        return $item['author-id'];
-               } elseif (!empty($item['contact-id'])) {
-                       return $item['contact-id'];
-               } else {
-                       $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+               }
+
+               if ($item['origin']) {
+                       $owner = User::getOwnerDataById($item['uid']);
+                       return $owner['id'];
+               }
+
+               if (!empty($item['causer-id']) && Contact::isSharing($item['causer-id'], $item['uid'], true)) {
+                       $cdata = Contact::getPublicAndUserContactID($item['causer-id'], $item['uid']);
+                       if (!empty($cdata['user'])) {
+                               return $cdata['user'];
+                       }
+               }
+
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       if (Contact::isSharingByURL($item['owner-link'], $item['uid'], true)) {
+                               $contact_id = Contact::getIdForURL($item['owner-link'], $item['uid']);
+                       } else {
+                               $contact_id = Contact::getIdForURL($item['owner-link']);
+                       }
                        if (!empty($contact_id)) {
                                return $contact_id;
                        }
                }
-               return $item['author-id'];
+
+               if (Contact::isSharingByURL($item['author-link'], $item['uid'], true)) {
+                       $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+               } else {
+                       $contact_id = Contact::getIdForURL($item['author-link']);
+               }
+
+               if (!empty($contact_id)) {
+                       return $contact_id;
+               }
+
+               Logger::warning('contact-id could not be fetched, using self contact instead.', ['uid' => $item['uid'], 'item' => $item]);
+               $self = Contact::selectFirst(['id'], ['self' => true, 'uid' => $item['uid']]);
+               return $self['id'];
        }
 
        /**
@@ -511,7 +542,7 @@ class Item
                        }
                } elseif ($item['network'] == Protocol::OSTATUS) {
                        // Check for an existing post with the same content. There seems to be a problem with OStatus.
-                       $condition = ['`body` = ? AND `network` = ? AND `created` = ? AND `contact-id` = ? AND `uid` = ?',
+                       $condition = ["`body` = ? AND `network` = ? AND `created` = ? AND `contact-id` = ? AND `uid` = ?",
                                        $item['body'], $item['network'], $item['created'], $item['contact-id'], $item['uid']];
                        if (Post::exists($condition)) {
                                Logger::notice('duplicated item with the same body found.', $item);
@@ -549,7 +580,7 @@ class Item
                if (!empty($item['uid'])) {
                        $owner = User::getOwnerDataById($item['uid'], false);
                        if (!$owner) {
-                               Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+                               Logger::warning('Missing item user owner data', ['uid' => $item['uid']]);
                                return false;
                        }
 
@@ -683,18 +714,23 @@ class Item
                        'uri-id', 'parent-uri-id',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'wall', 'private', 'origin', 'author-id'];
-               $condition = ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']];
+               $condition = ['uri-id' => [$item['thr-parent-id'], $item['parent-uri-id']], 'uid' => $item['uid']];
                $params = ['order' => ['id' => false]];
                $parent = Post::selectFirst($fields, $condition, $params);
 
-               if (!DBA::isResult($parent) && $item['origin']) {
-                       $stored = Item::storeForUserByUriId($item['thr-parent-id'], $item['uid']);
-                       Logger::info('Stored thread parent item for user', ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'stored' => $stored]);
-                       $parent = Post::selectFirst($fields, $condition, $params);
+               if (!DBA::isResult($parent) && Post::exists(['uri-id' => [$item['thr-parent-id'], $item['parent-uri-id']], 'uid' => 0])) {
+                       $stored = Item::storeForUserByUriId($item['thr-parent-id'], $item['uid'], ['post-reason' => Item::PR_COMPLETION]);
+                       if (!$stored && ($item['thr-parent-id'] != $item['parent-uri-id'])) {
+                               $stored = Item::storeForUserByUriId($item['parent-uri-id'], $item['uid'], ['post-reason' => Item::PR_COMPLETION]);
+                       }
+                       if ($stored) {
+                               Logger::info('Stored thread parent item for user', ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'stored' => $stored]);
+                               $parent = Post::selectFirst($fields, $condition, $params);
+                       }
                }
 
                if (!DBA::isResult($parent)) {
-                       Logger::notice('item parent was not found - ignoring item', ['thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
+                       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)]);
                        return [];
                }
 
@@ -709,7 +745,7 @@ class Item
                $toplevel_parent = Post::selectFirst($fields, $condition, $params);
 
                if (!DBA::isResult($toplevel_parent) && $item['origin']) {
-                       $stored = Item::storeForUserByUriId($item['parent-uri-id'], $item['uid']);
+                       $stored = Item::storeForUserByUriId($item['parent-uri-id'], $item['uid'], ['post-reason' => Item::PR_COMPLETION]);
                        Logger::info('Stored parent item for user', ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'stored' => $stored]);
                        $toplevel_parent = Post::selectFirst($fields, $condition, $params);
                }
@@ -780,20 +816,26 @@ class Item
                $uid = intval($item['uid']);
 
                $item['guid'] = self::guid($item, $notify);
-               $item['uri'] = substr(trim($item['uri'] ?? '') ?: self::newURI($item['uid'], $item['guid']), 0, 255);
+               $item['uri'] = substr(trim($item['uri'] ?? '') ?: self::newURI($item['guid']), 0, 255);
 
                // Store URI data
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
 
                // Backward compatibility: parent-uri used to be the direct parent uri.
                // If it is provided without a thr-parent, it probably is the old behavior.
-               $item['thr-parent'] = trim($item['thr-parent'] ?? $item['parent-uri'] ?? $item['uri']);
-               $item['parent-uri'] = $item['thr-parent'];
+               if (empty($item['thr-parent']) || empty($item['parent-uri'])) {
+                       $item['thr-parent'] = trim($item['thr-parent'] ?? $item['parent-uri'] ?? $item['uri']);
+                       $item['parent-uri'] = $item['thr-parent'];
+               }
 
-               $item['thr-parent-id'] = $item['parent-uri-id'] = ItemURI::getIdByURI($item['thr-parent']);
+               $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
+               $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
 
                // Store conversation data
-               $item = Conversation::insert($item);
+               $source = $item['source'] ?? '';
+               unset($item['conversation-uri']);
+               unset($item['conversation-href']);
+               unset($item['source']);
 
                /*
                 * Do we already have this item?
@@ -877,8 +919,6 @@ class Item
 
                $item['gravity'] = self::getGravity($item);
 
-               $item['language'] = self::getLanguage($item);
-
                $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
                        'photo' => $item['author-avatar'], 'network' => $item['network']];
                $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
@@ -887,20 +927,16 @@ class Item
                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
                $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
 
-               $actor = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
-               if (!$item['origin'] && ($item['uid'] != 0) && Contact::isSharing($actor, $item['uid'])) {
-                       $item['post-reason'] = self::PR_FOLLOWER;
-               }
+               $item['post-reason'] = self::getPostReason($item);
 
                // Ensure that there is an avatar cache
                Contact::checkAvatarCache($item['author-id']);
                Contact::checkAvatarCache($item['owner-id']);
 
-               // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
                $item['contact-id'] = self::contactId($item);
 
                if (!empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) &&
-                       empty($item['origin']) &&self::isTooOld($item)) {
+                       empty($item['origin']) && self::isTooOld($item)) {
                        Logger::info('Item is too old', ['item' => $item]);
                        return 0;
                }
@@ -966,11 +1002,19 @@ class Item
                } else {
                        $parent_id = 0;
                        $parent_origin = $item['origin'];
+
+                       if ($item['wall'] && empty($item['conversation'])) {
+                               $item['conversation'] = $item['parent-uri'] . '#context';
+                       }
                }
 
                $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
                $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
 
+               if (!empty($item['conversation']) && empty($item['conversation-id'])) {
+                       $item['conversation-id'] = ItemURI::getIdByURI($item['conversation']);
+               }
+
                // Is this item available in the global items (with uid=0)?
                if ($item['uid'] == 0) {
                        $item['global'] = true;
@@ -1062,11 +1106,9 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
 
-               if (stristr($item['verb'], Activity::POKE)) {
-                       $notify_type = Delivery::POKE;
-               } else {
-                       $notify_type = Delivery::POST;
-               }
+               $item['language'] = self::getLanguage($item);
+
+               $notify_type = Delivery::POST;
 
                // Filling item related side tables
                if (!empty($item['attach'])) {
@@ -1243,6 +1285,9 @@ class Item
                }
 
                if ($transmit) {
+                       if (!empty($source)) {
+                               Post\Activity::insert($item['uri-id'], $source);
+                       }
                        Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$posted_item['uri-id'], (int)$posted_item['uid']);
                }
 
@@ -1251,11 +1296,28 @@ class Item
                        self::updateDisplayCache($posted_item['uri-id']);
                }
 
-               if ($posted_item['origin'] && ($posted_item['uid'] != 0) && in_array($posted_item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
-                       DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_OUTBOX . $posted_item['uid']);
+               return $post_user_id;
+       }
+
+       /**
+        * Fetch the post reason for a given item array
+        *
+        * @param array $item
+        *
+        * @return integer
+        */
+       public static function getPostReason(array $item): int
+       {
+               $actor = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
+               if (empty($item['origin']) && ($item['uid'] != 0) && Contact::isSharing($actor, $item['uid'])) {
+                       return self::PR_FOLLOWER;
                }
 
-               return $post_user_id;
+               if (!empty($item['origin']) && empty($item['post-reason'])) {
+                       return self::PR_LOCAL;
+               }
+
+               return $item['post-reason'] ?? self::PR_NONE;
        }
 
        /**
@@ -1333,14 +1395,8 @@ class Item
 
                $uids = Tag::getUIDListByURIId($item['uri-id']);
                foreach ($uids as $uid) {
-                       if (Contact::isSharing($item['author-id'], $uid)) {
-                               $fields = [];
-                       } else {
-                               $fields = ['post-reason' => self::PR_TAG];
-                       }
-
-                       $stored = self::storeForUserByUriId($item['uri-id'], $uid, $fields);
-                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'fields' => $fields, 'stored' => $stored]);
+                       $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]);
                }
        }
 
@@ -1364,7 +1420,7 @@ class Item
                $condition = ['id' => $itemid, 'uid' => 0,
                        'network' => array_merge(Protocol::FEDERATED ,['']),
                        'visible' => true, 'deleted' => false, 'private' => [self::PUBLIC, self::UNLISTED]];
-               $item = Post::selectFirst(self::ITEM_FIELDLIST, $condition);
+               $item = Post::selectFirst(array_merge(self::ITEM_FIELDLIST, ['protocol']), $condition);
                if (!DBA::isResult($item)) {
                        Logger::warning('Item not found', ['condition' => $condition]);
                        return;
@@ -1432,6 +1488,7 @@ class Item
                        if ($origin_uid == $uid) {
                                $item['diaspora_signed_text'] = $signed_text;
                        }
+                       $item['post-reason'] = self::PR_DISTRIBUTE;
                        self::storeForUser($item, $uid);
                }
        }
@@ -1452,12 +1509,20 @@ class Item
                        return 0;
                }
 
-               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => $source_uid]);
+               $item = Post::selectFirst(array_merge(self::ITEM_FIELDLIST, ['protocol']), ['uri-id' => $uri_id, 'uid' => $source_uid]);
                if (!DBA::isResult($item)) {
                        Logger::warning('Item could not be fetched', ['uri-id' => $uri_id, 'uid' => $source_uid]);
                        return 0;
                }
 
+               if (($uid != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
+                       $owner = User::getOwnerDataById($uid);
+                       if (($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) && !Tag::isMentioned($uri_id, $owner['url'])) {
+                               Logger::info('Target user is a forum but is not mentioned here, thread will not be stored', ['uid' => $uid, 'uri-id' => $uri_id]);
+                               return 0;
+                       }
+               }
+
                if (($source_uid == 0) && (($item['private'] == self::PRIVATE) || !in_array($item['network'], Protocol::FEDERATED))) {
                        Logger::notice('Item is private or not from a federated network. It will not be stored for the user.', ['uri-id' => $uri_id, 'uid' => $uid, 'private' => $item['private'], 'network' => $item['network']]);
                        return 0;
@@ -1467,34 +1532,59 @@ class Item
 
                $item = array_merge($item, $fields);
 
+               $item['post-reason'] = self::getPostReason($item);
+
                $is_reshare = ($item['gravity'] == GRAVITY_ACTIVITY) && ($item['verb'] == Activity::ANNOUNCE);
 
-               if ((($item['gravity'] == GRAVITY_PARENT) || $is_reshare) &&
+               if (($uid != 0) && (($item['gravity'] == GRAVITY_PARENT) || $is_reshare) &&
                        DI::pConfig()->get($uid, 'system', 'accept_only_sharer') == self::COMPLETION_NONE &&
-                       !Contact::isSharingByURL($item['author-link'], $uid) &&
-                       !Contact::isSharingByURL($item['owner-link'], $uid)) {
-                       Logger::info('Contact is not a follower, thread will not be stored', ['author' => $item['author-link'], 'uid' => $uid]);
+                       !in_array($item['post-reason'], [self::PR_FOLLOWER, self::PR_TAG, self::PR_TO, self::PR_CC])) {
+                       Logger::info('Contact is not a follower, thread will not be stored', ['author' => $item['author-link'], 'uid' => $uid, 'uri-id' => $uri_id]);
                        return 0;
                }
 
-               if (($uri_id != $item['thr-parent-id']) && (($item['gravity'] == GRAVITY_COMMENT) || $is_reshare) && !Post::exists(['uri-id' => $item['thr-parent-id'], 'uid' => $uid])) {
-                       // Fetch the origin user for the post
-                       $origin_uid = self::GetOriginUidForUriId($item['thr-parent-id'], $uid);
-                       if (is_null($origin_uid)) {
-                               Logger::info('Origin item was not found', ['uid' => $uid, 'uri-id' => $item['thr-parent-id']]);
+               $causer = $item['causer-id'] ?: $item['author-id'];
+
+               if (($uri_id != $item['parent-uri-id']) && ($item['gravity'] == GRAVITY_COMMENT) && !Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
+                       if (!self::fetchParent($item['parent-uri-id'], $uid, $causer)) {
+                               Logger::info('Parent post had not been added', ['uri-id' => $item['parent-uri-id'], 'uid' => $uid, 'causer' => $causer]);
                                return 0;
                        }
-
-                       $causer = $item['causer-id'] ?: $item['author-id'];
-                       $result = self::storeForUserByUriId($item['thr-parent-id'], $uid, ['causer-id' => $causer, 'post-reason' => self::PR_FETCHED], $origin_uid);
-                       Logger::info('Fetched thread parent', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'causer' => $causer, 'result' => $result]);
+                       Logger::info('Fetched parent post', ['uri-id' => $item['parent-uri-id'], 'uid' => $uid, 'causer' => $causer]);
+               } elseif (($uri_id != $item['thr-parent-id']) && $is_reshare && !Post::exists(['uri-id' => $item['thr-parent-id'], 'uid' => $uid])) {
+                       if (!self::fetchParent($item['thr-parent-id'], $uid, $causer)) {
+                               Logger::info('Thread parent had not been added', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'causer' => $causer]);
+                               return 0;
+                       }
+                       Logger::info('Fetched thread parent', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'causer' => $causer]);
                }
 
                $stored = self::storeForUser($item, $uid);
-               Logger::info('Item stored for user', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'source-uid' => $source_uid, 'stored' => $stored]);
+               Logger::info('Item stored for user', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'causer' => $causer, 'source-uid' => $source_uid, 'stored' => $stored]);
                return $stored;
        }
 
+       /**
+        * Fetch the parent with the given uri-id
+        *
+        * @param integer $uri_id
+        * @param integer $uid
+        * @param integer $causer
+        *
+        * @return integer
+        */
+       private static function fetchParent(int $uri_id, int $uid, int $causer): int
+       {
+               // Fetch the origin user for the post
+               $origin_uid = self::GetOriginUidForUriId($uri_id, $uid);
+               if (is_null($origin_uid)) {
+                       Logger::info('Origin item was not found', ['uid' => $uid, 'uri-id' => $uri_id]);
+                       return 0;
+               }
+
+               return self::storeForUserByUriId($uri_id, $uid, ['causer-id' => $causer, 'post-reason' => self::PR_FETCHED], $origin_uid);
+       }
+
        /**
         * Returns the origin uid of a post if the given user is allowed to see it.
         *
@@ -1555,20 +1645,21 @@ class Item
         */
        private static function storeForUser(array $item, int $uid): int
        {
-               if (Post::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
+               $post = Post::selectFirst(['id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+               if (!empty($post['id'])) {
                        if (!empty($item['event-id'])) {
-                               $post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
-                               if (!empty($post['event-id'])) {
+                               $event_post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+                               if (!empty($event_post['event-id'])) {
                                        $event = DBA::selectFirst('event', ['edited', 'start', 'finish', 'summary', 'desc', 'location', 'nofinish', 'adjust'], ['id' => $item['event-id']]);
                                        if (!empty($event)) {
                                                // We aren't using "Event::store" here, since we don't want to trigger any further action
-                                               $ret = DBA::update('event', $event, ['id' => $post['event-id']]);
-                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $post['event-id'], 'ret' => $ret]);
+                                               $ret = DBA::update('event', $event, ['id' => $event_post['event-id']]);
+                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $event_post['event-id'], 'ret' => $ret]);
                                        }
                                }
                        }
-                       Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]);
-                       return 0;
+                       Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'id' => $post['id']]);
+                       return $post['id'];
                }
 
                // Data from the "post-user" table
@@ -1583,7 +1674,6 @@ class Item
                unset($item['event-id']);
                unset($item['hidden']);
                unset($item['notification-type']);
-               unset($item['post-reason']);
 
                // Data from the "post-delivery-data" table
                unset($item['postopts']);
@@ -1593,25 +1683,7 @@ class Item
                $item['origin'] = 0;
                $item['wall'] = 0;
 
-               if ($item['gravity'] == GRAVITY_PARENT) {
-                       $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
-               } else {
-                       $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']);
-               }
-
-               if (!empty($contact['id'])) {
-                       $item['contact-id'] = $contact['id'];
-               } else {
-                       // Shouldn't happen at all
-                       Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]);
-                       $self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
-                       if (!DBA::isResult($self)) {
-                               // Shouldn't happen even less
-                               Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
-                               return 0;
-                       }
-                       $item['contact-id'] = $self['id'];
-               }
+               $item['contact-id'] = self::contactId($item);
 
                $notify = false;
                if ($item['gravity'] == GRAVITY_PARENT) {
@@ -1670,7 +1742,7 @@ class Item
                        return;
                }
 
-               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
+               $item = Post::selectFirst(array_merge(self::ITEM_FIELDLIST, ['protocol']), ['id' => $itemid]);
 
                if (DBA::isResult($item)) {
                        // Preparing public shadow (removing user specific data)
@@ -1706,7 +1778,7 @@ class Item
         */
        private static function addShadowPost(int $itemid)
        {
-               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
+               $item = Post::selectFirst(array_merge(self::ITEM_FIELDLIST, ['protocol']), ['id' => $itemid]);
                if (!DBA::isResult($item)) {
                        return;
                }
@@ -1797,6 +1869,8 @@ class Item
                        return '';
                }
 
+               $naked_body = self::getDominantLanguage($naked_body);
+
                $availableLanguages = DI::l10n()->getAvailableLanguages();
                // See https://github.com/friendica/friendica/issues/10511
                // Persian is manually added to language detection until a persian translation is provided for the interface, at
@@ -1812,6 +1886,33 @@ class Item
                return '';
        }
 
+       /**
+        * Check if latin or non latin are dominant in the body and only return the dominant one
+        *
+        * @param string $body
+        * @return string
+        */
+       private static function getDominantLanguage(string $body): string
+       {
+               $latin = '';
+               $non_latin = '';
+               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;
+                       } else {
+                               $non_latin .= $character;
+                       }
+               }
+               return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+       }
+
        public static function getLanguageMessage(array $item): string
        {
                $iso639 = new \Matriphe\ISO639\ISO639;
@@ -1830,38 +1931,37 @@ class Item
         * Posts that are created on this system are using System::createUUID.
         * Received ActivityPub posts are using Processor::getGUIDByURL.
         *
-        * @param string $uri uri of an item entry
-        * @param string $host hostname for the GUID prefix
-        * @return string unique guid
+        * @param string      $uri uri of an item entry
+        * @param string|null $host hostname for the GUID prefix
+        * @return string Unique guid
         */
-       public static function guidFromUri(string $uri, string $host): string
+       public static function guidFromUri(string $uri, string $host = null): string
        {
                // Our regular guid routine is using this kind of prefix as well
                // We have to avoid that different routines could accidentally create the same value
                $parsed = parse_url($uri);
 
                // Remove the scheme to make sure that "https" and "http" doesn't make a difference
-               unset($parsed["scheme"]);
+               unset($parsed['scheme']);
 
                // Glue it together to be able to make a hash from it
-               $host_id = implode("/", $parsed);
+               $host_id = implode('/', $parsed);
 
                // Use a mixture of several hashes to provide some GUID like experience
-               return hash("crc32", $host) . '-'. hash('joaat', $host_id) . '-'. hash('fnv164', $host_id);
+               return hash('crc32', $host) . '-'. hash('joaat', $host_id) . '-'. hash('fnv164', $host_id);
        }
 
        /**
         * generate an unique URI
         *
-        * @param integer $uid  User id
-        * @param string  $guid An existing GUID (Otherwise it will be generated)
+        * @param string $guid An existing GUID (Otherwise it will be generated)
         *
         * @return string
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function newURI($uid, $guid = "")
+       public static function newURI(string $guid = ''): string
        {
-               if ($guid == "") {
+               if ($guid == '') {
                        $guid = System::createUUID();
                }
 
@@ -1911,15 +2011,15 @@ class Item
                        } else {
                                $condition = ['id' => $arr['contact-id'], 'self' => false];
                        }
-                       Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
+                       Contact::update(['failed' => false, 'local-data' => true, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
                }
                // Now do the same for the system wide contacts with uid=0
                if ($arr['private'] != self::PRIVATE) {
-                       Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
+                       Contact::update(['failed' => false, 'local-data' => true, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
                                ['id' => $arr['owner-id']]);
 
                        if ($arr['owner-id'] != $arr['author-id']) {
-                               Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
+                               Contact::update(['failed' => false, 'local-data' => true, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
                                        ['id' => $arr['author-id']]);
                        }
                }
@@ -2021,15 +2121,9 @@ class Item
                }
 
                if ($item['gravity'] == GRAVITY_PARENT) {
-                       $tags = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
-                       foreach ($tags as $tag) {
-                               if (Strings::compareLink($owner['url'], $tag['url'])) {
-                                       $mention = true;
-                                       Logger::info('Mention found in tag.', ['url' => $tag['url'], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
-                               }
-                       }
-
-                       if (!$mention) {
+                       if (Tag::isMentioned($item['uri-id'], $owner['url'])) {
+                               Logger::info('Mention found in tag.', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
+                       } else {
                                Logger::info('Top-level post without mention is deleted.', ['uri' => $item['uri'], $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
                                Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
                                return true;
@@ -2039,15 +2133,9 @@ class Item
 
                        Hook::callAll('tagged', $arr);
                } else {
-                       $tags = Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
-                       foreach ($tags as $tag) {
-                               if (Strings::compareLink($owner['url'], $tag['url'])) {
-                                       $mention = true;
-                                       Logger::info('Mention found in parent tag.', ['url' => $tag['url'], 'uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
-                               }
-                       }
-
-                       if (!$mention) {
+                       if (Tag::isMentioned($item['parent-uri-id'], $owner['url'])) {
+                               Logger::info('Mention found in parent tag.', ['uri' => $item['uri'], 'uid' => $uid, 'id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
+                       } else {
                                Logger::debug('No mentions found in parent, quitting here.', ['id' => $item_id, 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
                                return false;
                        }
@@ -2150,7 +2238,7 @@ class Item
                                $old_uri_id = $datarray["uri-id"] ?? 0;
                                $datarray["guid"] = System::createUUID();
                                unset($datarray["plink"]);
-                               $datarray["uri"] = self::newURI($contact['uid'], $datarray["guid"]);
+                               $datarray["uri"] = self::newURI($datarray["guid"]);
                                $datarray["uri-id"] = ItemURI::getIdByURI($datarray["uri"]);
                                $datarray["extid"] = Protocol::DFRN;
                                $urlpart = parse_url($datarray2['author-link']);
@@ -2456,7 +2544,7 @@ class Item
 
                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::notice('like: unknown item', ['id' => $item_id]);
+                       Logger::warning('Post had not been fetched', ['id' => $item_id]);
                        return false;
                }
 
@@ -2467,7 +2555,7 @@ class Item
                }
 
                if (!Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
-                       $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid);
+                       $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid, ['post-reason' => Item::PR_ACTIVITY]);
                        if (($item['parent-uri-id'] == $item['uri-id']) && !empty($stored)) {
                                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]);
                                if (!DBA::isResult($item)) {
@@ -2485,7 +2573,7 @@ class Item
                }
 
                // Retrieve the current logged in user's public contact
-               $author_id = Contact::getIdForURL($owner['url']);
+               $author_id = Contact::getPublicIdByUserId($uid);
                if (empty($author_id)) {
                        Logger::info('Empty public contact');
                        return false;
@@ -2522,7 +2610,7 @@ class Item
                                $activity = Activity::ANNOUNCE;
                                break;
                        default:
-                               Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
+                               Logger::warning('unknown verb', ['verb' => $verb, 'item' => $item_id]);
                                return false;
                }
 
@@ -2591,8 +2679,8 @@ class Item
 
                $new_item = [
                        'guid'          => System::createUUID(),
-                       'uri'           => self::newURI($item['uid']),
-                       'uid'           => $item['uid'],
+                       'uri'           => self::newURI(),
+                       'uid'           => $uid,
                        'contact-id'    => $owner['id'],
                        'wall'          => $item['wall'],
                        'origin'        => 1,
@@ -2615,22 +2703,20 @@ class Item
                        'unseen'        => 1,
                ];
 
-               $signed = Diaspora::createLikeSignature($uid, $new_item);
-               if (!empty($signed)) {
-                       $new_item['diaspora_signed_text'] = json_encode($signed);
+               if (in_array($activity, [Activity::LIKE, Activity::DISLIKE])) {
+                       $signed = Diaspora::createLikeSignature($uid, $new_item);
+                       if (!empty($signed)) {
+                               $new_item['diaspora_signed_text'] = json_encode($signed);
+                       }
                }
 
-               $new_item_id = self::insert($new_item);
+               self::insert($new_item, true);
 
                // If the parent item isn't visible then set it to visible
+               // @todo Check if this is still needed
                if (!$item['visible']) {
                        self::update(['visible' => true], ['id' => $item['id']]);
                }
-
-               $new_item['id'] = $new_item_id;
-
-               Hook::callAll('post_local_end', $new_item);
-
                return true;
        }
 
@@ -2831,7 +2917,8 @@ class Item
                        $shared_uri_id = 0;
                        $shared_links = [];
                }
-               $attachments = Post\Media::splitAttachments($item['uri-id'], $item['guid'] ?? '', $shared_links, $item['has-media']);
+
+               $attachments = Post\Media::splitAttachments($item['uri-id'], $item['guid'] ?? '', $shared_links, $item['has-media'] ?? false);
                $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? '');
 
                $item['body'] = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", "\n", $item['body']);
@@ -2924,9 +3011,13 @@ class Item
        {
                // Make sure that for example site parameters aren't used when testing if the link is contained in the body
                $urlparts = parse_url($url);
-               unset($urlparts['query']);
-               unset($urlparts['fragment']);
-               $url = Network::unparseURL($urlparts);
+               if (!empty($urlparts)) {
+                       unset($urlparts['query']);
+                       unset($urlparts['fragment']);
+                       $url = (string)Uri::fromParts($urlparts);
+               } else {
+                       return false;
+               }
 
                // Remove media links to only search in embedded content
                // @todo Check images for image link, audio for audio links, ...
@@ -3235,21 +3326,18 @@ class Item
 
                        $options = Post\QuestionOption::getByURIId($item['uri-id']);
                        foreach ($options as $key => $option) {
-                               $percent = $question['voters'] ? ($option['replies'] / $question['voters'] * 100) : 0;
-
-                               $options[$key]['percent'] = $percent;
-
                                if ($question['voters'] > 0) {
-                                       $options[$key]['vote'] = DI::l10n()->t('%s (%d%s, %d votes)', $option['name'], round($percent, 1), '%', $option['replies']);
+                                       $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));
                                } else {
-                                       $options[$key]['vote'] = DI::l10n()->t('%s (%d votes)', $option['name'], $option['replies']);
+                                       $options[$key]['vote'] = DI::l10n()->tt('%2$s (%1$d vote)', '%2$s (%1$d votes)', $option['replies'], $option['name'], );
                                }
                        }
 
                        if (!empty($question['voters']) && !empty($question['endtime'])) {
-                               $summary = DI::l10n()->t('%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'], Temporal::getRelativeDate($question['endtime']));
                        } elseif (!empty($question['voters'])) {
-                               $summary = DI::l10n()->t('%d voters.', $question['voters']);
+                               $summary = DI::l10n()->tt('%d voter.', '%d voters.', $question['voters']);
                        } elseif (!empty($question['endtime'])) {
                                $summary = DI::l10n()->t('Poll end: %s', Temporal::getRelativeDate($question['endtime']));
                        } else {
@@ -3410,7 +3498,9 @@ class Item
                        return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
                }
 
-               if ($fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri)) {
+               $fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri);
+
+               if ($fetched_uri) {
                        $item_id = self::searchByLink($fetched_uri, $uid);
                } else {
                        $item_id = Diaspora::fetchByURL($uri);
@@ -3437,18 +3527,7 @@ class Item
         */
        public static function getShareArray(array $item): array
        {
-               if (!preg_match("/(.*?)\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", $item['body'], $matches)) {
-                       return [];
-               }
-
-               $attribute_string = $matches[2];
-               $attributes = ['comment' => trim($matches[1]), 'shared' => trim($matches[3])];
-               foreach (['author', 'profile', 'avatar', 'guid', 'posted', 'link'] as $field) {
-                       if (preg_match("/$field=(['\"])(.+?)\\1/ism", $attribute_string, $matches)) {
-                               $attributes[$field] = trim(html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8'));
-                       }
-               }
-               return $attributes;
+               return BBCode::fetchShareAttributes($item['body']);
        }
 
        /**