]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #8575 from annando/post-delivery-data
[friendica.git] / src / Model / Item.php
index b960ba38f3dfa5fbb91d3a719b9416ef45632f3f..a4f45e3f457a065f5efe787338446d4fa4f72bed 100644 (file)
@@ -44,6 +44,7 @@ use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use Friendica\Worker\Delivery;
 use Text_LanguageDetect;
+use Friendica\Repository\PermissionSet as RepPermissionSet;
 
 class Item
 {
@@ -60,7 +61,7 @@ class Item
 
        // Field list that is used to display the items
        const DISPLAY_FIELDLIST = [
-               'uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
+               'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
                'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
                'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach', 'language',
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
@@ -76,10 +77,10 @@ class Item
        ];
 
        // Field list that is used to deliver items via the protocols
-       const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri', 'thr-parent', 'parent-uri', 'guid',
+       const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
                        'parent-guid', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
                        'private', 'title', 'body', 'location', 'coord', 'app',
-                       'attach', 'tag', 'deleted', 'extid', 'post-type',
+                       'attach', 'deleted', 'extid', 'post-type',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'author-id', 'author-link', 'owner-link', 'contact-uid',
                        'signed_text', 'signature', 'signer', 'network'];
@@ -93,10 +94,11 @@ class Item
        const CONTENT_FIELDLIST = ['language'];
 
        // All fields in the item table
-       const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', 'guid',
+       const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
+                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id',
                        'contact-id', 'type', 'wall', 'gravity', 'extid', 'icid', 'iaid', 'psid',
                        'created', 'edited', 'commented', 'received', 'changed', 'verb',
-                       'postopts', 'plink', 'resource-id', 'event-id', 'tag', 'attach', 'inform',
+                       'postopts', 'plink', 'resource-id', 'event-id', 'attach', 'inform',
                        'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
                        'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network',
@@ -113,6 +115,10 @@ class Item
                Activity::FOLLOW,
                Activity::ANNOUNCE];
 
+       const PUBLIC = 0;
+       const PRIVATE = 1;
+       const UNLISTED = 2;
+
        private static $legacy_mode = null;
 
        public static function isLegacyMode()
@@ -172,7 +178,7 @@ class Item
                }
 
                $pinned = [];
-               while ($useritem = self::fetch($useritems)) {
+               while ($useritem = DBA::fetch($useritems)) {
                        $pinned[] = $useritem['iid'];
                }
                DBA::close($useritems);
@@ -277,7 +283,7 @@ class Item
 
                // Fetch data from the item-content table whenever there is content there
                if (self::isLegacyMode()) {
-                       $legacy_fields = array_merge(ItemDeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
+                       $legacy_fields = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
                        foreach ($legacy_fields as $field) {
                                if (empty($row[$field]) && !empty($row['internal-item-' . $field])) {
                                        $row[$field] = $row['internal-item-' . $field];
@@ -311,19 +317,25 @@ class Item
                }
 
                if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
-                       // Build the tag string out of the term entries
-                       if (array_key_exists('tag', $row) && empty($row['tag'])) {
-                               $row['tag'] = Term::tagTextFromItemId($row['internal-iid']);
-                       }
-
                        // Build the file string out of the term entries
                        if (array_key_exists('file', $row) && empty($row['file'])) {
                                $row['file'] = Term::fileTextFromItemId($row['internal-iid']);
                        }
                }
 
-               if (array_key_exists('signed_text', $row) && array_key_exists('interaction', $row) && !is_null($row['interaction'])) {
-                       $row['signed_text'] = $row['interaction'];
+               if ($row['internal-psid'] == RepPermissionSet::PUBLIC) {
+                       if (array_key_exists('allow_cid', $row)) {
+                               $row['allow_cid'] = '';
+                       }
+                       if (array_key_exists('allow_gid', $row)) {
+                               $row['allow_gid'] = '';
+                       }
+                       if (array_key_exists('deny_cid', $row)) {
+                               $row['deny_cid'] = '';
+                       }
+                       if (array_key_exists('deny_gid', $row)) {
+                               $row['deny_gid'] = '';
+                       }
                }
 
                if (array_key_exists('ignored', $row) && array_key_exists('internal-user-ignored', $row) && !is_null($row['internal-user-ignored'])) {
@@ -334,8 +346,8 @@ class Item
                unset($row['internal-activity']);
                unset($row['internal-network']);
                unset($row['internal-iid']);
+               unset($row['internal-psid']);
                unset($row['internal-iaid']);
-               unset($row['internal-icid']);
                unset($row['internal-user-ignored']);
                unset($row['interaction']);
 
@@ -652,15 +664,15 @@ class Item
        {
                $fields = [];
 
-               $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent', 'guid',
+               $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
+                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id',
                        'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid',
                        'created', 'edited', 'commented', 'received', 'changed', 'psid',
-                       'resource-id', 'event-id', 'tag', 'attach', 'post-type', 'file',
+                       'resource-id', 'event-id', 'attach', 'post-type', 'file',
                        'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
                        'id' => 'item_id', 'network', 'icid', 'iaid', 'id' => 'internal-iid',
-                       'network' => 'internal-network', 'icid' => 'internal-icid',
-                       'iaid' => 'internal-iaid'];
+                       'network' => 'internal-network', 'iaid' => 'internal-iaid', 'psid' => 'internal-psid'];
 
                if ($usermode) {
                        $fields['user-item'] = ['pinned', 'notification-type', 'ignored' => 'internal-user-ignored'];
@@ -670,7 +682,7 @@ class Item
 
                $fields['item-content'] = array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST);
 
-               $fields['item-delivery-data'] = array_merge(ItemDeliveryData::LEGACY_FIELD_LIST, ItemDeliveryData::FIELD_LIST);
+               $fields['post-delivery-data'] = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, Post\DeliveryData::FIELD_LIST);
 
                $fields['permissionset'] = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
 
@@ -695,9 +707,7 @@ class Item
                        'nofinish' => 'event-nofinish','adjust' => 'event-adjust',
                        'ignore' => 'event-ignore', 'id' => 'event-id'];
 
-               $fields['sign'] = ['signed_text', 'signature', 'signer'];
-
-               $fields['diaspora-interaction'] = ['interaction'];
+               $fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text'];
 
                return $fields;
        }
@@ -782,10 +792,6 @@ class Item
                        $joins .= " LEFT JOIN `event` ON `event-id` = `event`.`id`";
                }
 
-               if (strpos($sql_commands, "`sign`.") !== false) {
-                       $joins .= " LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`";
-               }
-
                if (strpos($sql_commands, "`diaspora-interaction`.") !== false) {
                        $joins .= " LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `item`.`uri-id`";
                }
@@ -798,8 +804,8 @@ class Item
                        $joins .= " LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`";
                }
 
-               if (strpos($sql_commands, "`item-delivery-data`.") !== false) {
-                       $joins .= " LEFT JOIN `item-delivery-data` ON `item-delivery-data`.`iid` = `item`.`id`";
+               if (strpos($sql_commands, "`post-delivery-data`.") !== false) {
+                       $joins .= " LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin`";
                }
 
                if (strpos($sql_commands, "`permissionset`.") !== false) {
@@ -825,13 +831,10 @@ class Item
         *
         * @return string The field list
         */
-       private static function constructSelectFields($fields, $selected)
+       private static function constructSelectFields(array $fields, array $selected)
        {
                if (!empty($selected)) {
-                       $selected[] = 'internal-iid';
-                       $selected[] = 'internal-iaid';
-                       $selected[] = 'internal-icid';
-                       $selected[] = 'internal-network';
+                       $selected = array_merge($selected, ['internal-iid', 'internal-psid', 'internal-iaid', 'internal-network']);
                }
 
                if (in_array('verb', $selected)) {
@@ -842,11 +845,7 @@ class Item
                        $selected[] = 'internal-user-ignored';
                }
 
-               if (in_array('signed_text', $selected)) {
-                       $selected[] = 'interaction';
-               }
-
-               $legacy_fields = array_merge(ItemDeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
+               $legacy_fields = array_merge(Post\DeliveryData::LEGACY_FIELD_LIST, self::MIXED_CONTENT_FIELDLIST);
 
                $selection = [];
                foreach ($fields as $table => $table_fields) {
@@ -918,7 +917,7 @@ class Item
                // We cannot simply expand the condition to check for origin entries
                // The condition needn't to be a simple array but could be a complex condition.
                // And we have to execute this query before the update to ensure to fetch the same data.
-               $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'iaid', 'icid', 'tag', 'file'], $condition);
+               $items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'iaid', 'icid', 'file'], $condition);
 
                $content_fields = [];
                foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
@@ -932,7 +931,7 @@ class Item
                        }
                }
 
-               $delivery_data = ItemDeliveryData::extractFields($fields);
+               $delivery_data = Post\DeliveryData::extractFields($fields);
 
                $clear_fields = ['bookmark', 'type', 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'postopts', 'inform'];
                foreach ($clear_fields as $field) {
@@ -941,13 +940,6 @@ class Item
                        }
                }
 
-               if (array_key_exists('tag', $fields)) {
-                       $tags = $fields['tag'];
-                       $fields['tag'] = null;
-               } else {
-                       $tags = null;
-               }
-
                if (array_key_exists('file', $fields)) {
                        $files = $fields['file'];
                        $fields['file'] = null;
@@ -1020,13 +1012,6 @@ class Item
                                }
                        }
 
-                       if (!is_null($tags)) {
-                               Term::insertFromTagFieldByItemId($item['id'], $tags);
-                               if (!empty($item['tag'])) {
-                                       DBA::update('item', ['tag' => ''], ['id' => $item['id']]);
-                               }
-                       }
-
                        if (!is_null($files)) {
                                Term::insertFromFileFieldByItemId($item['id'], $files);
                                if (!empty($item['file'])) {
@@ -1034,7 +1019,7 @@ class Item
                                }
                        }
 
-                       ItemDeliveryData::update($item['id'], $delivery_data);
+                       Post\DeliveryData::update($item['uri-id'], $delivery_data);
 
                        self::updateThread($item['id']);
 
@@ -1062,11 +1047,11 @@ class Item
         * @param integer $priority  Priority for the notification
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function delete($condition, $priority = PRIORITY_HIGH)
+       public static function markForDeletion($condition, $priority = PRIORITY_HIGH)
        {
                $items = self::select(['id'], $condition);
                while ($item = self::fetch($items)) {
-                       self::deleteById($item['id'], $priority);
+                       self::markForDeletionById($item['id'], $priority);
                }
                DBA::close($items);
        }
@@ -1093,7 +1078,7 @@ class Item
                                // Delete notifications
                                DBA::delete('notify', ['iid' => $item['id'], 'uid' => $uid]);
                        } elseif ($item['uid'] == $uid) {
-                               self::deleteById($item['id'], PRIORITY_HIGH);
+                               self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } else {
                                Logger::log('Wrong ownership. Not deleting item ' . $item['id']);
                        }
@@ -1102,29 +1087,30 @@ class Item
        }
 
        /**
-        * Delete an item and notify others about it - if it was ours
+        * Mark an item for deletion, delete related data and notify others about it - if it was ours
         *
-        * @param integer $item_id  Item ID that should be delete
+        * @param integer $item_id
         * @param integer $priority Priority for the notification
         *
         * @return boolean success
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function deleteById($item_id, $priority = PRIORITY_HIGH)
+       public static function markForDeletionById($item_id, $priority = PRIORITY_HIGH)
        {
+               Logger::info('Mark item for deletion by id', ['id' => $item_id, 'callstack' => System::callstack()]);
                // locate item to be deleted
-               $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
+               $fields = ['id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin',
                        'deleted', 'file', 'resource-id', 'event-id', 'attach',
                        'verb', 'object-type', 'object', 'target', 'contact-id',
                        'icid', 'iaid', 'psid'];
                $item = self::selectFirst($fields, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::log('Item with ID ' . $item_id . " hasn't been found.", Logger::DEBUG);
+                       Logger::info('Item not found.', ['id' => $item_id]);
                        return false;
                }
 
                if ($item['deleted']) {
-                       Logger::log('Item with ID ' . $item_id . ' has already been deleted.', Logger::DEBUG);
+                       Logger::info('Item has already been marked for deletion.', ['id' => $item_id]);
                        return false;
                }
 
@@ -1179,9 +1165,6 @@ class Item
                        }
                }
 
-               // Delete tags that had been attached to other items
-               self::deleteTagsFromItem($item);
-
                // Delete notifications
                DBA::delete('notify', ['iid' => $item['id'], 'uid' => $item['uid']]);
 
@@ -1189,15 +1172,14 @@ class Item
                $item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
                DBA::update('item', $item_fields, ['id' => $item['id']]);
 
-               Term::insertFromTagFieldByItemId($item['id'], '');
                Term::insertFromFileFieldByItemId($item['id'], '');
                self::deleteThread($item['id'], $item['parent-uri']);
 
                if (!self::exists(["`uri` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri']])) {
-                       self::delete(['uri' => $item['uri'], 'uid' => 0, 'deleted' => false], $priority);
+                       self::markForDeletion(['uri' => $item['uri'], 'uid' => 0, 'deleted' => false], $priority);
                }
 
-               ItemDeliveryData::delete($item['id']);
+               Post\DeliveryData::delete($item['uri-id']);
 
                // We don't delete the item-activity here, since we need some of the data for ActivityPub
 
@@ -1214,14 +1196,13 @@ class Item
 
                // If it's the parent of a comment thread, kill all the kids
                if ($item['id'] == $item['parent']) {
-                       self::delete(['parent' => $item['parent'], 'deleted' => false], $priority);
+                       self::markForDeletion(['parent' => $item['parent'], 'deleted' => false], $priority);
                }
 
                // Is it our comment and/or our thread?
                if ($item['origin'] || $parent['origin']) {
-
                        // When we delete the original post we will delete all existing copies on the server as well
-                       self::delete(['uri' => $item['uri'], 'deleted' => false], $priority);
+                       self::markForDeletion(['uri' => $item['uri'], 'deleted' => false], $priority);
 
                        // send the notification upstream/downstream
                        Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
@@ -1234,48 +1215,11 @@ class Item
                        }
                }
 
-               Logger::log('Item with ID ' . $item_id . " has been deleted.", Logger::DEBUG);
+               Logger::info('Item has been marked for deletion.', ['id' => $item_id]);
 
                return true;
        }
 
-       private static function deleteTagsFromItem($item)
-       {
-               if (($item["verb"] != Activity::TAG) || ($item["object-type"] != Activity\ObjectType::TAGTERM)) {
-                       return;
-               }
-
-               $xo = XML::parseString($item["object"], false);
-               $xt = XML::parseString($item["target"], false);
-
-               if ($xt->type != Activity\ObjectType::NOTE) {
-                       return;
-               }
-
-               $i = self::selectFirst(['id', 'contact-id', 'tag'], ['uri' => $xt->id, 'uid' => $item['uid']]);
-               if (!DBA::isResult($i)) {
-                       return;
-               }
-
-               // For tags, the owner cannot remove the tag on the author's copy of the post.
-               $owner_remove = ($item["contact-id"] == $i["contact-id"]);
-               $author_copy = $item["origin"];
-
-               if (($owner_remove && $author_copy) || !$owner_remove) {
-                       return;
-               }
-
-               $tags = explode(',', $i["tag"]);
-               $newtags = [];
-               if (count($tags)) {
-                       foreach ($tags as $tag) {
-                               if (trim($tag) !== trim($xo->body)) {
-                                      $newtags[] = trim($tag);
-                               }
-                       }
-               }
-               self::update(['tag' => implode(',', $newtags)], ['id' => $i["id"]]);
-       }
 
        private static function guid($item, $notify)
        {
@@ -1407,7 +1351,7 @@ class Item
                }
 
                $item['guid'] = self::guid($item, $notify);
-               $item['uri'] = Strings::escapeTags(trim(($item['uri'] ?? '') ?: self::newURI($item['uid'], $item['guid'])));
+               $item['uri'] = substr(Strings::escapeTags(trim(($item['uri'] ?? '') ?: self::newURI($item['uid'], $item['guid']))), 0, 255);
 
                // Store URI data
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
@@ -1524,7 +1468,7 @@ class Item
                $item['edited']        = (isset($item['edited'])    ? DateTimeFormat::utc($item['edited'])    : $item['created']);
                $item['changed']       = (isset($item['changed'])   ? DateTimeFormat::utc($item['changed'])   : $item['created']);
                $item['commented']     = (isset($item['commented']) ? DateTimeFormat::utc($item['commented']) : $item['created']);
-               $item['title']         = trim($item['title'] ?? '');
+               $item['title']         = substr(trim($item['title'] ?? ''), 0, 255);
                $item['location']      = trim($item['location'] ?? '');
                $item['coord']         = trim($item['coord'] ?? '');
                $item['visible']       = (isset($item['visible']) ? intval($item['visible']) : 1);
@@ -1536,14 +1480,13 @@ class Item
                $item['object']        = trim($item['object'] ?? '');
                $item['target-type']   = trim($item['target-type'] ?? '');
                $item['target']        = trim($item['target'] ?? '');
-               $item['plink']         = trim($item['plink'] ?? '');
+               $item['plink']         = substr(trim($item['plink'] ?? ''), 0, 255);
                $item['allow_cid']     = trim($item['allow_cid'] ?? '');
                $item['allow_gid']     = trim($item['allow_gid'] ?? '');
                $item['deny_cid']      = trim($item['deny_cid'] ?? '');
                $item['deny_gid']      = trim($item['deny_gid'] ?? '');
-               $item['private']       = intval($item['private'] ?? 0);
+               $item['private']       = intval($item['private'] ?? self::PUBLIC);
                $item['body']          = trim($item['body'] ?? '');
-               $item['tag']           = trim($item['tag'] ?? '');
                $item['attach']        = trim($item['attach'] ?? '');
                $item['app']           = trim($item['app'] ?? '');
                $item['origin']        = intval($item['origin'] ?? 0);
@@ -1670,6 +1613,11 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                self::setHashtags($item);
 
+               // Store tags from the body if this hadn't been handled previously in the protocol classes
+               if (!Tag::existsForPost($item['uri-id'])) {
+                       Tag::storeFromBody($item['uri-id'], $item['body']);
+               }
+
                $item['thr-parent'] = $item['parent-uri'];
 
                $notify_type = Delivery::POST;
@@ -1691,7 +1639,7 @@ class Item
 
                        $fields = ['uri', 'parent-uri', 'id', 'deleted',
                                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
-                               'wall', 'private', 'forum_mode', 'origin'];
+                               'wall', 'private', 'forum_mode', 'origin', 'author-id'];
                        $condition = ['uri' => $item['parent-uri'], 'uid' => $item['uid']];
                        $params = ['order' => ['id' => false]];
                        $parent = self::selectFirst($fields, $condition, $params);
@@ -1737,8 +1685,8 @@ class Item
                                 * The original author commented, but as this is a comment, the permissions
                                 * weren't fixed up so it will still show the comment as private unless we fix it here.
                                 */
-                               if ((intval($parent['forum_mode']) == 1) && $parent['private']) {
-                                       $item['private'] = 0;
+                               if ((intval($parent['forum_mode']) == 1) && ($parent['private'] != self::PUBLIC)) {
+                                       $item['private'] = self::PUBLIC;
                                }
 
                                // If its a post that originated here then tag the thread as "mention"
@@ -1746,6 +1694,11 @@ class Item
                                        DBA::update('thread', ['mention' => true], ['iid' => $parent_id]);
                                        Logger::log('tagged thread ' . $parent_id . ' as mention for user ' . $item['uid'], Logger::DEBUG);
                                }
+
+                               // Update the contact relations
+                               if ($item['author-id'] != $parent['author-id']) {
+                                       DBA::update('contact-relation', ['last-interaction' => $item['created']], ['cid' => $parent['author-id'], 'relation-cid' => $item['author-id']], true);
+                               }
                        } else {
                                /*
                                 * Allow one to see reply tweets from status.net even when
@@ -1808,7 +1761,7 @@ class Item
 
                // ACL settings
                if (strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid)) {
-                       $private = 1;
+                       $private = self::PRIVATE;
                } else {
                        $private = $item['private'];
                }
@@ -1856,13 +1809,6 @@ class Item
 
                Logger::log('' . print_r($item,true), Logger::DATA);
 
-               if (array_key_exists('tag', $item)) {
-                       $tags = $item['tag'];
-                       unset($item['tag']);
-               } else {
-                       $tags = '';
-               }
-
                if (array_key_exists('file', $item)) {
                        $files = $item['file'];
                        unset($item['file']);
@@ -1889,7 +1835,7 @@ class Item
                        self::insertContent($item);
                }
 
-               $delivery_data = ItemDeliveryData::extractFields($item);
+               $delivery_data = Post\DeliveryData::extractFields($item);
 
                unset($item['postopts']);
                unset($item['inform']);
@@ -1934,9 +1880,9 @@ class Item
 
                if ($entries > 1) {
                        // There are duplicates. We delete our just created entry.
-                       Logger::log('Duplicated post occurred. uri = ' . $item['uri'] . ' uid = ' . $item['uid']);
+                       Logger::info('Delete duplicated item', ['id' => $current_post, 'uri' => $item['uri'], 'uid' => $item['uid'], 'guid' => $item['guid']]);
 
-                       // Yes, we could do a rollback here - but we are having many users with MyISAM.
+                       // Yes, we could do a rollback here - but we possibly are still having users with MyISAM.
                        DBA::delete('item', ['id' => $current_post]);
                        DBA::commit();
                        return 0;
@@ -1979,10 +1925,6 @@ class Item
 
                        if (!empty($dsprsig->signed_text) && empty($dsprsig->signature) && empty($dsprsig->signer)) {
                                DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $dsprsig->signed_text], true);
-                       } else {
-                               // The other fields are used by very old Friendica servers, so we currently store them differently
-                               DBA::insert('sign', ['iid' => $current_post, 'signed_text' => $dsprsig->signed_text,
-                                       'signature' => $dsprsig->signature, 'signer' => $dsprsig->signer]);
                        }
                }
 
@@ -1997,7 +1939,7 @@ class Item
                }
 
                if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) {
-                       ItemDeliveryData::insert($current_post, $delivery_data);
+                       Post\DeliveryData::insert($item['uri-id'], $delivery_data);
                }
 
                DBA::commit();
@@ -2006,10 +1948,6 @@ class Item
                 * Due to deadlock issues with the "term" table we are doing these steps after the commit.
                 * This is not perfect - but a workable solution until we found the reason for the problem.
                 */
-               if (!empty($tags)) {
-                       Term::insertFromTagFieldByItemId($current_post, $tags);
-               }
-
                if (!empty($files)) {
                        Term::insertFromFileFieldByItemId($current_post, $files);
                }
@@ -2217,7 +2155,7 @@ class Item
                // Only distribute public items from native networks
                $condition = ['id' => $itemid, 'uid' => 0,
                        'network' => array_merge(Protocol::FEDERATED ,['']),
-                       'visible' => true, 'deleted' => false, 'moderated' => false, 'private' => false];
+                       'visible' => true, 'deleted' => false, 'moderated' => false, 'private' => [self::PUBLIC, self::UNLISTED]];
                $item = self::selectFirst(self::ITEM_FIELDLIST, $condition);
                if (!DBA::isResult($item)) {
                        return;
@@ -2367,7 +2305,7 @@ class Item
                }
 
                // Is it a visible public post?
-               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
+               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || ($item["private"] == Item::PRIVATE)) {
                        return;
                }
 
@@ -2558,7 +2496,8 @@ class Item
                        Contact::unmarkForArchival($contact);
                }
 
-               $update = (!$arr['private'] && ((($arr['author-link'] ?? '') === ($arr['owner-link'] ?? '')) || ($arr["parent-uri"] === $arr["uri"])));
+               /// @todo On private posts we could obfuscate the date
+               $update = ($arr['private'] != self::PRIVATE);
 
                // Is it a forum? Then we don't care about the rules from above
                if (!$update && in_array($arr["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN]) && ($arr["parent-uri"] === $arr["uri"])) {
@@ -2568,11 +2507,18 @@ class Item
                }
 
                if ($update) {
-                       DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']],
-                               ['id' => $arr['contact-id']]);
+                       // The "self" contact id is used (for example in the connectors) when the contact is unknown
+                       // So we have to ensure to only update the last item when it had been our own post,
+                       // or it had been done by a "regular" contact.
+                       if (!empty($arr['wall'])) {
+                               $condition = ['id' => $arr['contact-id']];
+                       } else { 
+                               $condition = ['id' => $arr['contact-id'], 'self' => false];
+                       }
+                       DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
                }
                // Now do the same for the system wide contacts with uid=0
-               if (!$arr['private']) {
+               if ($arr['private'] != self::PRIVATE) {
                        DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']],
                                ['id' => $arr['owner-id']]);
 
@@ -2605,7 +2551,10 @@ class Item
 
                // This sorting is important when there are hashtags that are part of other hashtags
                // Otherwise there could be problems with hashtags like #test and #test2
-               rsort($tags);
+               // Because of this we are sorting from the longest to the shortest tag.
+               usort($tags, function($a, $b) {
+                       return strlen($b) <=> strlen($a);
+               });
 
                $URLSearchString = "^\[\]";
 
@@ -2613,9 +2562,6 @@ class Item
                if (DI::config()->get('system', 'local_tags')) {
                        $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
                                        "#[url=".DI::baseUrl()."/search?tag=$2]$2[/url]", $item["body"]);
-
-                       $item["tag"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
-                                       "#[url=".DI::baseUrl()."/search?tag=$2]$2[/url]", $item["tag"]);
                }
 
                // mask hashtags inside of url, bookmarks and attachments to avoid urls in urls
@@ -2647,13 +2593,6 @@ class Item
                        $newtag = '#[url=' . DI::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
 
                        $item["body"] = str_replace($tag, $newtag, $item["body"]);
-
-                       if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) {
-                               if (strlen($item["tag"])) {
-                                       $item["tag"] = ',' . $item["tag"];
-                               }
-                               $item["tag"] = $newtag . $item["tag"];
-                       }
                }
 
                // Convert back the masked hashtags
@@ -2717,9 +2656,7 @@ class Item
                if (!$mention) {
                        if (($community_page || $prvgroup) &&
                                  !$item['wall'] && !$item['origin'] && ($item['id'] == $item['parent'])) {
-                               // mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
-                               // delete it!
-                               Logger::log("no-mention top-level post to community or private group. delete.");
+                               Logger::info('Delete private group/communiy top-level item without mention', ['id' => $item_id, 'guid'=> $item['guid']]);
                                DBA::delete('item', ['id' => $item_id]);
                                return true;
                        }
@@ -2753,7 +2690,7 @@ class Item
 
                // also reset all the privacy bits to the forum default permissions
 
-               $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? 1 : 0;
+               $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? self::PRIVATE : self::PUBLIC;
 
                $psid = PermissionSet::getIdFromACL(
                        $user['uid'],
@@ -2800,7 +2737,7 @@ class Item
                        return false;
                }
 
-               if (($contact['network'] != Protocol::FEED) && $datarray['private']) {
+               if (($contact['network'] != Protocol::FEED) && ($datarray['private'] == self::PRIVATE)) {
                        Logger::log('Not public', Logger::DEBUG);
                        return false;
                }
@@ -2838,7 +2775,7 @@ class Item
                                $urlpart = parse_url($datarray2['author-link']);
                                $datarray["app"] = $urlpart["host"];
                        } else {
-                               $datarray['private'] = 0;
+                               $datarray['private'] = self::PUBLIC;
                        }
                }
 
@@ -3013,30 +2950,6 @@ class Item
                return $recipients;
        }
 
-       public static function getFeedTags($item)
-       {
-               $ret = [];
-               $matches = false;
-               $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|', $item['tag'], $matches);
-               if ($cnt) {
-                       for ($x = 0; $x < $cnt; $x ++) {
-                               if ($matches[1][$x]) {
-                                       $ret[$matches[2][$x]] = ['#', $matches[1][$x], $matches[2][$x]];
-                               }
-                       }
-               }
-               $matches = false;
-               $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|', $item['tag'], $matches);
-               if ($cnt) {
-                       for ($x = 0; $x < $cnt; $x ++) {
-                               if ($matches[1][$x]) {
-                                       $ret[] = ['@', $matches[1][$x], $matches[2][$x]];
-                               }
-                       }
-               }
-               return $ret;
-       }
-
        public static function expire($uid, $days, $network = "", $force = false)
        {
                if (!$uid || ($days < 1)) {
@@ -3102,7 +3015,7 @@ class Item
                                continue;
                        }
 
-                       self::deleteById($item['id'], PRIORITY_LOW);
+                       self::markForDeletionById($item['id'], PRIORITY_LOW);
 
                        ++$expired;
                }
@@ -3138,7 +3051,7 @@ class Item
         *            array $arr
         *            'post_id' => ID of posted item
         */
-       public static function performLike($item_id, $verb)
+       public static function performActivity($item_id, $verb)
        {
                if (!Session::isAuthenticated()) {
                        return false;
@@ -3165,6 +3078,10 @@ class Item
                        case 'unattendmaybe':
                                $activity = Activity::ATTENDMAYBE;
                                break;
+                       case 'follow':
+                       case 'unfollow':
+                               $activity = Activity::FOLLOW;
+                               break;
                        default:
                                Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
                                return false;
@@ -3243,7 +3160,7 @@ class Item
 
                // If it exists, mark it as deleted
                if (DBA::isResult($like_item)) {
-                       self::deleteById($like_item['id']);
+                       self::markForDeletionById($like_item['id']);
 
                        if (!$event_verb_flag || $like_item['verb'] == $activity) {
                                return true;
@@ -3367,7 +3284,7 @@ class Item
                        $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]];
                        if (!self::exists($condition)) {
                                DBA::delete('item', ['uri' => $itemuri, 'uid' => 0]);
-                               Logger::log("deleteThread: Deleted shadow for item ".$itemuri, Logger::DEBUG);
+                               Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri' => $itemuri]);
                        }
                }
        }
@@ -3382,7 +3299,7 @@ class Item
                 *
                 * default permissions - anonymous user
                 */
-               $sql = " AND NOT `item`.`private`";
+               $sql = sprintf(" AND `item`.`private` != %d", self::PRIVATE);
 
                // Profile owner - everything is visible
                if ($local_user && ($local_user == $owner_id)) {
@@ -3398,12 +3315,12 @@ class Item
                        $set = PermissionSet::get($owner_id, $remote_user);
 
                        if (!empty($set)) {
-                               $sql_set = " OR (`item`.`private` IN (1,2) AND `item`.`wall` AND `item`.`psid` IN (" . implode(',', $set) . "))";
+                               $sql_set = sprintf(" OR (`item`.`private` = %d AND `item`.`wall` AND `item`.`psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
                        } else {
                                $sql_set = '';
                        }
 
-                       $sql = " AND (NOT `item`.`private`" . $sql_set . ")";
+                       $sql = sprintf(" AND (`item`.`private` != %d", self::PRIVATE) . $sql_set . ")";
                }
 
                return $sql;
@@ -3505,7 +3422,7 @@ class Item
                                        continue;
                                }
 
-                               if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $app->contact['id']) && ($item['network'] == Protocol::DFRN)) {
+                               if ((local_user() == $item['uid']) && ($item['private'] == self::PRIVATE) && ($item['contact-id'] != $app->contact['id']) && ($item['network'] == Protocol::DFRN)) {
                                        $img_url = 'redir/' . $item['contact-id'] . '?url=' . urlencode($mtch[1]);
                                        $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
                                }
@@ -3540,7 +3457,7 @@ class Item
                        return $ev;
                }
 
-               $tags = Term::populateTagsFromItem($item);
+               $tags = Tag::populateFromItem($item);
 
                $item['tags'] = $tags['tags'];
                $item['hashtags'] = $tags['hashtags'];
@@ -3630,7 +3547,7 @@ class Item
                        $title .= ' ' . $mtch[2] . ' ' . DI::l10n()->t('bytes');
 
                        $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
-                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
+                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" rel="noopener noreferrer" >' . $icon . '</a>';
                }
 
                if ($as != '') {
@@ -3683,7 +3600,7 @@ class Item
                                $ret["title"] = DI::l10n()->t('link to source');
                        }
 
-               } elseif (!empty($item['plink']) && ($item['private'] != 1)) {
+               } elseif (!empty($item['plink']) && ($item['private'] != self::PRIVATE)) {
                        $ret = [
                                'href' => $item['plink'],
                                'orig' => $item['plink'],
@@ -3867,6 +3784,10 @@ class Item
                // first try to fetch the item via the GUID. This will work for all reshares that had been created on this system
                $shared_item = self::selectFirst(['title', 'body', 'attach'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
                if (!DBA::isResult($shared_item)) {
+                       if (empty($shared['link'])) {
+                               return $item;
+                       }
+
                        // Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts
                        $id = self::fetchByLink($shared['link'], $uid);
                        if (empty($id)) {