Skip to content

Commit

Permalink
Add field to all content response DTOs: canAuthUserModerate (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
BentiGorlich authored Aug 20, 2024
1 parent 4973e4a commit 194cbe3
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/Controller/Api/Entry/EntriesBaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
Expand Down
16 changes: 15 additions & 1 deletion src/Controller/Api/Post/PostsBaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions src/DTO/EntryCommentResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -134,6 +136,7 @@ public static function create(
$dto->editedAt = $editedAt;
$dto->lastActive = $lastActive;
$dto->childCount = $childCount;
$dto->canAuthUserModerate = $canAuthUserModerate;

return $dto;
}
Expand Down Expand Up @@ -184,6 +187,7 @@ public function jsonSerialize(): mixed
'lastActive' => $this->lastActive?->format(\DateTimeInterface::ATOM),
'childCount' => $this->childCount,
'children' => $this->children,
'canAuthUserModerate' => $this->canAuthUserModerate,
]);
}
}
6 changes: 5 additions & 1 deletion src/DTO/EntryResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -98,6 +100,7 @@ public static function create(
$dto->type = $type;
$dto->slug = $slug;
$dto->apId = $apId;
$dto->canAuthUserModerate = $canAuthUserModerate;

return $dto;
}
Expand Down Expand Up @@ -150,6 +153,7 @@ public function jsonSerialize(): mixed
'type' => $this->type,
'slug' => $this->slug,
'apId' => $this->apId,
'canAuthUserModerate' => $this->canAuthUserModerate,
]);
}
}
4 changes: 4 additions & 0 deletions src/DTO/PostCommentResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class PostCommentResponseDto implements \JsonSerializable
]
)]
public array $children = [];
public ?bool $canAuthUserModerate = null;

public static function create(
int $id,
Expand All @@ -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;
Expand All @@ -125,6 +127,7 @@ public static function create(
$dto->editedAt = $editedAt;
$dto->lastActive = $lastActive;
$dto->childCount = $childCount;
$dto->canAuthUserModerate = $canAuthUserModerate;

return $dto;
}
Expand Down Expand Up @@ -171,6 +174,7 @@ public function jsonSerialize(): mixed
'lastActive' => $this->lastActive?->format(\DateTimeInterface::ATOM),
'childCount' => $this->childCount,
'children' => $this->children,
'canAuthUserModerate' => $this->canAuthUserModerate,
]);
}

Expand Down
6 changes: 5 additions & 1 deletion src/DTO/PostResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -80,6 +82,7 @@ public static function create(
$dto->editedAt = $editedAt;
$dto->lastActive = $lastActive;
$dto->slug = $slug;
$dto->canAuthUserModerate = $canAuthUserModerate;

return $dto;
}
Expand Down Expand Up @@ -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,
]);
}
}
5 changes: 3 additions & 2 deletions src/Factory/EntryCommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ 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;
}

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);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Factory/PostCommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ 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;
}

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);
}

Expand Down

0 comments on commit 194cbe3

Please sign in to comment.