Status updates for shelving actions

This commit is contained in:
Mouse Reeve 2020-02-16 18:45:25 -08:00
parent f28ee934fc
commit 4eeb842350
6 changed files with 50 additions and 50 deletions

View file

@ -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 ''' ''' a status update '''
# TODO: handle @'ing users # TODO: handle @'ing users
@ -42,12 +42,17 @@ def create_status(user, content, reply_parent=None):
parser.feed(content) parser.feed(content)
content = parser.get_output() content = parser.get_output()
return models.Status.objects.create( status = models.Status.objects.create(
user=user, user=user,
content=content, content=content,
reply_parent=reply_parent, reply_parent=reply_parent,
) )
for book in mention_books:
status.mention_books.add(book)
return status
def get_status_json(status): def get_status_json(status):
''' create activitypub json for a 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'): def get_add_remove_json(user, book, shelf, action='Add'):
''' format an Add or Remove json blob ''' ''' format an Add or Remove json blob '''
uuid = uuid4() uuid = uuid4()

View file

@ -259,7 +259,7 @@ def handle_incoming_create(activity):
create_review(user, book, name, content, rating) create_review(user, book, name, content, rating)
except ValueError: except ValueError:
return HttpResponseBadRequest() return HttpResponseBadRequest()
else: elif not user.local:
try: try:
create_status(user, content) create_status(user, content)
except ValueError: except ValueError:

View file

@ -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 from django.conf import settings
import django.contrib.auth.models import django.contrib.auth.models
@ -56,20 +56,6 @@ class Migration(migrations.Migration):
('objects', django.contrib.auth.models.UserManager()), ('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( migrations.CreateModel(
name='Author', name='Author',
fields=[ fields=[
@ -118,11 +104,14 @@ 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', fedireads.utils.fields.JSONField(max_length=5000, null=True)), ('activity_type', models.CharField(default='Note', max_length=255)),
('local', models.BooleanField(default=True)), ('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)), ('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)), ('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')), ('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)), ('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', 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'), 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( migrations.AlterUniqueTogether(
name='shelf', name='shelf',
unique_together={('user', 'identifier')}, 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( migrations.CreateModel(
name='Review', name='Review',
fields=[ fields=[
@ -206,12 +178,4 @@ class Migration(migrations.Migration):
], ],
bases=('fedireads.status',), 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',),
),
] ]

View file

@ -8,6 +8,8 @@ class Status(models.Model):
''' any post, like a reply to a review, etc ''' ''' any post, like a 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')
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') activity_type = models.CharField(max_length=255, default='Note')
local = models.BooleanField(default=True) local = models.BooleanField(default=True)
privacy = models.CharField(max_length=255, default='public') privacy = models.CharField(max_length=255, default='public')

View file

@ -6,8 +6,8 @@ from urllib.parse import urlencode
from uuid import uuid4 from uuid import uuid4
from fedireads import models from fedireads import models
from fedireads.activity import create_review, get_status_json, get_create_json from fedireads.activity import create_review, create_status, get_status_json
from fedireads.activity import get_add_remove_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.remote_user import get_or_create_remote_user
from fedireads.broadcast import get_recipients, broadcast from fedireads.broadcast import get_recipients, broadcast
from fedireads.settings import DOMAIN from fedireads.settings import DOMAIN
@ -131,11 +131,25 @@ def handle_shelve(user, book, shelf):
# TODO: this should probably happen in incoming instead # TODO: this should probably happen in incoming instead
models.ShelfBook(book=book, shelf=shelf, added_by=user).save() 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') recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients) 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): def handle_unshelve(user, book, shelf):
''' a local user is getting a book put on their 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 = models.ShelfBook.objects.get(book=book, shelf=shelf)
row.delete() row.delete()
activity = get_add_remove_json(user, book, shelf, 'Remove') activity = get_remove_json(user, book, shelf)
recipients = get_recipients(user, 'public') recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients) broadcast(user, activity, recipients)

View file

@ -70,6 +70,11 @@
{% elif activity.status_type == 'Note' %} {% elif activity.status_type == 'Note' %}
posted</h2> posted</h2>
{{ activity.content | safe }} {{ activity.content | safe }}
{% for book in activity.mention_books.all %}
<div class="book-preview review">
{% include 'snippets/book.html' with book=book size=large description=True %}
</div>
{% endfor %}
{% else %} {% else %}
{# generic handling for a misc activity, which perhaps should not be displayed at all #} {# generic handling for a misc activity, which perhaps should not be displayed at all #}
did {{ activity.activity_type }} did {{ activity.activity_type }}