Rewrote Satori recent forum post fetch.

This commit is contained in:
flash 2023-09-06 19:35:50 +00:00
parent 9b2c409a24
commit 73e4597e16
3 changed files with 48 additions and 47 deletions

View file

@ -1189,14 +1189,14 @@ class Forum {
ForumTopicInfo|string|null $topicInfo = null, ForumTopicInfo|string|null $topicInfo = null,
UserInfo|string|null $userInfo = null, UserInfo|string|null $userInfo = null,
ForumPostInfo|string|null $upToPostInfo = null, ForumPostInfo|string|null $upToPostInfo = null,
ForumPostInfo|string|null $afterPostInfo = null,
?int $newerThanDays = null,
?array $searchQuery = null, ?array $searchQuery = null,
?bool $deleted = null, ?bool $deleted = null,
?Pagination $pagination = null ?Pagination $pagination = null
): array { ): array {
// remove this hack when search server // remove this hack when search server
$hasSearchQuery = $searchQuery !== null; $hasSearchQuery = $searchQuery !== null;
$hasAfterPostId = false;
$afterPostId = null;
$doSearchOrder = false; $doSearchOrder = false;
if($hasSearchQuery) { if($hasSearchQuery) {
if(!empty($searchQuery['type']) if(!empty($searchQuery['type'])
@ -1204,17 +1204,18 @@ class Forum {
&& $searchQuery['type'] !== 'forum:post') && $searchQuery['type'] !== 'forum:post')
return []; return [];
$userInfo = null;
$deleted = false; $deleted = false;
$pagination = null; $pagination = null;
$doSearchOrder = true; $doSearchOrder = true;
$afterPostInfo = null;
$newerThanDays = null;
if(!empty($searchQuery['author'])) if(!empty($searchQuery['author']))
$userInfo = $searchQuery['author']; $userInfo = $searchQuery['author'];
if(!empty($searchQuery['after'])) { if(!empty($searchQuery['after']))
$hasAfterPostId = true; $afterPostInfo = $searchQuery['after'];
$afterPostId = $searchQuery['after'];
}
$searchQuery = $searchQuery['query_string']; $searchQuery = $searchQuery['query_string'];
$hasSearchQuery = !empty($searchQuery); $hasSearchQuery = !empty($searchQuery);
@ -1228,11 +1229,15 @@ class Forum {
$userInfo = $userInfo->getId(); $userInfo = $userInfo->getId();
if($upToPostInfo instanceof ForumPostInfo) if($upToPostInfo instanceof ForumPostInfo)
$upToPostInfo = $upToPostInfo->getId(); $upToPostInfo = $upToPostInfo->getId();
if($afterPostInfo instanceof ForumPostInfo)
$afterPostInfo = $afterPostInfo->getId();
$hasCategoryInfo = $categoryInfo !== null; $hasCategoryInfo = $categoryInfo !== null;
$hasTopicInfo = $topicInfo !== null; $hasTopicInfo = $topicInfo !== null;
$hasUserInfo = $userInfo !== null; $hasUserInfo = $userInfo !== null;
$hasUpToPostInfo = $upToPostInfo !== null; $hasUpToPostInfo = $upToPostInfo !== null;
$hasAfterPostInfo = $afterPostInfo !== null;
$hasNewerThanDays = $newerThanDays !== null;
$hasDeleted = $deleted !== null; $hasDeleted = $deleted !== null;
$hasPagination = $pagination !== null; $hasPagination = $pagination !== null;
@ -1251,8 +1256,10 @@ class Forum {
$query .= sprintf(' %s user_id = ?', ++$args > 1 ? 'AND' : 'WHERE'); $query .= sprintf(' %s user_id = ?', ++$args > 1 ? 'AND' : 'WHERE');
if($hasUpToPostInfo) if($hasUpToPostInfo)
$query .= sprintf(' %s post_id < ?', ++$args > 1 ? 'AND' : 'WHERE'); $query .= sprintf(' %s post_id < ?', ++$args > 1 ? 'AND' : 'WHERE');
if($hasAfterPostId) if($hasAfterPostInfo)
$query .= sprintf(' %s post_id > ?', ++$args > 1 ? 'AND' : 'WHERE'); $query .= sprintf(' %s post_id > ?', ++$args > 1 ? 'AND' : 'WHERE');
if($hasNewerThanDays)
$query .= sprintf(' %s post_created > NOW() - INTERVAL ? DAY', ++$args > 1 ? 'AND' : 'WHERE');
if($hasSearchQuery) if($hasSearchQuery)
$query .= sprintf(' %s MATCH(post_text) AGAINST (? IN NATURAL LANGUAGE MODE)', ++$args > 1 ? 'AND' : 'WHERE'); $query .= sprintf(' %s MATCH(post_text) AGAINST (? IN NATURAL LANGUAGE MODE)', ++$args > 1 ? 'AND' : 'WHERE');
if($hasDeleted) if($hasDeleted)
@ -1280,8 +1287,10 @@ class Forum {
$stmt->addParameter(++$args, $userInfo); $stmt->addParameter(++$args, $userInfo);
if($hasUpToPostInfo) if($hasUpToPostInfo)
$stmt->addParameter(++$args, $upToPostInfo); $stmt->addParameter(++$args, $upToPostInfo);
if($hasAfterPostId) if($hasAfterPostInfo)
$stmt->addParameter(++$args, $afterPostId); $stmt->addParameter(++$args, $afterPostInfo);
if($hasNewerThanDays)
$stmt->addParameter(++$args, $newerThanDays);
if($hasSearchQuery) if($hasSearchQuery)
$stmt->addParameter(++$args, $searchQuery); $stmt->addParameter(++$args, $searchQuery);
if($hasPagination) { if($hasPagination) {

View file

@ -341,10 +341,10 @@ class MisuzuContext {
)); ));
$this->router->register(new SatoriRoutes( $this->router->register(new SatoriRoutes(
$this->dbConn,
$this->config->scopeTo('satori'), $this->config->scopeTo('satori'),
$this->usersCtx, $this->usersCtx,
$this->profileFields $this->profileFields,
$this->forum
)); ));
// below is still only otherwise available as stinky php files // below is still only otherwise available as stinky php files

View file

@ -2,22 +2,22 @@
namespace Misuzu\Satori; namespace Misuzu\Satori;
use RuntimeException; use RuntimeException;
use Index\Data\DbTools; use Index\Colour\Colour;
use Index\Data\IDbConnection;
use Index\Http\HttpFx; use Index\Http\HttpFx;
use Index\Routing\IRouter; use Index\Routing\IRouter;
use Index\Routing\IRouteHandler; use Index\Routing\IRouteHandler;
use Misuzu\Pagination; use Misuzu\Pagination;
use Misuzu\Config\IConfig; use Misuzu\Config\IConfig;
use Misuzu\Forum\Forum;
use Misuzu\Profile\ProfileFields; use Misuzu\Profile\ProfileFields;
use Misuzu\Users\UsersContext; use Misuzu\Users\UsersContext;
final class SatoriRoutes implements IRouteHandler { final class SatoriRoutes implements IRouteHandler {
public function __construct( public function __construct(
private IDbConnection $dbConn,
private IConfig $config, private IConfig $config,
private UsersContext $usersCtx, private UsersContext $usersCtx,
private ProfileFields $profileFields private ProfileFields $profileFields,
private Forum $forum
) {} ) {}
public function registerRoutes(IRouter $router): void { public function registerRoutes(IRouter $router): void {
@ -90,42 +90,34 @@ final class SatoriRoutes implements IRouteHandler {
$backlogDays = $this->config->getInteger('forum.backlog', 7); $backlogDays = $this->config->getInteger('forum.backlog', 7);
$startId = (string)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT); $startId = (string)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT);
$args = 0;
$stmt = $this->dbConn->prepare(sprintf(
'SELECT fp.post_id, ft.topic_id, ft.topic_title, fc.forum_id, fc.forum_name, u.user_id, u.username,'
. ' COALESCE(u.user_colour, r.role_colour), (SELECT MIN(post_id) = fp.post_id FROM msz_forum_posts WHERE topic_id = fp.topic_id)'
. ' FROM msz_forum_posts AS fp'
. ' LEFT JOIN msz_users AS u ON u.user_id = fp.user_id'
. ' LEFT JOIN msz_roles AS r ON r.role_id = u.display_role'
. ' LEFT JOIN msz_forum_topics AS ft ON ft.topic_id = fp.topic_id'
. ' LEFT JOIN msz_forum_categories AS fc ON fc.forum_id = fp.forum_id'
. ' WHERE post_id > ? AND post_deleted IS NULL AND post_created >= NOW() - INTERVAL ? DAY'
. ' AND fp.forum_id IN (%s)'
. ' ORDER BY post_id LIMIT ?',
DbTools::prepareListString($categoryIds)
));
$stmt->addParameter(++$args, $startId);
$stmt->addParameter(++$args, $backlogDays);
foreach($categoryIds as $categoryId)
$stmt->addParameter(++$args, $categoryId);
$stmt->addParameter(++$args, $batchSize);
$stmt->execute();
$posts = []; $posts = [];
$result = $stmt->getResult(); $postInfos = $this->forum->getPosts(
categoryInfo: $categoryIds,
afterPostInfo: $startId,
newerThanDays: $backlogDays,
pagination: new Pagination($batchSize),
deleted: false,
);
foreach($postInfos as $postInfo) {
$topicInfo = $this->forum->getTopic(postInfo: $postInfo);
$firstPostInfo = $this->forum->getPost(topicInfo: $topicInfo);
$categoryInfo = $this->forum->getCategory(topicInfo: $topicInfo);
$userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null;
$userColour = $this->usersCtx->getUserColour($userInfo);
while($result->next())
$posts[] = [ $posts[] = [
'post_id' => $result->getInteger(0), 'post_id' => (int)$postInfo->getId(),
'topic_id' => $result->getInteger(1), 'topic_id' => (int)$topicInfo->getId(),
'topic_title' => $result->getString(2), 'topic_title' => $topicInfo->getTitle(),
'forum_id' => $result->getInteger(3), 'forum_id' => (int)$categoryInfo->getId(),
'forum_name' => $result->getString(4), 'forum_name' => $categoryInfo->getName(),
'user_id' => $result->getInteger(5), 'user_id' => (int)$userInfo->getId(),
'username' => $result->getString(6), 'username' => $userInfo->getName(),
'user_colour' => $result->getInteger(7), 'user_colour' => Colour::toMisuzu($userColour),
'is_opening_post' => $result->getInteger(8), 'is_opening_post' => $postInfo->getId() === $firstPostInfo->getId(),
]; ];
}
return $posts; return $posts;
} }