Handle incoming review

This commit is contained in:
Mouse Reeve 2020-01-27 21:25:26 -08:00
parent b2b651a22f
commit c4ce4ae47e
2 changed files with 43 additions and 4 deletions

View file

@ -8,6 +8,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
from fedireads.settings import DOMAIN from fedireads.settings import DOMAIN
from fedireads.openlibrary import get_or_create_book
from fedireads import models from fedireads import models
import json import json
import requests import requests
@ -78,14 +79,16 @@ def inbox(request, username):
# TODO: should do some kind of checking if the user accepts # TODO: should do some kind of checking if the user accepts
# this action from the sender # this action from the sender
# but this will just throw an error if the user doesn't exist I guess # but this will just throw an error if the user doesn't exist I guess
#models.User.objects.get(username=username) models.User.objects.get(localname=username)
if activity['type'] == 'Add': if activity['type'] == 'Add':
return handle_add(activity) return handle_incoming_shelve(activity)
if activity['type'] == 'Follow': if activity['type'] == 'Follow':
return handle_incoming_follow(activity) return handle_incoming_follow(activity)
if activity['type'] == 'Create':
return handle_incoming_create(activity)
return HttpResponse() return HttpResponse()
@ -107,7 +110,7 @@ def handle_account_search(query):
return user return user
def handle_add(activity): def handle_incoming_shelve(activity):
''' receiving an Add activity (to shelve a book) ''' ''' receiving an Add activity (to shelve a book) '''
# TODO what happens here? If it's a remote over, then I think # TODO what happens here? If it's a remote over, then I think
# I should save both the activity and the ShelfBook entry. But # I should save both the activity and the ShelfBook entry. But
@ -132,6 +135,7 @@ def handle_add(activity):
return HttpResponse() return HttpResponse()
def handle_incoming_follow(activity): def handle_incoming_follow(activity):
''' '''
{ {
@ -245,7 +249,7 @@ def handle_review(user, book, name, content, rating):
'published': datetime.utcnow().isoformat(), 'published': datetime.utcnow().isoformat(),
'attributedTo': user.actor, 'attributedTo': user.actor,
'content': content, 'content': content,
'inReplyTo': book.activitypub_id, 'inReplyTo': book.openlibrary_key,
'rating': rating, # fedireads-only custom field 'rating': rating, # fedireads-only custom field
'to': 'https://www.w3.org/ns/activitystreams#Public' 'to': 'https://www.w3.org/ns/activitystreams#Public'
} }
@ -279,6 +283,39 @@ def handle_review(user, book, name, content, rating):
broadcast(user, activity, recipients) broadcast(user, activity, recipients)
def handle_incoming_create(activity):
''' someone did something, good on them '''
user = get_or_create_remote_user(activity['actor'])
uuid = activity['id']
# if it's an article and in reply to a book, we have a review
if activity['object']['type'] == 'Article' and \
'inReplyTo' in activity['object']:
possible_book = activity['object']['inReplyTo']
try:
book = get_or_create_book(possible_book)
models.Review(
uuid=uuid,
user=user,
content=activity,
activity_type='Article',
book=book,
work=book.works.first(),
name=activity['object']['name'],
rating=activity['object']['rating'],
review_content=activity['objet']['content'],
).save()
return HttpResponse()
except KeyError:
pass
models.Activity(
uuid=uuid,
user=user,
content=activity,
activity_type=activity['object']['type']
)
return HttpResponse()
@csrf_exempt @csrf_exempt
def outbox(request, username): def outbox(request, username):
''' outbox for the requested user ''' ''' outbox for the requested user '''

View file

@ -9,6 +9,8 @@ def get_or_create_book(olkey, user=None, update=True):
# check if this is a valid open library key, and a book # check if this is a valid open library key, and a book
olkey = olkey olkey = olkey
response = requests.get(OL_URL + olkey + '.json') response = requests.get(OL_URL + olkey + '.json')
if not response.ok:
response.raise_for_status()
# get the existing entry from our db, if it exists # get the existing entry from our db, if it exists
try: try: