diff --git a/src/Controller/Api/Entry/EntriesBaseApi.php b/src/Controller/Api/Entry/EntriesBaseApi.php index 6dc816133..8b34c280f 100644 --- a/src/Controller/Api/Entry/EntriesBaseApi.php +++ b/src/Controller/Api/Entry/EntriesBaseApi.php @@ -10,6 +10,7 @@ use App\DTO\EntryCommentResponseDto; use App\DTO\EntryDto; use App\DTO\EntryRequestDto; +use App\DTO\EntryResponseDto; use App\DTO\ImageDto; use App\Entity\Entry; use App\Entity\EntryComment; @@ -34,7 +35,7 @@ public function setCommentsFactory(EntryCommentFactory $commentsFactory) /** * Serialize a single entry to JSON. */ - protected function serializeEntry(EntryDto|Entry $dto, array $tags) + protected function serializeEntry(EntryDto|Entry $dto, array $tags): EntryResponseDto { $response = $this->entryFactory->createResponseDto($dto, $tags); @@ -43,6 +44,10 @@ protected function serializeEntry(EntryDto|Entry $dto, array $tags) $response->userVote = $dto instanceof EntryDto ? $dto->userVote : $dto->getUserChoice($this->getUserOrThrow()); } + if ($user = $this->getUser()) { + $response->canAuthUserModerate = $dto->getMagazine()->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } + return $response; } @@ -103,6 +108,10 @@ protected function serializeComment(EntryCommentDto $comment, array $tags): Entr $response->userVote = $comment->userVote; } + if ($user = $this->getUser()) { + $response->canAuthUserModerate = $comment->magazine->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } + return $response; } @@ -171,8 +180,13 @@ protected function serializeCommentTree(?EntryComment $comment, ?int $depth = nu if (null === $depth) { $depth = self::constrainDepth($this->request->getCurrentRequest()->get('d', self::DEPTH)); } + $canModerate = null; + if ($user = $this->getUser()) { + $canModerate = $comment->getMagazine()->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } - $commentTree = $this->commentsFactory->createResponseTree($comment, $depth); + $commentTree = $this->commentsFactory->createResponseTree($comment, $depth, $canModerate); + $commentTree->canAuthUserModerate = $canModerate; return $commentTree->jsonSerialize(); } diff --git a/src/Controller/Api/Post/PostsBaseApi.php b/src/Controller/Api/Post/PostsBaseApi.php index 0a3dbe6b7..d1df4625b 100644 --- a/src/Controller/Api/Post/PostsBaseApi.php +++ b/src/Controller/Api/Post/PostsBaseApi.php @@ -30,6 +30,10 @@ protected function serializePost(PostDto $dto, array $tags): PostResponseDto $response->userVote = $dto instanceof PostDto ? $dto->userVote : $dto->getUserChoice($this->getUserOrThrow()); } + if ($user = $this->getUser()) { + $response->canAuthUserModerate = $dto->getMagazine()->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } + return $response; } @@ -83,6 +87,10 @@ protected function serializePostComment(PostCommentDto $comment, array $tags): P $response->userVote = $comment instanceof PostCommentDto ? $comment->userVote : $comment->getUserChoice($this->getUserOrThrow()); } + if ($user = $this->getUser()) { + $response->canAuthUserModerate = $comment->getMagazine()->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } + return $response; } @@ -148,7 +156,13 @@ protected function serializePostCommentTree(?PostComment $comment, ?int $depth = $depth = self::constrainDepth($this->request->getCurrentRequest()->get('d', self::DEPTH)); } - $commentTree = $this->postCommentFactory->createResponseTree($comment, $depth); + $canModerate = null; + if ($user = $this->getUser()) { + $canModerate = $comment->getMagazine()->userIsModerator($user) || $user->isModerator() || $user->isAdmin(); + } + + $commentTree = $this->postCommentFactory->createResponseTree($comment, $depth, $canModerate); + $commentTree->canAuthUserModerate = $canModerate; return $commentTree->jsonSerialize(); } diff --git a/src/DTO/EntryCommentResponseDto.php b/src/DTO/EntryCommentResponseDto.php index 84770d96f..e9949870b 100644 --- a/src/DTO/EntryCommentResponseDto.php +++ b/src/DTO/EntryCommentResponseDto.php @@ -88,6 +88,7 @@ class EntryCommentResponseDto implements \JsonSerializable public array $children = []; #[OA\Property(description: 'The total number of children the comment has.')] public int $childCount = 0; + public ?bool $canAuthUserModerate = null; public static function create( ?int $id = null, @@ -111,6 +112,7 @@ public static function create( ?\DateTimeImmutable $editedAt = null, ?\DateTime $lastActive = null, int $childCount = 0, + ?bool $canAuthUserModerate = null, ): self { $dto = new EntryCommentResponseDto(); $dto->commentId = $id; @@ -134,6 +136,7 @@ public static function create( $dto->editedAt = $editedAt; $dto->lastActive = $lastActive; $dto->childCount = $childCount; + $dto->canAuthUserModerate = $canAuthUserModerate; return $dto; } @@ -184,6 +187,7 @@ public function jsonSerialize(): mixed 'lastActive' => $this->lastActive?->format(\DateTimeInterface::ATOM), 'childCount' => $this->childCount, 'children' => $this->children, + 'canAuthUserModerate' => $this->canAuthUserModerate, ]); } } diff --git a/src/DTO/EntryResponseDto.php b/src/DTO/EntryResponseDto.php index a43643db0..8d761a624 100644 --- a/src/DTO/EntryResponseDto.php +++ b/src/DTO/EntryResponseDto.php @@ -44,6 +44,7 @@ class EntryResponseDto implements \JsonSerializable public ?string $type = null; public ?string $slug = null; public ?string $apId = null; + public ?bool $canAuthUserModerate = null; public static function create( ?int $id = null, @@ -70,7 +71,8 @@ public static function create( ?\DateTime $lastActive = null, ?string $type = null, ?string $slug = null, - ?string $apId = null + ?string $apId = null, + ?bool $canAuthUserModerate = null, ): self { $dto = new EntryResponseDto(); $dto->entryId = $id; @@ -98,6 +100,7 @@ public static function create( $dto->type = $type; $dto->slug = $slug; $dto->apId = $apId; + $dto->canAuthUserModerate = $canAuthUserModerate; return $dto; } @@ -150,6 +153,7 @@ public function jsonSerialize(): mixed 'type' => $this->type, 'slug' => $this->slug, 'apId' => $this->apId, + 'canAuthUserModerate' => $this->canAuthUserModerate, ]); } } diff --git a/src/DTO/PostCommentResponseDto.php b/src/DTO/PostCommentResponseDto.php index 2ab8a634d..1925e073d 100644 --- a/src/DTO/PostCommentResponseDto.php +++ b/src/DTO/PostCommentResponseDto.php @@ -80,6 +80,7 @@ class PostCommentResponseDto implements \JsonSerializable ] )] public array $children = []; + public ?bool $canAuthUserModerate = null; public static function create( int $id, @@ -102,6 +103,7 @@ public static function create( ?\DateTimeImmutable $createdAt = null, ?\DateTimeImmutable $editedAt = null, ?\DateTime $lastActive = null, + ?bool $canAuthUserModerate = null, ): self { $dto = new PostCommentResponseDto(); $dto->commentId = $id; @@ -125,6 +127,7 @@ public static function create( $dto->editedAt = $editedAt; $dto->lastActive = $lastActive; $dto->childCount = $childCount; + $dto->canAuthUserModerate = $canAuthUserModerate; return $dto; } @@ -171,6 +174,7 @@ public function jsonSerialize(): mixed 'lastActive' => $this->lastActive?->format(\DateTimeInterface::ATOM), 'childCount' => $this->childCount, 'children' => $this->children, + 'canAuthUserModerate' => $this->canAuthUserModerate, ]); } diff --git a/src/DTO/PostResponseDto.php b/src/DTO/PostResponseDto.php index 85a874a08..c17d86a0a 100644 --- a/src/DTO/PostResponseDto.php +++ b/src/DTO/PostResponseDto.php @@ -36,6 +36,7 @@ class PostResponseDto implements \JsonSerializable public ?\DateTimeImmutable $createdAt = null; public ?\DateTimeImmutable $editedAt = null; public ?\DateTime $lastActive = null; + public ?bool $canAuthUserModerate = null; public static function create( int $id, @@ -57,7 +58,8 @@ public static function create( ?\DateTimeImmutable $createdAt = null, ?\DateTimeImmutable $editedAt = null, ?\DateTime $lastActive = null, - ?string $slug = null + ?string $slug = null, + ?bool $canAuthUserModerate = null, ): self { $dto = new PostResponseDto(); $dto->postId = $id; @@ -80,6 +82,7 @@ public static function create( $dto->editedAt = $editedAt; $dto->lastActive = $lastActive; $dto->slug = $slug; + $dto->canAuthUserModerate = $canAuthUserModerate; return $dto; } @@ -124,6 +127,7 @@ public function jsonSerialize(): mixed 'editedAt' => $this->editedAt?->format(\DateTimeInterface::ATOM), 'lastActive' => $this->lastActive?->format(\DateTimeInterface::ATOM), 'slug' => $this->slug, + 'canAuthUserModerate' => $this->canAuthUserModerate, ]); } } diff --git a/src/Factory/EntryCommentFactory.php b/src/Factory/EntryCommentFactory.php index 79b0a0add..ecaf77cb6 100644 --- a/src/Factory/EntryCommentFactory.php +++ b/src/Factory/EntryCommentFactory.php @@ -62,12 +62,13 @@ public function createResponseDto(EntryCommentDto|EntryComment $comment, array $ ); } - public function createResponseTree(EntryComment $comment, int $depth = -1): EntryCommentResponseDto + public function createResponseTree(EntryComment $comment, int $depth = -1, ?bool $canModerate = null): EntryCommentResponseDto { $commentDto = $this->createDto($comment); $toReturn = $this->createResponseDto($commentDto, $this->tagLinkRepository->getTagsOfEntryComment($comment), array_reduce($comment->children->toArray(), EntryCommentResponseDto::class.'::recursiveChildCount', 0)); $toReturn->isFavourited = $commentDto->isFavourited; $toReturn->userVote = $commentDto->userVote; + $toReturn->canAuthUserModerate = $canModerate; if (0 === $depth) { return $toReturn; @@ -75,7 +76,7 @@ public function createResponseTree(EntryComment $comment, int $depth = -1): Entr foreach ($comment->children as $childComment) { \assert($childComment instanceof EntryComment); - $child = $this->createResponseTree($childComment, $depth > 0 ? $depth - 1 : -1); + $child = $this->createResponseTree($childComment, $depth > 0 ? $depth - 1 : -1, $canModerate); array_push($toReturn->children, $child); } diff --git a/src/Factory/PostCommentFactory.php b/src/Factory/PostCommentFactory.php index c58cc38bb..03926e93c 100644 --- a/src/Factory/PostCommentFactory.php +++ b/src/Factory/PostCommentFactory.php @@ -63,12 +63,13 @@ public function createResponseDto(PostCommentDto|PostComment $comment, array $ta ); } - public function createResponseTree(PostComment $comment, int $depth): PostCommentResponseDto + public function createResponseTree(PostComment $comment, int $depth, ?bool $canModerate = null): PostCommentResponseDto { $commentDto = $this->createDto($comment); $toReturn = $this->createResponseDto($commentDto, $this->tagLinkRepository->getTagsOfPostComment($comment), array_reduce($comment->children->toArray(), PostCommentResponseDto::class.'::recursiveChildCount', 0)); $toReturn->isFavourited = $commentDto->isFavourited; $toReturn->userVote = $commentDto->userVote; + $toReturn->canAuthUserModerate = $canModerate; if (0 === $depth) { return $toReturn; @@ -76,7 +77,7 @@ public function createResponseTree(PostComment $comment, int $depth): PostCommen foreach ($comment->children as $childComment) { \assert($childComment instanceof PostComment); - $child = $this->createResponseTree($childComment, $depth > 0 ? $depth - 1 : -1); + $child = $this->createResponseTree($childComment, $depth > 0 ? $depth - 1 : -1, $canModerate); array_push($toReturn->children, $child); }