2023-07-15 23:58:17 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Comments;
|
|
|
|
|
|
|
|
use stdClass;
|
2023-07-22 15:02:41 +00:00
|
|
|
use RuntimeException;
|
2023-08-02 22:12:47 +00:00
|
|
|
use Misuzu\MisuzuContext;
|
|
|
|
use Misuzu\Users\Users;
|
2023-07-15 23:58:17 +00:00
|
|
|
|
|
|
|
class CommentsEx {
|
2023-08-02 22:12:47 +00:00
|
|
|
public function __construct(
|
|
|
|
private MisuzuContext $context,
|
|
|
|
private Comments $comments,
|
|
|
|
private Users $users,
|
|
|
|
private array $userInfos = [],
|
|
|
|
private array $userColours = []
|
|
|
|
) {}
|
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);
|
|
|
|
|
2023-08-02 22:12:47 +00:00
|
|
|
$hasUser = $this->context->isLoggedIn();
|
|
|
|
$info->user = $hasUser ? $this->context->getActiveUser() : null;
|
|
|
|
$info->colour = $hasUser ? $this->users->getUserColour($info->user) : null;
|
2023-07-29 18:15:30 +00:00
|
|
|
$info->perms = $hasUser ? perms_for_comments($info->user->getId()) : [];
|
2023-07-15 23:58:17 +00:00
|
|
|
$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];
|
2023-08-02 22:12:47 +00:00
|
|
|
$userColour = $this->userColours[$userId];
|
2023-07-15 23:58:17 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2023-08-02 22:12:47 +00:00
|
|
|
$userInfo = $this->users->getUser($userId, 'id');
|
|
|
|
$userColour = $this->users->getUserColour($userInfo);
|
2023-07-22 15:02:41 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-07-15 23:58:17 +00:00
|
|
|
$userInfo = null;
|
2023-08-02 22:12:47 +00:00
|
|
|
$userColour = null;
|
2023-07-15 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->userInfos[$userId] = $userInfo;
|
2023-08-02 22:12:47 +00:00
|
|
|
$this->userColours[$userId] = $userColour;
|
2023-07-15 23:58:17 +00:00
|
|
|
}
|
2023-08-02 22:12:47 +00:00
|
|
|
} else {
|
|
|
|
$userInfo = null;
|
|
|
|
$userColour = null;
|
|
|
|
}
|
2023-07-15 23:58:17 +00:00
|
|
|
|
|
|
|
$info = new stdClass;
|
|
|
|
$info->post = $postInfo;
|
|
|
|
$info->user = $userInfo;
|
2023-08-02 22:12:47 +00:00
|
|
|
$info->colour = $userColour;
|
2023-07-15 23:58:17 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|