2020-01-28 19:45:27 +00:00
|
|
|
''' handles all the activity coming out of the server '''
|
2020-02-11 03:42:21 +00:00
|
|
|
from django.http import HttpResponseNotFound, JsonResponse
|
2020-01-28 09:01:33 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
import requests
|
2020-02-11 03:42:21 +00:00
|
|
|
from urllib.parse import urlencode
|
2020-01-28 09:01:33 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
from fedireads import models
|
2020-02-18 04:12:19 +00:00
|
|
|
from fedireads.status import create_review, create_status
|
|
|
|
from fedireads import activitypub
|
2020-02-11 23:17:21 +00:00
|
|
|
from fedireads.remote_user import get_or_create_remote_user
|
|
|
|
from fedireads.broadcast import get_recipients, broadcast
|
2020-02-07 21:39:48 +00:00
|
|
|
from fedireads.settings import DOMAIN
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-01-28 19:13:13 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def outbox(request, username):
|
|
|
|
''' outbox for the requested user '''
|
|
|
|
user = models.User.objects.get(localname=username)
|
2020-02-11 03:42:21 +00:00
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
# paginated list of messages
|
|
|
|
if request.GET.get('page'):
|
|
|
|
limit = 20
|
|
|
|
min_id = request.GET.get('min_id')
|
|
|
|
max_id = request.GET.get('max_id')
|
|
|
|
|
2020-02-17 00:40:48 +00:00
|
|
|
query_path = user.outbox + '?'
|
2020-02-11 03:42:21 +00:00
|
|
|
# filters for use in the django queryset min/max
|
|
|
|
filters = {}
|
|
|
|
# params for the outbox page id
|
|
|
|
params = {'page': 'true'}
|
|
|
|
if min_id != None:
|
|
|
|
params['min_id'] = min_id
|
|
|
|
filters['id__gt'] = min_id
|
|
|
|
if max_id != None:
|
|
|
|
params['max_id'] = max_id
|
|
|
|
filters['id__lte'] = max_id
|
2020-02-17 00:20:11 +00:00
|
|
|
collection_id = query_path + urlencode(params)
|
2020-02-11 03:42:21 +00:00
|
|
|
|
|
|
|
outbox_page = {
|
2020-01-28 19:13:13 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-02-11 03:42:21 +00:00
|
|
|
'id': collection_id,
|
|
|
|
'type': 'OrderedCollectionPage',
|
2020-02-17 00:40:48 +00:00
|
|
|
'partOf': user.outbox,
|
2020-02-17 02:22:01 +00:00
|
|
|
'orderedItems': [],
|
2020-02-11 03:42:21 +00:00
|
|
|
}
|
2020-02-17 02:22:01 +00:00
|
|
|
statuses = models.Status.objects.filter(user=user, **filters).all()
|
|
|
|
for status in statuses[:limit]:
|
2020-02-18 04:12:19 +00:00
|
|
|
outbox_page['orderedItems'].append(activitypub.get_status(status))
|
2020-02-17 02:22:01 +00:00
|
|
|
|
2020-02-11 03:42:21 +00:00
|
|
|
if max_id:
|
2020-02-17 00:20:11 +00:00
|
|
|
outbox_page['next'] = query_path + \
|
2020-02-11 03:42:21 +00:00
|
|
|
urlencode({'min_id': max_id, 'page': 'true'})
|
|
|
|
if min_id:
|
2020-02-17 00:20:11 +00:00
|
|
|
outbox_page['prev'] = query_path + \
|
2020-02-11 03:42:21 +00:00
|
|
|
urlencode({'max_id': min_id, 'page': 'true'})
|
|
|
|
return JsonResponse(outbox_page)
|
|
|
|
|
|
|
|
# collection overview
|
2020-02-17 02:22:01 +00:00
|
|
|
size = models.Status.objects.filter(user=user).count()
|
2020-02-11 03:42:21 +00:00
|
|
|
return JsonResponse({
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': '%s/outbox' % user.actor,
|
|
|
|
'type': 'OrderedCollection',
|
|
|
|
'totalItems': size,
|
|
|
|
'first': '%s/outbox?page=true' % user.actor,
|
|
|
|
'last': '%s/outbox?min_id=0&page=true' % user.actor
|
|
|
|
})
|
2020-01-28 19:13:13 +00:00
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_account_search(query):
|
|
|
|
''' webfingerin' other servers '''
|
2020-01-28 19:13:13 +00:00
|
|
|
user = None
|
2020-01-28 09:01:33 +00:00
|
|
|
domain = query.split('@')[1]
|
|
|
|
try:
|
|
|
|
user = models.User.objects.get(username=query)
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
|
|
|
|
(domain, query)
|
|
|
|
response = requests.get(url)
|
|
|
|
if not response.ok:
|
|
|
|
response.raise_for_status()
|
|
|
|
data = response.json()
|
|
|
|
for link in data['links']:
|
|
|
|
if link['rel'] == 'self':
|
|
|
|
user = get_or_create_remote_user(link['href'])
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def handle_outgoing_follow(user, to_follow):
|
|
|
|
''' someone local wants to follow someone '''
|
|
|
|
uuid = uuid4()
|
|
|
|
activity = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-02-07 21:39:48 +00:00
|
|
|
'id': 'https://%s/%s' % (DOMAIN, str(uuid)),
|
2020-01-28 09:01:33 +00:00
|
|
|
'summary': '',
|
|
|
|
'type': 'Follow',
|
|
|
|
'actor': user.actor,
|
|
|
|
'object': to_follow.actor,
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:55:48 +00:00
|
|
|
errors = broadcast(user, activity, [to_follow.inbox])
|
|
|
|
for error in errors:
|
|
|
|
raise(error['error'])
|
2020-01-28 09:01:33 +00:00
|
|
|
|
|
|
|
|
2020-02-15 06:44:07 +00:00
|
|
|
def handle_outgoing_accept(user, to_follow, activity):
|
|
|
|
''' send an acceptance message to a follow request '''
|
|
|
|
to_follow.followers.add(user)
|
|
|
|
activity = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-02-18 01:53:40 +00:00
|
|
|
'id': '%s#accepts/follows/' % to_follow.absolute_id,
|
2020-02-15 06:44:07 +00:00
|
|
|
'type': 'Accept',
|
|
|
|
'actor': to_follow.actor,
|
|
|
|
'object': activity,
|
|
|
|
}
|
|
|
|
recipient = get_recipients(
|
|
|
|
to_follow,
|
|
|
|
'direct',
|
|
|
|
direct_recipients=[user]
|
|
|
|
)
|
|
|
|
broadcast(to_follow, activity, recipient)
|
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_shelve(user, book, shelf):
|
2020-01-28 19:13:13 +00:00
|
|
|
''' a local user is getting a book put on their shelf '''
|
2020-01-28 09:01:33 +00:00
|
|
|
# update the database
|
2020-02-17 02:22:01 +00:00
|
|
|
# TODO: this should probably happen in incoming instead
|
2020-01-28 09:01:33 +00:00
|
|
|
models.ShelfBook(book=book, shelf=shelf, added_by=user).save()
|
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
activity = activitypub.get_add(user, book, shelf)
|
2020-01-28 19:13:13 +00:00
|
|
|
recipients = get_recipients(user, 'public')
|
2020-01-28 09:01:33 +00:00
|
|
|
broadcast(user, activity, recipients)
|
|
|
|
|
2020-02-17 02:45:25 +00:00
|
|
|
# tell the world about this cool thing that happened
|
|
|
|
verb = {
|
|
|
|
'to-read': 'wants to read',
|
|
|
|
'reading': 'started reading',
|
|
|
|
'read': 'finished reading'
|
|
|
|
}[shelf.identifier]
|
|
|
|
name = user.name if user.name else user.localname
|
|
|
|
message = '%s %s %s' % (name, verb, book.data['title'])
|
|
|
|
status = create_status(user, message, mention_books=[book])
|
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
activity = activitypub.get_status(status)
|
|
|
|
create_activity = activitypub.get_create(user, activity)
|
2020-02-17 02:45:25 +00:00
|
|
|
|
|
|
|
broadcast(user, create_activity, recipients)
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-01-29 08:22:48 +00:00
|
|
|
def handle_unshelve(user, book, shelf):
|
|
|
|
''' a local user is getting a book put on their shelf '''
|
|
|
|
# update the database
|
2020-02-17 02:22:01 +00:00
|
|
|
# TODO: this should probably happen in incoming instead
|
2020-01-29 08:22:48 +00:00
|
|
|
row = models.ShelfBook.objects.get(book=book, shelf=shelf)
|
|
|
|
row.delete()
|
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
activity = activitypub.get_remove(user, book, shelf)
|
2020-01-29 08:22:48 +00:00
|
|
|
recipients = get_recipients(user, 'public')
|
|
|
|
|
|
|
|
broadcast(user, activity, recipients)
|
|
|
|
|
|
|
|
|
2020-01-28 09:01:33 +00:00
|
|
|
def handle_review(user, book, name, content, rating):
|
|
|
|
''' post a review '''
|
2020-02-15 19:13:49 +00:00
|
|
|
# validated and saves the review in the database so it has an id
|
|
|
|
review = create_review(user, book, name, content, rating)
|
|
|
|
|
2020-02-18 04:12:19 +00:00
|
|
|
review_activity = activitypub.get_review(review)
|
|
|
|
create_activity = activitypub.get_create(user, review_activity)
|
2020-01-28 09:01:33 +00:00
|
|
|
|
2020-02-15 19:13:49 +00:00
|
|
|
recipients = get_recipients(user, 'public')
|
2020-02-15 20:32:40 +00:00
|
|
|
broadcast(user, create_activity, recipients)
|
2020-01-28 09:01:33 +00:00
|
|
|
|