2020-01-28 19:45:27 +00:00
|
|
|
''' handles all of the activity coming in to the server '''
|
2020-04-22 13:53:22 +00:00
|
|
|
import json
|
2020-05-13 10:40:57 +00:00
|
|
|
from urllib.parse import urldefrag
|
|
|
|
|
2020-02-23 09:27:10 +00:00
|
|
|
import django.db.utils
|
2020-03-29 07:05:09 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2020-09-17 20:02:52 +00:00
|
|
|
import requests
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub, books_manager, models, outgoing
|
|
|
|
from bookwyrm import status as status_builder
|
|
|
|
from bookwyrm.remote_user import get_or_create_remote_user, refresh_remote_user
|
|
|
|
from bookwyrm.tasks import app
|
|
|
|
from bookwyrm.signatures import Signature
|
2020-04-01 00:00:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def inbox(request, username):
|
|
|
|
''' incoming activitypub events '''
|
|
|
|
# TODO: should do some kind of checking if the user accepts
|
|
|
|
# this action from the sender probably? idk
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
return shared_inbox(request)
|
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:
|
2020-10-05 22:23:39 +00:00
|
|
|
resp = request.body
|
|
|
|
activity = json.loads(resp)
|
2020-10-14 17:24:05 +00:00
|
|
|
if isinstance(activity, str):
|
|
|
|
activity = json.loads(activity)
|
2020-09-21 17:25:26 +00:00
|
|
|
activity_object = activity['object']
|
|
|
|
except (json.decoder.JSONDecodeError, KeyError):
|
2020-05-09 21:26:27 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2020-05-22 12:53:56 +00:00
|
|
|
if not has_valid_signature(request, activity):
|
2020-05-22 13:20:27 +00:00
|
|
|
if activity['type'] == 'Delete':
|
|
|
|
# Pretend that unauth'd deletes succeed. Auth may be failing because
|
|
|
|
# the resource or owner of the resource might have been deleted.
|
|
|
|
return HttpResponse()
|
2020-02-15 04:37:02 +00:00
|
|
|
return HttpResponse(status=401)
|
|
|
|
|
2020-03-29 06:52:07 +00:00
|
|
|
handlers = {
|
|
|
|
'Follow': handle_follow,
|
|
|
|
'Accept': handle_follow_accept,
|
|
|
|
'Reject': handle_follow_reject,
|
2020-03-29 07:05:09 +00:00
|
|
|
'Create': handle_create,
|
2020-10-17 00:00:10 +00:00
|
|
|
'Delete': handle_delete_status,
|
2020-03-29 06:52:07 +00:00
|
|
|
'Like': handle_favorite,
|
2020-03-30 14:13:32 +00:00
|
|
|
'Announce': handle_boost,
|
2020-03-30 01:42:34 +00:00
|
|
|
'Add': {
|
2020-04-01 00:00:01 +00:00
|
|
|
'Tag': handle_tag,
|
2020-11-02 23:10:41 +00:00
|
|
|
'Edition': handle_shelve,
|
|
|
|
'Work': handle_shelve,
|
2020-03-30 01:42:34 +00:00
|
|
|
},
|
|
|
|
'Undo': {
|
|
|
|
'Follow': handle_unfollow,
|
|
|
|
'Like': handle_unfavorite,
|
|
|
|
},
|
|
|
|
'Update': {
|
2020-10-16 22:40:23 +00:00
|
|
|
'Person': handle_update_user,
|
2020-11-02 17:34:46 +00:00
|
|
|
'Edition': handle_update_book,
|
|
|
|
'Work': handle_update_book,
|
2020-03-30 01:42:34 +00:00
|
|
|
},
|
2020-03-29 06:52:07 +00:00
|
|
|
}
|
|
|
|
activity_type = activity['type']
|
|
|
|
|
2020-03-30 01:42:34 +00:00
|
|
|
handler = handlers.get(activity_type, None)
|
|
|
|
if isinstance(handler, dict):
|
2020-09-21 17:25:26 +00:00
|
|
|
handler = handler.get(activity_object['type'], None)
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
if not handler:
|
|
|
|
return HttpResponseNotFound()
|
2020-02-19 08:13:06 +00:00
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
handler.delay(activity)
|
|
|
|
return HttpResponse()
|
2020-01-28 19:13:13 +00:00
|
|
|
|
2020-02-15 04:37:02 +00:00
|
|
|
|
2020-05-22 12:53:56 +00:00
|
|
|
def has_valid_signature(request, activity):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' verify incoming signature '''
|
2020-05-22 12:53:56 +00:00
|
|
|
try:
|
|
|
|
signature = Signature.parse(request)
|
|
|
|
|
|
|
|
key_actor = urldefrag(signature.key_id).url
|
|
|
|
if key_actor != activity.get('actor'):
|
|
|
|
raise ValueError("Wrong actor created signature.")
|
|
|
|
|
|
|
|
remote_user = get_or_create_remote_user(key_actor)
|
|
|
|
|
|
|
|
try:
|
|
|
|
signature.verify(remote_user.public_key, request)
|
|
|
|
except ValueError:
|
|
|
|
old_key = remote_user.public_key
|
|
|
|
refresh_remote_user(remote_user)
|
|
|
|
if remote_user.public_key == old_key:
|
|
|
|
raise # Key unchanged.
|
|
|
|
signature.verify(remote_user.public_key, request)
|
|
|
|
except (ValueError, requests.exceptions.HTTPError):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_follow(activity):
|
2020-02-15 21:07:57 +00:00
|
|
|
''' someone wants to follow a local user '''
|
2020-05-09 21:26:27 +00:00
|
|
|
# figure out who they want to follow -- not using get_or_create because
|
2020-10-15 00:29:43 +00:00
|
|
|
# we only care if you want to follow local users
|
|
|
|
try:
|
|
|
|
to_follow = models.User.objects.get(remote_id=activity['object'])
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
# some rando, who cares
|
|
|
|
return
|
|
|
|
if not to_follow.local:
|
|
|
|
# just ignore follow alerts about other servers. maybe they should be
|
|
|
|
# handled. maybe they shouldn't be sent at all.
|
|
|
|
return
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-05-09 21:26:27 +00:00
|
|
|
# figure out who the actor is
|
2020-10-16 00:32:24 +00:00
|
|
|
actor = get_or_create_remote_user(activity['actor'])
|
2020-02-23 09:27:10 +00:00
|
|
|
try:
|
2020-09-17 20:02:52 +00:00
|
|
|
relationship = models.UserFollowRequest.objects.create(
|
2020-10-16 00:32:24 +00:00
|
|
|
user_subject=actor,
|
2020-02-24 16:10:09 +00:00
|
|
|
user_object=to_follow,
|
2020-10-16 17:37:33 +00:00
|
|
|
remote_id=activity['id']
|
2020-02-23 09:27:10 +00:00
|
|
|
)
|
2020-03-14 22:20:01 +00:00
|
|
|
except django.db.utils.IntegrityError as err:
|
|
|
|
if err.__cause__.diag.constraint_name != 'userfollowrequest_unique':
|
|
|
|
raise
|
2020-10-16 21:28:25 +00:00
|
|
|
relationship = models.UserFollowRequest.objects.get(
|
|
|
|
remote_id=activity['id']
|
|
|
|
)
|
2020-10-16 19:24:29 +00:00
|
|
|
# send the accept normally for a duplicate request
|
2020-02-23 09:27:10 +00:00
|
|
|
|
2020-03-09 20:13:54 +00:00
|
|
|
if not to_follow.manually_approves_followers:
|
2020-03-21 22:21:27 +00:00
|
|
|
status_builder.create_notification(
|
|
|
|
to_follow,
|
|
|
|
'FOLLOW',
|
2020-10-16 00:32:24 +00:00
|
|
|
related_user=actor
|
2020-03-21 22:21:27 +00:00
|
|
|
)
|
2020-10-16 21:14:07 +00:00
|
|
|
outgoing.handle_accept(relationship)
|
2020-03-13 13:13:32 +00:00
|
|
|
else:
|
2020-10-16 00:32:24 +00:00
|
|
|
# Accept will be triggered manually
|
2020-03-21 22:21:27 +00:00
|
|
|
status_builder.create_notification(
|
|
|
|
to_follow,
|
|
|
|
'FOLLOW_REQUEST',
|
2020-10-16 00:32:24 +00:00
|
|
|
related_user=actor
|
2020-03-21 22:21:27 +00:00
|
|
|
)
|
2020-02-15 06:44:07 +00:00
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 07:05:09 +00:00
|
|
|
def handle_unfollow(activity):
|
2020-02-19 06:44:13 +00:00
|
|
|
''' unfollow a local user '''
|
|
|
|
obj = activity['object']
|
2020-09-17 20:02:52 +00:00
|
|
|
requester = get_or_create_remote_user(obj['actor'])
|
|
|
|
to_unfollow = models.User.objects.get(remote_id=obj['object'])
|
|
|
|
# raises models.User.DoesNotExist
|
2020-02-19 06:44:13 +00:00
|
|
|
|
|
|
|
to_unfollow.followers.remove(requester)
|
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_follow_accept(activity):
|
2020-02-15 04:24:35 +00:00
|
|
|
''' hurray, someone remote accepted a follow request '''
|
|
|
|
# figure out who they want to follow
|
2020-05-14 01:23:54 +00:00
|
|
|
requester = models.User.objects.get(remote_id=activity['object']['actor'])
|
2020-02-15 04:24:35 +00:00
|
|
|
# figure out who they are
|
|
|
|
accepter = get_or_create_remote_user(activity['actor'])
|
|
|
|
|
2020-03-13 17:04:39 +00:00
|
|
|
try:
|
2020-03-14 00:57:36 +00:00
|
|
|
request = models.UserFollowRequest.objects.get(
|
|
|
|
user_subject=requester,
|
|
|
|
user_object=accepter
|
|
|
|
)
|
2020-03-13 17:04:39 +00:00
|
|
|
request.delete()
|
|
|
|
except models.UserFollowRequest.DoesNotExist:
|
|
|
|
pass
|
2020-03-21 21:25:31 +00:00
|
|
|
accepter.followers.add(requester)
|
2020-02-15 04:24:35 +00:00
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_follow_reject(activity):
|
2020-03-13 17:04:39 +00:00
|
|
|
''' someone is rejecting a follow request '''
|
2020-05-14 01:23:54 +00:00
|
|
|
requester = models.User.objects.get(remote_id=activity['object']['actor'])
|
2020-03-13 17:04:39 +00:00
|
|
|
rejecter = get_or_create_remote_user(activity['actor'])
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
request = models.UserFollowRequest.objects.get(
|
|
|
|
user_subject=requester,
|
|
|
|
user_object=rejecter
|
|
|
|
)
|
|
|
|
request.delete()
|
2020-10-16 21:28:25 +00:00
|
|
|
#raises models.UserFollowRequest.DoesNotExist
|
2020-03-13 17:04:39 +00:00
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_create(activity):
|
2020-01-28 09:01:33 +00:00
|
|
|
''' someone did something, good on them '''
|
2020-09-17 20:02:52 +00:00
|
|
|
if activity['object'].get('type') not in \
|
2020-10-30 22:22:20 +00:00
|
|
|
['Note', 'Comment', 'Quotation', 'Review', 'GeneratedNote']:
|
2020-09-17 20:02:52 +00:00
|
|
|
# if it's an article or unknown type, ignore it
|
|
|
|
return
|
2020-02-15 05:00:11 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
2020-03-29 07:05:09 +00:00
|
|
|
if user.local:
|
|
|
|
# we really oughtn't even be sending in this case
|
2020-09-17 20:02:52 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# render the json into an activity object
|
|
|
|
serializer = activitypub.activity_objects[activity['object']['type']]
|
|
|
|
activity = serializer(**activity['object'])
|
|
|
|
|
|
|
|
# ignore notes that aren't replies to known statuses
|
|
|
|
if activity.type == 'Note':
|
|
|
|
reply = models.Status.objects.filter(
|
|
|
|
remote_id=activity.inReplyTo
|
|
|
|
).first()
|
|
|
|
if not reply:
|
|
|
|
return
|
|
|
|
|
2020-10-31 00:04:10 +00:00
|
|
|
# look up books
|
|
|
|
book_urls = []
|
|
|
|
if hasattr(activity, 'inReplyToBook'):
|
|
|
|
book_urls.append(activity.inReplyToBook)
|
|
|
|
if hasattr(activity, 'tag'):
|
2020-10-31 20:39:52 +00:00
|
|
|
book_urls += [t['href'] for t in activity.tag if t['type'] == 'Book']
|
2020-10-31 00:04:10 +00:00
|
|
|
for remote_id in book_urls:
|
2020-10-31 00:41:32 +00:00
|
|
|
books_manager.get_or_create_book(remote_id)
|
2020-10-31 00:04:10 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
model = models.activity_models[activity.type]
|
|
|
|
status = activity.to_model(model)
|
|
|
|
|
|
|
|
# create a notification if this is a reply
|
|
|
|
if status.reply_parent and status.reply_parent.user.local:
|
|
|
|
status_builder.create_notification(
|
|
|
|
status.reply_parent.user,
|
|
|
|
'REPLY',
|
|
|
|
related_user=status.user,
|
|
|
|
related_status=status,
|
2020-05-09 21:26:27 +00:00
|
|
|
)
|
2020-02-15 05:00:11 +00:00
|
|
|
|
|
|
|
|
2020-10-17 00:00:10 +00:00
|
|
|
@app.task
|
|
|
|
def handle_delete_status(activity):
|
|
|
|
''' remove a status '''
|
|
|
|
status_id = activity['object']['id']
|
|
|
|
try:
|
|
|
|
status = models.Status.objects.select_subclasses().get(
|
|
|
|
remote_id=status_id
|
|
|
|
)
|
|
|
|
except models.Status.DoesNotExist:
|
|
|
|
return
|
|
|
|
status_builder.delete_status(status)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_favorite(activity):
|
2020-02-19 08:13:06 +00:00
|
|
|
''' approval of your good good post '''
|
2020-09-17 20:02:52 +00:00
|
|
|
fav = activitypub.Like(**activity['object'])
|
|
|
|
# raises ValueError in to_model if a foreign key could not be resolved in
|
|
|
|
|
|
|
|
liker = get_or_create_remote_user(activity['actor'])
|
|
|
|
if liker.local:
|
|
|
|
return
|
2020-04-01 00:00:01 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
status = fav.to_model(models.Favorite)
|
2020-04-01 00:00:01 +00:00
|
|
|
|
|
|
|
status_builder.create_notification(
|
|
|
|
status.user,
|
|
|
|
'FAVORITE',
|
|
|
|
related_user=liker,
|
|
|
|
related_status=status,
|
|
|
|
)
|
2020-02-19 08:13:06 +00:00
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-29 06:52:07 +00:00
|
|
|
def handle_unfavorite(activity):
|
2020-03-21 22:21:27 +00:00
|
|
|
''' approval of your good good post '''
|
2020-09-17 20:02:52 +00:00
|
|
|
like = activitypub.Like(**activity['object'])
|
|
|
|
fav = models.Favorite.objects.filter(remote_id=like.id).first()
|
2020-03-21 22:21:27 +00:00
|
|
|
|
|
|
|
fav.delete()
|
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
2020-03-30 14:13:32 +00:00
|
|
|
def handle_boost(activity):
|
|
|
|
''' someone gave us a boost! '''
|
2020-09-17 20:02:52 +00:00
|
|
|
status_id = activity['object'].split('/')[-1]
|
|
|
|
status = models.Status.objects.get(id=status_id)
|
|
|
|
booster = get_or_create_remote_user(activity['actor'])
|
2020-03-30 14:13:32 +00:00
|
|
|
|
|
|
|
if not booster.local:
|
|
|
|
status_builder.create_boost_from_activity(booster, activity)
|
|
|
|
|
|
|
|
status_builder.create_notification(
|
|
|
|
status.user,
|
|
|
|
'BOOST',
|
|
|
|
related_user=booster,
|
|
|
|
related_status=status,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-01 00:00:01 +00:00
|
|
|
@app.task
|
|
|
|
def handle_tag(activity):
|
2020-05-10 18:29:10 +00:00
|
|
|
''' someone is tagging a book '''
|
2020-04-01 00:00:01 +00:00
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
|
|
|
if not user.local:
|
2020-05-09 21:26:27 +00:00
|
|
|
book = activity['target']['id']
|
2020-04-01 00:00:01 +00:00
|
|
|
status_builder.create_tag(user, book, activity['object']['name'])
|
2020-05-04 01:56:29 +00:00
|
|
|
|
|
|
|
|
2020-11-02 23:10:41 +00:00
|
|
|
@app.task
|
|
|
|
def handle_shelve(activity):
|
|
|
|
''' putting a book on a shelf '''
|
|
|
|
user = get_or_create_remote_user(activity['actor'])
|
|
|
|
book = books_manager.get_or_create_book(activity['object'])
|
|
|
|
try:
|
|
|
|
shelf = models.Shelf.objects.get(remote_id=activity['target'])
|
|
|
|
except models.Shelf.DoesNotExist:
|
|
|
|
return
|
|
|
|
if shelf.user != user:
|
|
|
|
# this doesn't add up.
|
|
|
|
return
|
|
|
|
shelf.books.add(book)
|
|
|
|
shelf.save()
|
|
|
|
|
|
|
|
|
2020-10-16 22:40:23 +00:00
|
|
|
@app.task
|
|
|
|
def handle_update_user(activity):
|
|
|
|
''' receive an updated user Person activity object '''
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(remote_id=activity['object']['id'])
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
# who is this person? who cares
|
|
|
|
return
|
|
|
|
activitypub.Person(
|
|
|
|
**activity['object']
|
|
|
|
).to_model(models.User, instance=user)
|
|
|
|
# model save() happens in the to_model function
|
|
|
|
|
|
|
|
|
2020-05-04 01:56:29 +00:00
|
|
|
@app.task
|
|
|
|
def handle_update_book(activity):
|
|
|
|
''' a remote instance changed a book (Document) '''
|
|
|
|
document = activity['object']
|
|
|
|
# check if we have their copy and care about their updates
|
|
|
|
book = models.Book.objects.select_subclasses().filter(
|
2020-11-02 17:34:46 +00:00
|
|
|
remote_id=document['id'],
|
2020-05-04 01:56:29 +00:00
|
|
|
sync=True,
|
|
|
|
).first()
|
|
|
|
if not book:
|
|
|
|
return
|
|
|
|
|
|
|
|
books_manager.update_book(book, data=document)
|