2023-02-15 09:42:56 -08:00
|
|
|
from datetime import timedelta
|
2022-12-11 11:37:28 -08:00
|
|
|
from typing import Literal
|
|
|
|
|
2023-02-07 11:07:15 -08:00
|
|
|
from django.http import HttpRequest
|
2022-12-11 11:37:28 -08:00
|
|
|
from django.shortcuts import get_object_or_404
|
2023-02-15 09:42:56 -08:00
|
|
|
from django.utils import timezone
|
2023-02-13 18:40:10 -08:00
|
|
|
from hatchway import ApiError, ApiResponse, Schema, api_view
|
2022-12-11 11:37:28 -08:00
|
|
|
|
2022-12-30 10:19:38 -08:00
|
|
|
from activities.models import (
|
|
|
|
Post,
|
|
|
|
PostAttachment,
|
|
|
|
PostInteraction,
|
|
|
|
PostInteractionStates,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
2022-12-20 01:59:06 -08:00
|
|
|
from activities.services import PostService
|
2022-12-11 11:37:28 -08:00
|
|
|
from api import schemas
|
2023-02-19 10:37:02 -08:00
|
|
|
from api.decorators import scope_required
|
2023-02-07 11:07:15 -08:00
|
|
|
from api.pagination import MastodonPaginator, PaginationResult
|
2022-12-11 11:37:28 -08:00
|
|
|
from core.models import Config
|
|
|
|
|
|
|
|
|
2023-02-15 09:42:56 -08: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 11:37:28 -08: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 09:42:56 -08:00
|
|
|
poll: PostPollSchema | None = None
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-13 18:40:10 -08:00
|
|
|
class EditStatusSchema(Schema):
|
|
|
|
status: str
|
|
|
|
sensitive: bool = False
|
|
|
|
spoiler_text: str | None = None
|
|
|
|
language: str | None = None
|
|
|
|
media_ids: list[str] = []
|
|
|
|
|
|
|
|
|
|
|
|
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 10:37:02 -08:00
|
|
|
@scope_required("write:statuses")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.post
|
|
|
|
def post_status(request, details: PostStatusSchema) -> schemas.Status:
|
2022-12-11 11:37:28 -08:00
|
|
|
# Check text length
|
|
|
|
if len(details.status) > Config.system.post_length:
|
2023-02-13 18:40:10 -08:00
|
|
|
raise ApiError(400, "Status is too long")
|
2022-12-11 11:37:28 -08:00
|
|
|
if len(details.status) == 0 and not details.media_ids:
|
2023-02-13 18:40:10 -08:00
|
|
|
raise ApiError(400, "Status is empty")
|
2022-12-11 11:37:28 -08: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 09:42:56 -08:00
|
|
|
question=details.poll.dict() if details.poll else None,
|
2022-12-11 11:37:28 -08:00
|
|
|
)
|
|
|
|
# Add their own timeline event for immediate visibility
|
|
|
|
TimelineEvent.add_post(request.identity, post)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(post, identity=request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.get
|
|
|
|
def status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:statuses")
|
2023-02-13 18:40:10 -08: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,
|
|
|
|
)
|
|
|
|
return schemas.Status.from_post(post)
|
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:statuses")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.delete
|
|
|
|
def delete_status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
|
|
|
if post.author != request.identity:
|
|
|
|
raise ApiError(401, "Not the author of this status")
|
2022-12-27 10:53:12 -08:00
|
|
|
PostService(post).delete()
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(post, identity=request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("read:statuses")
|
2023-02-13 18:40:10 -08: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 10:37:02 -08:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.get
|
|
|
|
def status_context(request, id: str) -> schemas.Context:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
|
|
|
ancestors, descendants = service.context(request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions(
|
2022-12-20 01:59:06 -08:00
|
|
|
ancestors + descendants, request.identity
|
2022-12-11 11:37:28 -08:00
|
|
|
)
|
2023-02-07 11:07:15 -08:00
|
|
|
return schemas.Context(
|
|
|
|
ancestors=[
|
2023-02-15 09:42:56 -08:00
|
|
|
schemas.Status.from_post(
|
|
|
|
p, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2023-02-07 11:07:15 -08:00
|
|
|
for p in reversed(ancestors)
|
2022-12-21 19:21:31 -08:00
|
|
|
],
|
2023-02-07 11:07:15 -08:00
|
|
|
descendants=[
|
2023-02-15 09:42:56 -08:00
|
|
|
schemas.Status.from_post(
|
|
|
|
p, interactions=interactions, identity=request.identity
|
|
|
|
)
|
|
|
|
for p in descendants
|
2022-12-11 11:37:28 -08:00
|
|
|
],
|
2023-02-07 11:07:15 -08:00
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.post
|
|
|
|
def favourite_status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
|
|
|
service.like_as(request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.post
|
|
|
|
def unfavourite_status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
|
|
|
service.unlike_as(request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.get
|
2022-12-30 10:19:38 -08: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 11:07:15 -08:00
|
|
|
) -> ApiResponse[list[schemas.Account]]:
|
2022-12-30 10:19:38 -08:00
|
|
|
"""
|
|
|
|
View who favourited a given status.
|
|
|
|
"""
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-30 10:19:38 -08:00
|
|
|
|
2023-01-08 22:06:09 -08:00
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 11:07:15 -08:00
|
|
|
pager: PaginationResult[PostInteraction] = paginator.paginate(
|
2022-12-30 10:19:38 -08:00
|
|
|
post.interactions.filter(
|
|
|
|
type=PostInteraction.Types.like,
|
|
|
|
state__in=PostInteractionStates.group_active(),
|
2023-01-08 22:06:09 -08:00
|
|
|
).select_related("identity"),
|
2022-12-30 10:19:38 -08:00
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
|
|
|
|
2023-02-07 11:07:15 -08:00
|
|
|
headers = {}
|
2022-12-30 10:19:38 -08:00
|
|
|
if pager.results:
|
2023-02-07 11:07:15 -08:00
|
|
|
headers = {"link": pager.link_header(request, ["limit"])}
|
|
|
|
return ApiResponse(
|
|
|
|
[
|
|
|
|
schemas.Account.from_identity(
|
|
|
|
interaction.identity,
|
|
|
|
include_counts=False,
|
|
|
|
)
|
|
|
|
for interaction in pager.results
|
|
|
|
],
|
|
|
|
headers=headers,
|
|
|
|
)
|
2022-12-30 10:19:38 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.post
|
|
|
|
def reblog_status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
|
|
|
service.boost_as(request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("write:favourites")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.post
|
|
|
|
def unreblog_status(request, id: str) -> schemas.Status:
|
2023-02-13 18:40:10 -08:00
|
|
|
post = post_for_id(request, id)
|
2022-12-20 01:59:06 -08:00
|
|
|
service = PostService(post)
|
|
|
|
service.unboost_as(request.identity)
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions([post], request.identity)
|
2023-02-15 09:42:56 -08:00
|
|
|
return schemas.Status.from_post(
|
|
|
|
post, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2023-03-11 10:17:20 -08: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
|
|
|
|
)
|