From 35a45f1c55fba69d690929c9420df565e7c5efcc Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Mon, 12 Dec 2022 00:54:51 -0700 Subject: A few more fixes and a bad test --- activities/models/post.py | 9 ++++++++- api/pagination.py | 20 ++++++++++++++++---- tests/activities/models/test_post_targets.py | 8 ++++---- tests/api/test_statuses.py | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 tests/api/test_statuses.py diff --git a/activities/models/post.py b/activities/models/post.py index da5a98b..8e355bf 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -784,13 +784,20 @@ class Post(StatorModel): reply_parent = None if self.in_reply_to: reply_parent = Post.objects.filter(object_uri=self.in_reply_to).first() + visibility_mapping = { + self.Visibilities.public: "public", + self.Visibilities.unlisted: "unlisted", + self.Visibilities.followers: "private", + self.Visibilities.mentioned: "direct", + self.Visibilities.local_only: "public", + } value = { "id": self.pk, "uri": self.object_uri, "created_at": format_ld_date(self.published), "account": self.author.to_mastodon_json(), "content": self.safe_content_remote(), - "visibility": "public", + "visibility": visibility_mapping[self.visibility], "sensitive": self.sensitive, "spoiler_text": self.summary or "", "media_attachments": [ diff --git a/api/pagination.py b/api/pagination.py index 0539ae8..6bd02c9 100644 --- a/api/pagination.py +++ b/api/pagination.py @@ -1,3 +1,6 @@ +from django.db import models + + class MastodonPaginator: """ Paginates in the Mastodon style (max_id, min_id, etc) @@ -5,7 +8,7 @@ class MastodonPaginator: def __init__( self, - anchor_model, + anchor_model: type[models.Model], sort_attribute: str = "created", default_limit: int = 20, max_limit: int = 40, @@ -24,19 +27,28 @@ class MastodonPaginator: limit: int | None, ): if max_id: - anchor = self.anchor_model.objects.get(pk=max_id) + try: + anchor = self.anchor_model.objects.get(pk=max_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__lt": getattr(anchor, self.sort_attribute)} ) if since_id: - anchor = self.anchor_model.objects.get(pk=since_id) + try: + anchor = self.anchor_model.objects.get(pk=since_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} ) if min_id: # Min ID requires items _immediately_ newer than specified, so we # invert the ordering to accomodate - anchor = self.anchor_model.objects.get(pk=min_id) + try: + anchor = self.anchor_model.objects.get(pk=min_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} ).order_by(self.sort_attribute) diff --git a/tests/activities/models/test_post_targets.py b/tests/activities/models/test_post_targets.py index 1e4bbc4..0fbfdd6 100644 --- a/tests/activities/models/test_post_targets.py +++ b/tests/activities/models/test_post_targets.py @@ -46,8 +46,8 @@ def test_post_targets_simple(identity, other_identity, remote_identity): post.local = False post.save() targets = async_to_sync(post.aget_targets)() - # Only targets locals - assert targets == {identity, other_identity} + # Only targets locals who are mentioned + assert targets == {other_identity} @pytest.mark.django_db @@ -87,11 +87,11 @@ def test_post_followers(identity, other_identity, remote_identity): targets = async_to_sync(post.aget_targets)() assert targets == {identity, other_identity, remote_identity} - # Remote post only targets local followers + # Remote post only targets local followers, not the author post.local = False post.save() targets = async_to_sync(post.aget_targets)() - assert targets == {identity, other_identity} + assert targets == {other_identity} # Local Only post only targets local followers post.local = True diff --git a/tests/api/test_statuses.py b/tests/api/test_statuses.py new file mode 100644 index 0000000..1b00642 --- /dev/null +++ b/tests/api/test_statuses.py @@ -0,0 +1,17 @@ +import pytest + + +@pytest.mark.django_db +def test_post_status(api_token, identity, client): + response = client.post( + "/api/v1/statuses", + HTTP_AUTHORIZATION=f"Bearer {api_token.token}", + HTTP_ACCEPT="application/json", + content_type="application/json", + data={ + "status": "Hello, world!", + "visibility": "unlisted", + }, + ).json() + assert response["content"] == "

Hello, world!

" + assert response["visibility"] == "unlisted" -- cgit v1.2.3