2023-02-15 17:42:56 +00:00
|
|
|
from datetime import timedelta
|
2022-12-11 19:37:28 +00:00
|
|
|
from typing import Literal
|
|
|
|
|
2023-02-07 19:07:15 +00:00
|
|
|
from django.http import HttpRequest
|
2022-12-11 19:37:28 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2023-02-15 17:42:56 +00:00
|
|
|
from django.utils import timezone
|
2023-02-14 02:40:10 +00:00
|
|
|
from hatchway import ApiError, ApiResponse, Schema, api_view
|
2022-12-11 19:37:28 +00:00
|
|
|
|
2022-12-30 18:19:38 +00:00
|
|
|
from activities.models import (
|
|
|
|
Post,
|
|
|
|
PostAttachment,
|
|
|
|
PostInteraction,
|
|
|
|
PostInteractionStates,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
2022-12-20 09:59:06 +00:00
|
|
|
from activities.services import PostService
|
2022-12-11 19:37:28 +00:00
|
|
|
from api import schemas
|
2023-02-19 18:37:02 +00:00
|
|
|
from api.decorators import scope_required
|
2023-03-24 01:09:03 +00:00
|
|
|
from api.pagination import MastodonPaginator, PaginatingApiResponse, PaginationResult
|
2022-12-11 19:37:28 +00:00
|
|
|
from core.models import Config
|
|
|
|
|
|
|
|
|
2023-02-15 17:42:56 +00:00
|
|
|
class PostPollSchema(Schema):
|
|
|
|
options: list[str]
|
|
|
|
expires_in: int
|
|
|
|
multiple: bool = False
|
|
|
|
hide_totals: bool = False
|
|
|
|
|
|
|
|
def dict(self):
|
|
|
|
return {
|
|
|
|
"type": "Question",
|
|
|
|
"mode": "anyOf" if self.multiple else "oneOf",
|
|
|
|
"options": [
|
|
|
|
{"name": name, "type": "Note", "votes": 0} for name in self.options
|
|
|
|
],
|
|
|
|
"voter_count": 0,
|
|
|
|
"end_time": timezone.now() + timedelta(seconds=self.expires_in),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 19:37:28 +00:00
|
|
|
class PostStatusSchema(Schema):
|
|
|
|
status: str
|
|
|
|
in_reply_to_id: str | None = None
|
|
|
|
sensitive: bool = False
|
|
|
|
spoiler_text: str | None = None
|
|
|
|
visibility: Literal["public", "unlisted", "private", "direct"] = "public"
|
|
|
|
language: str | None = None
|
|
|
|
scheduled_at: str | None = None
|
|
|
|
media_ids: list[str] = []
|
2023-02-15 17:42:56 +00:00
|
|
|
poll: PostPollSchema | None = None
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-04-11 15:35:36 +00:00
|
|
|
class MediaAttributesSchema(Schema):
|
|
|
|
id: str
|
|
|
|
description: str
|
|
|
|
|
|
|
|
|
2023-02-14 02:40:10 +00:00
|
|
|
class EditStatusSchema(Schema):
|
|
|
|
status: str
|
|
|
|
sensitive: bool = False
|
|
|
|
spoiler_text: str | None = None
|
|
|
|
language: str | None = None
|
|
|
|
media_ids: list[str] = []
|
2023-04-11 15:35:36 +00:00
|
|
|
media_attributes: list[MediaAttributesSchema] = []
|
2023-02-14 02:40:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def post_for_id(request: HttpRequest, id: str) -> Post:
|
|
|
|
"""
|
|
|
|
Common logic to get a Post object for an ID, taking visibility into
|
|
|
|
account.
|
|
|
|
"""
|
|
|
|
if request.identity:
|
|
|
|
queryset = Post.objects.not_hidden().visible_to(
|
|
|
|
request.identity, include_replies=True
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
queryset = Post.objects.not_hidden().unlisted()
|
|
|
|
return get_object_or_404(queryset, pk=id)
|
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
|
|
|
def post_status(request, details: PostStatusSchema) -> schemas.Status:
|
2022-12-11 19:37:28 +00:00
|
|
|
# Check text length
|
|
|
|
if len(details.status) > Config.system.post_length:
|
2023-02-14 02:40:10 +00:00
|
|
|
raise ApiError(400, "Status is too long")
|
2022-12-11 19:37:28 +00:00
|
|
|
if len(details.status) == 0 and not details.media_ids:
|
2023-02-14 02:40:10 +00:00
|
|
|
raise ApiError(400, "Status is empty")
|
2022-12-11 19:37:28 +00:00
|
|
|
# Grab attachments
|
|
|
|
attachments = [get_object_or_404(PostAttachment, pk=id) for id in details.media_ids]
|
|
|
|
# Create the Post
|
|
|
|
visibility_map = {
|
|
|
|
"public": Post.Visibilities.public,
|
|
|
|
"unlisted": Post.Visibilities.unlisted,
|
|
|
|
"private": Post.Visibilities.followers,
|
|
|
|
"direct": Post.Visibilities.mentioned,
|
|
|
|
}
|
|
|
|
reply_post = None
|
|
|
|
if details.in_reply_to_id:
|
|
|
|
try:
|
|
|
|
reply_post = Post.objects.get(pk=details.in_reply_to_id)
|
|
|
|
except Post.DoesNotExist:
|
|
|
|
pass
|
|
|
|
post = Post.create_local(
|
|
|
|
author=request.identity,
|
|
|
|
content=details.status,
|
|
|
|
summary=details.spoiler_text,
|
|
|
|
sensitive=details.sensitive,
|
|
|
|
visibility=visibility_map[details.visibility],
|
|
|
|
reply_to=reply_post,
|
|
|
|
attachments=attachments,
|
2023-02-15 17:42:56 +00:00
|
|
|
question=details.poll.dict() if details.poll else None,
|
2022-12-11 19:37:28 +00:00
|
|
|
)
|
|
|
|
# Add their own timeline event for immediate visibility
|
|
|
|
TimelineEvent.add_post(request.identity, post)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(post, identity=request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
|
|
|
def status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:statuses")
|
2023-02-14 02:40:10 +00:00
|
|
|
@api_view.put
|
|
|
|
def edit_status(request, id: str, details: EditStatusSchema) -> schemas.Status:
|
|
|
|
post = post_for_id(request, id)
|
|
|
|
if post.author != request.identity:
|
|
|
|
raise ApiError(401, "Not the author of this status")
|
|
|
|
# Grab attachments
|
|
|
|
attachments = [get_object_or_404(PostAttachment, pk=id) for id in details.media_ids]
|
|
|
|
# Update all details, as the client must provide them all
|
|
|
|
post.edit_local(
|
|
|
|
content=details.status,
|
|
|
|
summary=details.spoiler_text,
|
|
|
|
sensitive=details.sensitive,
|
|
|
|
attachments=attachments,
|
2023-04-11 15:35:36 +00:00
|
|
|
attachment_attributes=details.media_attributes,
|
2023-02-14 02:40:10 +00:00
|
|
|
)
|
|
|
|
return schemas.Status.from_post(post)
|
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.delete
|
|
|
|
def delete_status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
|
|
|
if post.author != request.identity:
|
|
|
|
raise ApiError(401, "Not the author of this status")
|
2022-12-27 18:53:12 +00:00
|
|
|
PostService(post).delete()
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(post, identity=request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-14 02:40:10 +00:00
|
|
|
@api_view.get
|
|
|
|
def status_source(request, id: str) -> schemas.StatusSource:
|
|
|
|
post = post_for_id(request, id)
|
|
|
|
return schemas.StatusSource.from_post(post)
|
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
|
|
|
def status_context(request, id: str) -> schemas.Context:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 09:59:06 +00:00
|
|
|
service = PostService(post)
|
|
|
|
ancestors, descendants = service.context(request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions(
|
2022-12-20 09:59:06 +00:00
|
|
|
ancestors + descendants, request.identity
|
2022-12-11 19:37:28 +00:00
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
return schemas.Context(
|
|
|
|
ancestors=[
|
2023-02-15 17:42:56 +00:00
|
|
|
schemas.Status.from_post(
|
|
|
|
p, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
for p in reversed(ancestors)
|
2022-12-22 03:21:31 +00:00
|
|
|
],
|
2023-02-07 19:07:15 +00:00
|
|
|
descendants=[
|
2023-02-15 17:42:56 +00:00
|
|
|
schemas.Status.from_post(
|
|
|
|
p, interactions=interactions, identity=request.identity
|
|
|
|
)
|
|
|
|
for p in descendants
|
2022-12-11 19:37:28 +00:00
|
|
|
],
|
2023-02-07 19:07:15 +00:00
|
|
|
)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
|
|
|
def favourite_status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 09:59:06 +00:00
|
|
|
service = PostService(post)
|
|
|
|
service.like_as(request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
|
|
|
def unfavourite_status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 09:59:06 +00:00
|
|
|
service = PostService(post)
|
|
|
|
service.unlike_as(request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-30 18:19:38 +00:00
|
|
|
def favourited_by(
|
|
|
|
request: HttpRequest,
|
|
|
|
id: str,
|
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> ApiResponse[list[schemas.Account]]:
|
2022-12-30 18:19:38 +00:00
|
|
|
"""
|
|
|
|
View who favourited a given status.
|
|
|
|
"""
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-30 18:19:38 +00:00
|
|
|
|
2023-01-09 06:06:09 +00:00
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 19:07:15 +00:00
|
|
|
pager: PaginationResult[PostInteraction] = paginator.paginate(
|
2022-12-30 18:19:38 +00:00
|
|
|
post.interactions.filter(
|
|
|
|
type=PostInteraction.Types.like,
|
|
|
|
state__in=PostInteractionStates.group_active(),
|
2023-01-09 06:06:09 +00:00
|
|
|
).select_related("identity"),
|
2022-12-30 18:19:38 +00:00
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
2023-03-31 19:40:15 +00:00
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
|
|
|
|
|
|
|
return PaginatingApiResponse(
|
|
|
|
[
|
|
|
|
schemas.Account.from_identity(
|
|
|
|
interaction.identity,
|
|
|
|
include_counts=False,
|
|
|
|
)
|
|
|
|
for interaction in pager.results
|
|
|
|
],
|
|
|
|
request=request,
|
|
|
|
include_params=[
|
|
|
|
"limit",
|
|
|
|
"id",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view.get
|
|
|
|
def reblogged_by(
|
|
|
|
request: HttpRequest,
|
|
|
|
id: str,
|
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
|
|
|
) -> ApiResponse[list[schemas.Account]]:
|
|
|
|
"""
|
|
|
|
View who reblogged a given status.
|
|
|
|
"""
|
|
|
|
post = post_for_id(request, id)
|
|
|
|
|
|
|
|
paginator = MastodonPaginator()
|
|
|
|
pager: PaginationResult[PostInteraction] = paginator.paginate(
|
|
|
|
post.interactions.filter(
|
|
|
|
type=PostInteraction.Types.boost,
|
|
|
|
state__in=PostInteractionStates.group_active(),
|
|
|
|
).select_related("identity"),
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
2022-12-30 18:19:38 +00:00
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
|
|
|
|
2023-03-24 01:09:03 +00:00
|
|
|
return PaginatingApiResponse(
|
2023-02-07 19:07:15 +00:00
|
|
|
[
|
|
|
|
schemas.Account.from_identity(
|
|
|
|
interaction.identity,
|
|
|
|
include_counts=False,
|
|
|
|
)
|
|
|
|
for interaction in pager.results
|
|
|
|
],
|
2023-03-24 01:09:03 +00:00
|
|
|
request=request,
|
|
|
|
include_params=[
|
|
|
|
"limit",
|
|
|
|
"id",
|
|
|
|
],
|
2023-02-07 19:07:15 +00:00
|
|
|
)
|
2022-12-30 18:19:38 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
|
|
|
def reblog_status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 09:59:06 +00:00
|
|
|
service = PostService(post)
|
|
|
|
service.boost_as(request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
|
|
|
def unreblog_status(request, id: str) -> schemas.Status:
|
2023-02-14 02:40:10 +00:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 09:59:06 +00:00
|
|
|
service = PostService(post)
|
|
|
|
service.unboost_as(request.identity)
|
2022-12-11 19:37:28 +00:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 17:42:56 +00:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2023-03-11 18:17:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@scope_required("write:bookmarks")
|
|
|
|
@api_view.post
|
|
|
|
def bookmark_status(request, id: str) -> schemas.Status:
|
|
|
|
post = post_for_id(request, id)
|
|
|
|
request.identity.bookmarks.get_or_create(post=post)
|
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, bookmarks={post.pk}, identity=request.identity
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@scope_required("write:bookmarks")
|
|
|
|
@api_view.post
|
|
|
|
def unbookmark_status(request, id: str) -> schemas.Status:
|
|
|
|
post = post_for_id(request, id)
|
|
|
|
request.identity.bookmarks.filter(post=post).delete()
|
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|