Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bookmark API and add tests for it #1425

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions config/packages/league_oauth2_server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ league_oauth2_server:
"user:bookmark",
"user:bookmark:add",
"user:bookmark:remove",
"user:bookmark:list",
"user:bookmark:list:read",
"user:bookmark:list:edit",
"user:bookmark:list:delete",
"user:bookmark_list",
"user:bookmark_list:read",
"user:bookmark_list:edit",
"user:bookmark_list:delete",
"user:message",
"user:message:read",
"user:message:create",
Expand Down
4 changes: 4 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ security:
'ROLE_OAUTH2_POST:EDIT',
'ROLE_OAUTH2_POST_COMMENT:CREATE',
'ROLE_OAUTH2_POST_COMMENT:EDIT',
'ROLE_OAUTH2_USER:BOOKMARK:ADD',
'ROLE_OAUTH2_USER:BOOKMARK:REMOVE',
'ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT',
]
ROLE_OAUTH2_DELETE:
[
'ROLE_OAUTH2_ENTRY:DELETE',
'ROLE_OAUTH2_ENTRY_COMMENT:DELETE',
'ROLE_OAUTH2_POST:DELETE',
'ROLE_OAUTH2_POST_COMMENT:DELETE',
'ROLE_OAUTH2_USER:BOOKMARK_LIST:DELETE',
]
ROLE_OAUTH2_REPORT:
[
Expand Down
37 changes: 25 additions & 12 deletions src/Controller/Api/Bookmark/BookmarkApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Controller\Api\Bookmark;

use App\Controller\Api\BaseApi;
use App\DTO\BookmarksDto;
use App\Schema\Errors\NotFoundErrorSchema;
use App\Schema\Errors\TooManyRequestsErrorSchema;
use App\Schema\Errors\UnauthorizedErrorSchema;
Expand All @@ -27,7 +28,7 @@ class BookmarkApiController extends BaseApi
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -61,7 +62,7 @@ class BookmarkApiController extends BaseApi
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:ADD')]
public function subjectBookmarkStandard(int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
Expand All @@ -75,7 +76,10 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
}
$this->bookmarkManager->addBookmarkToDefaultList($user, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -86,7 +90,7 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -120,7 +124,7 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:ADD')]
public function subjectBookmarkToList(string $list_name, int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
Expand All @@ -138,7 +142,10 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
}
$this->bookmarkManager->addBookmark($user, $list, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -149,7 +156,7 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -183,7 +190,7 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:REMOVE')]
public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
Expand All @@ -201,7 +208,10 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
}
$this->bookmarkRepository->removeBookmarkFromList($user, $list, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -212,7 +222,7 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -246,7 +256,7 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:REMOVE')]
public function subjectRemoveBookmarks(int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
Expand All @@ -260,6 +270,9 @@ public function subjectRemoveBookmarks(int $subject_id, string $subject_type, Ra
}
$this->bookmarkRepository->removeAllBookmarksForContent($user, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}
}
29 changes: 14 additions & 15 deletions src/Controller/Api/Bookmark/BookmarkListApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ enum: [...Entry::ENTRY_TYPE_OPTIONS, 'all']
in: 'query',
schema: new OA\Schema(type: 'integer', default: EntryRepository::PER_PAGE, maximum: self::MAX_PER_PAGE, minimum: self::MIN_PER_PAGE)
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:read'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:read'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:READ')]
public function front(
#[MapQueryParameter] ?int $list_id,
Expand Down Expand Up @@ -204,8 +204,8 @@ public function front(
],
content: new OA\JsonContent(ref: new Model(type: TooManyRequestsErrorSchema::class))
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:read'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:read'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:READ')]
public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
{
Expand All @@ -227,7 +227,7 @@ public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarkListDto::class),
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -255,8 +255,8 @@ public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
Expand All @@ -268,7 +268,7 @@ public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimi
}
$this->bookmarkListRepository->makeListDefault($user, $list);

return new JsonResponse(status: 200, headers: $headers);
return new JsonResponse(BookmarkListDto::fromList($list), status: 200, headers: $headers);
}

#[OA\Response(
Expand Down Expand Up @@ -311,8 +311,8 @@ public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimi
type: BookmarkListDto::class,
groups: ['common']
))]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto $dto, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
Expand All @@ -323,7 +323,6 @@ public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto
throw new NotFoundHttpException(headers: $headers);
}
$this->bookmarkListRepository->editList($user, $list, $dto);
$list = $this->bookmarkListRepository->findOneBy(['id' => $list->getId()]);

return new JsonResponse(BookmarkListDto::fromList($list), status: 200, headers: $headers);
}
Expand Down Expand Up @@ -364,8 +363,8 @@ public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
public function createList(string $list_name, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
Expand Down Expand Up @@ -416,8 +415,8 @@ public function createList(string $list_name, RateLimiterFactory $apiUpdateLimit
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:delete'])]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark_list:delete'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:DELETE')]
public function deleteList(string $list_name, RateLimiterFactory $apiDeleteLimiter): JsonResponse
{
Expand Down
13 changes: 13 additions & 0 deletions src/DTO/BookmarksDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\DTO;

class BookmarksDto
{
/**
* @var string[]|null
*/
public ?array $bookmarks = null;
}
43 changes: 42 additions & 1 deletion src/DTO/EntryCommentDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\DTO;

use App\Entity\Contracts\ContentVisibilityInterface;
use App\Entity\Contracts\VisibilityInterface;
use App\Entity\Entry;
use App\Entity\EntryComment;
Expand All @@ -14,7 +15,7 @@
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class EntryCommentDto
class EntryCommentDto implements ContentVisibilityInterface
{
public const MAX_BODY_LENGTH = 5000;

Expand Down Expand Up @@ -113,4 +114,44 @@ public function userChoice(): ?int
{
return $this->userVote;
}

public function getApId(): ?string
{
return $this->apId;
}

public function getMagazine(): ?Magazine
{
return $this->magazine;
}

public function getUser(): ?User
{
return $this->user;
}

public function getVisibility(): string
{
return $this->visibility;
}

public function isPrivate(): bool
{
return VisibilityInterface::VISIBILITY_PRIVATE === $this->visibility;
}

public function isSoftDeleted(): bool
{
return VisibilityInterface::VISIBILITY_SOFT_DELETED === $this->visibility;
}

public function isTrashed(): bool
{
return VisibilityInterface::VISIBILITY_TRASHED === $this->visibility;
}

public function isVisible(): bool
{
return VisibilityInterface::VISIBILITY_VISIBLE === $this->visibility;
}
}
7 changes: 7 additions & 0 deletions src/DTO/EntryCommentResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class EntryCommentResponseDto implements \JsonSerializable
public int $childCount = 0;
public ?bool $canAuthUserModerate = null;

/** @var string[]|null */
#[OA\Property(type: 'array', items: new OA\Items(type: 'string'))]
public ?array $bookmarks = null;

public static function create(
?int $id = null,
?UserSmallResponseDto $user = null,
Expand All @@ -113,6 +117,7 @@ public static function create(
?\DateTime $lastActive = null,
int $childCount = 0,
?bool $canAuthUserModerate = null,
?array $bookmarks = null,
): self {
$dto = new EntryCommentResponseDto();
$dto->commentId = $id;
Expand All @@ -137,6 +142,7 @@ public static function create(
$dto->lastActive = $lastActive;
$dto->childCount = $childCount;
$dto->canAuthUserModerate = $canAuthUserModerate;
$dto->bookmarks = $bookmarks;

return $dto;
}
Expand Down Expand Up @@ -188,6 +194,7 @@ public function jsonSerialize(): mixed
'childCount' => $this->childCount,
'children' => $this->children,
'canAuthUserModerate' => $this->canAuthUserModerate,
'bookmarks' => $this->bookmarks,
]);
}
}
Loading
Loading