Skip to content

Commit

Permalink
Reduce database queries for shownewposts
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 committed Oct 16, 2023
1 parent 46e8460 commit c70e150
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
23 changes: 14 additions & 9 deletions application/modules/forum/controllers/Shownewposts.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ public function indexAction()
$forums = $forumMapper->getForumItemsUser($this->getUser());
$topics = $topicMapper->getTopicsByForumIds(array_keys($forums));

$topicIds = [];
$topicsToShow = [];
foreach ($topics as $topic) {
if ($isAdmin || $forums[$topic->getForumId()]->getReadAccess()) {
$lastPost = ($this->getUser()) ? $topicMapper->getLastPostByTopicId($topic->getId(), $this->getUser()->getId()) : $topicMapper->getLastPostByTopicId($topic->getId());

if (!$lastPost->getRead()) {
$topicsToShow[] = [
'topic' => $topic,
'forumPrefix' => $forums[$topic->getForumId()]->getPrefix(),
'lastPost' => $lastPost,
];
}
$topicIds[] = $topic->getId();
}
}

$posts = $topicMapper->getLastPostsByTopicIds($topicIds, ($this->getUser()) ? $this->getUser()->getId() : null);

foreach ($posts as $post) {
if (!$post->getRead()) {
$topicsToShow[] = [
'topic' => $topics[$post->getTopicId()],
'forumPrefix' => $forums[$topics[$post->getTopicId()]->getForumId()]->getPrefix(),
'lastPost' => $post,
];
}
}

Expand Down
55 changes: 54 additions & 1 deletion application/modules/forum/mappers/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getTopicsByForumIds(array $ids, Pagination $pagination = null):
$topicModel->setTopicTitle($topicRow['topic_title']);
$topicModel->setDateCreated($topicRow['date_created']);
$topicModel->setCountPosts($topicRow['countPosts']);
$topics[] = $topicModel;
$topics[$topicRow['id']] = $topicModel;
}

return $topics;
Expand Down Expand Up @@ -266,6 +266,59 @@ public function getLastPostByTopicId(int $id, int $userId = null): ?PostModel
return $postModel;
}

/**
* Get last posts by topic ids and user id.
*
* @param array $ids
* @param int|null $userId
* @return PostModel[]|null
* @throws Exception
*/
public function getLastPostsByTopicIds(array $ids, int $userId = null): ?array
{
$select = $this->db()->select(['p.id', 'p.topic_id', 'date_created' => 'MAX(p.date_created)', 'p.user_id', 'p.forum_id'])
->from(['p' => 'forum_posts']);

if ($userId) {
$select->join(['tr' => 'forum_topics_read'], ['tr.user_id' => $userId, 'tr.topic_id = p.topic_id', 'tr.datetime >= p.date_created'], 'LEFT', ['topic_read' => 'tr.datetime'])
->join(['fr' => 'forum_read'], ['fr.user_id' => $userId, 'fr.forum_id = p.forum_id', 'fr.datetime >= p.date_created'], 'LEFT', ['forum_read' => 'fr.datetime']);
}

$lastPostsRows = $select->where(['p.topic_id' => $ids])
->order(['p.date_created' => 'DESC'])
->group(['p.topic_id'])
->execute()
->fetchRows();

if (empty($lastPostsRows)) {
return null;
}

$lastPosts = [];
foreach ($lastPostsRows as $lastPostRow) {
$postModel = new PostModel();
$userMapper = new UserMapper();
$postModel->setId($lastPostRow['id']);
$user = $userMapper->getUserById($lastPostRow['user_id']);

if ($user) {
$postModel->setAutor($user);
} else {
$postModel->setAutor($userMapper->getDummyUser());
}

$postModel->setDateCreated($lastPostRow['date_created']);
$postModel->setTopicId($lastPostRow['topic_id']);

if ($userId) {
$postModel->setRead($lastPostRow['topic_read'] || $lastPostRow['forum_read']);
}
$lastPosts[] = $postModel;
}

return $lastPosts;
}

/**
* Inserts or updates a topic.
*
Expand Down

0 comments on commit c70e150

Please sign in to comment.