summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-12 00:54:51 -0700
committerAndrew Godwin2022-12-12 11:56:49 -0700
commit35a45f1c55fba69d690929c9420df565e7c5efcc (patch)
tree0969cf0b9b6c2dec04ec16d0d7444ebff744e377
parent7f02d51ba04a533391a2c09b5f780fc8b0193ef7 (diff)
downloadtakahe-35a45f1c55fba69d690929c9420df565e7c5efcc.tar.gz
takahe-35a45f1c55fba69d690929c9420df565e7c5efcc.tar.bz2
takahe-35a45f1c55fba69d690929c9420df565e7c5efcc.zip
A few more fixes and a bad test
-rw-r--r--activities/models/post.py9
-rw-r--r--api/pagination.py20
-rw-r--r--tests/activities/models/test_post_targets.py8
-rw-r--r--tests/api/test_statuses.py17
4 files changed, 45 insertions, 9 deletions
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"] == "<p>Hello, world!</p>"
+ assert response["visibility"] == "unlisted"