]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Support for message delivering via uri-id
[friendica.git] / src / Model / Item.php
index cf844b10324ba408843049a2501f53fbb5056eaa..28e0e25f4cca7e3b3e929d60cbdb44a3d59119ac 100644 (file)
@@ -31,7 +31,6 @@ use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Model\Tag;
 use Friendica\Core\Worker;
-use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
@@ -78,13 +77,13 @@ class Item
                'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
                'wall', 'private', 'starred', 'origin', 'title', 'body', 'language',
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
-               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
+               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                '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',
-               'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish',
+               'event-created', 'event-edited', 'event-start', 'event-finish',
                'event-summary', 'event-desc', 'event-location', 'event-type',
                'event-nofinish', 'event-adjust', 'event-ignore', 'event-id',
                'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed'
@@ -92,35 +91,47 @@ class Item
 
        // Field list that is used to deliver items via the protocols
        const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
-                       'parent-guid', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
-                       'private', 'title', 'body', 'location', 'coord', 'app',
-                       'deleted', 'extid', 'post-type', 'gravity',
+                       'parent-guid', 'received', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
+                       'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
+                       'inform', 'deleted', 'extid', 'post-type', 'gravity',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
-                       'author-id', 'author-link', 'owner-link', 'contact-uid',
-                       'signed_text', 'network'];
-
-       // Field list for "post-content" table that is mixed with the item table
-       const MIXED_CONTENT_FIELDLIST = ['title', 'content-warning', 'body', 'location',
-                       'coord', 'app', 'rendered-hash', 'rendered-html', 'verb',
-                       'object-type', 'object', 'target-type', 'target', 'plink'];
-
-       // Field list for "post-content" table that is not present in the "item" table
-       const CONTENT_FIELDLIST = ['language', 'raw-body'];
+                       'author-id', 'author-link', 'owner-id', 'owner-link', 'contact-uid',
+                       'signed_text', 'network', 'wall', 'contact-id', 'plink', 'forum_mode', 'origin',
+                       'thr-parent-id', 'parent-uri-id', 'postopts', 'pubmail', 
+                       'event-created', 'event-edited', 'event-start', 'event-finish',
+                       'event-summary', 'event-desc', 'event-location', 'event-type',
+                       'event-nofinish', 'event-adjust', 'event-ignore', 'event-id'];
 
        // All fields in the item table
        const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
                        'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid',
-                       'contact-id', 'type', 'wall', 'gravity', 'extid', 'psid',
+                       'contact-id', 'wall', 'gravity', 'extid', 'psid',
                        'created', 'edited', 'commented', 'received', 'changed', 'verb',
                        'postopts', 'plink', 'resource-id', 'event-id', 'inform',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
-                       'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
+                       'private', 'pubmail', 'visible', 'starred',
                        'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network',
                        'title', 'content-warning', 'body', 'location', 'coord', 'app',
                        'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
                        'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                        'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'];
 
+       // Item fields that still are in use
+       const USED_FIELDLIST = ['id', 'parent', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id',
+               'thr-parent', 'thr-parent-id', 'created', 'edited', 'commented', 'received', 'changed',
+               'gravity', 'network', 'owner-id', 'author-id', 'causer-id', 'vid', 'extid', 'post-type',
+               'global', 'private', 'visible', 'deleted', 'uid', 'contact-id',
+               'wall', 'origin', 'pubmail', 'starred', 'unseen', 'mention', 'forum_mode', 'psid',
+               'event-id'];
+
+       // Legacy item fields that aren't stored any more in the item table
+       const LEGACY_FIELDLIST = ['uri-hash', 'iaid', 'icid', 'attach',
+               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 
+               'resource-id', 'inform', 'file', 'location', 'coord', 'tag', 'plink', 
+               'title', 'content-warning', 'body', 'app', 'verb', 'object-type', 'object', 
+               'target-type', 'target', 'author-name', 'author-link', 'author-avatar', 
+               'owner-name', 'owner-link', 'owner-avatar', 'rendered-hash', 'rendered-html'];
+
        // List of all verbs that don't need additional content data.
        // Never reorder or remove entries from this list. Just add new ones at the end, if needed.
        const ACTIVITIES = [
@@ -133,49 +144,6 @@ class Item
        const PRIVATE = 1;
        const UNLISTED = 2;
 
-       const TABLES = ['item', 'user-item', 'post-content', 'post-delivery-data', 'diaspora-interaction'];
-
-       private static function getItemFields()
-       {
-               $definition = DBStructure::definition('', false);
-
-               $postfields = [];
-               foreach (self::TABLES as $table) {
-                       $postfields[$table] = array_keys($definition[$table]['fields']);
-               }
-
-               return $postfields;
-       }
-
-       /**
-        * Set the pinned state of an item
-        *
-        * @param integer $iid    Item ID
-        * @param integer $uid    User ID
-        * @param boolean $pinned Pinned state
-        */
-       public static function setPinned(int $iid, int $uid, bool $pinned)
-       {
-               DBA::update('user-item', ['pinned' => $pinned], ['iid' => $iid, 'uid' => $uid], true);
-       }
-
-       /**
-        * Get the pinned state
-        *
-        * @param integer $iid Item ID
-        * @param integer $uid User ID
-        *
-        * @return boolean pinned state
-        */
-       public static function getPinned(int $iid, int $uid)
-       {
-               $useritem = DBA::selectFirst('user-item', ['pinned'], ['iid' => $iid, 'uid' => $uid]);
-               if (!DBA::isResult($useritem)) {
-                       return false;
-               }
-               return (bool)$useritem['pinned'];
-       }
-
        /**
         * Update existing item entries
         *
@@ -244,7 +212,8 @@ class Item
                DBA::close($items);
 
                foreach ($notify_items as $notify_item) {
-                       Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $notify_item);
+                       $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $notify_item]);
+                       Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$post['uri-id'], (int)$post['uid']);
                }
 
                return $rows;
@@ -283,14 +252,12 @@ class Item
                while ($item = Post::fetch($items)) {
                        if (in_array($item['uid'], [$uid, 0])) {
                                Post\User::update($item['uri-id'], $uid, ['hidden' => true], true);
+                               Post\ThreadUser::update($item['uri-id'], $uid, ['hidden' => true], true);
                        }
 
-                       // "Deleting" global items just means hiding them
-                       if ($item['uid'] == 0) {
-                               DBA::update('user-item', ['hidden' => true], ['iid' => $item['id'], 'uid' => $uid], true);
-                       } elseif ($item['uid'] == $uid) {
+                       if ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
-                       } else {
+                       } elseif ($item['uid'] != 0) {
                                Logger::log('Wrong ownership. Not deleting item ' . $item['id']);
                        }
                }
@@ -358,10 +325,9 @@ class Item
 
                // Set the item to "deleted"
                $item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
-               DBA::update('item', $item_fields, ['id' => $item['id']]);
+               Post::update($item_fields, ['id' => $item['id']]);
 
                Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
-               self::deleteThread($item['id'], $item['parent-uri-id']);
 
                if (!Post::exists(["`uri-id` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri-id']])) {
                        self::markForDeletion(['uri-id' => $item['uri-id'], 'uid' => 0, 'deleted' => false], $priority);
@@ -389,16 +355,11 @@ class Item
 
                        // send the notification upstream/downstream
                        if ($priority) {
-                               Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
+                               Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, (int)$item['uri-id'], (int)$item['uid']);
                        }
                } 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 = Post::selectFirst(['id'], ['uri-id' => $item['uri-id'], 'uid' => 0, 'deleted' => false]);
-                       if (DBA::isResult($global_item)) {
-                               DBA::update('user-item', ['hidden' => true], ['iid' => $global_item['id'], 'uid' => $item['uid']], true);
-                       }
+                       Post\ThreadUser::update($item['uri-id'], $item['uid'], ['hidden' => true]);
                }
 
                Logger::info('Item has been marked for deletion.', ['id' => $item_id]);
@@ -755,8 +716,6 @@ class Item
 
        public static function insert($item, $notify = false, $dontcache = false)
        {
-               $structure = self::getItemFields();
-
                $orig_item = $item;
 
                $priority = PRIORITY_HIGH;
@@ -946,32 +905,17 @@ class Item
 
                        // If its a post that originated here then tag the thread as "mention"
                        if ($item['origin'] && $item['uid']) {
-                               DBA::update('thread', ['mention' => true], ['iid' => $parent_id]);
-                               Logger::info('tagged thread as mention', ['parent' => $parent_id, 'uid' => $item['uid']]);
+                               DBA::update('post-thread-user', ['mention' => true], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
+                               Logger::info('tagged thread as mention', ['parent' => $parent_id, 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
                        }
 
                        // Update the contact relations
                        Contact\Relation::store($toplevel_parent['author-id'], $item['author-id'], $item['created']);
-
-                       unset($item['parent_origin']);
                } else {
                        $parent_id = 0;
                        $parent_origin = $item['origin'];
                }
 
-               // We don't store the causer link, only the id
-               unset($item['causer-link']);
-
-               // We don't store these fields anymore in the item table
-               unset($item['author-link']);
-               unset($item['author-name']);
-               unset($item['author-avatar']);
-               unset($item['author-network']);
-
-               unset($item['owner-link']);
-               unset($item['owner-name']);
-               unset($item['owner-avatar']);
-
                $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
                $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
 
@@ -980,7 +924,7 @@ class Item
                        $item["global"] = true;
 
                        // Set the global flag on all items if this was a global item entry
-                       DBA::update('item', ['global' => true], ['uri-id' => $item['uri-id']]);
+                       Post::update(['global' => true], ['uri-id' => $item['uri-id']]);
                } else {
                        $item['global'] = Post::exists(['uid' => 0, 'uri-id' => $item['uri-id']]);
                }
@@ -994,14 +938,10 @@ class Item
                        $item['edit'] = false;
                        $item['parent'] = $parent_id;
                        Hook::callAll('post_local', $item);
-                       unset($item['edit']);
                } else {
                        Hook::callAll('post_remote', $item);
                }
 
-               // Set after the insert because top-level posts are self-referencing
-               unset($item['parent']);
-
                if (!empty($item['cancel'])) {
                        Logger::log('post cancelled by addon.');
                        return 0;
@@ -1020,14 +960,9 @@ class Item
                        $item['deny_gid']
                );
 
-               unset($item['allow_cid']);
-               unset($item['allow_gid']);
-               unset($item['deny_cid']);
-               unset($item['deny_gid']);
-
-               // This array field is used to trigger some automatic reactions
-               // It is mainly used in the "post_local" hook.
-               unset($item['api_source']);
+               if (!empty($item['extid'])) {
+                       $item['external-id'] = ItemURI::getIdByURI($item['extid']);
+               }
 
                if ($item['verb'] == Activity::ANNOUNCE) {
                        self::setOwnerforResharedItem($item);
@@ -1040,10 +975,6 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
 
-               if (!empty($item['attach'])) {
-                       Post\Media::insertFromAttachment($item['uri-id'], $item['attach']);
-               }
-
                // Fill the cache field
                self::putInCache($item);
 
@@ -1053,42 +984,37 @@ class Item
                        $notify_type = Delivery::POST;
                }
 
-               if (!in_array($item['verb'], self::ACTIVITIES) && !Post\Content::insert($item['uri-id'], $item)) {
-                       // This shouldn't happen
-                       Logger::warning('No post-content stored, quitting', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'causer-id' => ($item['causer-id'] ?? 0), 'post-type' => $item['post-type'], 'network' => $item['network']]);
-                       return 0;
+               // Filling item related side tables
+               if (!empty($item['attach'])) {
+                       Post\Media::insertFromAttachment($item['uri-id'], $item['attach']);
                }
 
-               $body = $item['body'];
-               $verb = $item['verb'];
-
-               // We just remove everything that is content
-               foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
-                       unset($item[$field]);
+               if (empty($item['event-id'])) {
+                       unset($item['event-id']);
                }
 
-               unset($item['activity']);
+               Post::insert($item['uri-id'], $item);
 
-               // Filling item related side tables
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       Post\Thread::insert($item['uri-id'], $item);
+               }
+
+               if (!in_array($item['verb'], self::ACTIVITIES)) {
+                       Post\Content::insert($item['uri-id'], $item);
+               }
 
                // Diaspora signature
                if (!empty($item['diaspora_signed_text'])) {
                        DBA::replace('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $item['diaspora_signed_text']]);
                }
 
-               unset($item['diaspora_signed_text']);
-
                // Attached file links
                if (!empty($item['file'])) {
                        Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $item['file']);
                }
 
-               unset($item['file']);
-
                // Delivery relevant data
                $delivery_data = Post\DeliveryData::extractFields($item);
-               unset($item['postopts']);
-               unset($item['inform']);
 
                if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) {
                        Post\DeliveryData::insert($item['uri-id'], $delivery_data);
@@ -1096,36 +1022,39 @@ class 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'], $body);
+                       Tag::storeFromBody($item['uri-id'], $item['body']);
                }
 
-               $id = Post\User::insert($item['uri-id'], $item['uid'], $item);
-               if ($id) {
-                       // Remove all fields that aren't part of the item table
-                       foreach ($item as $field => $value) {
-                               if (!in_array($field, $structure['item'])) {
-                                       unset($item[$field]);
-                               }
-                       }
+               $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid']];
+               if (Post::exists($condition)) {
+                       Logger::notice('Item is already inserted - aborting', $condition);
+                       return 0;
+               }
 
-                       // We syncronize the id value of the of the post-user table with the item table
-                       $item['id'] = $id;
+               $id = Post\User::insert($item['uri-id'], $item['uid'], $item);
+               if (!$id) {
+                       Logger::notice('Post-User is already inserted - aborting', ['uid' => $item['uid'], 'uri-id' => $item['uri-id']]);
+                       return 0;
+               }
 
-                       $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
-                       if (Post::exists($condition)) {
-                               Logger::notice('Item is already inserted - aborting', $condition);
-                               return 0;
-                       }
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       $item['post-user-id'] = $id;
+                       Post\ThreadUser::insert($item['uri-id'], $item['uid'], $item);
+               }
 
-                       $result = DBA::insert('item', $item);
+               // Remove all fields that aren't part of the item table
+               $table_fields = DBStructure::getFieldsForTable('item', $item);
 
-                       // When the item was successfully stored we fetch the ID of the item.
-                       $current_post = DBA::lastInsertId();
-               } else {
-                       Logger::notice('Post-User is already inserted - aborting', ['uid' => $item['uid'], 'uri-id' => $item['uri-id']]);
-                       return 0;
+               // We remove all legacy fields that now are stored in other tables
+               foreach (self::LEGACY_FIELDLIST as $field) {
+                       unset($table_fields[$field]);
                }
 
+               $result = DBA::insert('item', $table_fields);
+
+               // When the item was successfully stored we fetch the ID of the item.
+               $current_post = DBA::lastInsertId();
+
                if (empty($current_post) || !DBA::isResult($result)) {
                        // On failure store the data into a spool file so that the "SpoolPost" worker can try again later.
                        Logger::warning('Could not store item. it will be spooled', ['result' => $result, 'id' => $current_post]);
@@ -1151,19 +1080,13 @@ class Item
                        $update_commented = in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]);
                } else {
                        // Update when it isn't a follow or tag verb
-                       $update_commented = !in_array($verb, [Activity::FOLLOW, Activity::TAG]);
+                       $update_commented = !in_array($item['verb'], [Activity::FOLLOW, Activity::TAG]);
                }
 
                if ($update_commented) {
-                       DBA::update('item', ['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
+                       Post::update(['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
                } else {
-                       DBA::update('item', ['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
-               }
-
-               if ($item['gravity'] === GRAVITY_PARENT) {
-                       self::addThread($current_post);
-               } else {
-                       self::updateThread($parent_id);
+                       Post::update(['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
                }
 
                // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
@@ -1196,9 +1119,10 @@ class Item
 
                self::updateContact($item);
 
-               UserItem::setNotification($current_post);
+               Post\UserNotification::setNotification($item['uri-id'], $item['uid']);
 
-               check_user_notification($current_post);
+               check_user_notification($item['uri-id'], $item['uid']);
+               //check_user_notification($current_post);
 
                // Distribute items to users who subscribed to their tags
                self::distributeByTags($item);
@@ -1219,7 +1143,7 @@ class Item
                }
 
                if ($transmit) {
-                       Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, $current_post);
+                       Worker::add(['priority' => $priority, 'dont_fork' => true], 'Notifier', $notify_type, (int)$item['uri-id'], (int)$item['uid']);
                }
 
                return $current_post;
@@ -1317,7 +1241,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' => [self::PUBLIC, self::UNLISTED]];
+                       'visible' => true, 'deleted' => false, 'private' => [self::PUBLIC, self::UNLISTED]];
                $item = Post::selectFirst(self::ITEM_FIELDLIST, $condition);
                if (!DBA::isResult($item)) {
                        return;
@@ -1496,7 +1420,7 @@ class Item
         */
        private static function addShadow($itemid)
        {
-               $fields = ['uid', 'private', 'moderated', 'visible', 'deleted', 'network', 'uri-id'];
+               $fields = ['uid', 'private', 'visible', 'deleted', 'network', 'uri-id'];
                $condition = ['id' => $itemid, 'parent' => [0, $itemid]];
                $item = Post::selectFirst($fields, $condition);
 
@@ -1510,7 +1434,7 @@ class Item
                }
 
                // Is it a visible public post?
-               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || ($item["private"] == self::PRIVATE)) {
+               if (!$item["visible"] || $item["deleted"]  || ($item["private"] == self::PRIVATE)) {
                        return;
                }
 
@@ -1943,7 +1867,7 @@ class Item
                        'owner-id' => $owner_id, 'private' => $private, 'psid' => $psid];
                self::update($fields, ['id' => $item_id]);
 
-               Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
+               Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, (int)$item['uri-id'], (int)$item['uid']);
 
                self::performActivity($item_id, 'announce', $uid);
 
@@ -2245,7 +2169,7 @@ class Item
                $condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY";
                $condition[] = $days;
 
-               $items = Post::select(['resource-id', 'starred', 'type', 'id', 'post-type', 'uid', 'uri-id'], $condition);
+               $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id'], $condition);
 
                if (!DBA::isResult($items)) {
                        return;
@@ -2278,9 +2202,9 @@ class Item
                                continue;
                        } elseif (!$expire_starred && intval($item['starred'])) {
                                continue;
-                       } elseif (!$expire_notes && (($item['type'] == 'note') || ($item['post-type'] == self::PT_PERSONAL_NOTE))) {
+                       } elseif (!$expire_notes && ($item['post-type'] == self::PT_PERSONAL_NOTE)) {
                                continue;
-                       } elseif (!$expire_items && ($item['type'] != 'note') && ($item['post-type'] != self::PT_PERSONAL_NOTE)) {
+                       } elseif (!$expire_items && ($item['post-type'] != self::PT_PERSONAL_NOTE)) {
                                continue;
                        }
 
@@ -2294,9 +2218,9 @@ class Item
 
        public static function firstPostDate($uid, $wall = false)
        {
-               $condition = ['uid' => $uid, 'wall' => $wall, 'deleted' => false, 'visible' => true, 'moderated' => false];
+               $condition = ['gravity' => GRAVITY_PARENT, 'uid' => $uid, 'wall' => $wall, 'deleted' => false, 'visible' => true];
                $params = ['order' => ['received' => false]];
-               $thread = DBA::selectFirst('thread', ['received'], $condition, $params);
+               $thread = Post::selectFirst(['received'], $condition, $params);
                if (DBA::isResult($thread)) {
                        return substr(DateTimeFormat::local($thread['received']), 0, 10);
                }
@@ -2509,75 +2433,6 @@ class Item
                return true;
        }
 
-       private static function addThread($itemid, $onlyshadow = false)
-       {
-               $fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail',
-                       '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 = Post::selectFirst($fields, $condition);
-
-               if (!DBA::isResult($item)) {
-                       return;
-               }
-
-               $item['iid'] = $itemid;
-
-               if (!$onlyshadow) {
-                       $result = DBA::replace('thread', $item);
-
-                       Logger::info('Add thread', ['item' => $itemid, 'result' => $result]);
-               }
-       }
-
-       private static function updateThread($itemid, $setmention = false)
-       {
-               $fields = ['uid', 'guid', 'created', 'edited', 'commented', 'received', 'changed', 'post-type',
-                       'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id',
-                       'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id'];
-
-               $item = Post::selectFirst($fields, ['id' => $itemid, 'gravity' => GRAVITY_PARENT]);
-               if (!DBA::isResult($item)) {
-                       return;
-               }
-
-               if ($setmention) {
-                       $item["mention"] = 1;
-               }
-
-               $fields = [];
-
-               foreach ($item as $field => $data) {
-                       if (!in_array($field, ["guid"])) {
-                               $fields[$field] = $data;
-                       }
-               }
-
-               $result = DBA::update('thread', $fields, ['iid' => $itemid]);
-
-               Logger::info('Update thread', ['item' => $itemid, 'guid' => $item["guid"], 'result' => $result]);
-       }
-
-       private static function deleteThread($itemid, $uri_id)
-       {
-               $item = DBA::selectFirst('thread', ['uid'], ['iid' => $itemid]);
-               if (!DBA::isResult($item)) {
-                       Logger::info('No thread found', ['id' => $itemid]);
-                       return;
-               }
-
-               $result = DBA::delete('thread', ['iid' => $itemid], ['cascade' => false]);
-
-               Logger::info('Deleted thread', ['item' => $itemid, 'result' => $result]);
-
-               $condition = ["`uri-id` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $uri_id, $item["uid"]];
-               if (!Post::exists($condition)) {
-                       DBA::delete('item', ['uri-id' => $uri_id, 'uid' => 0]);
-                       Post\User::delete(['uri-id' => $uri_id, 'uid' => 0]);
-                       Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri-id' => $uri_id]);
-               }
-       }
-
        /**
         * Fetch the SQL condition for the given user id
         *