Add announcements client API

This commit is contained in:
Andrew Godwin 2023-01-13 16:09:26 -07:00
parent 8b3106b852
commit 9876b81f66
4 changed files with 62 additions and 0 deletions

View file

@ -170,3 +170,19 @@ class Context(Schema):
class FamiliarFollowers(Schema):
id: str
accounts: list[Account]
class Announcement(Schema):
id: str
content: str
starts_at: str | None = Field(...)
ends_at: str | None = Field(...)
all_day: bool
published_at: str
updated_at: str
read: bool | None # Only missing for anonymous responses
mentions: list[Account]
statuses: list[Status]
tags: list[Tag]
emojis: list[CustomEmoji]
reactions: list

View file

@ -1,4 +1,5 @@
from .accounts import * # noqa
from .announcements import * # noqa
from .apps import * # noqa
from .emoji import * # noqa
from .filters import * # noqa

View file

@ -0,0 +1,23 @@
from django.shortcuts import get_object_or_404
from api import schemas
from api.decorators import identity_required
from api.views.base import api_router
from users.models import Announcement
from users.services import AnnouncementService
@api_router.get("/v1/announcements", response=list[schemas.Announcement])
@identity_required
def announcement_list(request):
return [
a.to_mastodon_json(request.user)
for a in AnnouncementService(request.user).visible()
]
@api_router.post("/v1/announcements/{pk}/dismiss")
@identity_required
def announcement_dismiss(request, pk: str):
announcement = get_object_or_404(Announcement, pk=pk)
AnnouncementService(request.user).mark_seen(announcement)

View file

@ -4,6 +4,8 @@ from django.db import models
from django.utils import timezone
from django.utils.safestring import mark_safe
from core.ld import format_ld_date
class Announcement(models.Model):
"""
@ -61,3 +63,23 @@ class Announcement(models.Model):
@property
def before_end(self) -> bool:
return timezone.now() <= self.end if self.end else True
def to_mastodon_json(self, user=None):
value = {
"id": str(self.id),
"content": self.html,
"starts_at": format_ld_date(self.start) if self.start else None,
"ends_at": format_ld_date(self.end) if self.end else None,
"all_day": False,
"published_at": format_ld_date(self.start or self.created),
"updated_at": format_ld_date(self.updated),
"mentions": [],
"statuses": [],
"tags": [],
"emojis": [],
"reactions": [],
}
if user:
# TODO: Aggregate query
value["read"] = self.seen.filter(id=user.id).exists()
return value