From 4eeb8423503d6e1763fdf9c6a15c86a221cc1f4f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 16 Feb 2020 18:45:25 -0800 Subject: [PATCH] Status updates for shelving actions --- fedireads/activity.py | 19 +++++++++-- fedireads/incoming.py | 2 +- fedireads/migrations/0001_initial.py | 48 ++++------------------------ fedireads/models/activity.py | 2 ++ fedireads/outgoing.py | 24 +++++++++++--- fedireads/templates/feed.html | 5 +++ 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/fedireads/activity.py b/fedireads/activity.py index 43611592d..1bbf1b0b7 100644 --- a/fedireads/activity.py +++ b/fedireads/activity.py @@ -33,7 +33,7 @@ def create_review(user, possible_book, name, content, rating): ) -def create_status(user, content, reply_parent=None): +def create_status(user, content, reply_parent=None, mention_books=None): ''' a status update ''' # TODO: handle @'ing users @@ -42,12 +42,17 @@ def create_status(user, content, reply_parent=None): parser.feed(content) content = parser.get_output() - return models.Status.objects.create( + status = models.Status.objects.create( user=user, content=content, reply_parent=reply_parent, ) + for book in mention_books: + status.mention_books.add(book) + + return status + def get_status_json(status): ''' create activitypub json for a status ''' @@ -115,6 +120,16 @@ def get_create_json(user, status_json): +def get_add_json(*args): + ''' activitypub Add activity ''' + return get_add_remove_json(*args, action='Add') + + +def get_remove_json(*args): + ''' activitypub Add activity ''' + return get_add_remove_json(*args, action='Remove') + + def get_add_remove_json(user, book, shelf, action='Add'): ''' format an Add or Remove json blob ''' uuid = uuid4() diff --git a/fedireads/incoming.py b/fedireads/incoming.py index 380efed45..50029e1fc 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -259,7 +259,7 @@ def handle_incoming_create(activity): create_review(user, book, name, content, rating) except ValueError: return HttpResponseBadRequest() - else: + elif not user.local: try: create_status(user, content) except ValueError: diff --git a/fedireads/migrations/0001_initial.py b/fedireads/migrations/0001_initial.py index 4b9ee2e10..85d90959f 100644 --- a/fedireads/migrations/0001_initial.py +++ b/fedireads/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.3 on 2020-02-15 22:50 +# Generated by Django 3.0.3 on 2020-02-17 02:39 from django.conf import settings import django.contrib.auth.models @@ -56,20 +56,6 @@ class Migration(migrations.Migration): ('objects', django.contrib.auth.models.UserManager()), ], ), - migrations.CreateModel( - name='Activity', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('uuid', models.CharField(max_length=255, unique=True)), - ('content', fedireads.utils.fields.JSONField(max_length=5000)), - ('activity_type', models.CharField(max_length=255)), - ('fedireads_type', models.CharField(blank=True, max_length=255, null=True)), - ('local', models.BooleanField(default=True)), - ('created_date', models.DateTimeField(auto_now_add=True)), - ('updated_date', models.DateTimeField(auto_now=True)), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), - ], - ), migrations.CreateModel( name='Author', fields=[ @@ -118,11 +104,14 @@ 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', fedireads.utils.fields.JSONField(max_length=5000, null=True)), + ('activity_type', models.CharField(default='Note', max_length=255)), ('local', models.BooleanField(default=True)), + ('privacy', models.CharField(default='public', max_length=255)), + ('sensitive', models.BooleanField(default=False)), ('content', models.TextField(blank=True, null=True)), ('created_date', models.DateTimeField(auto_now_add=True)), - ('updated_date', models.DateTimeField(auto_now=True)), + ('mention_books', models.ManyToManyField(related_name='mention_book', to='fedireads.Book')), + ('mention_users', models.ManyToManyField(related_name='mention_user', to=settings.AUTH_USER_MODEL)), ('reply_parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='fedireads.Status')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), ], @@ -175,27 +164,10 @@ class Migration(migrations.Migration): name='user_permissions', field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions'), ), - migrations.CreateModel( - name='ShelveActivity', - fields=[ - ('activity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='fedireads.Activity')), - ('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Book')), - ('shelf', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Shelf')), - ], - bases=('fedireads.activity',), - ), migrations.AlterUniqueTogether( name='shelf', unique_together={('user', 'identifier')}, ), - migrations.CreateModel( - name='ReviewActivity', - fields=[ - ('activity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='fedireads.Activity')), - ('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Book')), - ], - bases=('fedireads.activity',), - ), migrations.CreateModel( name='Review', fields=[ @@ -206,12 +178,4 @@ class Migration(migrations.Migration): ], bases=('fedireads.status',), ), - migrations.CreateModel( - name='FollowActivity', - fields=[ - ('activity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='fedireads.Activity')), - ('followed', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='followed', to=settings.AUTH_USER_MODEL)), - ], - bases=('fedireads.activity',), - ), ] diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index e91734f82..009d369e5 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -8,6 +8,8 @@ class Status(models.Model): ''' any post, like a reply to a review, etc ''' user = models.ForeignKey('User', on_delete=models.PROTECT) status_type = models.CharField(max_length=255, default='Note') + mention_users = models.ManyToManyField('User', related_name='mention_user') + mention_books = models.ManyToManyField('Book', related_name='mention_book') activity_type = models.CharField(max_length=255, default='Note') local = models.BooleanField(default=True) privacy = models.CharField(max_length=255, default='public') diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index d134d47de..b8587ad7d 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -6,8 +6,8 @@ from urllib.parse import urlencode from uuid import uuid4 from fedireads import models -from fedireads.activity import create_review, get_status_json, get_create_json -from fedireads.activity import get_add_remove_json +from fedireads.activity import create_review, create_status, get_status_json +from fedireads.activity import get_add_json, get_remove_json, get_create_json from fedireads.remote_user import get_or_create_remote_user from fedireads.broadcast import get_recipients, broadcast from fedireads.settings import DOMAIN @@ -131,11 +131,25 @@ def handle_shelve(user, book, shelf): # TODO: this should probably happen in incoming instead models.ShelfBook(book=book, shelf=shelf, added_by=user).save() - activity = get_add_remove_json(user, book, shelf, 'Add') + activity = get_add_json(user, book, shelf) recipients = get_recipients(user, 'public') - broadcast(user, activity, recipients) + # 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]) + + activity = get_status_json(status) + create_activity = get_create_json(user, activity) + + broadcast(user, create_activity, recipients) + def handle_unshelve(user, book, shelf): ''' a local user is getting a book put on their shelf ''' @@ -144,7 +158,7 @@ def handle_unshelve(user, book, shelf): row = models.ShelfBook.objects.get(book=book, shelf=shelf) row.delete() - activity = get_add_remove_json(user, book, shelf, 'Remove') + activity = get_remove_json(user, book, shelf) recipients = get_recipients(user, 'public') broadcast(user, activity, recipients) diff --git a/fedireads/templates/feed.html b/fedireads/templates/feed.html index 33f649ee1..9fb3d1e96 100644 --- a/fedireads/templates/feed.html +++ b/fedireads/templates/feed.html @@ -70,6 +70,11 @@ {% elif activity.status_type == 'Note' %} posted {{ activity.content | safe }} + {% for book in activity.mention_books.all %} +
+ {% include 'snippets/book.html' with book=book size=large description=True %} +
+ {% endfor %} {% else %} {# generic handling for a misc activity, which perhaps should not be displayed at all #} did {{ activity.activity_type }}