From ff5217efd8ba04700768aaab560a876ff46f8406 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 17 Feb 2020 21:23:08 -0800 Subject: [PATCH] Moves incoming activitypub json to module --- fedireads/activitypub/__init__.py | 3 +- fedireads/activitypub/collection.py | 58 +++++++++++++++++++++++++++-- fedireads/incoming.py | 44 ++-------------------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/fedireads/activitypub/__init__.py b/fedireads/activitypub/__init__.py index aef52ff71..ffe4fdb6c 100644 --- a/fedireads/activitypub/__init__.py +++ b/fedireads/activitypub/__init__.py @@ -1,6 +1,7 @@ ''' bring activitypub functions into the namespace ''' 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 .follow import get_follow_request, get_accept from .status import get_review, get_status diff --git a/fedireads/activitypub/collection.py b/fedireads/activitypub/collection.py index 157f14f5b..34ce23182 100644 --- a/fedireads/activitypub/collection.py +++ b/fedireads/activitypub/collection.py @@ -33,15 +33,65 @@ def get_outbox_page(user, page_id, statuses, max_id, min_id): 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 ''' return { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': path, + 'id': id_slug, 'type': 'OrderedCollection', 'totalItems': size, - 'first': '%s?page=true' % path, - 'last': '%s?min_id=0&page=true' % path + 'first': '%s?page=true' % id_slug, + 'last': '%s?min_id=0&page=true' % id_slug } diff --git a/fedireads/incoming.py b/fedireads/incoming.py index 490f4349e..0210ddec1 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -141,7 +141,8 @@ def get_followers(request, username): user = models.User.objects.get(localname=username) 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 @@ -152,45 +153,8 @@ def get_following(request, username): user = models.User.objects.get(localname=username) following = models.User.objects.filter(followers=user) - return format_follow_info(user, request.GET.get('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 + page = request.GET.get('page') + return JsonResponse(activitypub.get_following(user, page, following)) def handle_incoming_follow(activity):