]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Avoid multiple lines in post-view, added thread view
[friendica.git] / src / Model / Item.php
index 59928fd641b70386052c2ed5248dd927ae996991..888a81831ad00a3c519705d1c7a8dd14ba054523 100644 (file)
@@ -82,7 +82,7 @@ class Item
                'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network',
                'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type',
                'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
-               'writable', 'self', 'cid', 'alias', 'pinned',
+               'writable', 'self', 'cid', 'alias',
                'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish',
                'event-summary', 'event-desc', 'event-location', 'event-type',
                'event-nofinish', 'event-adjust', 'event-ignore', 'event-id',
@@ -96,7 +96,7 @@ class Item
                        'deleted', 'extid', 'post-type', 'gravity',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'author-id', 'author-link', 'owner-link', 'contact-uid',
-                       'signed_text', 'signature', 'signer', 'network'];
+                       'signed_text', 'network'];
 
        // Field list for "item-content" table that is mixed with the item table
        const MIXED_CONTENT_FIELDLIST = ['title', 'content-warning', 'body', 'location',
@@ -148,15 +148,6 @@ class Item
                return $postfields;
        }
 
-       public static function isLegacyMode()
-       {
-               if (is_null(self::$legacy_mode)) {
-                       self::$legacy_mode = (DI::config()->get("system", "post_update_version") < 1279);
-               }
-
-               return self::$legacy_mode;
-       }
-
        /**
         * Set the pinned state of an item
         *
@@ -223,13 +214,14 @@ class Item
         * Fetch a single item row
         *
         * @param mixed $stmt statement object
-        * @return array current row
+        * @return array|false current row or false
+        * @throws \Exception
         */
        public static function fetch($stmt)
        {
                $row = DBA::fetch($stmt);
 
-               if (is_bool($row)) {
+               if (!is_array($row)) {
                        return $row;
                }
 
@@ -261,22 +253,11 @@ class Item
                // We can always comment on posts from these networks
                if (array_key_exists('writable', $row) &&
                        in_array($row['internal-network'], Protocol::FEDERATED)) {
-                       $row['writable'] = true;
+                       $row['writable'] = 1;
                }
 
                // ---------------------- Transform item content data ----------------------
 
-               // Fetch data from the item-content table whenever there is content there
-               if (self::isLegacyMode()) {
-                       $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];
-                               }
-                               unset($row['internal-item-' . $field]);
-                       }
-               }
-
                if (array_key_exists('verb', $row)) {
                        if (!is_null($row['internal-verb'])) {
                                $row['verb'] = $row['internal-verb'];
@@ -352,7 +333,7 @@ class Item
         * @param bool   $do_close
         * @return array Data array
         */
-       public static function inArray($stmt, $do_close = true) {
+       public static function toArray($stmt, $do_close = true) {
                if (is_bool($stmt)) {
                        return $stmt;
                }
@@ -389,75 +370,6 @@ class Item
                return $retval;
        }
 
-       /**
-        * Retrieve a single record from the item table for a given user and returns it in an associative array
-        *
-        * @param integer $uid User ID
-        * @param array   $selected
-        * @param array   $condition
-        * @param array   $params
-        * @return bool|array
-        * @throws \Exception
-        * @see   DBA::select
-        */
-       public static function selectFirstForUser($uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $params['uid'] = $uid;
-
-               if (empty($selected)) {
-                       $selected = Item::DISPLAY_FIELDLIST;
-               }
-
-               return self::selectFirst($selected, $condition, $params);
-       }
-
-       /**
-        * Select rows from the item table for a given user
-        *
-        * @param integer $uid       User ID
-        * @param array   $selected  Array of selected fields, empty for all
-        * @param array   $condition Array of fields for condition
-        * @param array   $params    Array of several parameters
-        *
-        * @return boolean|object
-        * @throws \Exception
-        */
-       public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [])
-       {
-               $params['uid'] = $uid;
-
-               if (empty($selected)) {
-                       $selected = Item::DISPLAY_FIELDLIST;
-               }
-
-               return self::select($selected, $condition, $params);
-       }
-
-       /**
-        * Retrieve a single record from the item table and returns it in an associative array
-        *
-        * @param array $fields
-        * @param array $condition
-        * @param array $params
-        * @return bool|array
-        * @throws \Exception
-        * @see   DBA::select
-        */
-       public static function selectFirst(array $fields = [], array $condition = [], $params = [])
-       {
-               $params['limit'] = 1;
-
-               $result = self::select($fields, $condition, $params);
-
-               if (is_bool($result)) {
-                       return $result;
-               } else {
-                       $row = self::fetch($result);
-                       DBA::close($result);
-                       return $row;
-               }
-       }
-
        /**
         * Select rows from the item table and returns them as an array
         *
@@ -542,7 +454,7 @@ class Item
                $params['uid'] = $uid;
 
                if (empty($selected)) {
-                       $selected = Item::DISPLAY_FIELDLIST;
+                       $selected = self::DISPLAY_FIELDLIST;
                }
 
                return self::selectThread($selected, $condition, $params);
@@ -564,7 +476,7 @@ class Item
                $params['uid'] = $uid;
 
                if (empty($selected)) {
-                       $selected = Item::DISPLAY_FIELDLIST;
+                       $selected = self::DISPLAY_FIELDLIST;
                }
 
                return self::selectFirstThread($selected, $condition, $params);
@@ -662,7 +574,7 @@ class Item
                        'resource-id', 'event-id', 'post-type', 'file',
                        'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
-                       'id' => 'item_id', 'network', 'icid',
+                       'id' => 'item_id', 'network', 'icid', 'event-id',
                        'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid',
                        'network' => 'internal-network', 'psid' => 'internal-psid'];
 
@@ -702,7 +614,7 @@ class Item
                        'summary' => 'event-summary','desc' => 'event-desc',
                        'location' => 'event-location', 'type' => 'event-type',
                        'nofinish' => 'event-nofinish','adjust' => 'event-adjust',
-                       'ignore' => 'event-ignore', 'id' => 'event-id'];
+                       'ignore' => 'event-ignore'];
 
                $fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text'];
 
@@ -851,9 +763,6 @@ class Item
                foreach ($fields as $table => $table_fields) {
                        foreach ($table_fields as $field => $select) {
                                if (empty($selected) || in_array($select, $selected)) {
-                                       if (self::isLegacyMode() && in_array($select, $legacy_fields)) {
-                                               $selection[] = "`item`.`".$select."` AS `internal-item-" . $select . "`";
-                                       }
                                        if (is_int($field)) {
                                                $selection[] = "`" . $table . "`.`" . $select . "`";
                                        } else {
@@ -925,7 +834,7 @@ class Item
                foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
                        if (isset($fields[$field])) {
                                $content_fields[$field] = $fields[$field];
-                               if (in_array($field, self::CONTENT_FIELDLIST) || !self::isLegacyMode()) {
+                               if (in_array($field, self::CONTENT_FIELDLIST)) {
                                        unset($fields[$field]);
                                } else {
                                        $fields[$field] = null;
@@ -986,14 +895,6 @@ class Item
                                        $item_content = DBA::selectFirst('item-content', [], ['uri-id' => $item['uri-id']]);
                                        if (DBA::isResult($item_content)) {
                                                $item_fields = ['icid' => $item_content['id']];
-                                               // Clear all fields in the item table that have a content in the item-content table
-                                               if (self::isLegacyMode()) {
-                                                       foreach ($item_content as $field => $content) {
-                                                               if (in_array($field, self::MIXED_CONTENT_FIELDLIST) && !empty($content)) {
-                                                                       $item_fields[$field] = null;
-                                                               }
-                                                       }
-                                               }
                                                DBA::update('item', $item_fields, ['id' => $item['id']]);
                                        }
                                }
@@ -1040,8 +941,8 @@ class Item
         */
        public static function markForDeletion($condition, $priority = PRIORITY_HIGH)
        {
-               $items = self::select(['id'], $condition);
-               while ($item = self::fetch($items)) {
+               $items = Post::select(['id'], $condition);
+               while ($item = Post::fetch($items)) {
                        self::markForDeletionById($item['id'], $priority);
                }
                DBA::close($items);
@@ -1060,8 +961,8 @@ class Item
                        return;
                }
 
-               $items = self::select(['id', 'uid', 'uri-id'], $condition);
-               while ($item = self::fetch($items)) {
+               $items = Post::select(['id', 'uid', 'uri-id'], $condition);
+               while ($item = Post::fetch($items)) {
                        Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]);
 
                        // "Deleting" global items just means hiding them
@@ -1093,7 +994,7 @@ class Item
                        'deleted', 'file', 'resource-id', 'event-id',
                        'verb', 'object-type', 'object', 'target', 'contact-id',
                        'icid', 'psid', 'gravity'];
-               $item = self::selectFirst($fields, ['id' => $item_id]);
+               $item = Post::selectFirst($fields, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        Logger::info('Item not found.', ['id' => $item_id]);
                        return false;
@@ -1104,7 +1005,7 @@ class Item
                        return false;
                }
 
-               $parent = self::selectFirst(['origin'], ['id' => $item['parent']]);
+               $parent = Post::selectFirst(['origin'], ['id' => $item['parent']]);
                if (!DBA::isResult($parent)) {
                        $parent = ['origin' => false];
                }
@@ -1161,7 +1062,7 @@ class Item
                Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
                self::deleteThread($item['id'], $item['parent-uri']);
 
-               if (!self::exists(["`uri` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri']])) {
+               if (!Post::exists(["`uri` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri']])) {
                        self::markForDeletion(['uri' => $item['uri'], 'uid' => 0, 'deleted' => false], $priority);
                }
 
@@ -1186,12 +1087,14 @@ class Item
                        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']));
+                       if ($priority) {
+                               Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
+                       }
                } elseif ($item['uid'] != 0) {
                        Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]);
 
                        // When we delete just our local user copy of an item, we have to set a marker to hide it
-                       $global_item = self::selectFirst(['id'], ['uri' => $item['uri'], 'uid' => 0, 'deleted' => false]);
+                       $global_item = Post::selectFirst(['id'], ['uri' => $item['uri'], 'uid' => 0, 'deleted' => false]);
                        if (DBA::isResult($global_item)) {
                                DBA::update('user-item', ['hidden' => true], ['iid' => $global_item['id'], 'uid' => $item['uid']], true);
                        }
@@ -1283,8 +1186,8 @@ class Item
                 *
                $data = BBCode::getAttachmentData($item['body']);
                if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $item['body'], $match, PREG_SET_ORDER) || isset($data["type"]))
-                       && ($posttype != Item::PT_PERSONAL_NOTE)) {
-                       $posttype = Item::PT_PAGE;
+                       && ($posttype != self::PT_PERSONAL_NOTE)) {
+                       $posttype = self::PT_PAGE;
                        $objecttype = ACTIVITY_OBJ_BOOKMARK;
                }
                 */
@@ -1322,7 +1225,7 @@ class Item
        {
                // Checking if there is already an item with the same guid
                $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
-               if (self::exists($condition)) {
+               if (Post::exists($condition)) {
                        Logger::notice('Found already existing item', [
                                'guid' => $item['guid'],
                                'uid' => $item['uid'],
@@ -1333,7 +1236,7 @@ class Item
 
                $condition = ["`uri` = ? AND `network` IN (?, ?) AND `uid` = ?",
                        $item['uri'], $item['network'], Protocol::DFRN, $item['uid']];
-               if (self::exists($condition)) {
+               if (Post::exists($condition)) {
                        Logger::notice('duplicated item with the same uri found.', $item);
                        return true;
                }
@@ -1341,7 +1244,7 @@ class Item
                // On Friendica and Diaspora the GUID is unique
                if (in_array($item['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
                        $condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
-                       if (self::exists($condition)) {
+                       if (Post::exists($condition)) {
                                Logger::notice('duplicated item with the same guid found.', $item);
                                return true;
                        }
@@ -1349,7 +1252,7 @@ class Item
                        // 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` = ?",
                                        $item['body'], $item['network'], $item['created'], $item['contact-id'], $item['uid']];
-                       if (self::exists($condition)) {
+                       if (Post::exists($condition)) {
                                Logger::notice('duplicated item with the same body found.', $item);
                                return true;
                        }
@@ -1360,7 +1263,7 @@ class Item
                 * There is a timing issue here that sometimes creates double postings.
                 * An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
                 */
-               if (($item['uid'] == 0) && self::exists(['uri' => trim($item['uri']), 'uid' => 0])) {
+               if (($item['uid'] == 0) && Post::exists(['uri' => trim($item['uri']), 'uid' => 0])) {
                        Logger::notice('Global item already stored.', ['uri' => $item['uri'], 'network' => $item['network']]);
                        return true;
                }
@@ -1382,23 +1285,15 @@ class Item
                        return false;
                }
 
-               // check for create date and expire time
-               $expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
-
-               $user = DBA::selectFirst('user', ['expire'], ['uid' => $item['uid']]);
-               if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
-                       $expire_interval = $user['expire'];
-               }
+               if (!empty($item['uid'])) {
+                       $owner = User::getOwnerDataById($item['uid'], false);
+                       if (!$owner) {
+                               Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+                               return false;
+                       }
 
-               if (($expire_interval > 0) && !empty($item['created'])) {
-                       $expire_date = time() - ($expire_interval * 86400);
-                       $created_date = strtotime($item['created']);
-                       if ($created_date < $expire_date) {
-                               Logger::notice('Item created before expiration interval.', [
-                                       'created' => date('c', $created_date),
-                                       'expired' => date('c', $expire_date),
-                                       '$item' => $item
-                               ]);
+                       if ($owner['account_expired'] || $owner['account_removed']) {
+                               Logger::notice('Item user has been deleted/expired/removed', ['uid' => $item['uid'], 'deleted' => $owner['deleted'], 'account_expired' => $owner['account_expired'], 'account_removed' => $owner['account_removed']]);
                                return false;
                        }
                }
@@ -1436,7 +1331,7 @@ class Item
 
                        $condition = ['verb' => Activity::FOLLOW, 'uid' => $item['uid'],
                                'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']];
-                       if (self::exists($condition)) {
+                       if (Post::exists($condition)) {
                                // It happens that we receive multiple follow requests by the same author - we only store one.
                                Logger::info('Follow: Found existing follow request from author', ['author-id' => $item['author-id'], 'parent-uri' => $item['parent-uri']]);
                                return false;
@@ -1446,6 +1341,38 @@ class Item
                return true;
        }
 
+       /**
+        * Check if the item array is too old
+        *
+        * @param array $item
+        * @return boolean item is too old
+        */
+       public static function isTooOld(array $item)
+       {
+               // check for create date and expire time
+               $expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
+
+               $user = DBA::selectFirst('user', ['expire'], ['uid' => $item['uid']]);
+               if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
+                       $expire_interval = $user['expire'];
+               }
+
+               if (($expire_interval > 0) && !empty($item['created'])) {
+                       $expire_date = time() - ($expire_interval * 86400);
+                       $created_date = strtotime($item['created']);
+                       if ($created_date < $expire_date) {
+                               Logger::notice('Item created before expiration interval.', [
+                                       'created' => date('c', $created_date),
+                                       'expired' => date('c', $expire_date),
+                                       '$item' => $item
+                               ]);
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
        /**
         * Return the id of the given item array if it has been stored before
         *
@@ -1458,7 +1385,7 @@ class Item
                        $condition = ["`uri` = ? AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
                                trim($item['uri']), $item['uid'],
                                Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::DFRN, Protocol::OSTATUS];
-                       $existing = self::selectFirst(['id', 'network'], $condition);
+                       $existing = Post::selectFirst(['id', 'network'], $condition);
                        if (DBA::isResult($existing)) {
                                // We only log the entries with a different user id than 0. Otherwise we would have too many false positives
                                if ($item['uid'] != 0) {
@@ -1491,7 +1418,7 @@ class Item
                        'wall', 'private', 'forum_mode', 'origin', 'author-id'];
                $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid']];
                $params = ['order' => ['id' => false]];
-               $parent = self::selectFirst($fields, $condition, $params);
+               $parent = Post::selectFirst($fields, $condition, $params);
 
                if (!DBA::isResult($parent)) {
                        Logger::notice('item parent was not found - ignoring item', ['thr-parent' => $item['thr-parent'], 'uid' => $item['uid']]);
@@ -1506,7 +1433,7 @@ class Item
                        'parent-uri' => $parent['parent-uri'],
                        'uid' => $parent['uid']];
                $params = ['order' => ['id' => false]];
-               $toplevel_parent = self::selectFirst($fields, $condition, $params);
+               $toplevel_parent = Post::selectFirst($fields, $condition, $params);
                if (!DBA::isResult($toplevel_parent)) {
                        Logger::notice('item top level parent was not found - ignoring item', ['parent-uri' => $parent['parent-uri'], 'uid' => $parent['uid']]);
                        return [];
@@ -1554,8 +1481,9 @@ class Item
                        $item['origin'] = 1;
                        $item['network'] = Protocol::DFRN;
                        $item['protocol'] = Conversation::PARCEL_DIRECT;
+                       $item['direction'] = Conversation::PUSH;
 
-                       if (is_int($notify)) {
+                       if (in_array($notify, PRIORITIES)) {
                                $priority = $notify;
                        }
                } else {
@@ -1670,6 +1598,12 @@ class Item
                // 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]) &&
+                       self::isTooOld($item)) {
+                       Logger::info('Item is too old', ['item' => $item]);
+                       return 0;
+               }
+
                if (!self::isValid($item)) {
                        return 0;
                }
@@ -1682,7 +1616,7 @@ class Item
 
                        // If the thread originated from this node, we check the permission against the thread starter
                        $condition = ['uri' => $toplevel_parent['uri'], 'wall' => true];
-                       $localTopLevelParent = self::selectFirst(['uid'], $condition);
+                       $localTopLevelParent = Post::selectFirst(['uid'], $condition);
                        if (!empty($localTopLevelParent['uid']) && !self::isAllowedByUser($item, $localTopLevelParent['uid'])) {
                                return 0;
                        }
@@ -1759,7 +1693,7 @@ class Item
                        // Set the global flag on all items if this was a global item entry
                        DBA::update('item', ['global' => true], ['uri' => $item["uri"]]);
                } else {
-                       $item["global"] = self::exists(['uid' => 0, 'uri' => $item["uri"]]);
+                       $item["global"] = Post::exists(['uid' => 0, 'uri' => $item["uri"]]);
                }
 
                // ACL settings
@@ -1952,7 +1886,7 @@ class Item
                }
 
                if (!$dontcache) {
-                       $posted_item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $current_post]);
+                       $posted_item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $current_post]);
                        if (DBA::isResult($posted_item)) {
                                if ($notify) {
                                        Hook::callAll('post_local_end', $posted_item);
@@ -1985,7 +1919,7 @@ class Item
                $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin']));
 
                if ($transmit) {
-                       $transmit_item = Item::selectFirst(['verb', 'origin'], ['id' => $item['id']]);
+                       $transmit_item = Post::selectFirst(['verb', 'origin'], ['id' => $item['id']]);
                        // Don't relay participation messages
                        if (($transmit_item['verb'] == Activity::FOLLOW) && 
                                (!$transmit_item['origin'] || ($item['author-id'] != Contact::getPublicIdByUserId($uid)))) {
@@ -2013,7 +1947,7 @@ class Item
         */
        private static function setOwnerforResharedItem(array $item)
        {
-               $parent = self::selectFirst(['id', 'causer-id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-type'],
+               $parent = Post::selectFirst(['id', 'causer-id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-type'],
                        ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                if (!DBA::isResult($parent)) {
                        Logger::error('Parent not found', ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
@@ -2147,7 +2081,7 @@ class Item
        public static function distribute($itemid, $signed_text = '')
        {
                $condition = ["`id` IN (SELECT `parent` FROM `item` WHERE `id` = ?)", $itemid];
-               $parent = self::selectFirst(['owner-id'], $condition);
+               $parent = Post::selectFirst(['owner-id'], $condition);
                if (!DBA::isResult($parent)) {
                        return;
                }
@@ -2156,7 +2090,7 @@ class Item
                $condition = ['id' => $itemid, 'uid' => 0,
                        'network' => array_merge(Protocol::FEDERATED ,['']),
                        'visible' => true, 'deleted' => false, 'moderated' => false, 'private' => [self::PUBLIC, self::UNLISTED]];
-               $item = self::selectFirst(self::ITEM_FIELDLIST, $condition);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, $condition);
                if (!DBA::isResult($item)) {
                        return;
                }
@@ -2209,13 +2143,14 @@ class Item
                $origin_uid = 0;
 
                if ($item['uri'] != $item['parent-uri']) {
-                       $parents = self::select(['uid', 'origin'], ["`uri` = ? AND `uid` != 0", $item['parent-uri']]);
-                       while ($parent = self::fetch($parents)) {
+                       $parents = Post::select(['uid', 'origin'], ["`uri` = ? AND `uid` != 0", $item['parent-uri']]);
+                       while ($parent = Post::fetch($parents)) {
                                $users[$parent['uid']] = $parent['uid'];
                                if ($parent['origin'] && !$origin) {
                                        $origin_uid = $parent['uid'];
                                }
                        }
+                       DBA::close($parents);
                }
 
                foreach ($users as $uid) {
@@ -2236,7 +2171,7 @@ class Item
         */
        public static function storeForUserByUriId(int $uri_id, int $uid, array $fields = [])
        {
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]);
                if (!DBA::isResult($item)) {
                        return 0;
                }
@@ -2265,7 +2200,7 @@ class Item
         */
        private static function storeForUser(array $item, int $uid)
        {
-               if (self::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
+               if (Post::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
                        Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]);
                        return 0;
                }
@@ -2335,7 +2270,7 @@ class Item
        {
                $fields = ['uid', 'private', 'moderated', 'visible', 'deleted', 'network', 'uri'];
                $condition = ['id' => $itemid, 'parent' => [0, $itemid]];
-               $item = self::selectFirst($fields, $condition);
+               $item = Post::selectFirst($fields, $condition);
 
                if (!DBA::isResult($item)) {
                        return;
@@ -2347,7 +2282,7 @@ class Item
                }
 
                // Is it a visible public post?
-               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || ($item["private"] == Item::PRIVATE)) {
+               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || ($item["private"] == self::PRIVATE)) {
                        return;
                }
 
@@ -2356,11 +2291,11 @@ class Item
                        return;
                }
 
-               if (self::exists(['uri' => $item['uri'], 'uid' => 0])) {
+               if (Post::exists(['uri' => $item['uri'], 'uid' => 0])) {
                        return;
                }
 
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
 
                if (DBA::isResult($item)) {
                        // Preparing public shadow (removing user specific data)
@@ -2396,7 +2331,7 @@ class Item
         */
        private static function addShadowPost($itemid)
        {
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $itemid]);
                if (!DBA::isResult($item)) {
                        return;
                }
@@ -2413,12 +2348,12 @@ class Item
                }
 
                // Is there a shadow parent?
-               if (!self::exists(['uri' => $item['parent-uri'], 'uid' => 0])) {
+               if (!Post::exists(['uri' => $item['parent-uri'], 'uid' => 0])) {
                        return;
                }
 
                // Is there already a shadow entry?
-               if (self::exists(['uri' => $item['uri'], 'uid' => 0])) {
+               if (Post::exists(['uri' => $item['uri'], 'uid' => 0])) {
                        return;
                }
 
@@ -2445,7 +2380,7 @@ class Item
 
                // If this was a comment to a Diaspora post we don't get our comment back.
                // This means that we have to distribute the comment by ourselves.
-               if ($origin && self::exists(['id' => $parent, 'network' => Protocol::DIASPORA])) {
+               if ($origin && Post::exists(['id' => $parent, 'network' => Protocol::DIASPORA])) {
                        self::distribute($public_shadow);
                }
        }
@@ -2693,7 +2628,7 @@ class Item
                $community_page = (($user['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
                $prvgroup = (($user['page-flags'] == User::PAGE_FLAGS_PRVGROUP) ? true : false);
 
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        return false;
                }
@@ -2781,7 +2716,7 @@ class Item
 
                Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
 
-               Item::performActivity($item_id, 'announce', $uid);
+               self::performActivity($item_id, 'announce', $uid);
 
                return false;
        }
@@ -2808,7 +2743,7 @@ class Item
 
                Logger::info('Automatically reshare item', ['uid' => $item['uid'], 'id' => $item['id'], 'guid' => $item['guid'], 'uri-id' => $item['uri-id']]);
 
-               Item::performActivity($item['id'], 'announce', $item['uid']);
+               self::performActivity($item['id'], 'announce', $item['uid']);
        }
 
        public static function isRemoteSelf($contact, &$datarray)
@@ -2896,10 +2831,10 @@ class Item
                $datarray['api_source'] = true;
 
                // We have to tell the hooks who we are - this really should be improved
-               $_SESSION["authenticated"] = true;
-               $_SESSION["uid"] = $contact['uid'];
+               $_SESSION['authenticated'] = true;
+               $_SESSION['uid'] = $contact['uid'];
 
-               return $result;
+               return (bool)$result;
        }
 
        /**
@@ -3081,7 +3016,7 @@ class Item
                $condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY";
                $condition[] = $days;
 
-               $items = self::select(['file', 'resource-id', 'starred', 'type', 'id', 'post-type'], $condition);
+               $items = Post::select(['file', 'resource-id', 'starred', 'type', 'id', 'post-type'], $condition);
 
                if (!DBA::isResult($items)) {
                        return;
@@ -3100,7 +3035,9 @@ class Item
 
                $expired = 0;
 
-               while ($item = Item::fetch($items)) {
+               $priority = DI::config()->get('system', 'expire-notify-priority');
+
+               while ($item = Post::fetch($items)) {
                        // don't expire filed items
 
                        if (strpos($item['file'], '[') !== false) {
@@ -3113,13 +3050,13 @@ class Item
                                continue;
                        } elseif (!$expire_starred && intval($item['starred'])) {
                                continue;
-                       } elseif (!$expire_notes && (($item['type'] == 'note') || ($item['post-type'] == Item::PT_PERSONAL_NOTE))) {
+                       } elseif (!$expire_notes && (($item['type'] == 'note') || ($item['post-type'] == self::PT_PERSONAL_NOTE))) {
                                continue;
-                       } elseif (!$expire_items && ($item['type'] != 'note') && ($item['post-type'] != Item::PT_PERSONAL_NOTE)) {
+                       } elseif (!$expire_items && ($item['type'] != 'note') && ($item['post-type'] != self::PT_PERSONAL_NOTE)) {
                                continue;
                        }
 
-                       self::markForDeletionById($item['id'], PRIORITY_LOW);
+                       self::markForDeletionById($item['id'], $priority);
 
                        ++$expired;
                }
@@ -3164,7 +3101,7 @@ class Item
 
                Logger::notice('Start create activity', ['verb' => $verb, 'item' => $item_id, 'user' => $uid]);
 
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
+               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        Logger::log('like: unknown item ' . $item_id);
                        return false;
@@ -3176,10 +3113,10 @@ class Item
                        return false;
                }
 
-               if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
+               if (!Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
                        $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid);
                        if (($item['parent-uri-id'] == $item['uri-id']) && !empty($stored)) {
-                               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]);
+                               $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]);
                                if (!DBA::isResult($item)) {
                                        Logger::info('Could not fetch just created item - should not happen', ['stored' => $stored, 'uid' => $uid, 'item-uri' => $item_uri]);
                                        return false;
@@ -3257,7 +3194,7 @@ class Item
 
                $condition = ['vid' => $vids, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY,
                        'author-id' => $author_id, 'uid' => $item['uid'], 'thr-parent' => $item_uri];
-               $like_item = self::selectFirst(['id', 'guid', 'verb'], $condition);
+               $like_item = Post::selectFirst(['id', 'guid', 'verb'], $condition);
 
                if (DBA::isResult($like_item)) {
                        /**
@@ -3307,6 +3244,8 @@ class Item
                        'wall'          => $item['wall'],
                        'origin'        => 1,
                        'network'       => Protocol::DFRN,
+                       'protocol'      => Conversation::PARCEL_DIRECT,
+                       'direction'     => Conversation::PUSH,
                        'gravity'       => GRAVITY_ACTIVITY,
                        'parent'        => $item['id'],
                        'thr-parent'    => $item['uri'],
@@ -3348,7 +3287,7 @@ class Item
                        'moderated', 'visible', 'starred', 'contact-id', 'post-type', 'uri-id',
                        'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id'];
                $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
-               $item = self::selectFirst($fields, $condition);
+               $item = Post::selectFirst($fields, $condition);
 
                if (!DBA::isResult($item)) {
                        return;
@@ -3369,7 +3308,7 @@ class Item
                        'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id',
                        'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id'];
 
-               $item = self::selectFirst($fields, ['id' => $itemid, 'gravity' => GRAVITY_PARENT]);
+               $item = Post::selectFirst($fields, ['id' => $itemid, 'gravity' => GRAVITY_PARENT]);
                if (!DBA::isResult($item)) {
                        return;
                }
@@ -3405,7 +3344,7 @@ class Item
 
                if ($itemuri != "") {
                        $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]];
-                       if (!self::exists($condition)) {
+                       if (!Post::exists($condition)) {
                                DBA::delete('item', ['uri' => $itemuri, 'uid' => 0]);
                                Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri' => $itemuri]);
                        }
@@ -3435,7 +3374,7 @@ class Item
                        if (!empty($set)) {
                                $condition = ["(`private` != ? OR (`private` = ? AND `wall`
                                        AND `psid` IN (" . implode(', ', array_fill(0, count($set), '?')) . ")))",
-                                       Item::PRIVATE, Item::PRIVATE];
+                                       self::PRIVATE, self::PRIVATE];
                                $condition = array_merge($condition, $set);
                        }
                }
@@ -3453,7 +3392,7 @@ class Item
                 *
                 * default permissions - anonymous user
                 */
-               $sql = sprintf(" AND `item`.`private` != %d", self::PRIVATE);
+               $sql = sprintf(" AND `private` != %d", self::PRIVATE);
 
                // Profile owner - everything is visible
                if ($local_user && ($local_user == $owner_id)) {
@@ -3469,12 +3408,12 @@ class Item
                        $set = PermissionSet::get($owner_id, $remote_user);
 
                        if (!empty($set)) {
-                               $sql_set = sprintf(" OR (`item`.`private` = %d AND `item`.`wall` AND `item`.`psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
+                               $sql_set = sprintf(" OR (`private` = %d AND `wall` AND `psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
                        } else {
                                $sql_set = '';
                        }
 
-                       $sql = sprintf(" AND (`item`.`private` != %d", self::PRIVATE) . $sql_set . ")";
+                       $sql = sprintf(" AND (`private` != %d", self::PRIVATE) . $sql_set . ")";
                }
 
                return $sql;
@@ -3514,20 +3453,21 @@ class Item
         */
        public static function putInCache(&$item, $update = false)
        {
-               $body = $item["body"];
+               // Save original body to prevent addons to modify it
+               $body = $item['body'];
 
                $rendered_hash = $item['rendered-hash'] ?? '';
                $rendered_html = $item['rendered-html'] ?? '';
 
                if ($rendered_hash == ''
-                       || $rendered_html == ""
-                       || $rendered_hash != hash("md5", $item["body"])
-                       || DI::config()->get("system", "ignore_cache")
+                       || $rendered_html == ''
+                       || $rendered_hash != hash('md5', BBCode::VERSION . '::' . $body)
+                       || DI::config()->get('system', 'ignore_cache')
                ) {
                        self::addRedirToImageTags($item);
 
-                       $item["rendered-html"] = BBCode::convert($item["body"]);
-                       $item["rendered-hash"] = hash("md5", $item["body"]);
+                       $item['rendered-html'] = BBCode::convert($item['body']);
+                       $item['rendered-hash'] = hash('md5', BBCode::VERSION . '::' . $body);
 
                        $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
                        Hook::callAll('put_item_in_cache', $hook_data);
@@ -3536,27 +3476,27 @@ class Item
                        unset($hook_data);
 
                        // Force an update if the generated values differ from the existing ones
-                       if ($rendered_hash != $item["rendered-hash"]) {
+                       if ($rendered_hash != $item['rendered-hash']) {
                                $update = true;
                        }
 
                        // Only compare the HTML when we forcefully ignore the cache
-                       if (DI::config()->get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
+                       if (DI::config()->get('system', 'ignore_cache') && ($rendered_html != $item['rendered-html'])) {
                                $update = true;
                        }
 
-                       if ($update && !empty($item["id"])) {
+                       if ($update && !empty($item['id'])) {
                                self::update(
                                        [
-                                               'rendered-html' => $item["rendered-html"],
-                                               'rendered-hash' => $item["rendered-hash"]
+                                               'rendered-html' => $item['rendered-html'],
+                                               'rendered-hash' => $item['rendered-hash']
                                        ],
-                                       ['id' => $item["id"]]
+                                       ['id' => $item['id']]
                                );
                        }
                }
 
-               $item["body"] = $body;
+               $item['body'] = $body;
        }
 
        /**
@@ -3933,7 +3873,7 @@ class Item
                $uid = $item['uid'] ?? 0;
 
                // 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'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
+               $shared_item = Post::selectFirst(['title', 'body'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
                if (!DBA::isResult($shared_item)) {
                        if (empty($shared['link'])) {
                                return $item;
@@ -3946,7 +3886,7 @@ class Item
                                return $item;
                        }
 
-                       $shared_item = self::selectFirst(['title', 'body'], ['id' => $id]);
+                       $shared_item = Post::selectFirst(['title', 'body'], ['id' => $id]);
                        if (!DBA::isResult($shared_item)) {
                                return $item;
                        }