diff --git a/fedireads/activity.py b/fedireads/activity.py new file mode 100644 index 000000000..0fc54ba2b --- /dev/null +++ b/fedireads/activity.py @@ -0,0 +1,31 @@ +''' Handle user activity ''' +from fedireads import models +from fedireads.openlibrary import get_or_create_book +from fedireads.sanitize_html import InputHtmlParser + + +def create_review(user, possible_book, name, content, rating): + ''' a book review has been added ''' + # throws a value error if the book is not found + book = get_or_create_book(possible_book) + + # sanitize review html + import pdb;pdb.set_trace() + parser = InputHtmlParser() + parser.feed(content) + content = parser.get_output() + + # no ratings outside of 0-5 + rating = rating if 0 <= rating <= 5 else 0 + + review = models.Review.objects.create( + user=user, + book=book, + name=name, + rating=rating, + content=content, + ) + + return review + + diff --git a/fedireads/incoming.py b/fedireads/incoming.py index d51bb6c1e..d62c2033f 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -12,30 +12,7 @@ import requests from fedireads import models from fedireads import outgoing from fedireads.activity import create_review -from fedireads.openlibrary import get_or_create_book from fedireads.remote_user import get_or_create_remote_user -from fedireads.sanitize_html import InputHtmlParser - - -def webfinger(request): - ''' allow other servers to ask about a user ''' - resource = request.GET.get('resource') - if not resource and not resource.startswith('acct:'): - return HttpResponseBadRequest() - ap_id = resource.replace('acct:', '') - user = models.User.objects.filter(username=ap_id).first() - if not user: - return HttpResponseNotFound('No account found') - return JsonResponse({ - 'subject': 'acct:%s' % (user.username), - 'links': [ - { - 'rel': 'self', - 'type': 'application/activity+json', - 'href': user.actor - } - ] - }) @csrf_exempt diff --git a/fedireads/urls.py b/fedireads/urls.py index 7105089be..8767d2505 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -3,7 +3,7 @@ from django.conf.urls.static import static from django.contrib import admin from django.urls import path, re_path -from fedireads import incoming, outgoing, views, settings +from fedireads import incoming, outgoing, views, settings, wellknown urlpatterns = [ @@ -17,7 +17,11 @@ urlpatterns = [ re_path(r'^user/(?P\w+)/followers/?$', incoming.get_followers), re_path(r'^user/(?P\w+)/following/?$', incoming.get_following), # TODO: shelves need pages in the UI and for their activitypub Collection - re_path(r'^.well-known/webfinger/?$', incoming.webfinger), + + # .well-known endpoints + re_path(r'^.well-known/webfinger/?$', wellknown.webfinger), + re_path(r'^.well-known/nodeinfo/?$', wellknown.nodeinfo), + re_path(r'^api/v1/instance/?$', wellknown.instance_info), # TODO: re_path(r'^.well-known/host-meta/?$', incoming.host_meta), # ui views diff --git a/fedireads/wellknown.py b/fedireads/wellknown.py new file mode 100644 index 000000000..541c1dc20 --- /dev/null +++ b/fedireads/wellknown.py @@ -0,0 +1,72 @@ +''' responds to various requests to /.well-know ''' +from django.http import HttpResponseBadRequest, HttpResponseNotFound +from django.http import JsonResponse + +from fedireads import models +from fedireads.settings import DOMAIN + + +def webfinger(request): + ''' allow other servers to ask about a user ''' + if request.method != 'GET': + return HttpResponseNotFound() + + resource = request.GET.get('resource') + if not resource and not resource.startswith('acct:'): + return HttpResponseBadRequest() + ap_id = resource.replace('acct:', '') + user = models.User.objects.filter(username=ap_id).first() + if not user: + return HttpResponseNotFound('No account found') + return JsonResponse({ + 'subject': 'acct:%s' % (user.username), + 'links': [ + { + 'rel': 'self', + 'type': 'application/activity+json', + 'href': user.actor + } + ] + }) + + +def nodeinfo(request): + ''' idk what this is, but mastodon asked for it ''' + if request.method != 'GET': + return HttpResponseNotFound() + + return JsonResponse({ + 'links': [ + { + 'rel': 'http://nodeinfo.diaspora.software/ns/schema/2.0', + 'href': 'https://%s/nodeinfo/2.0' % DOMAIN + } + ] + }) + + +def instance_info(request): + ''' what this place is TODO: should be settable/editable ''' + if request.method != 'GET': + return HttpResponseNotFound() + + user_count = models.User.objects.count() + status_count = models.Status.objects.count() + return JsonResponse({ + 'uri': DOMAIN, + 'title': 'FediReads', + 'short_description': 'Social reading, decentralized', + 'description': '', + 'email': 'mousereeve@riseup.net', + 'version': '0.0.1', + 'stats': { + 'user_count': user_count, + 'status_count': status_count, + }, + 'thumbnail': '', # TODO: logo thumbnail + 'languages': [ + 'en' + ], + 'registrations': True, + 'approval_required': False, + })