Merge pull request #439 from mouse-reeve/filter-outbox

Updates outbox
This commit is contained in:
Mouse Reeve 2020-12-30 13:13:47 -08:00 committed by GitHub
commit 747167c579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 10 deletions

View file

@ -111,6 +111,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
queryset = Status.objects.filter(
user=self,
deleted=False,
privacy__in=['public', 'unlisted'],
).select_subclasses().order_by('-published_date')
return self.to_ordered_collection(queryset, \
remote_id=self.outbox, **kwargs)

View file

@ -2,8 +2,10 @@
import re
from django.db import IntegrityError, transaction
from django.http import HttpResponseNotFound, JsonResponse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET
from markdown import markdown
from requests import HTTPError
@ -20,17 +22,11 @@ from bookwyrm.utils import regex
@csrf_exempt
@require_GET
def outbox(request, username):
''' outbox for the requested user '''
if request.method != 'GET':
return HttpResponseNotFound()
user = get_object_or_404(models.User, localname=username)
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
# collection overview
return JsonResponse(
user.to_outbox(**request.GET),
encoder=activitypub.ActivityEncoder

View file

@ -3,7 +3,9 @@ import json
import pathlib
from unittest.mock import patch
from django.http import JsonResponse
from django.test import TestCase
from django.test.client import RequestFactory
import responses
from bookwyrm import models, outgoing
@ -14,6 +16,7 @@ class Outgoing(TestCase):
''' sends out activities '''
def setUp(self):
''' we'll need some data '''
self.factory = RequestFactory()
with patch('bookwyrm.models.user.set_remote_server'):
self.remote_user = models.User.objects.create_user(
'rat', 'rat@rat.com', 'ratword',
@ -24,7 +27,7 @@ class Outgoing(TestCase):
)
self.local_user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword', local=True,
remote_id='https://example.com/users/mouse',
localname='mouse', remote_id='https://example.com/users/mouse',
)
datafile = pathlib.Path(__file__).parent.joinpath(
@ -46,6 +49,45 @@ class Outgoing(TestCase):
)
def test_outbox(self):
''' returns user's statuses '''
request = self.factory.get('')
result = outgoing.outbox(request, 'mouse')
self.assertIsInstance(result, JsonResponse)
def test_outbox_bad_method(self):
''' can't POST to outbox '''
request = self.factory.post('')
result = outgoing.outbox(request, 'mouse')
self.assertEqual(result.status_code, 405)
def test_outbox_unknown_user(self):
''' should 404 for unknown and remote users '''
request = self.factory.post('')
result = outgoing.outbox(request, 'beepboop')
self.assertEqual(result.status_code, 405)
result = outgoing.outbox(request, 'rat')
self.assertEqual(result.status_code, 405)
def test_outbox_privacy(self):
''' don't show dms et cetera in outbox '''
models.Status.objects.create(
content='PRIVATE!!', user=self.local_user, privacy='direct')
models.Status.objects.create(
content='bffs ONLY', user=self.local_user, privacy='followers')
models.Status.objects.create(
content='unlisted status', user=self.local_user, privacy='unlisted')
models.Status.objects.create(
content='look at this', user=self.local_user, privacy='public')
request = self.factory.get('')
result = outgoing.outbox(request, 'mouse')
self.assertIsInstance(result, JsonResponse)
data = json.loads(result.content)
self.assertEqual(data['type'], 'OrderedCollection')
self.assertEqual(data['totalItems'], 2)
def test_handle_follow(self):
''' send a follow request '''
self.assertEqual(models.UserFollowRequest.objects.count(), 0)