misuzu/src/Comments/CommentsEx.php
flash d0e3f6ce65 Normalised custom exception usage in user classes.
Also updated the Index library to include the MediaType fix.
2023-07-22 15:02:45 +00:00

62 lines
1.9 KiB
PHP

<?php
namespace Misuzu\Comments;
use stdClass;
use RuntimeException;
use Misuzu\Users\User;
class CommentsEx {
private Comments $comments;
private array $userInfos;
public function __construct(Comments $comments, array $userInfos = []) {
$this->comments = $comments;
$this->userInfos = $userInfos;
}
public function getCommentsForLayout(CommentsCategoryInfo|string $category): object {
$info = new stdClass;
if(is_string($category))
$category = $this->comments->ensureCategory($category);
$info->user = User::getCurrent();
$info->category = $category;
$info->posts = [];
$root = $this->comments->getPosts($category, includeRepliesCount: true, includeVotesCount: true, includeDeleted: true);
foreach($root as $postInfo)
$info->posts[] = $this->decorateComment($postInfo);
return $info;
}
public function decorateComment(CommentsPostInfo $postInfo): object {
if($postInfo->hasUserId()) {
$userId = $postInfo->getUserId();
if(array_key_exists($userId, $this->userInfos)) {
$userInfo = $this->userInfos[$userId];
} else {
try {
$userInfo = User::byId($userId);
} catch(RuntimeException $ex) {
$userInfo = null;
}
$this->userInfos[$userId] = $userInfo;
}
} else $userInfo = null;
$info = new stdClass;
$info->post = $postInfo;
$info->user = $userInfo;
$info->vote = $this->comments->getPostVote($postInfo, $userInfo);
$info->replies = [];
$root = $this->comments->getPosts(parent: $postInfo, includeRepliesCount: true, includeVotesCount: true, includeDeleted: true);
foreach($root as $childInfo)
$info->replies[] = $this->decorateComment($childInfo);
return $info;
}
}