Finishes implementing reply activitypub json

Fixes #24
This commit is contained in:
Mouse Reeve 2020-03-12 16:45:18 -07:00
parent 1c3e0b9379
commit ee56ae86ec
3 changed files with 41 additions and 6 deletions

View file

@ -5,4 +5,4 @@ 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_unfollow, get_accept from .follow import get_follow_request, get_unfollow, get_accept
from .status import get_review, get_review_article, get_status, get_replies, \ from .status import get_review, get_review_article, get_status, get_replies, \
get_favorite, get_add_tag, get_remove_tag get_favorite, get_add_tag, get_remove_tag, get_replies_page

View file

@ -71,11 +71,29 @@ def get_replies(status, replies):
'type': 'CollectionPage', 'type': 'CollectionPage',
'next': '%s?only_other_accounts=true&page=true' % id_slug, 'next': '%s?only_other_accounts=true&page=true' % id_slug,
'partOf': id_slug, 'partOf': id_slug,
'items': [get_status(r) for r in replies] 'items': [get_status(r) for r in replies],
} }
} }
def get_replies_page(status, replies):
id_slug = status.absolute_id + '/replies?page=true&only_other_accounts=true'
items = []
for reply in replies:
if reply.user.local:
items.append(get_status(reply))
else:
items.append(reply.remote_id)
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': id_slug,
'type': 'CollectionPage',
'next': '%s&min_id=%d' % (id_slug, replies[len(replies) - 1].id),
'partOf': status.absolute_id + '/replies',
'items': [items]
}
def get_favorite(favorite): def get_favorite(favorite):
''' like a post ''' ''' like a post '''
return { return {

View file

@ -7,6 +7,7 @@ from django.http import HttpResponse, HttpResponseBadRequest, \
HttpResponseNotFound, JsonResponse HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import django.db.utils import django.db.utils
from django.db.models import Q
import json import json
import requests import requests
@ -162,11 +163,27 @@ def get_replies(request, username, status_id):
return HttpResponseNotFound() return HttpResponseNotFound()
replies = models.Status.objects.filter( replies = models.Status.objects.filter(
reply_parent=status reply_parent=status,
).first() ).select_subclasses()
replies_activity = activitypub.get_replies(status, [replies]) if request.GET.get('only_other_accounts'):
return JsonResponse(replies_activity) replies = replies.filter(
~Q(user=status.user)
)
else:
replies = replies.filter(user=status.user)
if request.GET.get('page'):
min_id = request.GET.get('min_id')
if min_id:
replies = replies.filter(id__gt=min_id)
max_id = request.GET.get('max_id')
if max_id:
replies = replies.filter(id__lte=max_id)
activity = activitypub.get_replies_page(status, replies)
return JsonResponse(activity)
return JsonResponse(activitypub.get_replies(status, replies))
@csrf_exempt @csrf_exempt