A few more fixes and a bad test

This commit is contained in:
Andrew Godwin 2022-12-12 00:54:51 -07:00
parent 7f02d51ba0
commit 35a45f1c55
4 changed files with 45 additions and 9 deletions

View file

@ -784,13 +784,20 @@ class Post(StatorModel):
reply_parent = None reply_parent = None
if self.in_reply_to: if self.in_reply_to:
reply_parent = Post.objects.filter(object_uri=self.in_reply_to).first() 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 = { value = {
"id": self.pk, "id": self.pk,
"uri": self.object_uri, "uri": self.object_uri,
"created_at": format_ld_date(self.published), "created_at": format_ld_date(self.published),
"account": self.author.to_mastodon_json(), "account": self.author.to_mastodon_json(),
"content": self.safe_content_remote(), "content": self.safe_content_remote(),
"visibility": "public", "visibility": visibility_mapping[self.visibility],
"sensitive": self.sensitive, "sensitive": self.sensitive,
"spoiler_text": self.summary or "", "spoiler_text": self.summary or "",
"media_attachments": [ "media_attachments": [

View file

@ -1,3 +1,6 @@
from django.db import models
class MastodonPaginator: class MastodonPaginator:
""" """
Paginates in the Mastodon style (max_id, min_id, etc) Paginates in the Mastodon style (max_id, min_id, etc)
@ -5,7 +8,7 @@ class MastodonPaginator:
def __init__( def __init__(
self, self,
anchor_model, anchor_model: type[models.Model],
sort_attribute: str = "created", sort_attribute: str = "created",
default_limit: int = 20, default_limit: int = 20,
max_limit: int = 40, max_limit: int = 40,
@ -24,19 +27,28 @@ class MastodonPaginator:
limit: int | None, limit: int | None,
): ):
if max_id: 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( queryset = queryset.filter(
**{self.sort_attribute + "__lt": getattr(anchor, self.sort_attribute)} **{self.sort_attribute + "__lt": getattr(anchor, self.sort_attribute)}
) )
if since_id: 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( queryset = queryset.filter(
**{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)}
) )
if min_id: if min_id:
# Min ID requires items _immediately_ newer than specified, so we # Min ID requires items _immediately_ newer than specified, so we
# invert the ordering to accomodate # 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( queryset = queryset.filter(
**{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)}
).order_by(self.sort_attribute) ).order_by(self.sort_attribute)

View file

@ -46,8 +46,8 @@ def test_post_targets_simple(identity, other_identity, remote_identity):
post.local = False post.local = False
post.save() post.save()
targets = async_to_sync(post.aget_targets)() targets = async_to_sync(post.aget_targets)()
# Only targets locals # Only targets locals who are mentioned
assert targets == {identity, other_identity} assert targets == {other_identity}
@pytest.mark.django_db @pytest.mark.django_db
@ -87,11 +87,11 @@ def test_post_followers(identity, other_identity, remote_identity):
targets = async_to_sync(post.aget_targets)() targets = async_to_sync(post.aget_targets)()
assert targets == {identity, other_identity, remote_identity} 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.local = False
post.save() post.save()
targets = async_to_sync(post.aget_targets)() targets = async_to_sync(post.aget_targets)()
assert targets == {identity, other_identity} assert targets == {other_identity}
# Local Only post only targets local followers # Local Only post only targets local followers
post.local = True post.local = True

View file

@ -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"