From c41e38e8092888efff5f6c9a2a82c4aed1cf0f86 Mon Sep 17 00:00:00 2001 From: Henri Dickson <90480431+alphatownsman@users.noreply.github.com> Date: Fri, 24 May 2024 09:51:31 -0400 Subject: [PATCH] add type to api url path --- api/urls.py | 74 ++++++++++++++++++------------------ api/views/accounts.py | 22 +++++------ api/views/announcements.py | 2 +- api/views/follow_requests.py | 4 +- api/views/media.py | 4 +- api/views/polls.py | 4 +- api/views/statuses.py | 32 ++++++++-------- 7 files changed, 72 insertions(+), 70 deletions(-) diff --git a/api/urls.py b/api/urls.py index 842aec8..63d54ff 100644 --- a/api/urls.py +++ b/api/urls.py @@ -32,20 +32,20 @@ urlpatterns = [ path("v1/accounts/familiar_followers", accounts.familiar_followers), path("v1/accounts/search", accounts.accounts_search), path("v1/accounts/lookup", accounts.lookup), - path("v1/accounts/", accounts.account), - path("v1/accounts//statuses", accounts.account_statuses), - path("v1/accounts//follow", accounts.account_follow), - path("v1/accounts//unfollow", accounts.account_unfollow), - path("v1/accounts//block", accounts.account_block), - path("v1/accounts//unblock", accounts.account_unblock), - path("v1/accounts//mute", accounts.account_mute), - path("v1/accounts//unmute", accounts.account_unmute), - path("v1/accounts//following", accounts.account_following), - path("v1/accounts//followers", accounts.account_followers), - path("v1/accounts//featured_tags", accounts.account_featured_tags), + path("v1/accounts/", accounts.account), + path("v1/accounts//statuses", accounts.account_statuses), + path("v1/accounts//follow", accounts.account_follow), + path("v1/accounts//unfollow", accounts.account_unfollow), + path("v1/accounts//block", accounts.account_block), + path("v1/accounts//unblock", accounts.account_unblock), + path("v1/accounts//mute", accounts.account_mute), + path("v1/accounts//unmute", accounts.account_unmute), + path("v1/accounts//following", accounts.account_following), + path("v1/accounts//followers", accounts.account_followers), + path("v1/accounts//featured_tags", accounts.account_featured_tags), # Announcements path("v1/announcements", announcements.announcement_list), - path("v1/announcements//dismiss", announcements.announcement_dismiss), + path("v1/announcements//dismiss", announcements.announcement_dismiss), # Apps path("v1/apps", apps.add_app), path("v1/apps/verify_credentials", apps.verify_credentials), @@ -58,8 +58,10 @@ urlpatterns = [ path("v1/filters", filters.list_filters), # Follow requests path("v1/follow_requests", follow_requests.follow_requests), - path("v1/follow_requests//authorize", follow_requests.accept_follow_request), - path("v1/follow_requests//reject", follow_requests.reject_follow_request), + path( + "v1/follow_requests//authorize", follow_requests.accept_follow_request + ), + path("v1/follow_requests//reject", follow_requests.reject_follow_request), # Instance path("v1/instance", instance.instance_info_v1), path("v1/instance/activity", instance.activity), @@ -70,24 +72,24 @@ urlpatterns = [ # Media path("v1/media", media.upload_media), path("v2/media", media.upload_media), - path("v1/media/", methods(get=media.get_media, put=media.update_media)), + path("v1/media/", methods(get=media.get_media, put=media.update_media)), path( - "v1/statuses/", + "v1/statuses/", methods( get=statuses.status, put=statuses.edit_status, delete=statuses.delete_status, ), ), - path("v1/statuses//source", statuses.status_source), + path("v1/statuses//source", statuses.status_source), # Notifications path("v1/notifications", notifications.notifications), path("v1/notifications/clear", notifications.dismiss_notifications), - path("v1/notifications/", notifications.get_notification), - path("v1/notifications//dismiss", notifications.dismiss_notification), + path("v1/notifications/", notifications.get_notification), + path("v1/notifications//dismiss", notifications.dismiss_notification), # Polls - path("v1/polls/", polls.get_poll), - path("v1/polls//votes", polls.vote_poll), + path("v1/polls/", polls.get_poll), + path("v1/polls//votes", polls.vote_poll), # Preferences path("v1/preferences", preferences.preferences), # Push @@ -105,26 +107,26 @@ urlpatterns = [ path("v2/search", search.search), # Statuses path("v1/statuses", statuses.post_status), - path("v1/statuses//context", statuses.status_context), - path("v1/statuses//favourite", statuses.favourite_status), - path("v1/statuses//unfavourite", statuses.unfavourite_status), - path("v1/statuses//favourited_by", statuses.favourited_by), - path("v1/statuses//reblog", statuses.reblog_status), - path("v1/statuses//unreblog", statuses.unreblog_status), - path("v1/statuses//reblogged_by", statuses.reblogged_by), - path("v1/statuses//bookmark", statuses.bookmark_status), - path("v1/statuses//unbookmark", statuses.unbookmark_status), - path("v1/statuses//pin", statuses.pin_status), - path("v1/statuses//unpin", statuses.unpin_status), + path("v1/statuses//context", statuses.status_context), + path("v1/statuses//favourite", statuses.favourite_status), + path("v1/statuses//unfavourite", statuses.unfavourite_status), + path("v1/statuses//favourited_by", statuses.favourited_by), + path("v1/statuses//reblog", statuses.reblog_status), + path("v1/statuses//unreblog", statuses.unreblog_status), + path("v1/statuses//reblogged_by", statuses.reblogged_by), + path("v1/statuses//bookmark", statuses.bookmark_status), + path("v1/statuses//unbookmark", statuses.unbookmark_status), + path("v1/statuses//pin", statuses.pin_status), + path("v1/statuses//unpin", statuses.unpin_status), # Tags path("v1/followed_tags", tags.followed_tags), - path("v1/tags/", tags.hashtag), - path("v1/tags//follow", tags.follow), - path("v1/tags//unfollow", tags.unfollow), + path("v1/tags/", tags.hashtag), + path("v1/tags//follow", tags.follow), + path("v1/tags//unfollow", tags.unfollow), # Timelines path("v1/timelines/home", timelines.home), path("v1/timelines/public", timelines.public), - path("v1/timelines/tag/", timelines.hashtag), + path("v1/timelines/tag/", timelines.hashtag), path("v1/conversations", timelines.conversations), path("v1/favourites", timelines.favourites), # Trends diff --git a/api/views/accounts.py b/api/views/accounts.py index 629051f..517238f 100644 --- a/api/views/accounts.py +++ b/api/views/accounts.py @@ -163,7 +163,7 @@ def lookup(request: HttpRequest, acct: str) -> schemas.Account: @scope_required("read:accounts") @api_view.get -def account(request, id: str) -> schemas.Account: +def account(request, id: int) -> schemas.Account: identity = get_object_or_404( Identity.objects.exclude(restriction=Identity.Restriction.blocked), pk=id, @@ -175,7 +175,7 @@ def account(request, id: str) -> schemas.Account: @api_view.get def account_statuses( request: HttpRequest, - id: str, + id: int, exclude_reblogs: bool = False, exclude_replies: bool = False, only_media: bool = False, @@ -238,7 +238,7 @@ def account_statuses( @scope_required("write:follows") @api_view.post -def account_follow(request, id: str, reblogs: bool = True) -> schemas.Relationship: +def account_follow(request, id: int, reblogs: bool = True) -> schemas.Relationship: identity = get_object_or_404( Identity.objects.exclude(restriction=Identity.Restriction.blocked), pk=id ) @@ -249,7 +249,7 @@ def account_follow(request, id: str, reblogs: bool = True) -> schemas.Relationsh @scope_required("write:follows") @api_view.post -def account_unfollow(request, id: str) -> schemas.Relationship: +def account_unfollow(request, id: int) -> schemas.Relationship: identity = get_object_or_404( Identity.objects.exclude(restriction=Identity.Restriction.blocked), pk=id ) @@ -260,7 +260,7 @@ def account_unfollow(request, id: str) -> schemas.Relationship: @scope_required("write:blocks") @api_view.post -def account_block(request, id: str) -> schemas.Relationship: +def account_block(request, id: int) -> schemas.Relationship: identity = get_object_or_404(Identity, pk=id) service = IdentityService(request.identity) service.block(identity) @@ -269,7 +269,7 @@ def account_block(request, id: str) -> schemas.Relationship: @scope_required("write:blocks") @api_view.post -def account_unblock(request, id: str) -> schemas.Relationship: +def account_unblock(request, id: int) -> schemas.Relationship: identity = get_object_or_404(Identity, pk=id) service = IdentityService(request.identity) service.unblock(identity) @@ -280,7 +280,7 @@ def account_unblock(request, id: str) -> schemas.Relationship: @api_view.post def account_mute( request, - id: str, + id: int, notifications: QueryOrBody[bool] = True, duration: QueryOrBody[int] = 0, ) -> schemas.Relationship: @@ -296,7 +296,7 @@ def account_mute( @scope_required("write:blocks") @api_view.post -def account_unmute(request, id: str) -> schemas.Relationship: +def account_unmute(request, id: int) -> schemas.Relationship: identity = get_object_or_404(Identity, pk=id) service = IdentityService(request.identity) service.unmute(identity) @@ -306,7 +306,7 @@ def account_unmute(request, id: str) -> schemas.Relationship: @api_view.get def account_following( request: HttpRequest, - id: str, + id: int, max_id: str | None = None, since_id: str | None = None, min_id: str | None = None, @@ -339,7 +339,7 @@ def account_following( @api_view.get def account_followers( request: HttpRequest, - id: str, + id: int, max_id: str | None = None, since_id: str | None = None, min_id: str | None = None, @@ -370,6 +370,6 @@ def account_followers( @api_view.get -def account_featured_tags(request: HttpRequest, id: str) -> list[schemas.FeaturedTag]: +def account_featured_tags(request: HttpRequest, id: int) -> list[schemas.FeaturedTag]: # Not implemented yet return [] diff --git a/api/views/announcements.py b/api/views/announcements.py index 27f1ab9..e6a2398 100644 --- a/api/views/announcements.py +++ b/api/views/announcements.py @@ -18,6 +18,6 @@ def announcement_list(request) -> list[schemas.Announcement]: @scope_required("write:notifications") @api_view.post -def announcement_dismiss(request, pk: str): +def announcement_dismiss(request, pk: int): announcement = get_object_or_404(Announcement, pk=pk) AnnouncementService(request.user).mark_seen(announcement) diff --git a/api/views/follow_requests.py b/api/views/follow_requests.py index cf54b92..f707c91 100644 --- a/api/views/follow_requests.py +++ b/api/views/follow_requests.py @@ -38,7 +38,7 @@ def follow_requests( @api_view.post def accept_follow_request( request: HttpRequest, - id: str | None = None, + id: int | None = None, ) -> schemas.Relationship: source_identity = get_object_or_404( Identity.objects.exclude(restriction=Identity.Restriction.blocked), pk=id @@ -51,7 +51,7 @@ def accept_follow_request( @api_view.post def reject_follow_request( request: HttpRequest, - id: str | None = None, + id: int | None = None, ) -> schemas.Relationship: source_identity = get_object_or_404( Identity.objects.exclude(restriction=Identity.Restriction.blocked), pk=id diff --git a/api/views/media.py b/api/views/media.py index e654e0d..4a9af27 100644 --- a/api/views/media.py +++ b/api/views/media.py @@ -52,7 +52,7 @@ def upload_media( @api_view.get def get_media( request, - id: str, + id: int, ) -> schemas.MediaAttachment: attachment = get_object_or_404(PostAttachment, pk=id) if attachment.post: @@ -67,7 +67,7 @@ def get_media( @api_view.put def update_media( request, - id: str, + id: int, description: QueryOrBody[str] = "", focus: QueryOrBody[str] = "0,0", ) -> schemas.MediaAttachment: diff --git a/api/views/polls.py b/api/views/polls.py index 7fa58c9..4e91afc 100644 --- a/api/views/polls.py +++ b/api/views/polls.py @@ -12,14 +12,14 @@ class PostVoteSchema(Schema): @scope_required("read:statuses") @api_view.get -def get_poll(request, id: str) -> schemas.Poll: +def get_poll(request, id: int) -> schemas.Poll: post = get_object_or_404(Post, pk=id, type=Post.Types.question) return schemas.Poll.from_post(post, identity=request.identity) @scope_required("write:statuses") @api_view.post -def vote_poll(request, id: str, details: PostVoteSchema) -> schemas.Poll: +def vote_poll(request, id: int, details: PostVoteSchema) -> schemas.Poll: post = get_object_or_404(Post, pk=id, type=Post.Types.question) PostInteraction.create_votes(post, request.identity, details.choices) post.refresh_from_db() diff --git a/api/views/statuses.py b/api/views/statuses.py index 4e7dea0..c36e383 100644 --- a/api/views/statuses.py +++ b/api/views/statuses.py @@ -64,7 +64,7 @@ class EditStatusSchema(Schema): media_attributes: list[MediaAttributesSchema] = [] -def post_for_id(request: HttpRequest, id: str) -> Post: +def post_for_id(request: HttpRequest, id: int) -> Post: """ Common logic to get a Post object for an ID, taking visibility into account. @@ -118,7 +118,7 @@ def post_status(request, details: PostStatusSchema) -> schemas.Status: @scope_required("read:statuses") @api_view.get -def status(request, id: str) -> schemas.Status: +def status(request, id: int) -> schemas.Status: post = post_for_id(request, id) interactions = PostInteraction.get_post_interactions([post], request.identity) return schemas.Status.from_post( @@ -128,7 +128,7 @@ def status(request, id: str) -> schemas.Status: @scope_required("write:statuses") @api_view.put -def edit_status(request, id: str, details: EditStatusSchema) -> schemas.Status: +def edit_status(request, id: int, details: EditStatusSchema) -> schemas.Status: post = post_for_id(request, id) if post.author != request.identity: raise ApiError(401, "Not the author of this status") @@ -147,7 +147,7 @@ def edit_status(request, id: str, details: EditStatusSchema) -> schemas.Status: @scope_required("write:statuses") @api_view.delete -def delete_status(request, id: str) -> schemas.Status: +def delete_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) if post.author != request.identity: raise ApiError(401, "Not the author of this status") @@ -157,14 +157,14 @@ def delete_status(request, id: str) -> schemas.Status: @scope_required("read:statuses") @api_view.get -def status_source(request, id: str) -> schemas.StatusSource: +def status_source(request, id: int) -> schemas.StatusSource: post = post_for_id(request, id) return schemas.StatusSource.from_post(post) @scope_required("read:statuses") @api_view.get -def status_context(request, id: str) -> schemas.Context: +def status_context(request, id: int) -> schemas.Context: post = post_for_id(request, id) service = PostService(post) ancestors, descendants = service.context(request.identity) @@ -189,7 +189,7 @@ def status_context(request, id: str) -> schemas.Context: @scope_required("write:favourites") @api_view.post -def favourite_status(request, id: str) -> schemas.Status: +def favourite_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) service = PostService(post) service.like_as(request.identity) @@ -201,7 +201,7 @@ def favourite_status(request, id: str) -> schemas.Status: @scope_required("write:favourites") @api_view.post -def unfavourite_status(request, id: str) -> schemas.Status: +def unfavourite_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) service = PostService(post) service.unlike_as(request.identity) @@ -214,7 +214,7 @@ def unfavourite_status(request, id: str) -> schemas.Status: @api_view.get def favourited_by( request: HttpRequest, - id: str, + id: int, max_id: str | None = None, since_id: str | None = None, min_id: str | None = None, @@ -256,7 +256,7 @@ def favourited_by( @api_view.get def reblogged_by( request: HttpRequest, - id: str, + id: int, max_id: str | None = None, since_id: str | None = None, min_id: str | None = None, @@ -297,7 +297,7 @@ def reblogged_by( @scope_required("write:favourites") @api_view.post -def reblog_status(request, id: str) -> schemas.Status: +def reblog_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) service = PostService(post) service.boost_as(request.identity) @@ -309,7 +309,7 @@ def reblog_status(request, id: str) -> schemas.Status: @scope_required("write:favourites") @api_view.post -def unreblog_status(request, id: str) -> schemas.Status: +def unreblog_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) service = PostService(post) service.unboost_as(request.identity) @@ -321,7 +321,7 @@ def unreblog_status(request, id: str) -> schemas.Status: @scope_required("write:bookmarks") @api_view.post -def bookmark_status(request, id: str) -> schemas.Status: +def bookmark_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) request.identity.bookmarks.get_or_create(post=post) interactions = PostInteraction.get_post_interactions([post], request.identity) @@ -332,7 +332,7 @@ def bookmark_status(request, id: str) -> schemas.Status: @scope_required("write:bookmarks") @api_view.post -def unbookmark_status(request, id: str) -> schemas.Status: +def unbookmark_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) request.identity.bookmarks.filter(post=post).delete() interactions = PostInteraction.get_post_interactions([post], request.identity) @@ -343,7 +343,7 @@ def unbookmark_status(request, id: str) -> schemas.Status: @scope_required("write:accounts") @api_view.post -def pin_status(request, id: str) -> schemas.Status: +def pin_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) try: PostService(post).pin_as(request.identity) @@ -357,7 +357,7 @@ def pin_status(request, id: str) -> schemas.Status: @scope_required("write:accounts") @api_view.post -def unpin_status(request, id: str) -> schemas.Status: +def unpin_status(request, id: int) -> schemas.Status: post = post_for_id(request, id) PostService(post).unpin_as(request.identity) interactions = PostInteraction.get_post_interactions([post], request.identity)