From 8643603f8b83e15b39ec94d9a1d6266a7897fdd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 10:04:47 +0000 Subject: [PATCH 1/6] Fix "Item couldn't be fetched." --- database.sql | 4 ++-- static/dbstructure.config.php | 2 +- static/dbview.config.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/database.sql b/database.sql index 1ca1f962b..d5844f595 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.03-dev (Red Hot Poker) --- DB_UPDATE_VERSION 1387 +-- DB_UPDATE_VERSION 1388 -- ------------------------------------------ @@ -1643,7 +1643,7 @@ CREATE VIEW `post-view` AS SELECT LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id` LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin` LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` - STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` + STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` AND `parent-item`.`uid` = `item`.`uid` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`; -- diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 18a0d5981..5199dae33 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', 1387); + define('DB_UPDATE_VERSION', 1388); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index b987eab42..a94f4c74c 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -190,7 +190,7 @@ LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id` LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin` LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` - STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent` + STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` AND `parent-item`.`uid` = `item`.`uid` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`" ], "category-view" => [ From 608d3b2b3292b824babc312e883e3e7776e730a4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 10:14:38 +0000 Subject: [PATCH 2/6] The view can be made simpler --- database.sql | 2 +- static/dbview.config.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/database.sql b/database.sql index d5844f595..8c4ab5bae 100644 --- a/database.sql +++ b/database.sql @@ -1643,7 +1643,7 @@ CREATE VIEW `post-view` AS SELECT LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id` LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin` LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` - STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` AND `parent-item`.`uid` = `item`.`uid` + STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`; -- diff --git a/static/dbview.config.php b/static/dbview.config.php index a94f4c74c..1a9f194b8 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -190,7 +190,7 @@ LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id` LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin` LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` - STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` AND `parent-item`.`uid` = `item`.`uid` + STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`uri-id` = `item`.`parent-uri-id` STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`" ], "category-view" => [ From 3d4a1a8083c43084dfa4e6954c835038e94b85cf Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 12:12:26 +0000 Subject: [PATCH 3/6] Added "parent-uri-id" to the test data --- tests/datasets/api.fixture.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 93b896ebf..ca3fc3d1d 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -193,6 +193,7 @@ return [ 'unseen' => 1, 'body' => 'Parent status', 'parent' => 1, + 'parent-uri-id' => 1, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, 'starred' => 1, @@ -217,6 +218,7 @@ return [ 'unseen' => 0, 'body' => 'Reply', 'parent' => 1, + 'parent-uri-id' => 1, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, 'starred' => 0, @@ -238,6 +240,7 @@ return [ 'unseen' => 0, 'body' => 'Other user status', 'parent' => 3, + 'parent-uri-id' => 3, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -258,6 +261,7 @@ return [ 'unseen' => 0, 'body' => 'Friend user reply', 'parent' => 1, + 'parent-uri-id' => 1, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -279,6 +283,7 @@ return [ 'unseen' => 0, 'body' => '[share]Shared status[/share]', 'parent' => 1, + 'parent-uri-id' => 1, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -303,6 +308,7 @@ return [ 'unseen' => 0, 'body' => 'Friend user status', 'parent' => 6, + 'parent-uri-id' => 6, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, From 5adc150d7ae66f26ed19f5a2281182ae99e6c4ae Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 12:49:53 +0000 Subject: [PATCH 4/6] Avoid using the item table/improved error messages --- include/api.php | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/include/api.php b/include/api.php index d55f7ce7a..31cffc180 100644 --- a/include/api.php +++ b/include/api.php @@ -1881,14 +1881,14 @@ function api_statuses_show($type) $conversation = !empty($_REQUEST['conversation']); // try to fetch the item for the local user - or the public item, if there is no local one - $uri_item = Post::selectFirst(['uri'], ['id' => $id]); + $uri_item = Post::selectFirst(['uri-id'], ['id' => $id]); if (!DBA::isResult($uri_item)) { - throw new BadRequestException("There is no status with this id."); + throw new BadRequestException(sprintf("There is no status with the id %d", $id)); } - $item = Post::selectFirst(['id'], ['uri' => $uri_item['uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]); + $item = Post::selectFirst(['id'], ['uri-id' => $uri_item['uri-id'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]); if (!DBA::isResult($item)) { - throw new BadRequestException("There is no status with this id."); + throw new BadRequestException(sprintf("There is no status with the uri-id %d for the given user.", $uri_item['uri-id'])); } $id = $item['id']; @@ -1905,7 +1905,7 @@ function api_statuses_show($type) /// @TODO How about copying this to above methods which don't check $r ? if (!DBA::isResult($statuses)) { - throw new BadRequestException("There is no status with this id."); + throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id)); } $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); @@ -2171,35 +2171,28 @@ function api_statuses_mentions($type) $start = max(0, ($page - 1) * $count); - $query = "SELECT `item`.`id` FROM `user-item` - INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` AND `item`.`gravity` IN (?, ?) - WHERE (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`) AND - `user-item`.`uid` = ? AND `user-item`.`notification-type` & ? != 0 - AND `user-item`.`iid` > ?"; $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(), UserItem::NOTIF_EXPLICIT_TAGGED | UserItem::NOTIF_IMPLICIT_TAGGED | UserItem::NOTIF_THREAD_COMMENT | UserItem::NOTIF_DIRECT_COMMENT | UserItem::NOTIF_DIRECT_THREAD_COMMENT, $since_id]; + $query = "`gravity` IN (?, ?) AND `id` IN (SELECT `iid` FROM `user-item` + WHERE (`hidden` IS NULL OR NOT `hidden`) AND + `uid` = ? AND `notification-type` & ? != 0 + AND `iid` > ?"; + if ($max_id > 0) { - $query .= " AND `item`.`id` <= ?"; + $query .= " AND `iid` <= ?"; $condition[] = $max_id; } - $query .= " ORDER BY `user-item`.`iid` DESC LIMIT ?, ?"; - $condition[] = $start; - $condition[] = $count; + $query .= ")"; - $useritems = DBA::p($query, $condition); - $itemids = []; - while ($useritem = DBA::fetch($useritems)) { - $itemids[] = $useritem['id']; - } - DBA::close($useritems); + $condition = array_merge([$query], $condition); $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; - $statuses = Post::selectForUser(api_user(), [], ['id' => $itemids], $params); + $statuses = Post::selectForUser(api_user(), [], $condition, $params); $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type); @@ -2875,7 +2868,7 @@ function api_format_items_activities($item, $type = "json") $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri'], 'gravity' => GRAVITY_ACTIVITY]; $ret = Post::selectForUser($item['uid'], ['author-id', 'verb'], $condition); - while ($parent_item = Item::fetch($ret)) { + while ($parent_item = Post::fetch($ret)) { // not used as result should be structured like other user data //builtin_activity_puller($i, $activities); From 0c8bb0bc18a1cc8b381b077618d0818d9cd6dd85 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 13:10:32 +0000 Subject: [PATCH 5/6] Use a better function to add the element to the beginning of the array --- include/api.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/api.php b/include/api.php index 31cffc180..63df897ee 100644 --- a/include/api.php +++ b/include/api.php @@ -2171,17 +2171,17 @@ function api_statuses_mentions($type) $start = max(0, ($page - 1) * $count); + $query = "`gravity` IN (?, ?) AND `id` IN (SELECT `iid` FROM `user-item` + WHERE (`hidden` IS NULL OR NOT `hidden`) AND + `uid` = ? AND `notification-type` & ? != 0 + AND `iid` > ?"; + $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(), UserItem::NOTIF_EXPLICIT_TAGGED | UserItem::NOTIF_IMPLICIT_TAGGED | UserItem::NOTIF_THREAD_COMMENT | UserItem::NOTIF_DIRECT_COMMENT | UserItem::NOTIF_DIRECT_THREAD_COMMENT, $since_id]; - $query = "`gravity` IN (?, ?) AND `id` IN (SELECT `iid` FROM `user-item` - WHERE (`hidden` IS NULL OR NOT `hidden`) AND - `uid` = ? AND `notification-type` & ? != 0 - AND `iid` > ?"; - if ($max_id > 0) { $query .= " AND `iid` <= ?"; $condition[] = $max_id; @@ -2189,7 +2189,7 @@ function api_statuses_mentions($type) $query .= ")"; - $condition = array_merge([$query], $condition); + array_unshift($condition, $query); $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser(api_user(), [], $condition, $params); From d78892fc1bbbece820ab2ac5be766b59dde7a54d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Jan 2021 14:57:55 +0000 Subject: [PATCH 6/6] Improved API datasets --- include/api.php | 4 ++-- tests/datasets/api.fixture.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index 63df897ee..ccb6f718e 100644 --- a/include/api.php +++ b/include/api.php @@ -1964,12 +1964,12 @@ function api_conversation_show($type) Logger::info(API_LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]); // try to fetch the item for the local user - or the public item, if there is no local one - $item = Post::selectFirst(['parent-uri'], ['id' => $id]); + $item = Post::selectFirst(['parent-uri-id'], ['id' => $id]); if (!DBA::isResult($item)) { throw new BadRequestException("There is no status with this id."); } - $parent = Post::selectFirst(['id'], ['uri' => $item['parent-uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]); + $parent = Post::selectFirst(['id'], ['uri-id' => $item['parent-uri-id'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]); if (!DBA::isResult($parent)) { throw new BadRequestException("There is no status with this id."); } diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index ca3fc3d1d..f8c696c87 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -194,6 +194,8 @@ return [ 'body' => 'Parent status', 'parent' => 1, 'parent-uri-id' => 1, + 'thr-parent-id' => 1, + 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, 'starred' => 1, @@ -219,6 +221,8 @@ return [ 'body' => 'Reply', 'parent' => 1, 'parent-uri-id' => 1, + 'thr-parent-id' => 1, + 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/selfcontact', 'wall' => 1, 'starred' => 0, @@ -241,6 +245,8 @@ return [ 'body' => 'Other user status', 'parent' => 3, 'parent-uri-id' => 3, + 'thr-parent-id' => 3, + 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -262,6 +268,8 @@ return [ 'body' => 'Friend user reply', 'parent' => 1, 'parent-uri-id' => 1, + 'thr-parent-id' => 1, + 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -284,6 +292,8 @@ return [ 'body' => '[share]Shared status[/share]', 'parent' => 1, 'parent-uri-id' => 1, + 'thr-parent-id' => 1, + 'gravity' => GRAVITY_COMMENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0, @@ -309,6 +319,8 @@ return [ 'body' => 'Friend user status', 'parent' => 6, 'parent-uri-id' => 6, + 'thr-parent-id' => 6, + 'gravity' => GRAVITY_PARENT, 'author-link' => 'http://localhost/profile/othercontact', 'wall' => 1, 'starred' => 0,