Endpoint for status

This commit is contained in:
Mouse Reeve 2020-02-15 12:32:40 -08:00
parent 8aba8e80e4
commit 8bd566bccb
6 changed files with 53 additions and 16 deletions

View file

@ -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 @csrf_exempt
def get_followers(request, username): def get_followers(request, username):
''' return a list of followers for an actor ''' ''' return a list of followers for an actor '''
@ -267,32 +287,36 @@ def handle_incoming_create(activity):
return HttpResponseBadRequest() return HttpResponseBadRequest()
response = HttpResponse() response = HttpResponse()
# if it's an article and in reply to a book, we have a review if activity['object'].get('fedireadsType') == 'Review' and \
if activity['object']['fedireadsType'] == 'Review' and \
'inReplyTo' in activity['object']: 'inReplyTo' in activity['object']:
book = activity['object']['inReplyTo'] book = activity['object']['inReplyTo']
book = book.split('/')[-1]
name = activity['object'].get('name') name = activity['object'].get('name')
content = activity['object'].get('content') content = activity['object'].get('content')
rating = activity['object'].get('rating') rating = activity['object'].get('rating')
try: if user.local:
create_review(user, book, name, content, rating) review_id = activity['object']['id'].split('/')[-1]
except ValueError: review = models.Review.objects.get(id=review_id)
return HttpResponseBadRequest() else:
models.ReviewActivity( try:
review = create_review(user, book, name, content, rating)
except ValueError:
return HttpResponseBadRequest()
models.ReviewActivity.objects.create(
uuid=activity['id'], uuid=activity['id'],
user=user, user=user,
content=activity, content=activity['object'],
activity_type=activity['object']['type'], activity_type=activity['object']['type'],
book=book, book=review.book,
).save() )
else: else:
models.Activity( models.Activity.objects.create(
uuid=activity['id'], uuid=activity['id'],
user=user, user=user,
content=activity, content=activity,
activity_type=activity['object']['type'] activity_type=activity['object']['type']
).save() )
return response return response

View file

@ -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 from django.conf import settings
import django.contrib.auth.models import django.contrib.auth.models
@ -122,6 +122,7 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status_type', models.CharField(default='Note', max_length=255)), ('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)), ('content', models.TextField(blank=True, null=True)),
('created_date', models.DateTimeField(auto_now_add=True)), ('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)), ('updated_date', models.DateTimeField(auto_now=True)),

View file

@ -56,6 +56,7 @@ class Status(models.Model):
''' reply to a review, etc ''' ''' reply to a review, etc '''
user = models.ForeignKey('User', on_delete=models.PROTECT) user = models.ForeignKey('User', on_delete=models.PROTECT)
status_type = models.CharField(max_length=255, default='Note') status_type = models.CharField(max_length=255, default='Note')
activity = JSONField(max_length=5000, null=True)
reply_parent = models.ForeignKey( reply_parent = models.ForeignKey(
'self', 'self',
null=True, null=True,

View file

@ -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', '@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s/activity' % review_path, 'id': '%s/activity' % review_path,
@ -269,5 +271,5 @@ def handle_review(user, book, name, content, rating):
} }
recipients = get_recipients(user, 'public') recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients) broadcast(user, create_activity, recipients)

View file

@ -16,6 +16,15 @@ urlpatterns = [
re_path(r'^user/(?P<username>\w+)/outbox/?$', outgoing.outbox), re_path(r'^user/(?P<username>\w+)/outbox/?$', outgoing.outbox),
re_path(r'^user/(?P<username>\w+)/followers/?$', incoming.get_followers), re_path(r'^user/(?P<username>\w+)/followers/?$', incoming.get_followers),
re_path(r'^user/(?P<username>\w+)/following/?$', incoming.get_following), re_path(r'^user/(?P<username>\w+)/following/?$', incoming.get_following),
re_path(
r'^user/(?P<username>\w+)/status/(?P<status_id>\d+)/?$',
incoming.get_status
),
re_path(
r'^user/(?P<username>\w+)/status/(?P<status_id>\d+)/activity/?$',
incoming.get_status
),
re_path(r'^user/(?P<username>\w+)/status/?$', incoming.get_following),
# TODO: shelves need pages in the UI and for their activitypub Collection # TODO: shelves need pages in the UI and for their activitypub Collection
# .well-known endpoints # .well-known endpoints

View file

@ -55,7 +55,7 @@ def nodeinfo(request):
"version": "2.0", "version": "2.0",
"software": { "software": {
"name": "mastodon", "name": "mastodon",
"version": "3.0.1" "version": "0.0.1"
}, },
"protocols": [ "protocols": [
"activitypub" "activitypub"