Skip to content

Commit

Permalink
implement outbox
Browse files Browse the repository at this point in the history
only first page. for #383
  • Loading branch information
snarfed committed Nov 23, 2023
1 parent f8b7d0c commit e5abdc0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
19 changes: 15 additions & 4 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
subdomain_wrap,
unwrap,
)
from models import Follower, Object, User
from models import fetch_objects, Follower, Object, User
from protocol import Protocol
import webfinger

Expand Down Expand Up @@ -932,18 +932,29 @@ def follower_collection(id, collection):
@app.get(f'/ap/web/<regex("{DOMAIN_RE}"):id>/outbox')
# special case Web users without /ap/web/ prefix, for backward compatibility
@app.get(f'/<regex("{DOMAIN_RE}"):id>/outbox')
@flask_util.cached(cache, CACHE_TIME)
def outbox(id):
protocol = Protocol.for_request(fed='web')
assert protocol
if not protocol:
error(f"Couldn't determine protocol", status=404)

user = protocol.get_by_id(id)
if not user:
error(f'User {id} not found', status=404)

query = Object.query(Object.users == user.key)
objects, before, after = fetch_objects(query, by=Object.updated, user=user)

return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': request.url,
'summary': f"{id}'s outbox",
'type': 'OrderedCollection',
'totalItems': 0,
# TODO. needs to handle deleted
# 'totalItems': query.count(),
'first': {
'type': 'CollectionPage',
'partOf': request.base_url,
'items': [],
'items': [ActivityPub.convert(obj) for obj in objects],
},
}, {'Content-Type': as2.CONTENT_TYPE}
36 changes: 27 additions & 9 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,33 +1550,51 @@ def test_following_collection_page(self, *_):
'items': [ACTOR],
}, resp.json)

def test_outbox_fake(self, *_):
self.make_user('foo.com', cls=Fake)
resp = self.client.get(f'/ap/foo.com/outbox',
def test_outbox_fake_empty(self, *_):
self.make_user('fake:foo', cls=Fake)
resp = self.client.get(f'/ap/fake:foo/outbox',
base_url='https://fa.brid.gy')
self.assertEqual(200, resp.status_code)
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://fa.brid.gy/ap/foo.com/outbox',
'summary': "foo.com's outbox",
'id': 'https://fa.brid.gy/ap/fake:foo/outbox',
'summary': "fake:foo's outbox",
'type': 'OrderedCollection',
'totalItems': 0,
'first': {
'type': 'CollectionPage',
'partOf': 'https://fa.brid.gy/ap/foo.com/outbox',
'partOf': 'https://fa.brid.gy/ap/fake:foo/outbox',
'items': [],
},
}, resp.json)

def test_outbox_web(self, *_):
def test_outbox_fake_objects(self, *_):
user = self.make_user('fake:foo', cls=Fake)
for i, obj in enumerate([REPLY, MENTION, LIKE, DELETE]):
self.store_object(id=str(i), users=[user.key], as2=obj)

resp = self.client.get(f'/ap/fake:foo/outbox',
base_url='https://fa.brid.gy')
self.assertEqual(200, resp.status_code)
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://fa.brid.gy/ap/fake:foo/outbox',
'summary': "fake:foo's outbox",
'type': 'OrderedCollection',
'first': {
'type': 'CollectionPage',
'partOf': 'https://fa.brid.gy/ap/fake:foo/outbox',
'items': [DELETE, LIKE, MENTION, REPLY],
},
}, resp.json)

def test_outbox_web_empty(self, *_):
resp = self.client.get(f'/user.com/outbox')
self.assertEqual(200, resp.status_code)
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'http://localhost/user.com/outbox',
'summary': "user.com's outbox",
'type': 'OrderedCollection',
'totalItems': 0,
'first': {
'type': 'CollectionPage',
'partOf': 'http://localhost/user.com/outbox',
Expand Down

0 comments on commit e5abdc0

Please sign in to comment.