Merge pull request #434 from renatolond/activitypub/use_activitypub_content_type

Create a ActivitypubResponse class and use it in the views
This commit is contained in:
Mouse Reeve 2020-12-30 12:37:04 -08:00 committed by GitHub
commit b71eda6e38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 18 deletions

View file

@ -11,6 +11,7 @@ from .note import Tombstone
from .interaction import Boost, Like
from .ordered_collection import OrderedCollection, OrderedCollectionPage
from .person import Person, PublicKey
from .response import ActivitypubResponse
from .book import Edition, Work, Author
from .verbs import Create, Delete, Undo, Update
from .verbs import Follow, Accept, Reject

View file

@ -0,0 +1,18 @@
from django.http import JsonResponse
from .base_activity import ActivityEncoder
class ActivitypubResponse(JsonResponse):
"""
A class to be used in any place that's serializing responses for
Activitypub enabled clients. Uses JsonResponse under the hood, but already
configures some stuff beforehand. Made to be a drop-in replacement of
JsonResponse.
"""
def __init__(self, data, encoder=ActivityEncoder, safe=True,
json_dumps_params=None, **kwargs):
if 'content_type' not in kwargs:
kwargs['content_type'] = 'application/activity+json'
super().__init__(data, encoder, safe, json_dumps_params, **kwargs)

View file

@ -13,7 +13,7 @@ from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET
from bookwyrm import outgoing
from bookwyrm.activitypub import ActivityEncoder
from bookwyrm.activitypub import ActivityEncoder, ActivitypubResponse
from bookwyrm import forms, models, books_manager
from bookwyrm import goodreads_import
from bookwyrm.settings import PAGE_LENGTH
@ -380,7 +380,7 @@ def user_page(request, username):
if is_api_request(request):
# we have a json request
return JsonResponse(user.to_activity(), encoder=ActivityEncoder)
return ActivitypubResponse(user.to_activity())
# otherwise we're at a UI view
try:
@ -448,7 +448,7 @@ def followers_page(request, username):
return HttpResponseNotFound()
if is_api_request(request):
return JsonResponse(user.to_followers_activity(**request.GET))
return ActivitypubResponse(user.to_followers_activity(**request.GET))
data = {
'title': '%s: followers' % user.name,
@ -469,7 +469,7 @@ def following_page(request, username):
return HttpResponseNotFound()
if is_api_request(request):
return JsonResponse(user.to_following_activity(**request.GET))
return ActivitypubResponse(user.to_following_activity(**request.GET))
data = {
'title': '%s: following' % user.name,
@ -499,7 +499,7 @@ def status_page(request, username, status_id):
return HttpResponseNotFound()
if is_api_request(request):
return JsonResponse(status.to_activity(), encoder=ActivityEncoder)
return ActivitypubResponse(status.to_activity())
data = {
'title': 'Status by %s' % user.username,
@ -532,10 +532,7 @@ def replies_page(request, username, status_id):
if status.user.localname != username:
return HttpResponseNotFound()
return JsonResponse(
status.to_replies(**request.GET),
encoder=ActivityEncoder
)
return ActivitypubResponse(status.to_replies(**request.GET))
@login_required
@ -567,7 +564,7 @@ def book_page(request, book_id):
return HttpResponseNotFound()
if is_api_request(request):
return JsonResponse(book.to_activity(), encoder=ActivityEncoder)
return ActivitypubResponse(book.to_activity())
if isinstance(book, models.Work):
book = book.get_default_edition()
@ -679,10 +676,7 @@ def editions_page(request, book_id):
work = get_object_or_404(models.Work, id=book_id)
if is_api_request(request):
return JsonResponse(
work.to_edition_list(**request.GET),
encoder=ActivityEncoder
)
return ActivitypubResponse(work.to_edition_list(**request.GET))
data = {
'title': 'Editions of %s' % work.title,
@ -698,7 +692,7 @@ def author_page(request, author_id):
author = get_object_or_404(models.Author, id=author_id)
if is_api_request(request):
return JsonResponse(author.to_activity(), encoder=ActivityEncoder)
return ActivitypubResponse(author.to_activity())
books = models.Work.objects.filter(
Q(authors=author) | Q(editions__authors=author)).distinct()
@ -718,8 +712,7 @@ def tag_page(request, tag_id):
return HttpResponseNotFound()
if is_api_request(request):
return JsonResponse(
tag_obj.to_activity(**request.GET), encoder=ActivityEncoder)
return ActivitypubResponse(tag_obj.to_activity(**request.GET))
books = models.Edition.objects.filter(
usertag__tag__identifier=tag_id
@ -770,7 +763,7 @@ def shelf_page(request, username, shelf_identifier):
if is_api_request(request):
return JsonResponse(shelf.to_activity(**request.GET))
return ActivitypubResponse(shelf.to_activity(**request.GET))
data = {
'title': '%s\'s %s shelf' % (user.display_name, shelf.name),