Moved forum index into the router.
This commit is contained in:
parent
e53f7fdc08
commit
706fbe8283
4 changed files with 161 additions and 152 deletions
|
@ -1,149 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace Misuzu;
|
|
||||||
|
|
||||||
use stdClass;
|
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
|
|
||||||
die('Script must be called through the Misuzu route dispatcher.');
|
|
||||||
|
|
||||||
$currentUser = $msz->authInfo->userInfo;
|
|
||||||
$categories = $msz->forumCtx->categories->getCategories(hidden: false, asTree: true);
|
|
||||||
|
|
||||||
foreach($categories as $categoryId => $category) {
|
|
||||||
$perms = $msz->authInfo->getPerms('forum', $category->info);
|
|
||||||
if(!$perms->check(Perm::F_CATEGORY_LIST)) {
|
|
||||||
unset($categories[$categoryId]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$unread = false;
|
|
||||||
|
|
||||||
if($category->info->mayHaveChildren)
|
|
||||||
foreach($category->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($category->info->isListing) {
|
|
||||||
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);
|
|
||||||
if($childUnread)
|
|
||||||
$unread = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$child->perms = $childPerms;
|
|
||||||
$child->unread = $childUnread;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($category->info->mayHaveTopics && !$unread)
|
|
||||||
$unread = $msz->forumCtx->categories->checkCategoryUnread($category->info, $currentUser);
|
|
||||||
|
|
||||||
if(!$category->info->isListing) {
|
|
||||||
if(!array_key_exists('0', $categories)) {
|
|
||||||
$categories['0'] = $root = new stdClass;
|
|
||||||
$root->info = null;
|
|
||||||
$root->perms = 0;
|
|
||||||
$root->unread = false;
|
|
||||||
$root->colour = null;
|
|
||||||
$root->children = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$categories['0']->children[$categoryId] = $category;
|
|
||||||
unset($categories[$categoryId]);
|
|
||||||
|
|
||||||
if($category->info->mayHaveChildren || $category->info->mayHaveTopics) {
|
|
||||||
$catIds = [$category->info->id];
|
|
||||||
foreach($category->childIds as $childId) {
|
|
||||||
$childPerms = $msz->authInfo->getPerms('forum', $childId);
|
|
||||||
if($childPerms->check(Perm::F_CATEGORY_LIST))
|
|
||||||
$catIds[] = $childId;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$lastPostInfo = $msz->forumCtx->posts->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
|
|
||||||
} catch(RuntimeException $ex) {
|
|
||||||
$lastPostInfo = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($lastPostInfo !== null) {
|
|
||||||
$category->lastPost = new stdClass;
|
|
||||||
$category->lastPost->info = $lastPostInfo;
|
|
||||||
$category->lastPost->topicInfo = $msz->forumCtx->topics->getTopic(postInfo: $lastPostInfo);
|
|
||||||
|
|
||||||
if($lastPostInfo->userId !== null) {
|
|
||||||
$category->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId);
|
|
||||||
$category->lastPost->colour = $msz->usersCtx->getUserColour($category->lastPost->user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$category->perms = $perms;
|
|
||||||
$category->unread = $unread;
|
|
||||||
}
|
|
||||||
|
|
||||||
Template::render('forum.index', [
|
|
||||||
'forum_categories' => $categories,
|
|
||||||
'forum_empty' => empty($categories),
|
|
||||||
'forum_show_mark_as_read' => $currentUser !== null,
|
|
||||||
]);
|
|
|
@ -1,21 +1,174 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Misuzu\Forum;
|
namespace Misuzu\Forum;
|
||||||
|
|
||||||
|
use stdClass;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Index\Http\{HttpRequest,HttpResponseBuilder};
|
use Index\Http\{HttpRequest,HttpResponseBuilder};
|
||||||
use Index\Http\Routing\{HttpPost,RouteHandler,RouteHandlerTrait};
|
use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler,RouteHandlerTrait};
|
||||||
use Index\Urls\{UrlFormat,UrlSource,UrlSourceTrait};
|
use Index\Urls\{UrlFormat,UrlSource,UrlSourceTrait};
|
||||||
use Misuzu\{CSRF,Perm};
|
use Misuzu\{CSRF,Perm,Template};
|
||||||
use Misuzu\Auth\AuthInfo;
|
use Misuzu\Auth\AuthInfo;
|
||||||
|
use Misuzu\Users\UsersContext;
|
||||||
|
|
||||||
class ForumCategoriesRoutes implements RouteHandler, UrlSource {
|
class ForumCategoriesRoutes implements RouteHandler, UrlSource {
|
||||||
use RouteHandlerTrait, UrlSourceTrait;
|
use RouteHandlerTrait, UrlSourceTrait;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ForumContext $forum,
|
private ForumContext $forum,
|
||||||
|
private UsersContext $usersCtx,
|
||||||
private AuthInfo $authInfo,
|
private AuthInfo $authInfo,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
#[HttpGet('/forum')]
|
||||||
|
#[UrlFormat('forum-index', '/forum')]
|
||||||
|
public function getIndex() {
|
||||||
|
$cats = $this->forum->categories->getCategories(hidden: false, asTree: true);
|
||||||
|
|
||||||
|
// Filtering! This really needs cleaning up...
|
||||||
|
foreach($cats as $catId => $category) {
|
||||||
|
$perms = $this->authInfo->getPerms('forum', $category->info);
|
||||||
|
|
||||||
|
// Check if we're allowed to list this category, separate from viewing for some reason...
|
||||||
|
if(!$perms->check(Perm::F_CATEGORY_LIST)) {
|
||||||
|
unset($cats[$catId]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$unread = false;
|
||||||
|
|
||||||
|
// This is not fully recursive, idk why... probably shove this into ForumContext?
|
||||||
|
if($category->info->mayHaveChildren && !$unread)
|
||||||
|
foreach($category->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($category->info->isListing) {
|
||||||
|
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);
|
||||||
|
if($childUnread)
|
||||||
|
$unread = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$child->perms = $childPerms;
|
||||||
|
$child->unread = $childUnread;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is listed second despite being less expensive because the subcategories listed also need to show unread :sob:
|
||||||
|
if($category->info->mayHaveTopics)
|
||||||
|
$unread = $this->forum->categories->checkCategoryUnread($category->info, $this->authInfo->userInfo);
|
||||||
|
|
||||||
|
// Create virtual root category if we need to list non-listing categories in root
|
||||||
|
if(!$category->info->isListing) {
|
||||||
|
if(!array_key_exists('0', $cats)) {
|
||||||
|
$cats['0'] = $root = new stdClass;
|
||||||
|
$root->info = null;
|
||||||
|
$root->perms = 0;
|
||||||
|
$root->unread = false;
|
||||||
|
$root->colour = null;
|
||||||
|
$root->children = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$cats['0']->children[$categoryId] = $category;
|
||||||
|
unset($cats[$categoryId]);
|
||||||
|
|
||||||
|
if($category->info->mayHaveChildren || $category->info->mayHaveTopics) {
|
||||||
|
$catIds = [$category->info->id];
|
||||||
|
foreach($category->childIds as $childId) {
|
||||||
|
$childPerms = $this->authInfo->getPerms('forum', $childId);
|
||||||
|
if($childPerms->check(Perm::F_CATEGORY_LIST))
|
||||||
|
$catIds[] = $childId;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$lastPostInfo = $this->forum->posts->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
|
||||||
|
} catch(RuntimeException $ex) {
|
||||||
|
$lastPostInfo = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($lastPostInfo !== null) {
|
||||||
|
$category->lastPost = new stdClass;
|
||||||
|
$category->lastPost->info = $lastPostInfo;
|
||||||
|
$category->lastPost->topicInfo = $this->forum->topics->getTopic(postInfo: $lastPostInfo);
|
||||||
|
|
||||||
|
if($lastPostInfo->userId !== null) {
|
||||||
|
$category->lastPost->user = $this->usersCtx->getUserInfo($lastPostInfo->userId);
|
||||||
|
$category->lastPost->colour = $this->usersCtx->getUserColour($category->lastPost->user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$category->perms = $perms;
|
||||||
|
$category->unread = $unread;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Template::renderRaw('forum.index', [
|
||||||
|
'forum_categories' => $cats,
|
||||||
|
'forum_empty' => empty($cats),
|
||||||
|
'forum_show_mark_as_read' => $this->authInfo->isLoggedIn,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
#[HttpPost('/forum/mark-as-read')]
|
#[HttpPost('/forum/mark-as-read')]
|
||||||
#[UrlFormat('forum-mark-as-read', '/forum/mark-as-read', ['cat' => '<category>', 'rec' => '<recursive>'])]
|
#[UrlFormat('forum-mark-as-read', '/forum/mark-as-read', ['cat' => '<category>', 'rec' => '<recursive>'])]
|
||||||
public function postMarkAsRead(HttpResponseBuilder $response, HttpRequest $request) {
|
public function postMarkAsRead(HttpResponseBuilder $response, HttpRequest $request) {
|
||||||
|
|
|
@ -29,7 +29,6 @@ class LegacyRoutes implements RouteHandler, UrlSource {
|
||||||
$urls->register('auth-two-factor', '/auth/twofactor.php', ['token' => '<token>']);
|
$urls->register('auth-two-factor', '/auth/twofactor.php', ['token' => '<token>']);
|
||||||
$urls->register('auth-revert', '/auth/revert.php', ['csrf' => '<csrf>']);
|
$urls->register('auth-revert', '/auth/revert.php', ['csrf' => '<csrf>']);
|
||||||
|
|
||||||
$urls->register('forum-index', '/forum');
|
|
||||||
$urls->register('forum-leaderboard', '/forum/leaderboard.php', ['id' => '<id>', 'mode' => '<mode>']);
|
$urls->register('forum-leaderboard', '/forum/leaderboard.php', ['id' => '<id>', 'mode' => '<mode>']);
|
||||||
$urls->register('forum-topic-new', '/forum/posting.php', ['f' => '<forum>']);
|
$urls->register('forum-topic-new', '/forum/posting.php', ['f' => '<forum>']);
|
||||||
$urls->register('forum-reply-new', '/forum/posting.php', ['t' => '<topic>']);
|
$urls->register('forum-reply-new', '/forum/posting.php', ['t' => '<topic>']);
|
||||||
|
@ -185,6 +184,11 @@ class LegacyRoutes implements RouteHandler, UrlSource {
|
||||||
), true);
|
), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[HttpGet('/forum/index.php')]
|
||||||
|
public function getForumIndexPHP(HttpResponseBuilder $response, HttpRequest $request): void {
|
||||||
|
$response->redirect($this->urls->format('forum-index'), true);
|
||||||
|
}
|
||||||
|
|
||||||
#[HttpGet('/forum/post.php')]
|
#[HttpGet('/forum/post.php')]
|
||||||
public function getForumPostPHP(HttpResponseBuilder $response, HttpRequest $request): void {
|
public function getForumPostPHP(HttpResponseBuilder $response, HttpRequest $request): void {
|
||||||
$response->redirect($this->urls->format('forum-post', [
|
$response->redirect($this->urls->format('forum-post', [
|
||||||
|
|
|
@ -188,6 +188,7 @@ class MisuzuContext {
|
||||||
|
|
||||||
$routingCtx->register(new \Misuzu\Forum\ForumCategoriesRoutes(
|
$routingCtx->register(new \Misuzu\Forum\ForumCategoriesRoutes(
|
||||||
$this->forumCtx,
|
$this->forumCtx,
|
||||||
|
$this->usersCtx,
|
||||||
$this->authInfo,
|
$this->authInfo,
|
||||||
));
|
));
|
||||||
$routingCtx->register(new \Misuzu\Forum\ForumTopicsRoutes(
|
$routingCtx->register(new \Misuzu\Forum\ForumTopicsRoutes(
|
||||||
|
|
Reference in a new issue