Skip to content

Commit

Permalink
Merge pull request #3410 from NamelessMC/bugfix/forum-index-showing-t…
Browse files Browse the repository at this point in the history
…opics-without-view-other-topics-permission

Fix forum index showing topics without view other topics permission
  • Loading branch information
samerton authored Aug 2, 2023
2 parents 0396359 + 955b4a1 commit 3cbca4a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 34 deletions.
98 changes: 65 additions & 33 deletions modules/Forum/classes/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ public function listAllForums(array $groups = [0], int $user_id = 0): array {
// Get discussion forums
$forums = $this->_db->query(
<<<SQL
SELECT f.*
SELECT
f.*,
EXISTS (
SELECT p.ID
FROM nl2_forums_permissions p
WHERE p.group_id IN ($groups_in)
AND p.forum_id = f.id
AND p.view_other_topics = 1
) view_other_topics
FROM nl2_forums AS f
WHERE f.parent = ?
AND f.id IN
Expand All @@ -88,7 +96,11 @@ public function listAllForums(array $groups = [0], int $user_id = 0): array {
// Get latest post from any sub-subforums
$subforums = $this->getAnySubforums($item->id, $groups, 0, true, $user_id);

$latest_post = [$item->last_post_date, $item->last_user_posted, $item->last_topic_posted];
if ($item->view_other_topics) {
$latest_post = [$item->last_post_date, $item->last_user_posted, $item->last_topic_posted];
} else {
$latest_post = $this->getLatestPostInOwnTopicForum($item->id, $user_id);
}

if (count($subforums)) {
$return[$forum->id]['subforums'][$item->id]->subforums = [];
Expand Down Expand Up @@ -733,41 +745,18 @@ public function getAnySubforums(

foreach ($subforums_query->results() as $result) {
$to_add = new stdClass();
$to_add->id = Output::getClean($result->id);
$to_add->id = $result->id;
$to_add->forum_title = Output::getClean($result->forum_title);
$to_add->icon = Output::getPurified($result->icon);
$to_add->category = false;

// Latest post
if ($onlyOwnTopics && $result->view_other_topics != '1') {
// Get the latest topic that the user can view
$latest_post = $this->_db->query(
<<<SQL
SELECT
p.topic_id,
p.created,
p.post_date,
p.post_creator
FROM nl2_topics t
LEFT JOIN nl2_posts p
ON p.id = (
SELECT id
FROM nl2_posts sp
WHERE sp.topic_id = t.id
AND sp.deleted = 0
ORDER BY sp.created DESC LIMIT 1
)
WHERE t.forum_id = ?
AND (t.topic_creator = ? OR t.sticky = 1)
SQL,
[$result->id, $user_id]
);

if ($latest_post->count() && $latest_post = $latest_post->first()) {
$to_add->last_post_date = $latest_post->created ?? strtotime($latest_post->post_date);
$to_add->last_user_posted = $latest_post->post_creator;
$to_add->last_topic_posted = $latest_post->topic_id;
}
if ($onlyOwnTopics && $result->view_other_topics !== 1) {
[
$to_add->last_post_date,
$to_add->last_user_posted,
$to_add->last_topic_posted,
] = $this->getLatestPostInOwnTopicForum($result->id, $user_id);
} else {
$to_add->last_post_date = $result->last_post_date;
$to_add->last_user_posted = $result->last_user_posted;
Expand All @@ -776,7 +765,7 @@ public function getAnySubforums(

$ret[] = $to_add;

$subforums = $this->getAnySubforums($result->id, $groups, ++$depth);
$subforums = $this->getAnySubforums($result->id, $groups, ++$depth, $onlyOwnTopics, $user_id);

if (count($subforums)) {
foreach ($subforums as $subforum) {
Expand Down Expand Up @@ -808,4 +797,47 @@ public static function getAccessibleLabels(array $labels, array $user_groups): a
return $prev;
}, []);
}

/**
* Get the latest post in a "View own topic" forum
* This could be a topic created by the user, or a sticky topic
*
* @param int $forumId
* @param int $userId
* @return array|null Time of latest post, post creator ID, topic ID
*/
private function getLatestPostInOwnTopicForum(int $forumId, int $userId): ?array {
$latest_post = $this->_db->query(
<<<SQL
SELECT
p.topic_id,
p.created,
p.post_date,
p.post_creator
FROM nl2_topics t
LEFT JOIN nl2_posts p
ON p.id = (
SELECT id
FROM nl2_posts sp
WHERE sp.topic_id = t.id
AND sp.deleted = 0
ORDER BY sp.created DESC LIMIT 1
)
WHERE t.forum_id = ?
AND (t.topic_creator = ? OR t.sticky = 1)
ORDER BY p.created, p.post_date DESC LIMIT 1
SQL,
[$forumId, $userId]
);

if ($latest_post->count() && $latest_post = $latest_post->first()) {
return [
$latest_post->created ?? strtotime($latest_post->post_date),
$latest_post->post_creator,
$latest_post->topic_id,
];
}

return null;
}
}
2 changes: 1 addition & 1 deletion modules/Forum/pages/forum/view_forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
if ($forum->canViewOtherTopics($subforum->id, $user_groups)) {
$latest_post = DB::getInstance()->query('SELECT * FROM nl2_topics WHERE forum_id = ? AND deleted = 0 ORDER BY topic_reply_date DESC', [$subforum->id])->results();
} else {
$latest_post = DB::getInstance()->query('SELECT * FROM nl2_topics WHERE forum_id = ? AND deleted = 0 AND topic_creator = ? ORDER BY topic_reply_date DESC', [$subforum->id, $user_id])->results();
$latest_post = DB::getInstance()->query('SELECT * FROM nl2_topics WHERE forum_id = ? AND deleted = 0 AND (topic_creator = ? OR sticky = 1) ORDER BY topic_reply_date DESC', [$subforum->id, $user_id])->results();
}

$subforum_topics = count($latest_post);
Expand Down

0 comments on commit 3cbca4a

Please sign in to comment.