From 5e846dd7c29fe93e583c195e97513e221dac31b9 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 05:51:25 +0000 Subject: [PATCH 01/20] The thread table is replaced by post-thread and post-thread-user --- database.sql | 160 +++++++++++++++----------- include/api.php | 12 +- mod/notes.php | 2 +- src/Model/Item.php | 8 +- src/Model/Post.php | 14 ++- src/Model/Post/Thread.php | 97 ++++++++++++++++ src/Module/Conversation/Community.php | 2 +- src/Worker/Expire.php | 13 ++- src/Worker/ExpirePosts.php | 1 + src/Worker/RemoveUnusedContacts.php | 9 +- static/dbstructure.config.php | 26 ++++- static/dbview.config.php | 131 ++++++++++----------- update.php | 36 +++++- 13 files changed, 358 insertions(+), 153 deletions(-) create mode 100644 src/Model/Post/Thread.php diff --git a/database.sql b/database.sql index 60dca5f5f..98aa1620e 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.03-dev (Red Hot Poker) --- DB_UPDATE_VERSION 1397 +-- DB_UPDATE_VERSION 1398 -- ------------------------------------------ @@ -1125,6 +1125,32 @@ CREATE TABLE IF NOT EXISTS `post-tag` ( FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='post relation to tags'; +-- +-- TABLE post-thread +-- +CREATE TABLE IF NOT EXISTS `post-thread` ( + `uri-id` int unsigned NOT NULL COMMENT 'Id of the item-uri table entry that contains the item uri', + `owner-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'Item owner', + `author-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'Item author', + `causer-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'Link to the contact table with uid=0 of the contact that caused the item creation', + `network` char(4) NOT NULL DEFAULT '' COMMENT '', + `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', + `received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', + `changed` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Date that something in the conversation changed, indicating clients should fetch the conversation again', + `commented` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', + PRIMARY KEY(`uri-id`), + INDEX `owner-id` (`owner-id`), + INDEX `author-id` (`author-id`), + INDEX `causer-id` (`causer-id`), + INDEX `received` (`received`), + INDEX `commented` (`commented`), + INDEX `changed` (`changed`), + FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, + FOREIGN KEY (`owner-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT, + FOREIGN KEY (`author-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT, + FOREIGN KEY (`causer-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT +) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Thread related data'; + -- -- TABLE post-thread-user -- @@ -1135,6 +1161,7 @@ CREATE TABLE IF NOT EXISTS `post-thread-user` ( `starred` boolean NOT NULL DEFAULT '0' COMMENT '', `ignored` boolean NOT NULL DEFAULT '0' COMMENT 'Ignore updates for this thread', `wall` boolean NOT NULL DEFAULT '0' COMMENT 'This item was posted to the wall of uid', + `mention` boolean NOT NULL DEFAULT '0' COMMENT '', `pubmail` boolean NOT NULL DEFAULT '0' COMMENT '', `forum_mode` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '', PRIMARY KEY(`uid`,`uri-id`), @@ -1641,43 +1668,43 @@ CREATE VIEW `post-view` AS SELECT DROP VIEW IF EXISTS `post-thread-view`; CREATE VIEW `post-thread-view` AS SELECT `item`.`id` AS `id`, - `thread`.`iid` AS `iid`, + `item`.`id` AS `iid`, `item`.`id` AS `item_id`, - `thread`.`uid` AS `uid`, + `post-thread-user`.`uid` AS `uid`, `item`.`parent` AS `parent`, `item`.`uri` AS `uri`, - `item`.`uri-id` AS `uri-id`, + `post-thread`.`uri-id` AS `uri-id`, `item`.`parent-uri` AS `parent-uri`, `item`.`parent-uri-id` AS `parent-uri-id`, `item`.`thr-parent` AS `thr-parent`, `item`.`thr-parent-id` AS `thr-parent-id`, `item`.`guid` AS `guid`, `item`.`type` AS `type`, - `thread`.`wall` AS `wall`, + `post-thread-user`.`wall` AS `wall`, `item`.`gravity` AS `gravity`, `item`.`extid` AS `extid`, - `thread`.`created` AS `created`, + `post-thread`.`created` AS `created`, `item`.`edited` AS `edited`, - `thread`.`commented` AS `commented`, - `thread`.`received` AS `received`, - `thread`.`changed` AS `changed`, - `thread`.`post-type` AS `post-type`, - `thread`.`private` AS `private`, - `thread`.`pubmail` AS `pubmail`, - `thread`.`moderated` AS `moderated`, - `thread`.`ignored` AS `ignored`, - `thread`.`visible` AS `visible`, - `thread`.`starred` AS `starred`, + `post-thread`.`commented` AS `commented`, + `post-thread`.`received` AS `received`, + `post-thread`.`changed` AS `changed`, + `item`.`post-type` AS `post-type`, + `item`.`private` AS `private`, + `post-thread-user`.`pubmail` AS `pubmail`, + `item`.`moderated` AS `moderated`, + `post-thread-user`.`ignored` AS `ignored`, + `item`.`visible` AS `visible`, + `post-thread-user`.`starred` AS `starred`, `item`.`bookmark` AS `bookmark`, - `item`.`unseen` AS `unseen`, - `thread`.`deleted` AS `deleted`, - `thread`.`origin` AS `origin`, - `thread`.`forum_mode` AS `forum_mode`, - `thread`.`mention` AS `mention`, + `post-user`.`unseen` AS `unseen`, + `item`.`deleted` AS `deleted`, + `post-user`.`origin` AS `origin`, + `post-thread-user`.`forum_mode` AS `forum_mode`, + `item`.`mention` AS `mention`, `item`.`global` AS `global`, - `thread`.`network` AS `network`, + `post-thread`.`network` AS `network`, `item`.`vid` AS `vid`, - `item`.`psid` AS `psid`, + `post-user`.`psid` AS `psid`, IF (`item`.`vid` IS NULL, '', `verb`.`name`) AS `verb`, `post-content`.`title` AS `title`, `post-content`.`content-warning` AS `content-warning`, @@ -1695,7 +1722,7 @@ CREATE VIEW `post-thread-view` AS SELECT `post-content`.`target-type` AS `target-type`, `post-content`.`target` AS `target`, `post-content`.`resource-id` AS `resource-id`, - `thread`.`contact-id` AS `contact-id`, + `post-user`.`contact-id` AS `contact-id`, `contact`.`url` AS `contact-link`, `contact`.`addr` AS `contact-addr`, `contact`.`name` AS `contact-name`, @@ -1720,7 +1747,7 @@ CREATE VIEW `post-thread-view` AS SELECT `contact`.`avatar-date` AS `avatar-date`, `contact`.`thumb` AS `thumb`, `contact`.`dfrn-id` AS `dfrn-id`, - `thread`.`author-id` AS `author-id`, + `post-thread`.`author-id` AS `author-id`, `author`.`url` AS `author-link`, `author`.`addr` AS `author-addr`, IF (`contact`.`url` = `author`.`url` AND `contact`.`name` != '', `contact`.`name`, `author`.`name`) AS `author-name`, @@ -1729,7 +1756,7 @@ CREATE VIEW `post-thread-view` AS SELECT `author`.`network` AS `author-network`, `author`.`blocked` AS `author-blocked`, `author`.`hidden` AS `author-hidden`, - `thread`.`owner-id` AS `owner-id`, + `post-thread`.`owner-id` AS `owner-id`, `owner`.`url` AS `owner-link`, `owner`.`addr` AS `owner-addr`, IF (`contact`.`url` = `owner`.`url` AND `contact`.`name` != '', `contact`.`name`, `owner`.`name`) AS `owner-name`, @@ -1753,10 +1780,10 @@ CREATE VIEW `post-thread-view` AS SELECT `post-delivery-data`.`queue_count` AS `delivery_queue_count`, `post-delivery-data`.`queue_done` AS `delivery_queue_done`, `post-delivery-data`.`queue_failed` AS `delivery_queue_failed`, - IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_cid`) AS `allow_cid`, - IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_gid`) AS `allow_gid`, - IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_cid`) AS `deny_cid`, - IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_gid`) AS `deny_gid`, + IF (`post-user`.`psid` IS NULL, '', `permissionset`.`allow_cid`) AS `allow_cid`, + IF (`post-user`.`psid` IS NULL, '', `permissionset`.`allow_gid`) AS `allow_gid`, + IF (`post-user`.`psid` IS NULL, '', `permissionset`.`deny_cid`) AS `deny_cid`, + IF (`post-user`.`psid` IS NULL, '', `permissionset`.`deny_gid`) AS `deny_gid`, `item`.`event-id` AS `event-id`, `event`.`created` AS `event-created`, `event`.`edited` AS `event-edited`, @@ -1776,18 +1803,20 @@ CREATE VIEW `post-thread-view` AS SELECT `parent-item-author`.`url` AS `parent-author-link`, `parent-item-author`.`name` AS `parent-author-name`, `parent-item-author`.`network` AS `parent-author-network` - FROM `thread` - STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `thread`.`author-id` - STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `thread`.`owner-id` - STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id` + FROM `post-thread` + STRAIGHT_JOIN `post-thread-user` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `post-user` ON `post-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `item` ON `item`.`uri-id` = `post-thread`.`uri-id` AND `item`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-user`.`contact-id` + STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post-thread`.`author-id` + STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `post-thread`.`owner-id` + STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `post-thread`.`causer-id` LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid` LEFT JOIN `event` ON `event`.`id` = `item`.`event-id` - LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `thread`.`uri-id` - LEFT JOIN `post-content` ON `post-content`.`uri-id` = `thread`.`uri-id` - LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `thread`.`uri-id` AND `thread`.`origin` - LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` + LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `post-thread`.`uri-id` + LEFT JOIN `post-content` ON `post-content`.`uri-id` = `post-thread`.`uri-id` + LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `post-thread`.`uri-id` AND `post-user`.`origin` + LEFT JOIN `permissionset` ON `permissionset`.`id` = `post-user`.`psid` STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`; @@ -1846,13 +1875,13 @@ CREATE VIEW `network-item-view` AS SELECT `item`.`contact-id` AS `contact-id`, `ownercontact`.`contact-type` AS `contact-type` FROM `item` - INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `thread`.`uid` - LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `thread`.`uid` AND `author`.`cid` = `thread`.`author-id` - LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `thread`.`uid` AND `owner`.`cid` = `thread`.`owner-id` - LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `thread`.`owner-id` - WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated` + INNER JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `parent-item`.`contact-id` + LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `parent-item`.`uid` + LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `parent-item`.`uid` AND `author`.`cid` = `parent-item`.`author-id` + LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `parent-item`.`uid` AND `owner`.`cid` = `parent-item`.`owner-id` + LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `parent-item`.`owner-id` + WHERE `parent-item`.`visible` AND NOT `parent-item`.`deleted` AND NOT `parent-item`.`moderated` AND (NOT `contact`.`readonly` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`) AND (`post-user`.`hidden` IS NULL OR NOT `post-user`.`hidden`) AND (`author`.`blocked` IS NULL OR NOT `author`.`blocked`) @@ -1863,27 +1892,28 @@ CREATE VIEW `network-item-view` AS SELECT -- DROP VIEW IF EXISTS `network-thread-view`; CREATE VIEW `network-thread-view` AS SELECT - `item`.`uri-id` AS `uri-id`, + `post-thread`.`uri-id` AS `uri-id`, `item`.`uri` AS `uri`, `item`.`parent-uri-id` AS `parent-uri-id`, - `thread`.`iid` AS `parent`, - `thread`.`received` AS `received`, - `thread`.`commented` AS `commented`, - `thread`.`created` AS `created`, - `thread`.`uid` AS `uid`, - `thread`.`starred` AS `starred`, - `thread`.`mention` AS `mention`, - `thread`.`network` AS `network`, - `thread`.`contact-id` AS `contact-id`, + `item`.`id` AS `parent`, + `post-thread`.`received` AS `received`, + `post-thread`.`commented` AS `commented`, + `post-thread`.`created` AS `created`, + `post-thread-user`.`uid` AS `uid`, + `post-thread-user`.`starred` AS `starred`, + `post-thread-user`.`mention` AS `mention`, + `post-thread`.`network` AS `network`, + `post-user`.`contact-id` AS `contact-id`, `ownercontact`.`contact-type` AS `contact-type` - FROM `thread` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` - LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `thread`.`uid` - LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `thread`.`uid` AND `author`.`cid` = `thread`.`author-id` - LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `thread`.`uid` AND `owner`.`cid` = `thread`.`owner-id` - LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `thread`.`owner-id` - WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated` + FROM `post-thread` + STRAIGHT_JOIN `post-thread-user` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `post-user` ON `post-user`.`uri-id` = `post-thread`.`uri-id` AND `post-user`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `item` ON `item`.`uri-id` = `post-thread`.`uri-id` AND `item`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-user`.`contact-id` + LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `post-user`.`uid` AND `author`.`cid` = `post-thread`.`author-id` + LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `post-user`.`uid` AND `owner`.`cid` = `post-thread`.`owner-id` + LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `post-thread`.`owner-id` + WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` AND (NOT `contact`.`readonly` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`) AND (`post-user`.`hidden` IS NULL OR NOT `post-user`.`hidden`) AND (`author`.`blocked` IS NULL OR NOT `author`.`blocked`) diff --git a/include/api.php b/include/api.php index 4c90ab80d..e6d62c57a 100644 --- a/include/api.php +++ b/include/api.php @@ -1118,8 +1118,8 @@ function api_statuses_update($type) if ($throttle_day > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); - $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom]; - $posts_day = DBA::count('thread', $condition); + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom]; + $posts_day = Post::count($condition); if ($posts_day > $throttle_day) { Logger::log('Daily posting limit reached for user '.api_user(), Logger::DEBUG); @@ -1132,8 +1132,8 @@ function api_statuses_update($type) if ($throttle_week > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); - $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom]; - $posts_week = DBA::count('thread', $condition); + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom]; + $posts_week = Post::count($condition); if ($posts_week > $throttle_week) { Logger::log('Weekly posting limit reached for user '.api_user(), Logger::DEBUG); @@ -1146,8 +1146,8 @@ function api_statuses_update($type) if ($throttle_month > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); - $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom]; - $posts_month = DBA::count('thread', $condition); + $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom]; + $posts_month = Post::count($condition); if ($posts_month > $throttle_month) { Logger::log('Monthly posting limit reached for user '.api_user(), Logger::DEBUG); diff --git a/mod/notes.php b/mod/notes.php index b7f840b08..9fa34e526 100644 --- a/mod/notes.php +++ b/mod/notes.php @@ -82,7 +82,7 @@ function notes_content(App $a, $update = false) $params = ['order' => ['created' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; - $r = Post::selectThreadForUser(local_user(), ['uri-id'], $condition, $params); + $r = Post::selectForUser(local_user(), ['uri-id'], $condition, $params); $count = 0; diff --git a/src/Model/Item.php b/src/Model/Item.php index 5041b7ef5..70530c45b 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -979,6 +979,10 @@ class Item Post\Media::insertFromAttachment($item['uri-id'], $item['attach']); } + 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); } @@ -2203,9 +2207,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, 'moderated' => false]; $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); } diff --git a/src/Model/Post.php b/src/Model/Post.php index 73742b3b2..d4aa9f1ee 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -194,7 +194,7 @@ class Post 'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name', 'parent-author-network', 'signed_text', 'language', 'raw-body'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST); - if ($view == 'post-thread-view') { + if ($view != 'post-view') { $selected = array_merge($selected, ['ignored', 'iid']); } } @@ -448,6 +448,18 @@ class Post $affected = max($affected, DBA::affectedRows()); } + $update_fields = DBStructure::getFieldsForTable('post-thread', $fields); + if (!empty($update_fields)) { + $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]); + $uriids = array_column($rows, 'uri-id'); + if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) { + DBA::rollback(); + Logger::notice('Updating post-thread failed', ['fields' => $update_fields, 'condition' => $condition]); + return false; + } + $affected = max($affected, DBA::affectedRows()); + } + $update_fields = DBStructure::getFieldsForTable('post-thread-user', $fields); if (!empty($update_fields)) { $rows = DBA::selectToArray('post-view', ['post-user-id'], $thread_condition); diff --git a/src/Model/Post/Thread.php b/src/Model/Post/Thread.php new file mode 100644 index 000000000..f5a5345df --- /dev/null +++ b/src/Model/Post/Thread.php @@ -0,0 +1,97 @@ +. + * + */ + +namespace Friendica\Model\Post; + +use \BadMethodCallException; +use Friendica\Core\Protocol; +use Friendica\Database\Database; +use Friendica\Database\DBA; +use Friendica\Database\DBStructure; +use Friendica\Model\Post; + +class Thread +{ + /** + * Insert a new post-thread entry + * + * @param integer $uri_id + * @param array $fields + * @return bool success + * @throws \Exception + */ + public static function insert(int $uri_id, array $data = []) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + $fields = DBStructure::getFieldsForTable('post-thread', $data); + + // Additionally assign the key fields + $fields['uri-id'] = $uri_id; + + return DBA::insert('post-thread', $fields, Database::INSERT_IGNORE); + } + + /** + * Update a post-thread entry + * + * @param integer $uri_id + * @param array $data + * @param bool $insert_if_missing + * @return bool + * @throws \Exception + */ + public static function update(int $uri_id, array $data = [], bool $insert_if_missing = false) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + $fields = DBStructure::getFieldsForTable('post-thread', $data); + + // Remove the key fields + unset($fields['uri-id']); + + if (empty($fields)) { + return true; + } + + return DBA::update('post-thread', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []); + } + + /** + * Delete a row from the post-thread table + * + * @param array $conditions Field condition(s) + * @param array $options + * - cascade: If true we delete records in other tables that depend on the one we're deleting through + * relations (default: true) + * + * @return boolean was the delete successful? + * @throws \Exception + */ + public static function delete(array $conditions, array $options = []) + { + return DBA::delete('post-thread', $conditions, $options); + } +} diff --git a/src/Module/Conversation/Community.php b/src/Module/Conversation/Community.php index 2febbe5a1..026c31f1d 100644 --- a/src/Module/Conversation/Community.php +++ b/src/Module/Conversation/Community.php @@ -336,7 +336,7 @@ class Community extends BaseModule $condition[] = $item_id; } else { if (local_user() && !empty($_REQUEST['no_sharer'])) { - $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `thread` AS t1 WHERE `t1`.`uri-id` = `thread`.`uri-id` AND `t1`.`uid` = ?)"; + $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-view`.`uri-id` AND `post-user`.`uid` = ?)"; $condition[] = local_user(); } diff --git a/src/Worker/Expire.php b/src/Worker/Expire.php index 2c419c401..70a4f913a 100644 --- a/src/Worker/Expire.php +++ b/src/Worker/Expire.php @@ -52,12 +52,17 @@ class Expire } DBA::close($rows); - // Normally we shouldn't have orphaned data at all. - // If we do have some, then we have to check why. - Logger::log('Deleting orphaned item content - start', Logger::DEBUG); + Logger::info('Deleting orphaned post-content - start'); + /// @todo Replace "item with "post-user" in the future when "item" is removed $condition = ["NOT EXISTS (SELECT `uri-id` FROM `item` WHERE `item`.`uri-id` = `post-content`.`uri-id`)"]; DBA::delete('post-content', $condition); - Logger::log('Orphaned item content deleted: ' . DBA::affectedRows(), Logger::DEBUG); + Logger::info('Orphaned post-content deleted', ['rows' => DBA::affectedRows()]); + + Logger::info('Deleting orphaned post-thread - start'); + /// @todo Replace "item with "post-user" in the future when "item" is removed + $condition = ["NOT EXISTS (SELECT `uri-id` FROM `item` WHERE `item`.`uri-id` = `post-thread`.`uri-id`)"]; + DBA::delete('post-thread', $condition); + Logger::info('Orphaned item content deleted', ['rows' => DBA::affectedRows()]); // make this optional as it could have a performance impact on large sites if (intval(DI::config()->get('system', 'optimize_items'))) { diff --git a/src/Worker/ExpirePosts.php b/src/Worker/ExpirePosts.php index 34e43ec5b..010aef696 100644 --- a/src/Worker/ExpirePosts.php +++ b/src/Worker/ExpirePosts.php @@ -47,6 +47,7 @@ class ExpirePosts if (!empty($expire_days)) { do { Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]); + /// @todo replace that query later $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN (SELECT `uri-id` FROM `thread` INNER JOIN `contact` ON `id` = `contact-id` AND NOT `notify_new_posts` diff --git a/src/Worker/RemoveUnusedContacts.php b/src/Worker/RemoveUnusedContacts.php index c2a071987..f90abe369 100644 --- a/src/Worker/RemoveUnusedContacts.php +++ b/src/Worker/RemoveUnusedContacts.php @@ -24,6 +24,7 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; +use Friendica\Database\DBStructure; use Friendica\Model\Photo; /** @@ -37,8 +38,8 @@ class RemoveUnusedContacts AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < UTC_TIMESTAMP() - INTERVAL ? DAY)) AND NOT `id` IN (SELECT `author-id` FROM `item`) AND NOT `id` IN (SELECT `owner-id` FROM `item`) AND NOT `id` IN (SELECT `causer-id` FROM `item`) AND NOT `id` IN (SELECT `cid` FROM `post-tag`) - AND NOT `id` IN (SELECT `contact-id` FROM `item`) AND NOT `id` IN (SELECT `author-id` FROM `thread`) - AND NOT `id` IN (SELECT `owner-id` FROM `thread`) AND NOT `id` IN (SELECT `contact-id` FROM `thread`) + AND NOT `id` IN (SELECT `contact-id` FROM `item`) AND NOT `id` IN (SELECT `author-id` FROM `post-thread`) + AND NOT `id` IN (SELECT `owner-id` FROM `post-thread`) AND NOT `id` IN (SELECT `causer-id` FROM `post-thread`) AND NOT `id` IN (SELECT `contact-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `user-contact`) AND NOT `id` IN (SELECT `cid` FROM `event`) AND NOT `id` IN (SELECT `contact-id` FROM `group_member`)", 0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, 365]; @@ -49,6 +50,10 @@ class RemoveUnusedContacts $contacts = DBA::select('contact', ['id', 'uid'], $condition); while ($contact = DBA::fetch($contacts)) { if (Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']])) { + if (DBStructure::existsTable('thread')) { + DBA::delete('thread', ['owner-id' => $contact['id']]); + DBA::delete('thread', ['author-id' => $contact['id']]); + } DBA::delete('contact', ['id' => $contact['id']]); if ((++$count % 1000) == 0) { Logger::notice('In removal', ['count' => $count, 'total' => $total]); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 6b8aba3ac..370479b99 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1397); + define('DB_UPDATE_VERSION', 1398); } return [ @@ -1181,6 +1181,29 @@ return [ "cid" => ["cid"] ] ], + "post-thread" => [ + "comment" => "Thread related data", + "fields" => [ + "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], + "owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item owner"], + "author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item author"], + "causer-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Link to the contact table with uid=0 of the contact that caused the item creation"], + "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""], + "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], + "received" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], + "changed" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Date that something in the conversation changed, indicating clients should fetch the conversation again"], + "commented" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""] + ], + "indexes" => [ + "PRIMARY" => ["uri-id"], + "owner-id" => ["owner-id"], + "author-id" => ["author-id"], + "causer-id" => ["causer-id"], + "received" => ["received"], + "commented" => ["commented"], + "changed" => ["changed"] + ] + ], "post-thread-user" => [ "comment" => "Thread related data per user", "fields" => [ @@ -1190,6 +1213,7 @@ return [ "starred" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "ignored" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Ignore updates for this thread"], "wall" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "This item was posted to the wall of uid"], + "mention" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "pubmail" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "forum_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""] ], diff --git a/static/dbview.config.php b/static/dbview.config.php index 088e426ba..98fe5fba7 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -193,43 +193,43 @@ "post-thread-view" => [ "fields" => [ "id" => ["item", "id"], - "iid" => ["thread", "iid"], + "iid" => ["item", "id"], "item_id" => ["item", "id"], - "uid" => ["thread", "uid"], + "uid" => ["post-thread-user", "uid"], "parent" => ["item", "parent"], "uri" => ["item", "uri"], - "uri-id" => ["item", "uri-id"], + "uri-id" => ["post-thread", "uri-id"], "parent-uri" => ["item", "parent-uri"], "parent-uri-id" => ["item", "parent-uri-id"], "thr-parent" => ["item", "thr-parent"], "thr-parent-id" => ["item", "thr-parent-id"], "guid" => ["item", "guid"], "type" => ["item", "type"], - "wall" => ["thread", "wall"], + "wall" => ["post-thread-user", "wall"], "gravity" => ["item", "gravity"], "extid" => ["item", "extid"], - "created" => ["thread", "created"], + "created" => ["post-thread", "created"], "edited" => ["item", "edited"], - "commented" => ["thread", "commented"], - "received" => ["thread", "received"], - "changed" => ["thread", "changed"], - "post-type" => ["thread", "post-type"], - "private" => ["thread", "private"], - "pubmail" => ["thread", "pubmail"], - "moderated" => ["thread", "moderated"], - "ignored" => ["thread", "ignored"], - "visible" => ["thread", "visible"], - "starred" => ["thread", "starred"], + "commented" => ["post-thread", "commented"], + "received" => ["post-thread", "received"], + "changed" => ["post-thread", "changed"], + "post-type" => ["item", "post-type"], + "private" => ["item", "private"], + "pubmail" => ["post-thread-user", "pubmail"], + "moderated" => ["item", "moderated"], + "ignored" => ["post-thread-user", "ignored"], + "visible" => ["item", "visible"], + "starred" => ["post-thread-user", "starred"], "bookmark" => ["item", "bookmark"], - "unseen" => ["item", "unseen"], - "deleted" => ["thread", "deleted"], - "origin" => ["thread", "origin"], - "forum_mode" => ["thread", "forum_mode"], - "mention" => ["thread", "mention"], + "unseen" => ["post-user", "unseen"], + "deleted" => ["item", "deleted"], + "origin" => ["post-user", "origin"], + "forum_mode" => ["post-thread-user", "forum_mode"], + "mention" => ["item", "mention"], "global" => ["item", "global"], - "network" => ["thread", "network"], + "network" => ["post-thread", "network"], "vid" => ["item", "vid"], - "psid" => ["item", "psid"], + "psid" => ["post-user", "psid"], "verb" => "IF (`item`.`vid` IS NULL, '', `verb`.`name`)", "title" => ["post-content", "title"], "content-warning" => ["post-content", "content-warning"], @@ -247,7 +247,7 @@ "target-type" => ["post-content", "target-type"], "target" => ["post-content", "target"], "resource-id" => ["post-content", "resource-id"], - "contact-id" => ["thread", "contact-id"], + "contact-id" => ["post-user", "contact-id"], "contact-link" => ["contact", "url"], "contact-addr" => ["contact", "addr"], "contact-name" => ["contact", "name"], @@ -272,7 +272,7 @@ "avatar-date" => ["contact", "avatar-date"], "thumb" => ["contact", "thumb"], "dfrn-id" => ["contact", "dfrn-id"], - "author-id" => ["thread", "author-id"], + "author-id" => ["post-thread", "author-id"], "author-link" => ["author", "url"], "author-addr" => ["author", "addr"], "author-name" => "IF (`contact`.`url` = `author`.`url` AND `contact`.`name` != '', `contact`.`name`, `author`.`name`)", @@ -281,7 +281,7 @@ "author-network" => ["author", "network"], "author-blocked" => ["author", "blocked"], "author-hidden" => ["author", "hidden"], - "owner-id" => ["thread", "owner-id"], + "owner-id" => ["post-thread", "owner-id"], "owner-link" => ["owner", "url"], "owner-addr" => ["owner", "addr"], "owner-name" => "IF (`contact`.`url` = `owner`.`url` AND `contact`.`name` != '', `contact`.`name`, `owner`.`name`)", @@ -305,10 +305,10 @@ "delivery_queue_count" => ["post-delivery-data", "queue_count"], "delivery_queue_done" => ["post-delivery-data", "queue_done"], "delivery_queue_failed" => ["post-delivery-data", "queue_failed"], - "allow_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_cid`)", - "allow_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_gid`)", - "deny_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_cid`)", - "deny_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_gid`)", + "allow_cid" => "IF (`post-user`.`psid` IS NULL, '', `permissionset`.`allow_cid`)", + "allow_gid" => "IF (`post-user`.`psid` IS NULL, '', `permissionset`.`allow_gid`)", + "deny_cid" => "IF (`post-user`.`psid` IS NULL, '', `permissionset`.`deny_cid`)", + "deny_gid" => "IF (`post-user`.`psid` IS NULL, '', `permissionset`.`deny_gid`)", "event-id" => ["item", "event-id"], "event-created" => ["event", "created"], "event-edited" => ["event", "edited"], @@ -329,18 +329,20 @@ "parent-author-name" => ["parent-item-author", "name"], "parent-author-network" => ["parent-item-author", "network"], ], - "query" => "FROM `thread` - STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `thread`.`author-id` - STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `thread`.`owner-id` - STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id` + "query" => "FROM `post-thread` + STRAIGHT_JOIN `post-thread-user` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `post-user` ON `post-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `item` ON `item`.`uri-id` = `post-thread`.`uri-id` AND `item`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-user`.`contact-id` + STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post-thread`.`author-id` + STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `post-thread`.`owner-id` + STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `post-thread`.`causer-id` LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid` LEFT JOIN `event` ON `event`.`id` = `item`.`event-id` - LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `thread`.`uri-id` - LEFT JOIN `post-content` ON `post-content`.`uri-id` = `thread`.`uri-id` - LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `thread`.`uri-id` AND `thread`.`origin` - LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` + LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `post-thread`.`uri-id` + LEFT JOIN `post-content` ON `post-content`.`uri-id` = `post-thread`.`uri-id` + LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `post-thread`.`uri-id` AND `post-user`.`origin` + LEFT JOIN `permissionset` ON `permissionset`.`id` = `post-user`.`psid` STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`" ], @@ -393,13 +395,13 @@ "contact-type" => ["ownercontact", "contact-type"], ], "query" => "FROM `item` - INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `thread`.`uid` - LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `thread`.`uid` AND `author`.`cid` = `thread`.`author-id` - LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `thread`.`uid` AND `owner`.`cid` = `thread`.`owner-id` - LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `thread`.`owner-id` - WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated` + INNER JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `parent-item`.`contact-id` + LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `parent-item`.`uid` + LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `parent-item`.`uid` AND `author`.`cid` = `parent-item`.`author-id` + LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `parent-item`.`uid` AND `owner`.`cid` = `parent-item`.`owner-id` + LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `parent-item`.`owner-id` + WHERE `parent-item`.`visible` AND NOT `parent-item`.`deleted` AND NOT `parent-item`.`moderated` AND (NOT `contact`.`readonly` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`) AND (`post-user`.`hidden` IS NULL OR NOT `post-user`.`hidden`) AND (`author`.`blocked` IS NULL OR NOT `author`.`blocked`) @@ -407,28 +409,29 @@ ], "network-thread-view" => [ "fields" => [ - "uri-id" => ["item", "uri-id"], + "uri-id" => ["post-thread", "uri-id"], "uri" => ["item", "uri"], "parent-uri-id" => ["item", "parent-uri-id"], - "parent" => ["thread", "iid"], - "received" => ["thread", "received"], - "commented" => ["thread", "commented"], - "created" => ["thread", "created"], - "uid" => ["thread", "uid"], - "starred" => ["thread", "starred"], - "mention" => ["thread", "mention"], - "network" => ["thread", "network"], - "contact-id" => ["thread", "contact-id"], + "parent" => ["item", "id"], + "received" => ["post-thread", "received"], + "commented" => ["post-thread", "commented"], + "created" => ["post-thread", "created"], + "uid" => ["post-thread-user", "uid"], + "starred" => ["post-thread-user", "starred"], + "mention" => ["post-thread-user", "mention"], + "network" => ["post-thread", "network"], + "contact-id" => ["post-user", "contact-id"], "contact-type" => ["ownercontact", "contact-type"], ], - "query" => "FROM `thread` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` - STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid` - LEFT JOIN `post-user` ON `post-user`.`uri-id` = `item`.`uri-id` AND `post-user`.`uid` = `thread`.`uid` - LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `thread`.`uid` AND `author`.`cid` = `thread`.`author-id` - LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `thread`.`uid` AND `owner`.`cid` = `thread`.`owner-id` - LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `thread`.`owner-id` - WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated` + "query" => "FROM `post-thread` + STRAIGHT_JOIN `post-thread-user` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id` + STRAIGHT_JOIN `post-user` ON `post-user`.`uri-id` = `post-thread`.`uri-id` AND `post-user`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `item` ON `item`.`uri-id` = `post-thread`.`uri-id` AND `item`.`uid` = `post-thread-user`.`uid` + STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-user`.`contact-id` + LEFT JOIN `user-contact` AS `author` ON `author`.`uid` = `post-user`.`uid` AND `author`.`cid` = `post-thread`.`author-id` + LEFT JOIN `user-contact` AS `owner` ON `owner`.`uid` = `post-user`.`uid` AND `owner`.`cid` = `post-thread`.`owner-id` + LEFT JOIN `contact` AS `ownercontact` ON `ownercontact`.`id` = `post-thread`.`owner-id` + WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` AND (NOT `contact`.`readonly` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`) AND (`post-user`.`hidden` IS NULL OR NOT `post-user`.`hidden`) AND (`author`.`blocked` IS NULL OR NOT `author`.`blocked`) diff --git a/update.php b/update.php index 94029065b..9f85926c1 100644 --- a/update.php +++ b/update.php @@ -295,7 +295,7 @@ function update_1349() function update_1351() { - if (!DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) { + if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) { return Update::FAILED; } @@ -519,7 +519,7 @@ function pre_update_1365() return Update::FAILED; } - if (!DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) { + if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) { return Update::FAILED; } @@ -548,7 +548,7 @@ function pre_update_1376() return Update::FAILED; } - if (!DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) { + if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) { return Update::FAILED; } @@ -589,15 +589,15 @@ function pre_update_1377() return Update::FAILED; } - if (!DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) { + if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) { return Update::FAILED; } - if (!DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) { + if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) { return Update::FAILED; } - if (!DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) { + if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) { return Update::FAILED; } @@ -735,3 +735,27 @@ function update_1397() return Update::SUCCESS; } + +function update_1398() +{ + if (!DBStructure::existsTable('thread')) { + return Update::SUCCESS; + } + + if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`) + SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) { + return Update::FAILED; + } + + if (!DBStructure::existsTable('thread')) { + return Update::SUCCESS; + } + + if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `thread` ON `thread`.`uid` = `post-thread-user`.`uid` AND `thread`.`uri-id` = `post-thread-user`.`uri-id` + SET `post-thread-user`.`mention` = `thread`.`mention`")) { + return Update::FAILED; + } + + return Update::SUCCESS; + +} \ No newline at end of file From 2282bb4a19222a2779a353c542cc1765fcef73cc Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 09:46:29 +0000 Subject: [PATCH 02/20] Fix test data --- src/Model/Item.php | 3 ++- tests/datasets/api.fixture.php | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 70530c45b..106d50e76 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -899,8 +899,9 @@ class Item // If its a post that originated here then tag the thread as "mention" if ($item['origin'] && $item['uid']) { + DBA::update('post-thread-user', ['mention' => true], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]); DBA::update('thread', ['mention' => true], ['iid' => $parent_id]); - Logger::info('tagged thread as mention', ['parent' => $parent_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 diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index aa99fc7cb..4cba5eeb8 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -419,34 +419,36 @@ return [ 'msg_cache' => 'A test reply from an item', ], ], - 'thread' => [ + 'post-thread' => [ [ - 'iid' => 1, 'uri-id' => 1, - 'visible' => 1, - 'contact-id' => 42, 'author-id' => 42, 'owner-id' => 42, + ], + [ + 'uri-id' => 3, + 'author-id' => 43, + 'owner-id' => 43, + ], + [ + 'uri-id' => 6, + 'author-id' => 44, + 'owner-id' => 44, + ], + ], + 'post-thread-user' => [ + [ + 'uri-id' => 1, 'uid' => 42, 'wall' => 1, ], [ - 'iid' => 3, 'uri-id' => 3, - 'visible' => 1, - 'contact-id' => 43, - 'author-id' => 43, - 'owner-id' => 43, 'uid' => 0, 'wall' => 1, ], [ - 'iid' => 6, 'uri-id' => 6, - 'visible' => 1, - 'contact-id' => 44, - 'author-id' => 44, - 'owner-id' => 44, 'uid' => 0, 'wall' => 1, ], From 2e4ff986992ebbd8c4fcb1f6c5a505d7c6618c72 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 17:48:51 +0000 Subject: [PATCH 03/20] Set test data --- tests/datasets/api.fixture.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 4cba5eeb8..b2b877dea 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -424,16 +424,19 @@ return [ 'uri-id' => 1, 'author-id' => 42, 'owner-id' => 42, + 'network' => 'dfrn', ], [ 'uri-id' => 3, 'author-id' => 43, 'owner-id' => 43, + 'network' => 'dfrn', ], [ 'uri-id' => 6, 'author-id' => 44, 'owner-id' => 44, + 'network' => 'dfrn', ], ], 'post-thread-user' => [ From 2b6b72f7fd59002560524b00d34be1142b6cdd44 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 17:56:43 +0000 Subject: [PATCH 04/20] Fix test data --- tests/datasets/api.fixture.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index b2b877dea..1be3f9b78 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -447,12 +447,12 @@ return [ ], [ 'uri-id' => 3, - 'uid' => 0, + 'uid' => 42, 'wall' => 1, ], [ 'uri-id' => 6, - 'uid' => 0, + 'uid' => 42, 'wall' => 1, ], ], From 69aad853ee23d2fa679fbc7f558cbe3853b7ed06 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 18:14:15 +0000 Subject: [PATCH 05/20] Handle empty values for API output --- include/api.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/api.php b/include/api.php index e6d62c57a..2da72f8d2 100644 --- a/include/api.php +++ b/include/api.php @@ -2931,6 +2931,10 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso $ret = []; + if (empty($items)) { + return $ret; + } + foreach ((array)$items as $item) { list($status_user, $author_user, $owner_user) = api_item_get_user($a, $item); From f6e4aa6ad372f2b08782c7120c4ec6162b1e76b8 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 21:49:31 +0000 Subject: [PATCH 06/20] Test for test --- tests/legacy/ApiTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 265f502f0..1d383183c 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1677,7 +1677,7 @@ class ApiTest extends FixtureTest $_REQUEST['max_id'] = 10; $_REQUEST['exclude_replies'] = true; $result = api_statuses_public_timeline('json'); - self::assertNotEmpty($result['status']); + self::assertNotEmpty($result['status'], var_export($result, true)); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -1731,7 +1731,7 @@ class ApiTest extends FixtureTest { $_REQUEST['max_id'] = 10; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status']); + self::assertNotEmpty($result['status'], var_export($result, true)); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -1746,7 +1746,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status']); + self::assertNotEmpty($result['status'], var_export($result, true)); foreach ($result['status'] as $status) { self::assertStatus($status); } From 38de3a53a5c9d47cffd95a7c686d9a353565bdee Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 22:14:09 +0000 Subject: [PATCH 07/20] Enhanced test data --- tests/legacy/ApiTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 1d383183c..265f502f0 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1677,7 +1677,7 @@ class ApiTest extends FixtureTest $_REQUEST['max_id'] = 10; $_REQUEST['exclude_replies'] = true; $result = api_statuses_public_timeline('json'); - self::assertNotEmpty($result['status'], var_export($result, true)); + self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -1731,7 +1731,7 @@ class ApiTest extends FixtureTest { $_REQUEST['max_id'] = 10; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status'], var_export($result, true)); + self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); } @@ -1746,7 +1746,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status'], var_export($result, true)); + self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); } From f92816cf62a296a5cb73be9c13a7e297344c536b Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 22:14:53 +0000 Subject: [PATCH 08/20] More test data --- tests/datasets/api.fixture.php | 95 +++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 1be3f9b78..3e9e53164 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -211,6 +211,46 @@ return [ 'plink' => 'http://localhost/display/6', ], ], + 'post-thread' => [ + [ + 'uri-id' => 1, + 'author-id' => 42, + 'owner-id' => 42, + 'causer-id' => 42, + 'network' => 'dfrn', + ], + [ + 'uri-id' => 3, + 'author-id' => 43, + 'owner-id' => 43, + 'causer-id' => 43, + 'network' => 'dfrn', + ], + [ + 'uri-id' => 6, + 'author-id' => 44, + 'owner-id' => 44, + 'causer-id' => 44, + 'network' => 'dfrn', + ], + ], + 'post-thread-user' => [ + [ + 'uri-id' => 1, + 'uid' => 42, + 'wall' => 1, + ], + [ + 'uri-id' => 3, + 'uid' => 42, + 'wall' => 1, + ], + [ + 'uri-id' => 6, + 'uid' => 42, + 'wall' => 1, + ], + ], 'post-user' => [ [ 'id' => 1, @@ -266,6 +306,7 @@ return [ [ 'id' => 1, 'uri-id' => 1, + 'uri' => '1', 'visible' => 1, 'contact-id' => 42, 'author-id' => 42, @@ -276,7 +317,9 @@ return [ 'unseen' => 1, 'parent' => 1, 'parent-uri-id' => 1, + 'parent-uri' => '1', 'thr-parent-id' => 1, + 'thr-parent' => '1', 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -291,6 +334,7 @@ return [ [ 'id' => 2, 'uri-id' => 2, + 'uri' => '2', 'visible' => 1, 'contact-id' => 42, 'author-id' => 42, @@ -301,7 +345,9 @@ return [ 'unseen' => 0, 'parent' => 1, 'parent-uri-id' => 1, + 'parent-uri' => '1', 'thr-parent-id' => 1, + 'thr-parent' => '1', 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -312,6 +358,7 @@ return [ [ 'id' => 3, 'uri-id' => 3, + 'uri' => '3', 'visible' => 1, 'contact-id' => 43, 'author-id' => 43, @@ -322,7 +369,9 @@ return [ 'unseen' => 0, 'parent' => 3, 'parent-uri-id' => 3, + 'parent-uri' => '3', 'thr-parent-id' => 3, + 'thr-parent' => '3', 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -333,6 +382,7 @@ return [ [ 'id' => 4, 'uri-id' => 4, + 'uri' => '4', 'visible' => 1, 'contact-id' => 44, 'author-id' => 44, @@ -344,7 +394,9 @@ return [ 'body' => 'Friend user reply', 'parent' => 1, 'parent-uri-id' => 1, + 'parent-uri' => '1', 'thr-parent-id' => 1, + 'thr-parent' => '1', 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -356,6 +408,7 @@ return [ 'id' => 5, 'uri-id' => 5, + 'uri' => '5', 'visible' => 1, 'contact-id' => 42, 'author-id' => 42, @@ -366,7 +419,9 @@ return [ 'unseen' => 0, 'parent' => 1, 'parent-uri-id' => 1, + 'parent-uri' => '1', 'thr-parent-id' => 1, + 'thr-parent' => '1', 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -381,6 +436,7 @@ return [ [ 'id' => 6, 'uri-id' => 6, + 'uri' => '6', 'visible' => 1, 'contact-id' => 44, 'author-id' => 44, @@ -391,7 +447,9 @@ return [ 'unseen' => 0, 'parent' => 6, 'parent-uri-id' => 6, + 'parent-uri' => '6', 'thr-parent-id' => 6, + 'thr-parent' => '6', 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -419,43 +477,6 @@ return [ 'msg_cache' => 'A test reply from an item', ], ], - 'post-thread' => [ - [ - 'uri-id' => 1, - 'author-id' => 42, - 'owner-id' => 42, - 'network' => 'dfrn', - ], - [ - 'uri-id' => 3, - 'author-id' => 43, - 'owner-id' => 43, - 'network' => 'dfrn', - ], - [ - 'uri-id' => 6, - 'author-id' => 44, - 'owner-id' => 44, - 'network' => 'dfrn', - ], - ], - 'post-thread-user' => [ - [ - 'uri-id' => 1, - 'uid' => 42, - 'wall' => 1, - ], - [ - 'uri-id' => 3, - 'uid' => 42, - 'wall' => 1, - ], - [ - 'uri-id' => 6, - 'uid' => 42, - 'wall' => 1, - ], - ], 'profile' => [ [ 'id' => 1, From 859d026edf07d4e8088493e6e09d5ff113e7f0d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 22:41:33 +0000 Subject: [PATCH 09/20] Some more test data --- include/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/api.php b/include/api.php index 2da72f8d2..1cb1880b4 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,7 +1819,7 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); From 0cd8c78e2d2624b95f79a7546d04a628281d1c64 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 4 Feb 2021 22:51:33 +0000 Subject: [PATCH 10/20] Use the id instead of iid --- include/api.php | 6 +++--- tests/datasets/api.fixture.php | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/api.php b/include/api.php index 1cb1880b4..69c8ace2b 100644 --- a/include/api.php +++ b/include/api.php @@ -1810,15 +1810,15 @@ function api_statuses_networkpublic_timeline($type) $start = max(0, ($page - 1) * $count); - $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `iid` > ? AND `private` = ?", + $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `id` > ? AND `private` = ?", GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC]; if ($max_id > 0) { - $condition[0] .= " AND `iid` <= ?"; + $condition[0] .= " AND `id` <= ?"; $condition[] = $max_id; } - $params = ['order' => ['iid' => true], 'limit' => [$start, $count]]; + $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 3e9e53164..407629709 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -320,6 +320,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', + 'private' => 0, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -348,6 +349,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', + 'private' => 0, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -372,6 +374,7 @@ return [ 'parent-uri' => '3', 'thr-parent-id' => 3, 'thr-parent' => '3', + 'private' => 0, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -397,6 +400,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', + 'private' => 0, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -422,6 +426,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', + 'private' => 0, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -450,6 +455,7 @@ return [ 'parent-uri' => '6', 'thr-parent-id' => 6, 'thr-parent' => '6', + 'private' => 0, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, From 239bf686af401097f8c87ed38bec69ecb5dd2fb2 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 05:53:44 +0000 Subject: [PATCH 11/20] Just some more tests --- include/api.php | 3 ++- tests/datasets/api.fixture.php | 45 ++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/api.php b/include/api.php index 69c8ace2b..b183d8839 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,7 +1819,8 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); +// $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Post::select(Item::DISPLAY_FIELDLIST, $condition, $params); $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 407629709..a07232a8c 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -19,6 +19,9 @@ * */ +use Friendica\Core\Protocol; +use Friendica\Model\Contact; +use Friendica\Model\Item; use Friendica\Model\Notification; return [ @@ -70,8 +73,8 @@ return [ 'about' => 'User used in tests', 'pending' => 0, 'blocked' => 0, - 'rel' => 1, - 'network' => 'dfrn', + 'rel' => Contact::FOLLOWER, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], // Having the same name and nick allows us to test @@ -86,8 +89,8 @@ return [ 'url' => 'http://localhost/profile/othercontact', 'pending' => 0, 'blocked' => 0, - 'rel' => 0, - 'network' => 'dfrn', + 'rel' => Contact::NOTHING, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], [ @@ -100,8 +103,8 @@ return [ 'url' => 'http://localhost/profile/friendcontact', 'pending' => 0, 'blocked' => 0, - 'rel' => 2, - 'network' => 'dfrn', + 'rel' => Contact::SHARING, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], [ @@ -114,8 +117,8 @@ return [ 'url' => 'http://localhost/profile/friendcontact', 'pending' => 0, 'blocked' => 0, - 'rel' => 2, - 'network' => 'dfrn', + 'rel' => Contact::SHARING, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], [ @@ -128,8 +131,8 @@ return [ 'url' => 'http://localhost/profile/mutualcontact', 'pending' => 0, 'blocked' => 0, - 'rel' => 3, - 'network' => 'dfrn', + 'rel' => Contact::FRIEND, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], [ @@ -142,8 +145,8 @@ return [ 'url' => 'http://localhost/profile/mutualcontact', 'pending' => 0, 'blocked' => 0, - 'rel' => 2, - 'network' => 'dfrn', + 'rel' => Contact::SHARING, + 'network' => Protocol::DFRN, 'location' => 'DFRN', ], ], @@ -217,21 +220,21 @@ return [ 'author-id' => 42, 'owner-id' => 42, 'causer-id' => 42, - 'network' => 'dfrn', + 'network' => Protocol::DFRN, ], [ 'uri-id' => 3, 'author-id' => 43, 'owner-id' => 43, 'causer-id' => 43, - 'network' => 'dfrn', + 'network' => Protocol::DFRN, ], [ 'uri-id' => 6, 'author-id' => 44, 'owner-id' => 44, 'causer-id' => 44, - 'network' => 'dfrn', + 'network' => Protocol::DFRN, ], ], 'post-thread-user' => [ @@ -320,7 +323,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -349,7 +352,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, @@ -374,7 +377,7 @@ return [ 'parent-uri' => '3', 'thr-parent-id' => 3, 'thr-parent' => '3', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -400,7 +403,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -426,7 +429,7 @@ return [ 'parent-uri' => '1', 'thr-parent-id' => 1, 'thr-parent' => '1', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, @@ -455,7 +458,7 @@ return [ 'parent-uri' => '6', 'thr-parent-id' => 6, 'thr-parent' => '6', - 'private' => 0, + 'private' => Item::PUBLIC, 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, From 289923a17c53a48a4c5ea3d3c7cfeea71574eacf Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 06:23:34 +0000 Subject: [PATCH 12/20] Testing the query --- include/api.php | 7 ++++--- tests/legacy/ApiTest.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/api.php b/include/api.php index b183d8839..7e32a43ad 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,9 +1819,10 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; -// $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); - $statuses = Post::select(Item::DISPLAY_FIELDLIST, $condition, $params); - + $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); +if (empty($statuses)) { + return ['test' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; +} $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); bindComments($ret); diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 265f502f0..14d925b52 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1746,7 +1746,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status']); + self::assertNotEmpty($result['status'], var_export($result, true)); foreach ($result['status'] as $status) { self::assertStatus($status); } From 686bf6b32f32852a24286f053feb1c293361e3b6 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 06:33:34 +0000 Subject: [PATCH 13/20] Some more debug output --- include/api.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/api.php b/include/api.php index 7e32a43ad..b406a4799 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,12 +1819,14 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params); + $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); if (empty($statuses)) { return ['test' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; } - $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); - + $ret = api_format_items($statuses, $user_info, false, $type); +if (empty($ret)) { + return ['ret' => $ret, 'test' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; +} bindComments($ret); $data = ['status' => $ret]; From 3446829a19d74b7be51344660b74715e5de1b7dd Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 06:41:34 +0000 Subject: [PATCH 14/20] And again ... --- include/api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index b406a4799..d14ca3e3e 100644 --- a/include/api.php +++ b/include/api.php @@ -1821,11 +1821,11 @@ function api_statuses_networkpublic_timeline($type) $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); if (empty($statuses)) { - return ['test' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; + return ['status' => [], 'statuses' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; } $ret = api_format_items($statuses, $user_info, false, $type); if (empty($ret)) { - return ['ret' => $ret, 'test' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; + return ['status' => [], 'ret' => $ret, 'statuses' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; } bindComments($ret); From bc6d87afa88035e36e26254386adf2ec1d0c7cdb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 06:56:24 +0000 Subject: [PATCH 15/20] test again --- include/api.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index d14ca3e3e..b65cf77f6 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,9 +1819,10 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); +// $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); + $statuses = Post::toArray(Post::select(Item::DISPLAY_FIELDLIST, $condition, $params)); if (empty($statuses)) { - return ['status' => [], 'statuses' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; + return ['status' => [], 'statuses2' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; } $ret = api_format_items($statuses, $user_info, false, $type); if (empty($ret)) { From 15b709594fbbc7e6e9022e574e5c75e7a7da5c59 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 07:14:09 +0000 Subject: [PATCH 16/20] Improving test data --- include/api.php | 11 ++---- tests/datasets/api.fixture.php | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/include/api.php b/include/api.php index b65cf77f6..a96681b83 100644 --- a/include/api.php +++ b/include/api.php @@ -1819,15 +1819,10 @@ function api_statuses_networkpublic_timeline($type) } $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; -// $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); - $statuses = Post::toArray(Post::select(Item::DISPLAY_FIELDLIST, $condition, $params)); -if (empty($statuses)) { - return ['status' => [], 'statuses2' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; -} + $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params)); + $ret = api_format_items($statuses, $user_info, false, $type); -if (empty($ret)) { - return ['status' => [], 'ret' => $ret, 'statuses' => $statuses, 'condition' => $condition, 'params' => $params, 'db' => DBA::errorNo(), 'msg' => DBA::errorMessage()]; -} + bindComments($ret); $data = ['status' => $ret]; diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index a07232a8c..b30da447c 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -253,6 +253,21 @@ return [ 'uid' => 42, 'wall' => 1, ], + [ + 'uri-id' => 1, + 'uid' => 0, + 'wall' => 0, + ], + [ + 'uri-id' => 3, + 'uid' => 0, + 'wall' => 0, + ], + [ + 'uri-id' => 6, + 'uid' => 0, + 'wall' => 0, + ], ], 'post-user' => [ [ @@ -303,6 +318,54 @@ return [ 'unseen' => 0, 'origin' => 1, ], + [ + 'id' => 7, + 'uri-id' => 1, + 'uid' => 0, + 'contact-id' => 42, + 'unseen' => 1, + 'origin' => 0, + ], + [ + 'id' => 8, + 'uri-id' => 2, + 'uid' => 0, + 'contact-id' => 42, + 'unseen' => 0, + 'origin' => 0, + ], + [ + 'id' => 9, + 'uri-id' => 3, + 'uid' => 0, + 'contact-id' => 43, + 'unseen' => 0, + 'origin' => 0, + ], + [ + 'id' => 10, + 'uri-id' => 4, + 'uid' => 0, + 'contact-id' => 44, + 'unseen' => 0, + 'origin' => 0, + ], + [ + 'id' => 11, + 'uri-id' => 5, + 'uid' => 0, + 'contact-id' => 42, + 'unseen' => 0, + 'origin' => 0, + ], + [ + 'id' => 12, + 'uri-id' => 6, + 'uid' => 0, + 'contact-id' => 44, + 'unseen' => 0, + 'origin' => 0, + ], ], 'item' => [ From 09c2f424be74b51cadf30de28c3cea42a80a53c4 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 07:27:15 +0000 Subject: [PATCH 17/20] More test data --- tests/datasets/api.fixture.php | 160 +++++++++++++++++++++++++++++++++ tests/legacy/ApiTest.php | 2 +- 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index b30da447c..8a683da95 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -529,6 +529,166 @@ return [ 'origin' => 1, 'guid' => '6', ], + [ + 'id' => 7, + 'uri-id' => 1, + 'uri' => '1', + 'visible' => 1, + 'contact-id' => 42, + 'author-id' => 42, + 'owner-id' => 42, + 'causer-id' => 42, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 1, + 'parent' => 7, + 'parent-uri-id' => 1, + 'parent-uri' => '1', + 'thr-parent-id' => 1, + 'thr-parent' => '1', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_PARENT, + 'author-link' => 'http://localhost/profile/selfcontact', + 'wall' => 0, + 'starred' => 1, + 'origin' => 0, + 'allow_cid' => '', + 'allow_gid' => '', + 'deny_cid' => '', + 'deny_gid' => '', + 'guid' => '1', + ], + [ + 'id' => 8, + 'uri-id' => 2, + 'uri' => '2', + 'visible' => 1, + 'contact-id' => 42, + 'author-id' => 42, + 'owner-id' => 42, + 'causer-id' => 42, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 0, + 'parent' => 7, + 'parent-uri-id' => 1, + 'parent-uri' => '1', + 'thr-parent-id' => 1, + 'thr-parent' => '1', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_COMMENT, + 'author-link' => 'http://localhost/profile/selfcontact', + 'wall' => 0, + 'starred' => 0, + 'origin' => 0, + 'guid' => '2', + ], + [ + 'id' => 9, + 'uri-id' => 3, + 'uri' => '3', + 'visible' => 1, + 'contact-id' => 43, + 'author-id' => 43, + 'owner-id' => 42, + 'causer-id' => 43, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 0, + 'parent' => 9, + 'parent-uri-id' => 3, + 'parent-uri' => '3', + 'thr-parent-id' => 3, + 'thr-parent' => '3', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_PARENT, + 'author-link' => 'http://localhost/profile/othercontact', + 'wall' => 0, + 'starred' => 0, + 'origin' => 0, + 'guid' => '3', + ], + [ + 'id' => 10, + 'uri-id' => 4, + 'uri' => '4', + 'visible' => 1, + 'contact-id' => 44, + 'author-id' => 44, + 'owner-id' => 42, + 'causer-id' => 44, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 0, + 'body' => 'Friend user reply', + 'parent' => 7, + 'parent-uri-id' => 1, + 'parent-uri' => '1', + 'thr-parent-id' => 1, + 'thr-parent' => '1', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_COMMENT, + 'author-link' => 'http://localhost/profile/othercontact', + 'wall' => 0, + 'starred' => 0, + 'origin' => 0, + 'guid' => '4', + ], + [ + + 'id' => 11, + 'uri-id' => 5, + 'uri' => '5', + 'visible' => 1, + 'contact-id' => 42, + 'author-id' => 42, + 'owner-id' => 42, + 'causer-id' => 42, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 0, + 'parent' => 7, + 'parent-uri-id' => 1, + 'parent-uri' => '1', + 'thr-parent-id' => 1, + 'thr-parent' => '1', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_COMMENT, + 'author-link' => 'http://localhost/profile/othercontact', + 'wall' => 0, + 'starred' => 0, + 'origin' => 1, + 'allow_cid' => '', + 'allow_gid' => '', + 'deny_cid' => '', + 'deny_gid' => '', + 'guid' => '5', + ], + [ + 'id' => 12, + 'uri-id' => 6, + 'uri' => '6', + 'visible' => 1, + 'contact-id' => 44, + 'author-id' => 44, + 'owner-id' => 42, + 'causer-id' => 44, + 'uid' => 0, + 'verb' => 'http://activitystrea.ms/schema/1.0/post', + 'unseen' => 0, + 'parent' => 12, + 'parent-uri-id' => 6, + 'parent-uri' => '6', + 'thr-parent-id' => 6, + 'thr-parent' => '6', + 'private' => Item::PUBLIC, + 'gravity' => GRAVITY_PARENT, + 'author-link' => 'http://localhost/profile/othercontact', + 'wall' => 0, + 'starred' => 0, + 'origin' => 0, + 'guid' => '6', + ], ], 'notify' => [ [ diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 14d925b52..265f502f0 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -1746,7 +1746,7 @@ class ApiTest extends FixtureTest { $_REQUEST['page'] = -2; $result = api_statuses_networkpublic_timeline('json'); - self::assertNotEmpty($result['status'], var_export($result, true)); + self::assertNotEmpty($result['status']); foreach ($result['status'] as $status) { self::assertStatus($status); } From 45dedee0a2206e539aadabd81d7959c474427925 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 07:39:34 +0000 Subject: [PATCH 18/20] Partly reverting change --- mod/notes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod/notes.php b/mod/notes.php index 9fa34e526..b7f840b08 100644 --- a/mod/notes.php +++ b/mod/notes.php @@ -82,7 +82,7 @@ function notes_content(App $a, $update = false) $params = ['order' => ['created' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; - $r = Post::selectForUser(local_user(), ['uri-id'], $condition, $params); + $r = Post::selectThreadForUser(local_user(), ['uri-id'], $condition, $params); $count = 0; From 265f46692f42c6611283471c51f2e1b5e428c513 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 07:44:39 +0000 Subject: [PATCH 19/20] Avoid foreign key problems when deleting contacts --- src/Worker/RemoveContact.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Worker/RemoveContact.php b/src/Worker/RemoveContact.php index 25d61f555..e1e6cbcf9 100644 --- a/src/Worker/RemoveContact.php +++ b/src/Worker/RemoveContact.php @@ -52,6 +52,8 @@ class RemoveContact { Logger::info('Delete removed contact item', ['id' => $item['id'], 'guid' => $item['guid']]); DBA::delete('item', ['id' => $item['id']]); Post\User::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]); + Post\Thread::delete(['uri-id' => $item['uri-id']]); + Post\ThreadUser::delete(['uri-id' => $item['uri-id'], 'uid' => $item['uid']]); } DBA::close($items); } while (Post::exists($condition)); From 4e192dede532771dd317ba8fb70740f506d3cebb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Feb 2021 13:40:15 +0000 Subject: [PATCH 20/20] EOL added --- update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update.php b/update.php index 9f85926c1..f9c0f16df 100644 --- a/update.php +++ b/update.php @@ -758,4 +758,4 @@ function update_1398() return Update::SUCCESS; -} \ No newline at end of file +}