From 9876b81f66226d79954e24bb81e35cdb2f2c7a37 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 13 Jan 2023 16:09:26 -0700 Subject: [PATCH] Add announcements client API --- api/schemas.py | 16 ++++++++++++++++ api/views/__init__.py | 1 + api/views/announcements.py | 23 +++++++++++++++++++++++ users/models/announcement.py | 22 ++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 api/views/announcements.py diff --git a/api/schemas.py b/api/schemas.py index 9f8541f..8163799 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -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 diff --git a/api/views/__init__.py b/api/views/__init__.py index 917b8cd..26fcbaf 100644 --- a/api/views/__init__.py +++ b/api/views/__init__.py @@ -1,4 +1,5 @@ from .accounts import * # noqa +from .announcements import * # noqa from .apps import * # noqa from .emoji import * # noqa from .filters import * # noqa diff --git a/api/views/announcements.py b/api/views/announcements.py new file mode 100644 index 0000000..0fd44af --- /dev/null +++ b/api/views/announcements.py @@ -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) diff --git a/users/models/announcement.py b/users/models/announcement.py index cc55fd2..c73cfa5 100644 --- a/users/models/announcement.py +++ b/users/models/announcement.py @@ -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