misuzu/src/Comments/CommentsEx.php

61 lines
2.2 KiB
PHP
Raw Normal View History

2023-07-15 23:58:17 +00:00
<?php
namespace Misuzu\Comments;
use stdClass;
use RuntimeException;
use Misuzu\MisuzuContext;
2023-08-30 22:37:21 +00:00
use Misuzu\Perm;
use Misuzu\Auth\AuthInfo;
use Misuzu\Users\UsersContext;
2023-07-15 23:58:17 +00:00
class CommentsEx {
public function __construct(
private AuthInfo $authInfo,
private Comments $comments,
private UsersContext $usersCtx
) {}
2023-07-15 23:58:17 +00:00
public function getCommentsForLayout(CommentsCategoryInfo|string $category): object {
$info = new stdClass;
if(is_string($category))
$category = $this->comments->ensureCategory($category);
$hasUser = $this->authInfo->isLoggedIn();
$info->user = $hasUser ? $this->authInfo->getUserInfo() : null;
$info->colour = $this->usersCtx->getUserColour($info->user);
2023-08-30 22:37:21 +00:00
$info->perms = $this->authInfo->getPerms('global')->checkMany([
'can_post' => Perm::G_COMMENTS_CREATE,
'can_delete' => Perm::G_COMMENTS_DELETE_OWN | Perm::G_COMMENTS_DELETE_ANY,
'can_delete_any' => Perm::G_COMMENTS_DELETE_ANY,
'can_pin' => Perm::G_COMMENTS_PIN,
'can_lock' => Perm::G_COMMENTS_LOCK,
'can_vote' => Perm::G_COMMENTS_VOTE,
]);
2023-07-15 23:58:17 +00:00
$info->category = $category;
$info->posts = [];
$root = $this->comments->getPosts($category, includeRepliesCount: true, includeVotesCount: true, replies: false);
2023-07-15 23:58:17 +00:00
foreach($root as $postInfo)
$info->posts[] = $this->decorateComment($postInfo);
return $info;
}
public function decorateComment(CommentsPostInfo $postInfo): object {
$userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null;
2023-07-15 23:58:17 +00:00
$info = new stdClass;
$info->post = $postInfo;
$info->user = $userInfo;
$info->colour = $this->usersCtx->getUserColour($userInfo);
2023-07-15 23:58:17 +00:00
$info->vote = $this->comments->getPostVote($postInfo, $userInfo);
$info->replies = [];
$root = $this->comments->getPosts(parentInfo: $postInfo, includeRepliesCount: true, includeVotesCount: true);
2023-07-15 23:58:17 +00:00
foreach($root as $childInfo)
$info->replies[] = $this->decorateComment($childInfo);
return $info;
}
}