From 8bd566bccb3c3ad890ab22dc864536c65b4eb3fe Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 15 Feb 2020 12:32:40 -0800 Subject: [PATCH] Endpoint for status --- fedireads/incoming.py | 48 +++++++++++++++++++++------- fedireads/migrations/0001_initial.py | 3 +- fedireads/models/activity.py | 1 + fedireads/outgoing.py | 6 ++-- fedireads/urls.py | 9 ++++++ fedireads/wellknown.py | 2 +- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/fedireads/incoming.py b/fedireads/incoming.py index d62c2033..6adde34a 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -138,6 +138,26 @@ def get_actor(request, username): }) +@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() + + return JsonResponse(status.activity) + + + + @csrf_exempt def get_followers(request, username): ''' return a list of followers for an actor ''' @@ -267,32 +287,36 @@ def handle_incoming_create(activity): return HttpResponseBadRequest() response = HttpResponse() - # if it's an article and in reply to a book, we have a review - if activity['object']['fedireadsType'] == 'Review' and \ + if activity['object'].get('fedireadsType') == 'Review' and \ 'inReplyTo' in activity['object']: book = activity['object']['inReplyTo'] + book = book.split('/')[-1] name = activity['object'].get('name') content = activity['object'].get('content') rating = activity['object'].get('rating') - try: - create_review(user, book, name, content, rating) - except ValueError: - return HttpResponseBadRequest() - models.ReviewActivity( + if user.local: + review_id = activity['object']['id'].split('/')[-1] + review = models.Review.objects.get(id=review_id) + else: + try: + review = create_review(user, book, name, content, rating) + except ValueError: + return HttpResponseBadRequest() + models.ReviewActivity.objects.create( uuid=activity['id'], user=user, - content=activity, + content=activity['object'], activity_type=activity['object']['type'], - book=book, - ).save() + book=review.book, + ) else: - models.Activity( + models.Activity.objects.create( uuid=activity['id'], user=user, content=activity, activity_type=activity['object']['type'] - ).save() + ) return response diff --git a/fedireads/migrations/0001_initial.py b/fedireads/migrations/0001_initial.py index 6e6c9ff4..b9036bdf 100644 --- a/fedireads/migrations/0001_initial.py +++ b/fedireads/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.2 on 2020-02-15 18:25 +# Generated by Django 3.0.2 on 2020-02-15 20:11 from django.conf import settings import django.contrib.auth.models @@ -122,6 +122,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('status_type', models.CharField(default='Note', max_length=255)), + ('activity', django.contrib.postgres.fields.jsonb.JSONField(max_length=5000, null=True)), ('content', models.TextField(blank=True, null=True)), ('created_date', models.DateTimeField(auto_now_add=True)), ('updated_date', models.DateTimeField(auto_now=True)), diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index 619ab163..855637f5 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -56,6 +56,7 @@ class Status(models.Model): ''' reply to a review, etc ''' user = models.ForeignKey('User', on_delete=models.PROTECT) status_type = models.CharField(max_length=255, default='Note') + activity = JSONField(max_length=5000, null=True) reply_parent = models.ForeignKey( 'self', null=True, diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 20e7b625..016aa703 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -252,8 +252,10 @@ def handle_review(user, book, name, content, rating): } } } + review.activity = review_activity + review.save() - activity = { + create_activity = { '@context': 'https://www.w3.org/ns/activitystreams', 'id': '%s/activity' % review_path, @@ -269,5 +271,5 @@ def handle_review(user, book, name, content, rating): } recipients = get_recipients(user, 'public') - broadcast(user, activity, recipients) + broadcast(user, create_activity, recipients) diff --git a/fedireads/urls.py b/fedireads/urls.py index 3cbeb524..97177928 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -16,6 +16,15 @@ urlpatterns = [ re_path(r'^user/(?P\w+)/outbox/?$', outgoing.outbox), re_path(r'^user/(?P\w+)/followers/?$', incoming.get_followers), re_path(r'^user/(?P\w+)/following/?$', incoming.get_following), + re_path( + r'^user/(?P\w+)/status/(?P\d+)/?$', + incoming.get_status + ), + re_path( + r'^user/(?P\w+)/status/(?P\d+)/activity/?$', + incoming.get_status + ), + re_path(r'^user/(?P\w+)/status/?$', incoming.get_following), # TODO: shelves need pages in the UI and for their activitypub Collection # .well-known endpoints diff --git a/fedireads/wellknown.py b/fedireads/wellknown.py index a0a4ec8e..186a3754 100644 --- a/fedireads/wellknown.py +++ b/fedireads/wellknown.py @@ -55,7 +55,7 @@ def nodeinfo(request): "version": "2.0", "software": { "name": "mastodon", - "version": "3.0.1" + "version": "0.0.1" }, "protocols": [ "activitypub"