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

View file

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

View file

@ -2,22 +2,22 @@
namespace Misuzu\Satori;
use RuntimeException;
use Index\Data\DbTools;
use Index\Data\IDbConnection;
use Index\Colour\Colour;
use Index\Http\HttpFx;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Pagination;
use Misuzu\Config\IConfig;
use Misuzu\Forum\Forum;
use Misuzu\Profile\ProfileFields;
use Misuzu\Users\UsersContext;
final class SatoriRoutes implements IRouteHandler {
public function __construct(
private IDbConnection $dbConn,
private IConfig $config,
private UsersContext $usersCtx,
private ProfileFields $profileFields
private ProfileFields $profileFields,
private Forum $forum
) {}
public function registerRoutes(IRouter $router): void {
@ -90,42 +90,34 @@ final class SatoriRoutes implements IRouteHandler {
$backlogDays = $this->config->getInteger('forum.backlog', 7);
$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 = [];
$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[] = [
'post_id' => $result->getInteger(0),
'topic_id' => $result->getInteger(1),
'topic_title' => $result->getString(2),
'forum_id' => $result->getInteger(3),
'forum_name' => $result->getString(4),
'user_id' => $result->getInteger(5),
'username' => $result->getString(6),
'user_colour' => $result->getInteger(7),
'is_opening_post' => $result->getInteger(8),
'post_id' => (int)$postInfo->getId(),
'topic_id' => (int)$topicInfo->getId(),
'topic_title' => $topicInfo->getTitle(),
'forum_id' => (int)$categoryInfo->getId(),
'forum_name' => $categoryInfo->getName(),
'user_id' => (int)$userInfo->getId(),
'username' => $userInfo->getName(),
'user_colour' => Colour::toMisuzu($userColour),
'is_opening_post' => $postInfo->getId() === $firstPostInfo->getId(),
];
}
return $posts;
}