mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-22 16:16:39 +00:00
move outgoing activitypub json into ap module
This commit is contained in:
parent
75ef3baabd
commit
e7dc4af907
4 changed files with 86 additions and 52 deletions
|
@ -1,5 +1,6 @@
|
||||||
''' bring activitypub functions into the namespace '''
|
''' bring activitypub functions into the namespace '''
|
||||||
from .actor import get_actor
|
from .actor import get_actor
|
||||||
from .collection import get_add, get_remove
|
from .collection import get_outbox, get_outbox_page, get_add, get_remove
|
||||||
from .create import get_create
|
from .create import get_create
|
||||||
|
from .follow import get_follow_request, get_accept
|
||||||
from .status import get_review, get_status
|
from .status import get_review, get_status
|
||||||
|
|
|
@ -1,5 +1,49 @@
|
||||||
''' activitypub json for collections '''
|
''' activitypub json for collections '''
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
from .status import get_status
|
||||||
|
|
||||||
|
def get_outbox(user, size):
|
||||||
|
''' helper function for creating an outbox '''
|
||||||
|
return get_ordered_collection(user.outbox, size)
|
||||||
|
|
||||||
|
|
||||||
|
def get_outbox_page(user, page_id, statuses, max_id, min_id):
|
||||||
|
''' helper for formatting outbox pages '''
|
||||||
|
# not generalizing this more because the format varies for some reason
|
||||||
|
page = {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
'id': page_id,
|
||||||
|
'type': 'OrderedCollectionPage',
|
||||||
|
'partOf': user.outbox,
|
||||||
|
'orderedItems': [],
|
||||||
|
}
|
||||||
|
|
||||||
|
for status in statuses:
|
||||||
|
page['orderedItems'].append(get_status(status))
|
||||||
|
|
||||||
|
if max_id:
|
||||||
|
page['next'] = user.outbox + '?' + \
|
||||||
|
urlencode({'min_id': max_id, 'page': 'true'})
|
||||||
|
if min_id:
|
||||||
|
page['prev'] = user.outbox + '?' + \
|
||||||
|
urlencode({'max_id': min_id, 'page': 'true'})
|
||||||
|
|
||||||
|
return page
|
||||||
|
|
||||||
|
|
||||||
|
def get_ordered_collection(path, size):
|
||||||
|
''' create an ordered collection '''
|
||||||
|
return {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
'id': path,
|
||||||
|
'type': 'OrderedCollection',
|
||||||
|
'totalItems': size,
|
||||||
|
'first': '%s?page=true' % path,
|
||||||
|
'last': '%s?min_id=0&page=true' % path
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_add(*args):
|
def get_add(*args):
|
||||||
''' activitypub Add activity '''
|
''' activitypub Add activity '''
|
||||||
|
|
28
fedireads/activitypub/follow.py
Normal file
28
fedireads/activitypub/follow.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
''' makin' freinds inthe ap json format '''
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from fedireads.settings import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
def get_follow_request(user, to_follow):
|
||||||
|
''' a local user wants to follow someone '''
|
||||||
|
uuid = uuid4()
|
||||||
|
return {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
'id': 'https://%s/%s' % (DOMAIN, str(uuid)),
|
||||||
|
'summary': '',
|
||||||
|
'type': 'Follow',
|
||||||
|
'actor': user.actor,
|
||||||
|
'object': to_follow.actor,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_accept(user, to_follow, request_activity):
|
||||||
|
return {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
'id': '%s#accepts/follows/' % to_follow.absolute_id,
|
||||||
|
'type': 'Accept',
|
||||||
|
'actor': to_follow.actor,
|
||||||
|
'object': activity,
|
||||||
|
}
|
||||||
|
|
|
@ -3,14 +3,12 @@ from django.http import HttpResponseNotFound, JsonResponse
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
from fedireads import models
|
from fedireads import models
|
||||||
from fedireads.status import create_review, create_status
|
from fedireads.status import create_review, create_status
|
||||||
from fedireads import activitypub
|
from fedireads import activitypub
|
||||||
from fedireads.remote_user import get_or_create_remote_user
|
from fedireads.remote_user import get_or_create_remote_user
|
||||||
from fedireads.broadcast import get_recipients, broadcast
|
from fedireads.broadcast import get_recipients, broadcast
|
||||||
from fedireads.settings import DOMAIN
|
|
||||||
|
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
|
@ -26,7 +24,6 @@ def outbox(request, username):
|
||||||
min_id = request.GET.get('min_id')
|
min_id = request.GET.get('min_id')
|
||||||
max_id = request.GET.get('max_id')
|
max_id = request.GET.get('max_id')
|
||||||
|
|
||||||
query_path = user.outbox + '?'
|
|
||||||
# filters for use in the django queryset min/max
|
# filters for use in the django queryset min/max
|
||||||
filters = {}
|
filters = {}
|
||||||
# params for the outbox page id
|
# params for the outbox page id
|
||||||
|
@ -37,37 +34,20 @@ def outbox(request, username):
|
||||||
if max_id != None:
|
if max_id != None:
|
||||||
params['max_id'] = max_id
|
params['max_id'] = max_id
|
||||||
filters['id__lte'] = max_id
|
filters['id__lte'] = max_id
|
||||||
collection_id = query_path + urlencode(params)
|
|
||||||
|
|
||||||
outbox_page = {
|
page_id = user.outbox + '?' + urlencode(params)
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
statuses = models.Status.objects.filter(
|
||||||
'id': collection_id,
|
user=user,
|
||||||
'type': 'OrderedCollectionPage',
|
**filters
|
||||||
'partOf': user.outbox,
|
).all()[:limit]
|
||||||
'orderedItems': [],
|
|
||||||
}
|
|
||||||
statuses = models.Status.objects.filter(user=user, **filters).all()
|
|
||||||
for status in statuses[:limit]:
|
|
||||||
outbox_page['orderedItems'].append(activitypub.get_status(status))
|
|
||||||
|
|
||||||
if max_id:
|
return JsonResponse(
|
||||||
outbox_page['next'] = query_path + \
|
activitypub.get_outbox_page(user, page_id, statuses, max_id, min_id)
|
||||||
urlencode({'min_id': max_id, 'page': 'true'})
|
)
|
||||||
if min_id:
|
|
||||||
outbox_page['prev'] = query_path + \
|
|
||||||
urlencode({'max_id': min_id, 'page': 'true'})
|
|
||||||
return JsonResponse(outbox_page)
|
|
||||||
|
|
||||||
# collection overview
|
# collection overview
|
||||||
size = models.Status.objects.filter(user=user).count()
|
size = models.Status.objects.filter(user=user).count()
|
||||||
return JsonResponse({
|
return JsonResponse(activitypub.get_outbox(user, size))
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
||||||
'id': '%s/outbox' % user.actor,
|
|
||||||
'type': 'OrderedCollection',
|
|
||||||
'totalItems': size,
|
|
||||||
'first': '%s/outbox?page=true' % user.actor,
|
|
||||||
'last': '%s/outbox?min_id=0&page=true' % user.actor
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def handle_account_search(query):
|
def handle_account_search(query):
|
||||||
|
@ -91,16 +71,7 @@ def handle_account_search(query):
|
||||||
|
|
||||||
def handle_outgoing_follow(user, to_follow):
|
def handle_outgoing_follow(user, to_follow):
|
||||||
''' someone local wants to follow someone '''
|
''' someone local wants to follow someone '''
|
||||||
uuid = uuid4()
|
activity = activitypub.get_follow_request(user, to_follow)
|
||||||
activity = {
|
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
||||||
'id': 'https://%s/%s' % (DOMAIN, str(uuid)),
|
|
||||||
'summary': '',
|
|
||||||
'type': 'Follow',
|
|
||||||
'actor': user.actor,
|
|
||||||
'object': to_follow.actor,
|
|
||||||
}
|
|
||||||
|
|
||||||
errors = broadcast(user, activity, [to_follow.inbox])
|
errors = broadcast(user, activity, [to_follow.inbox])
|
||||||
for error in errors:
|
for error in errors:
|
||||||
raise(error['error'])
|
raise(error['error'])
|
||||||
|
@ -109,18 +80,8 @@ def handle_outgoing_follow(user, to_follow):
|
||||||
def handle_outgoing_accept(user, to_follow, activity):
|
def handle_outgoing_accept(user, to_follow, activity):
|
||||||
''' send an acceptance message to a follow request '''
|
''' send an acceptance message to a follow request '''
|
||||||
to_follow.followers.add(user)
|
to_follow.followers.add(user)
|
||||||
activity = {
|
activity = activitypub.get_accept(user, to_follow, activity)
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
recipient = get_recipients(to_follow, 'direct', direct_recipients=[user])
|
||||||
'id': '%s#accepts/follows/' % to_follow.absolute_id,
|
|
||||||
'type': 'Accept',
|
|
||||||
'actor': to_follow.actor,
|
|
||||||
'object': activity,
|
|
||||||
}
|
|
||||||
recipient = get_recipients(
|
|
||||||
to_follow,
|
|
||||||
'direct',
|
|
||||||
direct_recipients=[user]
|
|
||||||
)
|
|
||||||
broadcast(to_follow, activity, recipient)
|
broadcast(to_follow, activity, recipient)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue