From 06faf86d05b5a2de67be90826f45ffd9ad9fb945 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 19 Dec 2024 23:41:52 +0000 Subject: [PATCH] Moved forum category views into the router. --- public-legacy/forum/forum.php | 171 ---------------------------- src/Forum/ForumCategoriesRoutes.php | 163 +++++++++++++++++++++++++- src/LegacyRoutes.php | 10 +- 3 files changed, 170 insertions(+), 174 deletions(-) delete mode 100644 public-legacy/forum/forum.php diff --git a/public-legacy/forum/forum.php b/public-legacy/forum/forum.php deleted file mode 100644 index a0bd412..0000000 --- a/public-legacy/forum/forum.php +++ /dev/null @@ -1,171 +0,0 @@ -forumCtx->categories->getCategory(categoryId: $categoryId); -} catch(RuntimeException $ex) { - Template::throwError(404); -} - -$perms = $msz->authInfo->getPerms('forum', $categoryInfo); - -$currentUser = $msz->authInfo->userInfo; -$currentUserId = $currentUser === null ? '0' : $currentUser->id; - -if(!$perms->check(Perm::F_CATEGORY_VIEW)) - Template::throwError(403); - -if($msz->usersCtx->hasActiveBan($currentUser)) - $perms = $perms->apply(fn($calc) => $calc & (Perm::F_CATEGORY_LIST | Perm::F_CATEGORY_VIEW)); - -if($categoryInfo->isLink) { - $msz->forumCtx->categories->incrementCategoryClicks($categoryInfo); - Tools::redirect($categoryInfo->linkTarget ?? '/'); - return; -} - -$forumPagination = Pagination::fromInput($msz->forumCtx->topics->countTopics( - categoryInfo: $categoryInfo, - global: true, - deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false -), 20); - -if(!$forumPagination->validOffset) - Template::throwError(404); - -$children = []; -$topics = []; - -if($categoryInfo->mayHaveChildren) { - $children = $msz->forumCtx->categories->getCategoryChildren($categoryInfo, hidden: false, asTree: true); - - foreach($children as $childId => $child) { - $childPerms = $msz->authInfo->getPerms('forum', $child->info); - if(!$childPerms->check(Perm::F_CATEGORY_LIST)) { - unset($category->children[$childId]); - continue; - } - - $childUnread = false; - - if($child->info->mayHaveChildren) { - foreach($child->children as $grandChildId => $grandChild) { - $grandChildPerms = $msz->authInfo->getPerms('forum', $grandChild->info); - if(!$grandChildPerms->check(Perm::F_CATEGORY_LIST)) { - unset($child->children[$grandChildId]); - continue; - } - - $grandChildUnread = false; - - if($grandChild->info->mayHaveTopics) { - $catIds = [$grandChild->info->id]; - foreach($grandChild->childIds as $greatGrandChildId) { - $greatGrandChildPerms = $msz->authInfo->getPerms('forum', $greatGrandChildId); - if(!$greatGrandChildPerms->check(Perm::F_CATEGORY_LIST)) - $catIds[] = $greatGrandChildId; - } - - $grandChildUnread = $msz->forumCtx->categories->checkCategoryUnread($catIds, $currentUser); - if($grandChildUnread) - $childUnread = true; - } - - $grandChild->perms = $grandChildPerms; - $grandChild->unread = $grandChildUnread; - } - } - - if($child->info->mayHaveChildren || $child->info->mayHaveTopics) { - $catIds = [$child->info->id]; - foreach($child->childIds as $grandChildId) { - $grandChildPerms = $msz->authInfo->getPerms('forum', $grandChildId); - if($grandChildPerms->check(Perm::F_CATEGORY_LIST)) - $catIds[] = $grandChildId; - } - - try { - $lastPostInfo = $msz->forumCtx->posts->getPost(categoryInfos: $catIds, getLast: true, deleted: false); - } catch(RuntimeException $ex) { - $lastPostInfo = null; - } - - if($lastPostInfo !== null) { - $child->lastPost = new stdClass; - $child->lastPost->info = $lastPostInfo; - $child->lastPost->topicInfo = $msz->forumCtx->topics->getTopic(postInfo: $lastPostInfo); - - if($lastPostInfo->userId !== null) { - $child->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId); - $child->lastPost->colour = $msz->usersCtx->getUserColour($child->lastPost->user); - } - } - } - - if($child->info->mayHaveTopics && !$childUnread) - $childUnread = $msz->forumCtx->categories->checkCategoryUnread($child->info, $currentUser); - - $child->perms = $childPerms; - $child->unread = $childUnread; - } -} - -if($categoryInfo->mayHaveTopics) { - $topicInfos = $msz->forumCtx->topics->getTopics( - categoryInfo: $categoryInfo, - global: true, - deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false, - pagination: $forumPagination, - ); - - foreach($topicInfos as $topicInfo) { - $topics[] = $topic = new stdClass; - $topic->info = $topicInfo; - $topic->unread = $msz->forumCtx->topics->checkTopicUnread($topicInfo, $currentUser); - $topic->participated = $msz->forumCtx->topics->checkTopicParticipated($topicInfo, $currentUser); - - if($topicInfo->userId !== null) { - $topic->user = $msz->usersCtx->getUserInfo($topicInfo->userId); - $topic->colour = $msz->usersCtx->getUserColour($topic->user); - } - - try { - $topic->lastPost = new stdClass; - $topic->lastPost->info = $lastPostInfo = $msz->forumCtx->posts->getPost( - topicInfo: $topicInfo, - getLast: true, - deleted: $topicInfo->deleted ? null : false, - ); - - if($lastPostInfo->userId !== null) { - $topic->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId); - $topic->lastPost->colour = $msz->usersCtx->getUserColour($topic->lastPost->user); - } - } catch(RuntimeException $ex) { - $topic->lastPost = null; - } - } -} - -$perms = $perms->checkMany([ - 'can_create_topic' => Perm::F_TOPIC_CREATE, -]); - -Template::render('forum.forum', [ - 'forum_breadcrumbs' => iterator_to_array($msz->forumCtx->categories->getCategoryAncestry($categoryInfo)), - 'global_accent_colour' => $msz->forumCtx->categories->getCategoryColour($categoryInfo), - 'forum_info' => $categoryInfo, - 'forum_children' => $children, - 'forum_topics' => $topics, - 'forum_pagination' => $forumPagination, - 'forum_show_mark_as_read' => $currentUser !== null, - 'forum_perms' => $perms, -]); diff --git a/src/Forum/ForumCategoriesRoutes.php b/src/Forum/ForumCategoriesRoutes.php index 1a9c52e..bc37c43 100644 --- a/src/Forum/ForumCategoriesRoutes.php +++ b/src/Forum/ForumCategoriesRoutes.php @@ -6,7 +6,7 @@ use RuntimeException; use Index\Http\{HttpRequest,HttpResponseBuilder}; use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler,RouteHandlerTrait}; use Index\Urls\{UrlFormat,UrlSource,UrlSourceTrait}; -use Misuzu\{CSRF,Perm,Template}; +use Misuzu\{CSRF,Pagination,Perm,Template}; use Misuzu\Auth\AuthInfo; use Misuzu\Users\UsersContext; @@ -21,6 +21,7 @@ class ForumCategoriesRoutes implements RouteHandler, UrlSource { #[HttpGet('/forum')] #[UrlFormat('forum-index', '/forum')] + #[UrlFormat('forum-category-root', '/forum', fragment: '')] public function getIndex() { $cats = $this->forum->categories->getCategories(hidden: false, asTree: true); @@ -169,6 +170,166 @@ class ForumCategoriesRoutes implements RouteHandler, UrlSource { ]); } + #[HttpGet('/forum/([0-9]+)')] + #[UrlFormat('forum-category', '/forum/', ['p' => ''])] + public function getCategory(HttpResponseBuilder $response, HttpRequest $request, string $catId) { + try { + $category = $this->forum->categories->getCategory(categoryId: $catId); + } catch(RuntimeException $ex) { + return 404; + } + + $perms = $this->authInfo->getPerms('forum', $category); + if(!$perms->check(Perm::F_CATEGORY_VIEW)) + return 403; + + if($category->isLink) { + $this->forum->categories->incrementCategoryClicks($category); + $response->redirect($category->linkTarget ?? '/'); + return; + } + + if($this->usersCtx->hasActiveBan($this->authInfo->userInfo)) + $perms = $perms->apply(fn(int $calc) => $calc & (Perm::F_CATEGORY_LIST | Perm::F_CATEGORY_VIEW)); + + $pagination = Pagination::fromRequest($request, $this->forum->topics->countTopics( + categoryInfo: $category, + global: true, + deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false + ), 20, 'p'); + if(!$pagination->validOffset) + return 404; + + $children = []; + $topics = []; + + if($category->mayHaveChildren) { + $children = $this->forum->categories->getCategoryChildren($category, hidden: false, asTree: true); + + foreach($children as $childId => $child) { + $childPerms = $this->authInfo->getPerms('forum', $child->info); + if(!$childPerms->check(Perm::F_CATEGORY_LIST)) { + unset($category->children[$childId]); + continue; + } + + $childUnread = false; + + if($child->info->mayHaveChildren) { + foreach($child->children as $grandChildId => $grandChild) { + $grandChildPerms = $this->authInfo->getPerms('forum', $grandChild->info); + if(!$grandChildPerms->check(Perm::F_CATEGORY_LIST)) { + unset($child->children[$grandChildId]); + continue; + } + + $grandChildUnread = false; + + if($grandChild->info->mayHaveTopics) { + $catIds = [$grandChild->info->id]; + foreach($grandChild->childIds as $greatGrandChildId) { + $greatGrandChildPerms = $this->authInfo->getPerms('forum', $greatGrandChildId); + if(!$greatGrandChildPerms->check(Perm::F_CATEGORY_LIST)) + $catIds[] = $greatGrandChildId; + } + + $grandChildUnread = $this->forum->categories->checkCategoryUnread($catIds, $this->authInfo->userInfo); + if($grandChildUnread) + $childUnread = true; + } + + $grandChild->perms = $grandChildPerms; + $grandChild->unread = $grandChildUnread; + } + } + + if($child->info->mayHaveChildren || $child->info->mayHaveTopics) { + $catIds = [$child->info->id]; + foreach($child->childIds as $grandChildId) { + $grandChildPerms = $this->authInfo->getPerms('forum', $grandChildId); + if($grandChildPerms->check(Perm::F_CATEGORY_LIST)) + $catIds[] = $grandChildId; + } + + try { + $lastPostInfo = $this->forum->posts->getPost(categoryInfos: $catIds, getLast: true, deleted: false); + } catch(RuntimeException $ex) { + $lastPostInfo = null; + } + + if($lastPostInfo !== null) { + $child->lastPost = new stdClass; + $child->lastPost->info = $lastPostInfo; + $child->lastPost->topicInfo = $this->forum->topics->getTopic(postInfo: $lastPostInfo); + + if($lastPostInfo->userId !== null) { + $child->lastPost->user = $this->usersCtx->getUserInfo($lastPostInfo->userId); + $child->lastPost->colour = $this->usersCtx->getUserColour($child->lastPost->user); + } + } + } + + if($child->info->mayHaveTopics && !$childUnread) + $childUnread = $this->forum->categories->checkCategoryUnread($child->info, $this->authInfo->userInfo); + + $child->perms = $childPerms; + $child->unread = $childUnread; + } + } + + if($category->mayHaveTopics) { + $topicInfos = $this->forum->topics->getTopics( + categoryInfo: $category, + global: true, + deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false, + pagination: $pagination, + ); + + foreach($topicInfos as $topicInfo) { + $topics[] = $topic = new stdClass; + $topic->info = $topicInfo; + $topic->unread = $this->forum->topics->checkTopicUnread($topicInfo, $this->authInfo->userInfo); + $topic->participated = $this->forum->topics->checkTopicParticipated($topicInfo, $this->authInfo->userInfo); + + if($topicInfo->userId !== null) { + $topic->user = $this->usersCtx->getUserInfo($topicInfo->userId); + $topic->colour = $this->usersCtx->getUserColour($topic->user); + } + + try { + $topic->lastPost = new stdClass; + $topic->lastPost->info = $lastPostInfo = $this->forum->posts->getPost( + topicInfo: $topicInfo, + getLast: true, + deleted: $topicInfo->deleted ? null : false, + ); + + if($lastPostInfo->userId !== null) { + $topic->lastPost->user = $this->usersCtx->getUserInfo($lastPostInfo->userId); + $topic->lastPost->colour = $this->usersCtx->getUserColour($topic->lastPost->user); + } + } catch(RuntimeException $ex) { + $topic->lastPost = null; + } + } + } + + $perms = $perms->checkMany([ + 'can_create_topic' => Perm::F_TOPIC_CREATE, + ]); + + return Template::renderRaw('forum.forum', [ + 'forum_breadcrumbs' => iterator_to_array($this->forum->categories->getCategoryAncestry($category)), + 'global_accent_colour' => $this->forum->categories->getCategoryColour($category), + 'forum_info' => $category, + 'forum_children' => $children, + 'forum_topics' => $topics, + 'forum_pagination' => $pagination, + 'forum_show_mark_as_read' => $this->authInfo->isLoggedIn, + 'forum_perms' => $perms, + ]); + } + #[HttpPost('/forum/mark-as-read')] #[UrlFormat('forum-mark-as-read', '/forum/mark-as-read', ['cat' => '', 'rec' => ''])] public function postMarkAsRead(HttpResponseBuilder $response, HttpRequest $request) { diff --git a/src/LegacyRoutes.php b/src/LegacyRoutes.php index df5d906..e35fdce 100644 --- a/src/LegacyRoutes.php +++ b/src/LegacyRoutes.php @@ -32,8 +32,6 @@ class LegacyRoutes implements RouteHandler, UrlSource { $urls->register('forum-leaderboard', '/forum/leaderboard.php', ['id' => '', 'mode' => '']); $urls->register('forum-topic-new', '/forum/posting.php', ['f' => '']); $urls->register('forum-reply-new', '/forum/posting.php', ['t' => '']); - $urls->register('forum-category', '/forum/forum.php', ['f' => '', 'p' => '']); - $urls->register('forum-category-root', '/forum/index.php', fragment: ''); $urls->register('forum-topic', '/forum/topic.php', ['t' => '', 'page' => ''], ''); $urls->register('forum-topic-create', '/forum/posting.php', ['f' => '']); $urls->register('forum-post', '/forum/topic.php', ['p' => ''], 'p'); @@ -189,6 +187,14 @@ class LegacyRoutes implements RouteHandler, UrlSource { $response->redirect($this->urls->format('forum-index'), true); } + #[HttpGet('/forum/forum.php')] + public function getForumForumPHP(HttpResponseBuilder $response, HttpRequest $request): void { + $response->redirect($this->urls->format('forum-category', [ + 'forum' => $request->getParam('f', FILTER_SANITIZE_NUMBER_INT), + 'page' => $request->getParam('p', FILTER_SANITIZE_NUMBER_INT), + ]), true); + } + #[HttpGet('/forum/post.php')] public function getForumPostPHP(HttpResponseBuilder $response, HttpRequest $request): void { $response->redirect($this->urls->format('forum-post', [