Moves incoming activitypub json to module

This commit is contained in:
Mouse Reeve 2020-02-17 21:23:08 -08:00
parent beb1e13050
commit ff5217efd8
3 changed files with 60 additions and 45 deletions

View file

@ -1,6 +1,7 @@
''' 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_outbox, get_outbox_page, get_add, get_remove from .collection import get_outbox, get_outbox_page, get_add, get_remove, \
get_following, get_followers
from .create import get_create from .create import get_create
from .follow import get_follow_request, get_accept from .follow import get_follow_request, get_accept
from .status import get_review, get_status from .status import get_review, get_status

View file

@ -33,15 +33,65 @@ def get_outbox_page(user, page_id, statuses, max_id, min_id):
return page return page
def get_ordered_collection(path, size): def get_followers(user, page, follow_queryset):
''' list of people who follow a user '''
id_slug = '%s/followers' % user.actor
return get_follow_info(id_slug, page, follow_queryset)
def get_following(user, page, follow_queryset):
''' list of people who follow a user '''
id_slug = '%s/following' % user.actor
return get_follow_info(id_slug, page, follow_queryset)
def get_follow_info(id_slug, page, follow_queryset):
''' a list of followers or following '''
if page:
return get_follow_page(follow_queryset, id_slug, page)
count = follow_queryset.count()
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': id_slug,
'type': 'OrderedCollection',
'totalItems': count,
'first': '%s?page=1' % id_slug,
}
# TODO: generalize these pagination functions
def get_follow_page(user_list, id_slug, page):
''' format a list of followers/following '''
page = int(page)
page_length = 10
start = (page - 1) * page_length
end = start + page_length
follower_page = user_list.all()[start:end]
data = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s?page=%d' % (id_slug, page),
'type': 'OrderedCollectionPage',
'totalItems': user_list.count(),
'partOf': id_slug,
'orderedItems': [u.actor for u in follower_page],
}
if end <= user_list.count():
# there are still more pages
data['next'] = '%s?page=%d' % (id_slug, page + 1)
if start > 0:
data['prev'] = '%s?page=%d' % (id_slug, page - 1)
return data
def get_ordered_collection(id_slug, size):
''' create an ordered collection ''' ''' create an ordered collection '''
return { return {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'id': path, 'id': id_slug,
'type': 'OrderedCollection', 'type': 'OrderedCollection',
'totalItems': size, 'totalItems': size,
'first': '%s?page=true' % path, 'first': '%s?page=true' % id_slug,
'last': '%s?min_id=0&page=true' % path 'last': '%s?min_id=0&page=true' % id_slug
} }

View file

@ -141,7 +141,8 @@ def get_followers(request, username):
user = models.User.objects.get(localname=username) user = models.User.objects.get(localname=username)
followers = user.followers followers = user.followers
return format_follow_info(user, request.GET.get('page'), followers) page = request.GET.get('page')
return JsonResponse(activitypub.get_followers(user, page, followers))
@csrf_exempt @csrf_exempt
@ -152,45 +153,8 @@ def get_following(request, username):
user = models.User.objects.get(localname=username) user = models.User.objects.get(localname=username)
following = models.User.objects.filter(followers=user) following = models.User.objects.filter(followers=user)
return format_follow_info(user, request.GET.get('page'), following) page = request.GET.get('page')
return JsonResponse(activitypub.get_following(user, page, following))
def format_follow_info(user, page, follow_queryset):
''' create the activitypub json for followers/following '''
id_slug = '%s/following' % user.actor
if page:
return JsonResponse(get_follow_page(follow_queryset, id_slug, page))
count = follow_queryset.count()
return JsonResponse({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': id_slug,
'type': 'OrderedCollection',
'totalItems': count,
'first': '%s?page=1' % id_slug,
})
def get_follow_page(user_list, id_slug, page):
''' format a list of followers/following '''
page = int(page)
page_length = 10
start = (page - 1) * page_length
end = start + page_length
follower_page = user_list.all()[start:end]
data = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s?page=%d' % (id_slug, page),
'type': 'OrderedCollectionPage',
'totalItems': user_list.count(),
'partOf': id_slug,
'orderedItems': [u.actor for u in follower_page],
}
if end <= user_list.count():
# there are still more pages
data['next'] = '%s?page=%d' % (id_slug, page + 1)
if start > 0:
data['prev'] = '%s?page=%d' % (id_slug, page - 1)
return data
def handle_incoming_follow(activity): def handle_incoming_follow(activity):