79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
|
<?php
|
||
|
namespace Misuzu\Forum;
|
||
|
|
||
|
use RuntimeException;
|
||
|
use Index\Http\{HttpRequest,HttpResponseBuilder};
|
||
|
use Index\Http\Routing\{HttpPost,RouteHandler,RouteHandlerTrait};
|
||
|
use Index\Urls\{UrlFormat,UrlSource,UrlSourceTrait};
|
||
|
use Misuzu\{CSRF,Perm};
|
||
|
use Misuzu\Auth\AuthInfo;
|
||
|
|
||
|
class ForumCategoriesRoutes implements RouteHandler, UrlSource {
|
||
|
use RouteHandlerTrait, UrlSourceTrait;
|
||
|
|
||
|
public function __construct(
|
||
|
private ForumContext $forum,
|
||
|
private AuthInfo $authInfo,
|
||
|
) {}
|
||
|
|
||
|
#[HttpPost('/forum/mark-as-read')]
|
||
|
#[UrlFormat('forum-mark-as-read', '/forum/mark-as-read', ['cat' => '<category>', 'rec' => '<recursive>'])]
|
||
|
public function postMarkAsRead(HttpResponseBuilder $response, HttpRequest $request) {
|
||
|
if(!$this->authInfo->isLoggedIn)
|
||
|
return 401;
|
||
|
|
||
|
if(!CSRF::validate($request->getHeaderLine('X-CSRF-token')))
|
||
|
return 403;
|
||
|
$response->setHeader('X-CSRF-Token', CSRF::token());
|
||
|
|
||
|
$catId = (string)$request->getParam('cat', FILTER_SANITIZE_NUMBER_INT);
|
||
|
$recursive = !empty($request->getParam('rec'));
|
||
|
|
||
|
// root category purge must be recursive
|
||
|
if($categoryId === '')
|
||
|
return 400;
|
||
|
|
||
|
if($catId === '')
|
||
|
$cats = $this->forum->categories->getCategories();
|
||
|
elseif($recursive)
|
||
|
$cats = $this->forum->categories->getCategoryChildren(parentInfo: $catId, includeSelf: true);
|
||
|
else
|
||
|
try {
|
||
|
$cats = [$this->forum->categories->getCategory(categoryId: $catId)];
|
||
|
} catch(RuntimeException $ex) {
|
||
|
$cats = [];
|
||
|
}
|
||
|
|
||
|
if(empty($cats)) {
|
||
|
$response->setStatusCode(404);
|
||
|
return [
|
||
|
'error' => [
|
||
|
'name' => 'forum:category:none',
|
||
|
'text' => "Couldn't find that forum category.",
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
$success = false;
|
||
|
foreach($cats as $category) {
|
||
|
$perms = $this->authInfo->getPerms('forum', $category);
|
||
|
if($perms->check(Perm::F_CATEGORY_LIST)) {
|
||
|
$this->forum->categories->updateUserReadCategory($this->authInfo->userInfo, $category);
|
||
|
$success = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(!$success) {
|
||
|
$response->setStatusCode(403);
|
||
|
return [
|
||
|
'error' => [
|
||
|
'name' => 'forum:category:access',
|
||
|
'text' => "You're not allowed to access this forum category.",
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
return 204;
|
||
|
}
|
||
|
}
|