API: (hopefully) improves speed of the public timeline

This commit is contained in:
Michael 2021-06-12 15:19:56 +00:00
parent cd8ce7eada
commit 24cc82f396
6 changed files with 124 additions and 11 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.06-rc (Siberian Iris) -- Friendica 2021.06-rc (Siberian Iris)
-- DB_UPDATE_VERSION 1421 -- DB_UPDATE_VERSION 1422
-- ------------------------------------------ -- ------------------------------------------
@ -1874,7 +1874,9 @@ CREATE VIEW `post-thread-user-view` AS SELECT
`parent-post`.`author-id` AS `parent-author-id`, `parent-post`.`author-id` AS `parent-author-id`,
`parent-post-author`.`url` AS `parent-author-link`, `parent-post-author`.`url` AS `parent-author-link`,
`parent-post-author`.`name` AS `parent-author-name`, `parent-post-author`.`name` AS `parent-author-name`,
`parent-post-author`.`network` AS `parent-author-network` `parent-post-author`.`network` AS `parent-author-network`,
`parent-post-author`.`blocked` AS `parent-author-blocked`,
`parent-post-author`.`hidden` AS `parent-author-hidden`
FROM `post-thread-user` FROM `post-thread-user`
INNER JOIN `post-user` ON `post-user`.`id` = `post-thread-user`.`post-user-id` INNER JOIN `post-user` ON `post-user`.`id` = `post-thread-user`.`post-user-id`
STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-thread-user`.`contact-id` STRAIGHT_JOIN `contact` ON `contact`.`id` = `post-thread-user`.`contact-id`
@ -1938,6 +1940,31 @@ CREATE VIEW `post-view` AS SELECT
`post-content`.`target-type` AS `target-type`, `post-content`.`target-type` AS `target-type`,
`post-content`.`target` AS `target`, `post-content`.`target` AS `target`,
`post-content`.`resource-id` AS `resource-id`, `post-content`.`resource-id` AS `resource-id`,
`post`.`author-id` AS `contact-id`,
`author`.`url` AS `contact-link`,
`author`.`addr` AS `contact-addr`,
`author`.`name` AS `contact-name`,
`author`.`nick` AS `contact-nick`,
`author`.`thumb` AS `contact-avatar`,
`author`.`network` AS `contact-network`,
`author`.`blocked` AS `contact-blocked`,
`author`.`hidden` AS `contact-hidden`,
`author`.`readonly` AS `contact-readonly`,
`author`.`archive` AS `contact-archive`,
`author`.`pending` AS `contact-pending`,
`author`.`rel` AS `contact-rel`,
`author`.`uid` AS `contact-uid`,
`author`.`contact-type` AS `contact-contact-type`,
IF (`post`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `author`.`writable`) AS `writable`,
false AS `self`,
`author`.`id` AS `cid`,
`author`.`alias` AS `alias`,
`author`.`photo` AS `photo`,
`author`.`name-date` AS `name-date`,
`author`.`uri-date` AS `uri-date`,
`author`.`avatar-date` AS `avatar-date`,
`author`.`thumb` AS `thumb`,
`author`.`dfrn-id` AS `dfrn-id`,
`post`.`author-id` AS `author-id`, `post`.`author-id` AS `author-id`,
`author`.`url` AS `author-link`, `author`.`url` AS `author-link`,
`author`.`addr` AS `author-addr`, `author`.`addr` AS `author-addr`,
@ -1973,7 +2000,9 @@ CREATE VIEW `post-view` AS SELECT
`parent-post`.`author-id` AS `parent-author-id`, `parent-post`.`author-id` AS `parent-author-id`,
`parent-post-author`.`url` AS `parent-author-link`, `parent-post-author`.`url` AS `parent-author-link`,
`parent-post-author`.`name` AS `parent-author-name`, `parent-post-author`.`name` AS `parent-author-name`,
`parent-post-author`.`network` AS `parent-author-network` `parent-post-author`.`network` AS `parent-author-network`,
`parent-post-author`.`blocked` AS `parent-author-blocked`,
`parent-post-author`.`hidden` AS `parent-author-hidden`
FROM `post` FROM `post`
STRAIGHT_JOIN `post-thread` ON `post-thread`.`uri-id` = `post`.`parent-uri-id` STRAIGHT_JOIN `post-thread` ON `post-thread`.`uri-id` = `post`.`parent-uri-id`
STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post`.`author-id` STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post`.`author-id`
@ -2033,6 +2062,31 @@ CREATE VIEW `post-thread-view` AS SELECT
`post-content`.`target-type` AS `target-type`, `post-content`.`target-type` AS `target-type`,
`post-content`.`target` AS `target`, `post-content`.`target` AS `target`,
`post-content`.`resource-id` AS `resource-id`, `post-content`.`resource-id` AS `resource-id`,
`post-thread`.`author-id` AS `contact-id`,
`author`.`url` AS `contact-link`,
`author`.`addr` AS `contact-addr`,
`author`.`name` AS `contact-name`,
`author`.`nick` AS `contact-nick`,
`author`.`thumb` AS `contact-avatar`,
`author`.`network` AS `contact-network`,
`author`.`blocked` AS `contact-blocked`,
`author`.`hidden` AS `contact-hidden`,
`author`.`readonly` AS `contact-readonly`,
`author`.`archive` AS `contact-archive`,
`author`.`pending` AS `contact-pending`,
`author`.`rel` AS `contact-rel`,
`author`.`uid` AS `contact-uid`,
`author`.`contact-type` AS `contact-contact-type`,
IF (`post`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `author`.`writable`) AS `writable`,
false AS `self`,
`author`.`id` AS `cid`,
`author`.`alias` AS `alias`,
`author`.`photo` AS `photo`,
`author`.`name-date` AS `name-date`,
`author`.`uri-date` AS `uri-date`,
`author`.`avatar-date` AS `avatar-date`,
`author`.`thumb` AS `thumb`,
`author`.`dfrn-id` AS `dfrn-id`,
`post-thread`.`author-id` AS `author-id`, `post-thread`.`author-id` AS `author-id`,
`author`.`url` AS `author-link`, `author`.`url` AS `author-link`,
`author`.`addr` AS `author-addr`, `author`.`addr` AS `author-addr`,
@ -2068,7 +2122,9 @@ CREATE VIEW `post-thread-view` AS SELECT
`parent-post`.`author-id` AS `parent-author-id`, `parent-post`.`author-id` AS `parent-author-id`,
`parent-post-author`.`url` AS `parent-author-link`, `parent-post-author`.`url` AS `parent-author-link`,
`parent-post-author`.`name` AS `parent-author-name`, `parent-post-author`.`name` AS `parent-author-name`,
`parent-post-author`.`network` AS `parent-author-network` `parent-post-author`.`network` AS `parent-author-network`,
`parent-post-author`.`blocked` AS `parent-author-blocked`,
`parent-post-author`.`hidden` AS `parent-author-hidden`
FROM `post-thread` FROM `post-thread`
INNER JOIN `post` ON `post`.`uri-id` = `post-thread`.`uri-id` INNER JOIN `post` ON `post`.`uri-id` = `post-thread`.`uri-id`
STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post-thread`.`author-id` STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `post-thread`.`author-id`

View file

@ -342,13 +342,14 @@ class Post
* @param array $selected Array of selected fields, empty for all * @param array $selected Array of selected fields, empty for all
* @param array $condition Array of fields for condition * @param array $condition Array of fields for condition
* @param array $params Array of several parameters * @param array $params Array of several parameters
* @param bool $user_mode true = post-user-view, false = post-view
* *
* @return boolean|object * @return boolean|object
* @throws \Exception * @throws \Exception
*/ */
public static function selectForUser($uid, array $selected = [], array $condition = [], $params = []) public static function selectForUser($uid, array $selected = [], array $condition = [], $params = [], bool $user_mode = true)
{ {
return self::selectViewForUser('post-user-view', $uid, $selected, $condition, $params); return self::selectViewForUser($user_mode ? 'post-user-view' : 'post-view', $uid, $selected, $condition, $params);
} }
/** /**

View file

@ -52,7 +52,7 @@ class Preferences extends BaseApi
$sensitive = false; $sensitive = false;
$language = $user['language']; $language = $user['language'];
$media = DI::pConfig()->get($uid, 'nsfw', 'disable') ? 'show_all' : 'default'; $media = DI::pConfig()->get($uid, 'nsfw', 'disable') ? 'show_all' : 'default';
$spoilers = DI::pConfig()->get($uid, 'system', 'disable_cw'); $spoilers = (bool)DI::pConfig()->get($uid, 'system', 'disable_cw');
$preferences = new \Friendica\Object\Api\Mastodon\Preferences($visibility, $sensitive, $language, $media, $spoilers); $preferences = new \Friendica\Object\Api\Mastodon\Preferences($visibility, $sensitive, $language, $media, $spoilers);

View file

@ -58,7 +58,7 @@ class PublicTimeline extends BaseApi
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC, $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC,
'uid' => 0, 'network' => Protocol::FEDERATED, 'parent-author-blocked' => false, 'parent-author-hidden' => false]; 'network' => Protocol::FEDERATED, 'parent-author-blocked' => false, 'parent-author-hidden' => false];
if ($request['local']) { if ($request['local']) {
$condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
@ -95,11 +95,11 @@ class PublicTimeline extends BaseApi
["NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `parent-author-id` AND (`blocked` OR `ignored`))", $uid]); ["NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `parent-author-id` AND (`blocked` OR `ignored`))", $uid]);
} }
$items = Post::selectForUser($uid, ['uri-id', 'uid'], $condition, $params); $items = Post::selectForUser($uid, ['uri-id'], $condition, $params, false);
$statuses = []; $statuses = [];
while ($item = Post::fetch($items)) { while ($item = Post::fetch($items)) {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $item['uid']); $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
} }
DBA::close($items); DBA::close($items);

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1421); define('DB_UPDATE_VERSION', 1422);
} }
return [ return [

View file

@ -356,6 +356,8 @@
"parent-author-link" => ["parent-post-author", "url"], "parent-author-link" => ["parent-post-author", "url"],
"parent-author-name" => ["parent-post-author", "name"], "parent-author-name" => ["parent-post-author", "name"],
"parent-author-network" => ["parent-post-author", "network"], "parent-author-network" => ["parent-post-author", "network"],
"parent-author-blocked" => ["parent-post-author", "blocked"],
"parent-author-hidden" => ["parent-post-author", "hidden"],
], ],
"query" => "FROM `post-thread-user` "query" => "FROM `post-thread-user`
INNER JOIN `post-user` ON `post-user`.`id` = `post-thread-user`.`post-user-id` INNER JOIN `post-user` ON `post-user`.`id` = `post-thread-user`.`post-user-id`
@ -417,6 +419,31 @@
"target-type" => ["post-content", "target-type"], "target-type" => ["post-content", "target-type"],
"target" => ["post-content", "target"], "target" => ["post-content", "target"],
"resource-id" => ["post-content", "resource-id"], "resource-id" => ["post-content", "resource-id"],
"contact-id" => ["post", "author-id"],
"contact-link" => ["author", "url"],
"contact-addr" => ["author", "addr"],
"contact-name" => ["author", "name"],
"contact-nick" => ["author", "nick"],
"contact-avatar" => ["author", "thumb"],
"contact-network" => ["author", "network"],
"contact-blocked" => ["author", "blocked"],
"contact-hidden" => ["author", "hidden"],
"contact-readonly" => ["author", "readonly"],
"contact-archive" => ["author", "archive"],
"contact-pending" => ["author", "pending"],
"contact-rel" => ["author", "rel"],
"contact-uid" => ["author", "uid"],
"contact-contact-type" => ["author", "contact-type"],
"writable" => "IF (`post`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `author`.`writable`)",
"self" => "false",
"cid" => ["author", "id"],
"alias" => ["author", "alias"],
"photo" => ["author", "photo"],
"name-date" => ["author", "name-date"],
"uri-date" => ["author", "uri-date"],
"avatar-date" => ["author", "avatar-date"],
"thumb" => ["author", "thumb"],
"dfrn-id" => ["author", "dfrn-id"],
"author-id" => ["post", "author-id"], "author-id" => ["post", "author-id"],
"author-link" => ["author", "url"], "author-link" => ["author", "url"],
"author-addr" => ["author", "addr"], "author-addr" => ["author", "addr"],
@ -453,6 +480,8 @@
"parent-author-link" => ["parent-post-author", "url"], "parent-author-link" => ["parent-post-author", "url"],
"parent-author-name" => ["parent-post-author", "name"], "parent-author-name" => ["parent-post-author", "name"],
"parent-author-network" => ["parent-post-author", "network"], "parent-author-network" => ["parent-post-author", "network"],
"parent-author-blocked" => ["parent-post-author", "blocked"],
"parent-author-hidden" => ["parent-post-author", "hidden"],
], ],
"query" => "FROM `post` "query" => "FROM `post`
STRAIGHT_JOIN `post-thread` ON `post-thread`.`uri-id` = `post`.`parent-uri-id` STRAIGHT_JOIN `post-thread` ON `post-thread`.`uri-id` = `post`.`parent-uri-id`
@ -510,6 +539,31 @@
"target-type" => ["post-content", "target-type"], "target-type" => ["post-content", "target-type"],
"target" => ["post-content", "target"], "target" => ["post-content", "target"],
"resource-id" => ["post-content", "resource-id"], "resource-id" => ["post-content", "resource-id"],
"contact-id" => ["post-thread", "author-id"],
"contact-link" => ["author", "url"],
"contact-addr" => ["author", "addr"],
"contact-name" => ["author", "name"],
"contact-nick" => ["author", "nick"],
"contact-avatar" => ["author", "thumb"],
"contact-network" => ["author", "network"],
"contact-blocked" => ["author", "blocked"],
"contact-hidden" => ["author", "hidden"],
"contact-readonly" => ["author", "readonly"],
"contact-archive" => ["author", "archive"],
"contact-pending" => ["author", "pending"],
"contact-rel" => ["author", "rel"],
"contact-uid" => ["author", "uid"],
"contact-contact-type" => ["author", "contact-type"],
"writable" => "IF (`post`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `author`.`writable`)",
"self" => "false",
"cid" => ["author", "id"],
"alias" => ["author", "alias"],
"photo" => ["author", "photo"],
"name-date" => ["author", "name-date"],
"uri-date" => ["author", "uri-date"],
"avatar-date" => ["author", "avatar-date"],
"thumb" => ["author", "thumb"],
"dfrn-id" => ["author", "dfrn-id"],
"author-id" => ["post-thread", "author-id"], "author-id" => ["post-thread", "author-id"],
"author-link" => ["author", "url"], "author-link" => ["author", "url"],
"author-addr" => ["author", "addr"], "author-addr" => ["author", "addr"],
@ -546,6 +600,8 @@
"parent-author-link" => ["parent-post-author", "url"], "parent-author-link" => ["parent-post-author", "url"],
"parent-author-name" => ["parent-post-author", "name"], "parent-author-name" => ["parent-post-author", "name"],
"parent-author-network" => ["parent-post-author", "network"], "parent-author-network" => ["parent-post-author", "network"],
"parent-author-blocked" => ["parent-post-author", "blocked"],
"parent-author-hidden" => ["parent-post-author", "hidden"],
], ],
"query" => "FROM `post-thread` "query" => "FROM `post-thread`
INNER JOIN `post` ON `post`.`uri-id` = `post-thread`.`uri-id` INNER JOIN `post` ON `post`.`uri-id` = `post-thread`.`uri-id`