2020-01-28 19:45:27 +00:00
|
|
|
''' handles all of the activity coming in to the server '''
|
2020-01-30 04:56:18 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
from Crypto.Signature import pkcs1_15
|
|
|
|
from Crypto.Hash import SHA256
|
2020-01-28 09:01:33 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest, \
|
|
|
|
HttpResponseNotFound, JsonResponse
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
import json
|
2020-01-30 04:56:18 +00:00
|
|
|
import requests
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
from fedireads import activitypub
|
2020-01-28 19:45:27 +00:00
|
|
|
from fedireads import models
|
2020-02-15 06:44:07 +00:00
|
|
|
from fedireads import outgoing
|
2020-02-21 06:19:19 +00:00
|
|
|
from fedireads.status import create_review, create_status, create_tag
|
2020-02-15 06:44:07 +00:00
|
|
|
from fedireads.remote_user import get_or_create_remote_user
|
2020-01-28 09:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
2020-01-28 19:13:13 +00:00
|
|
|
def shared_inbox(request):
|
|
|
|
''' incoming activitypub events '''
|
2020-01-30 04:56:18 +00:00
|
|
|
# TODO: should this be functionally different from the non-shared inbox??
|
2020-01-28 19:13:13 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
return HttpResponseNotFound()
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-01-28 19:13:13 +00:00
|
|
|
try:
|
|
|
|
activity = json.loads(request.body)
|
|
|
|
except json.decoder.JSONDecodeError:
|
2020-02-15 04:37:02 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
verify_signature(request)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponse(status=401)
|
|
|
|
|
|
|
|
response = HttpResponseNotFound()
|
2020-02-17 02:22:01 +00:00
|
|
|
if activity['type'] == 'Follow':
|
2020-02-15 04:37:02 +00:00
|
|
|
response = handle_incoming_follow(activity)
|
|
|
|
|
2020-02-19 06:44:13 +00:00
|
|
|
elif activity['type'] == 'Undo':
|
|
|
|
response = handle_incoming_undo(activity)
|
|
|
|
|
2020-02-15 21:14:43 +00:00
|
|
|
elif activity['type'] == 'Create':
|
2020-02-15 04:37:02 +00:00
|
|
|
response = handle_incoming_create(activity)
|
|
|
|
|
2020-02-15 21:14:43 +00:00
|
|
|
elif activity['type'] == 'Accept':
|
2020-02-15 04:37:02 +00:00
|
|
|
response = handle_incoming_follow_accept(activity)
|
|
|
|
|
2020-02-19 08:13:06 +00:00
|
|
|
elif activity['type'] == 'Like':
|
|
|
|
response = handle_incoming_favorite(activity)
|
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
elif activity['type'] == 'Add':
|
|
|
|
response = handle_incoming_add(activity)
|
|
|
|
|
2020-02-17 02:22:01 +00:00
|
|
|
# TODO: Add, Undo, Remove, etc
|
2020-02-15 21:14:43 +00:00
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
return response
|
2020-01-28 19:13:13 +00:00
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
|
|
|
|
def verify_signature(request):
|
|
|
|
''' verify rsa signature '''
|
2020-01-30 04:56:18 +00:00
|
|
|
signature_dict = {}
|
2020-02-15 04:37:02 +00:00
|
|
|
for pair in request.headers['Signature'].split(','):
|
2020-01-30 04:56:18 +00:00
|
|
|
k, v = pair.split('=', 1)
|
|
|
|
v = v.replace('"', '')
|
|
|
|
signature_dict[k] = v
|
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
try:
|
|
|
|
key_id = signature_dict['keyId']
|
|
|
|
headers = signature_dict['headers']
|
|
|
|
signature = b64decode(signature_dict['signature'])
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError('Invalid auth header')
|
2020-01-30 04:56:18 +00:00
|
|
|
|
|
|
|
response = requests.get(
|
|
|
|
key_id,
|
|
|
|
headers={'Accept': 'application/activity+json'}
|
|
|
|
)
|
|
|
|
if not response.ok:
|
2020-02-15 04:37:02 +00:00
|
|
|
raise ValueError('Could not load public key')
|
2020-01-30 04:56:18 +00:00
|
|
|
|
|
|
|
actor = response.json()
|
|
|
|
key = RSA.import_key(actor['publicKey']['publicKeyPem'])
|
|
|
|
|
|
|
|
comparison_string = []
|
|
|
|
for signed_header_name in headers.split(' '):
|
|
|
|
if signed_header_name == '(request-target)':
|
|
|
|
comparison_string.append('(request-target): post %s' % request.path)
|
|
|
|
else:
|
|
|
|
comparison_string.append('%s: %s' % (
|
|
|
|
signed_header_name,
|
|
|
|
request.headers[signed_header_name]
|
|
|
|
))
|
|
|
|
comparison_string = '\n'.join(comparison_string)
|
|
|
|
|
|
|
|
signer = pkcs1_15.new(key)
|
|
|
|
digest = SHA256.new()
|
|
|
|
digest.update(comparison_string.encode())
|
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
# raises a ValueError if it fails
|
|
|
|
signer.verify(digest, signature)
|
2020-02-15 04:04:22 +00:00
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
return True
|
2020-01-28 09:01:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def inbox(request, username):
|
|
|
|
''' incoming activitypub events '''
|
|
|
|
# TODO: should do some kind of checking if the user accepts
|
2020-01-30 04:56:18 +00:00
|
|
|
# this action from the sender probably? idk
|
2020-02-22 22:02:03 +00:00
|
|
|
# but this will just throw a 404 if the user doesn't exist
|
|
|
|
try:
|
|
|
|
models.User.objects.get(localname=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-01-30 04:56:18 +00:00
|
|
|
return shared_inbox(request)
|
2020-01-28 09:01:33 +00:00
|
|
|
|
|
|
|
|
2020-01-28 19:13:13 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def get_actor(request, username):
|
|
|
|
''' return an activitypub actor object '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-02-22 22:02:03 +00:00
|
|
|
try:
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
2020-02-18 04:12:19 +00:00
|
|
|
return JsonResponse(activitypub.get_actor(user))
|
2020-01-28 19:13:13 +00:00
|
|
|
|
2020-01-29 23:59:43 +00:00
|
|
|
|
2020-02-15 20:32:40 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def get_status(request, username, status_id):
|
|
|
|
''' return activity json for a specific status '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
status = models.Status.objects.get(id=status_id)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if user != status.user:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
return JsonResponse(activitypub.get_status(status))
|
2020-02-15 20:32:40 +00:00
|
|
|
|
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def get_replies(request, username, status_id):
|
|
|
|
''' ordered collection of replies to a status '''
|
2020-02-18 05:58:02 +00:00
|
|
|
# TODO: this isn't a full implmentation
|
2020-02-18 05:39:08 +00:00
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-02-18 05:50:30 +00:00
|
|
|
status = models.Status.objects.get(id=status_id)
|
|
|
|
if status.user.localname != username:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
replies = models.Status.objects.filter(
|
2020-02-18 05:50:30 +00:00
|
|
|
reply_parent=status
|
2020-02-18 05:39:08 +00:00
|
|
|
).first()
|
|
|
|
|
2020-02-18 05:58:02 +00:00
|
|
|
replies_activity = activitypub.get_replies(status, [replies])
|
2020-02-18 05:39:08 +00:00
|
|
|
return JsonResponse(replies_activity)
|
|
|
|
|
|
|
|
|
2020-01-29 19:45:19 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def get_followers(request, username):
|
|
|
|
''' return a list of followers for an actor '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
followers = user.followers
|
2020-02-18 05:23:08 +00:00
|
|
|
page = request.GET.get('page')
|
|
|
|
return JsonResponse(activitypub.get_followers(user, page, followers))
|
2020-01-29 19:45:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def get_following(request, username):
|
|
|
|
''' return a list of following for an actor '''
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
user = models.User.objects.get(localname=username)
|
|
|
|
following = models.User.objects.filter(followers=user)
|
2020-02-18 05:23:08 +00:00
|
|
|
page = request.GET.get('page')
|
|
|
|
return JsonResponse(activitypub.get_following(user, page, following))
|
2020-01-29 19:45:19 +00:00
|
|
|
|
2020-01-28 19:13:13 +00:00
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_incoming_follow(activity):
|
2020-02-15 21:07:57 +00:00
|
|
|
''' someone wants to follow a local user '''
|
2020-01-28 09:01:33 +00:00
|
|
|
# figure out who they want to follow
|
|
|
|
to_follow = models.User.objects.get(actor=activity['object'])
|
|
|
|
# figure out who they are
|
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
2020-02-15 06:44:07 +00:00
|
|
|
# TODO: allow users to manually approve requests
|
2020-02-19 06:44:13 +00:00
|
|
|
models.UserRelationship.objects.create(
|
|
|
|
user_subject=to_follow,
|
|
|
|
user_object=user,
|
|
|
|
status='follow_request',
|
|
|
|
relationship_id=activity['id']
|
|
|
|
)
|
2020-02-15 06:44:07 +00:00
|
|
|
outgoing.handle_outgoing_accept(user, to_follow, activity)
|
|
|
|
return HttpResponse()
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-02-19 06:44:13 +00:00
|
|
|
def handle_incoming_undo(activity):
|
|
|
|
''' unfollow a local user '''
|
|
|
|
obj = activity['object']
|
|
|
|
if not obj['type'] == 'Follow':
|
|
|
|
#idk how to undo other things
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
try:
|
|
|
|
requester = get_or_create_remote_user(obj['actor'])
|
|
|
|
to_unfollow = models.User.objects.get(actor=obj['object'])
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
to_unfollow.followers.remove(requester)
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
2020-02-15 04:24:35 +00:00
|
|
|
def handle_incoming_follow_accept(activity):
|
|
|
|
''' hurray, someone remote accepted a follow request '''
|
|
|
|
# figure out who they want to follow
|
|
|
|
requester = models.User.objects.get(actor=activity['object']['actor'])
|
|
|
|
# figure out who they are
|
|
|
|
accepter = get_or_create_remote_user(activity['actor'])
|
|
|
|
|
|
|
|
accepter.followers.add(requester)
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_incoming_create(activity):
|
|
|
|
''' someone did something, good on them '''
|
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
2020-02-15 05:00:11 +00:00
|
|
|
|
|
|
|
if not 'object' in activity:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-02-19 01:30:23 +00:00
|
|
|
# TODO: should only create notes if they are relevent to a book,
|
|
|
|
# so, not every single thing someone posts on mastodon
|
2020-02-15 05:00:11 +00:00
|
|
|
response = HttpResponse()
|
2020-02-17 02:22:01 +00:00
|
|
|
content = activity['object'].get('content')
|
2020-02-15 20:32:40 +00:00
|
|
|
if activity['object'].get('fedireadsType') == 'Review' and \
|
2020-02-19 01:30:23 +00:00
|
|
|
'inReplyToBook' in activity['object']:
|
|
|
|
book = activity['object']['inReplyToBook']
|
2020-02-15 20:32:40 +00:00
|
|
|
book = book.split('/')[-1]
|
2020-02-15 19:13:49 +00:00
|
|
|
name = activity['object'].get('name')
|
|
|
|
rating = activity['object'].get('rating')
|
2020-02-15 20:32:40 +00:00
|
|
|
if user.local:
|
|
|
|
review_id = activity['object']['id'].split('/')[-1]
|
2020-02-17 02:22:01 +00:00
|
|
|
models.Review.objects.get(id=review_id)
|
2020-02-15 20:32:40 +00:00
|
|
|
else:
|
|
|
|
try:
|
2020-02-17 02:22:01 +00:00
|
|
|
create_review(user, book, name, content, rating)
|
2020-02-15 20:32:40 +00:00
|
|
|
except ValueError:
|
|
|
|
return HttpResponseBadRequest()
|
2020-02-17 02:45:25 +00:00
|
|
|
elif not user.local:
|
2020-02-17 02:22:01 +00:00
|
|
|
try:
|
|
|
|
create_status(user, content)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-02-15 05:00:11 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-02-19 08:13:06 +00:00
|
|
|
def handle_incoming_favorite(activity):
|
|
|
|
''' approval of your good good post '''
|
|
|
|
try:
|
|
|
|
status_id = activity['object'].split('/')[-1]
|
|
|
|
status = models.Status.objects.get(id=status_id)
|
|
|
|
liker = get_or_create_remote_user(activity['actor'])
|
|
|
|
except (models.Status.DoesNotExist, models.User.DoesNotExist):
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
if not liker.local:
|
|
|
|
status.favorites.add(liker)
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
def handle_incoming_add(activity):
|
|
|
|
''' someone is tagging or shelving a book '''
|
|
|
|
if activity['object']['type'] == 'Tag':
|
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
|
|
|
if not user.local:
|
2020-02-21 06:37:50 +00:00
|
|
|
book = activity['target']['id'].split('/')[-1]
|
2020-02-21 06:19:19 +00:00
|
|
|
create_tag(user, book, activity['object']['name'])
|
|
|
|
return HttpResponse()
|
|
|
|
return HttpResponse()
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
def handle_incoming_accept(activity):
|
|
|
|
''' someone is accepting a follow request '''
|
|
|
|
# our local user
|
|
|
|
user = models.User.objects.get(actor=activity['actor'])
|
|
|
|
# the person our local user wants to follow, who said yes
|
|
|
|
followed = get_or_create_remote_user(activity['object']['actor'])
|
|
|
|
|
|
|
|
# save this relationship in the db
|
|
|
|
followed.followers.add(user)
|
|
|
|
|
2020-02-07 23:11:53 +00:00
|
|
|
return HttpResponse()
|
2020-01-28 19:45:27 +00:00
|
|
|
|