Skip to content

Commit

Permalink
Handle QueryAssertionError in following users/playlists.
Browse files Browse the repository at this point in the history
Fixes #8.
  • Loading branch information
nekitdev committed Feb 20, 2024
1 parent ce74be8 commit cc9420b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
24 changes: 21 additions & 3 deletions melody/kit/endpoints/v1/self.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from uuid import UUID

from aiofiles import open as async_open
from edgedb import QueryAssertionError
from fastapi import Body, Depends
from fastapi.responses import FileResponse
from typing_extensions import Annotated
Expand All @@ -13,7 +14,12 @@
from melody.kit.dependencies.images import ImageDependency
from melody.kit.dependencies.request_urls import RequestURLDependency
from melody.kit.enums import EntityType, Platform, PrivacyType, Tag
from melody.kit.errors.users import UserImageNotFound, UserNotFound
from melody.kit.errors.users import (
UserFollowSelfForbidden,
UserFollowSelfPlaylistsForbidden,
UserImageNotFound,
UserNotFound,
)
from melody.kit.models.pagination import Pagination
from melody.kit.models.user import (
UserAlbums,
Expand Down Expand Up @@ -438,7 +444,13 @@ async def get_self_following(
async def add_self_following(
context: FollowingWriteTokenDependency, ids: UUIDListDependency
) -> None:
await database.add_user_following(user_id=context.user_id, ids=ids)
self_id = context.user_id

try:
await database.add_user_following(user_id=self_id, ids=ids)

except QueryAssertionError:
raise UserFollowSelfForbidden(self_id) from None


@v1.delete(
Expand Down Expand Up @@ -489,7 +501,13 @@ async def get_self_followed_playlists(
async def add_self_followed_playlists(
context: LibraryWriteTokenDependency, ids: UUIDListDependency
) -> None:
await database.add_user_followed_playlists(user_id=context.user_id, ids=ids)
self_id = context.user_id

try:
await database.add_user_followed_playlists(user_id=self_id, ids=ids)

except QueryAssertionError:
raise UserFollowSelfPlaylistsForbidden(self_id) from None


@v1.delete(
Expand Down
2 changes: 2 additions & 0 deletions melody/kit/errors/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ErrorCode(Enum):
USER_NOT_FOUND = 13501
USER_INACCESSIBLE = 13502
USER_IMAGE_NOT_FOUND = 13503
USER_FOLLOW_SELF_FORBIDDEN = 13504
USER_FOLLOW_SELF_PLAYLISTS_FORBIDDEN = 13505

CLIENT_ERROR = 13600
CLIENT_NOT_FOUND = 13601
Expand Down
24 changes: 24 additions & 0 deletions melody/kit/errors/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"UserNotFound",
"UserInaccessible",
"UserImageNotFound",
"UserFollowSelfForbidden",
"UserFollowSelfPlaylistsForbidden",
)


Expand Down Expand Up @@ -63,3 +65,25 @@ def __init__(self, user_id: UUID) -> None:
class UserImageNotFound(UserError):
def __init__(self, user_id: UUID) -> None:
super().__init__(user_id, user_image_not_found(user_id))


USER_CAN_NOT_FOLLOW_SELF = "user `{}` can not follow themselves"
user_can_not_follow_self = USER_CAN_NOT_FOLLOW_SELF.format


@default_code(ErrorCode.USER_FOLLOW_SELF_FORBIDDEN)
@default_status_code(status.HTTP_403_FORBIDDEN)
class UserFollowSelfForbidden(UserError):
def __init__(self, user_id: UUID) -> None:
super().__init__(user_id, user_can_not_follow_self(user_id))


USER_CAN_NOT_FOLLOW_SELF_PLAYLISTS = "user `{}` can not follow their own playlists"
user_can_not_follow_self_playlists = USER_CAN_NOT_FOLLOW_SELF_PLAYLISTS.format


@default_code(ErrorCode.USER_FOLLOW_SELF_PLAYLISTS_FORBIDDEN)
@default_status_code(status.HTTP_403_FORBIDDEN)
class UserFollowSelfPlaylistsForbidden(UserError):
def __init__(self, user_id: UUID) -> None:
super().__init__(user_id, user_can_not_follow_self_playlists(user_id))
7 changes: 7 additions & 0 deletions schema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ module default {

multi followed_playlists extending with_linked_at: Playlist;

trigger forbid_follow_self_playlists after insert, update for each do (
assert(
not exists (__new__.playlists intersect __new__.followed_playlists),
message := "users can not follow their own playlists",
)
);

followed_playlist_count := count(.followed_playlists);

multi streams := .<user[is Stream];
Expand Down

0 comments on commit cc9420b

Please sign in to comment.