From 39931e2e69731818622b97a94f2fefca81dde215 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 28 Sep 2020 17:26:15 -0700 Subject: [PATCH] Adds status type for app-generated statuses --- bookwyrm/activitypub/__init__.py | 2 +- bookwyrm/activitypub/note.py | 6 ++++++ bookwyrm/models/__init__.py | 2 +- bookwyrm/models/status.py | 15 +++++++++++++++ bookwyrm/outgoing.py | 10 ++++------ bookwyrm/status.py | 23 ++++++++++++++++++++--- 6 files changed, 47 insertions(+), 11 deletions(-) diff --git a/bookwyrm/activitypub/__init__.py b/bookwyrm/activitypub/__init__.py index 9ff57a215..03c714a6b 100644 --- a/bookwyrm/activitypub/__init__.py +++ b/bookwyrm/activitypub/__init__.py @@ -3,7 +3,7 @@ import inspect import sys from .base_activity import ActivityEncoder, Image, PublicKey, Signature -from .note import Note, Article, Comment, Review, Quotation +from .note import Note, GeneratedNote, Article, Comment, Review, Quotation from .interaction import Boost, Like from .ordered_collection import OrderedCollection, OrderedCollectionPage from .person import Person diff --git a/bookwyrm/activitypub/note.py b/bookwyrm/activitypub/note.py index 7a5c0a76a..14fd178ff 100644 --- a/bookwyrm/activitypub/note.py +++ b/bookwyrm/activitypub/note.py @@ -28,6 +28,12 @@ class Article(Note): type: str = 'Article' +@dataclass(init=False) +class GeneratedNote(Note): + ''' just a re-typed note ''' + type: str = 'NoteUpdate' + + @dataclass(init=False) class Comment(Note): ''' like a note but with a book ''' diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index b941d447e..1ed4a6737 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -6,7 +6,7 @@ from .book import Book, Work, Edition, Author from .connector import Connector from .relationship import UserFollows, UserFollowRequest, UserBlocks from .shelf import Shelf, ShelfBook -from .status import Status, Review, Comment, Quotation +from .status import Status, GeneratedStatus, Review, Comment, Quotation from .status import Favorite, Boost, Notification, ReadThrough from .tag import Tag from .user import User diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index a90534d17..8ab3f0eb9 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -99,6 +99,21 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel): **kwargs ) +class GeneratedStatus(Status): + ''' these are app-generated messages about user activity ''' + @property + def ap_pure_content(self): + ''' indicate the book in question for mastodon (or w/e) users ''' + message = self.content + books = ', '.join( + '"%s"' % (self.book.local_id, self.book.title) \ + for book in self.mention_books + ) + return '%s %s' % (message, books) + + activity_serializer = activitypub.GeneratedNote + pure_activity_serializer = activitypub.Note + class Comment(Status): ''' like a review but without a rating and transient ''' diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index bc0aab2a5..9d894a26b 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -12,6 +12,7 @@ from bookwyrm.broadcast import broadcast from bookwyrm.status import create_review, create_status from bookwyrm.status import create_quotation, create_comment from bookwyrm.status import create_tag, create_notification, create_rating +from bookwyrm.status import create_generated_note from bookwyrm.remote_user import get_or_create_remote_user @@ -107,14 +108,12 @@ def handle_shelve(user, book, shelf): broadcast(user, shelve.to_add_activity(user)) # tell the world about this cool thing that happened - verb = { + message = { 'to-read': 'wants to read', 'reading': 'started reading', 'read': 'finished reading' }[shelf.identifier] - message = '%s "%s"' % (verb, book.title) - status = create_status(user, message, mention_books=[book]) - status.status_type = 'Update' + status = create_generated_note(user, message, mention_books=[book]) status.save() if shelf.identifier == 'reading': @@ -187,8 +186,7 @@ def handle_import_books(user, items): if new_books: message = 'imported {} books'.format(len(new_books)) - status = create_status(user, message, mention_books=new_books) - status.status_type = 'Update' + status = create_generated_note(user, message, mention_books=new_books) status.save() broadcast(user, status.to_create_activity(user)) diff --git a/bookwyrm/status.py b/bookwyrm/status.py index f6bfb9cf1..0c13638e2 100644 --- a/bookwyrm/status.py +++ b/bookwyrm/status.py @@ -101,8 +101,26 @@ def get_status(remote_id): ).first() -def create_status(user, content, reply_parent=None, mention_books=None, - remote_id=None): +def create_generated_note(user, content, mention_books=None): + ''' a note created by the app about user activity ''' + # sanitize input html + parser = InputHtmlParser() + parser.feed(content) + content = parser.get_output() + + status = models.GeneratedStatus.objects.create( + user=user, + content=content, + ) + + if mention_books: + for book in mention_books: + status.mention_books.add(book) + + return status + + +def create_status(user, content, reply_parent=None, mention_books=None): ''' a status update ''' # TODO: handle @'ing users @@ -115,7 +133,6 @@ def create_status(user, content, reply_parent=None, mention_books=None, user=user, content=content, reply_parent=reply_parent, - remote_id=remote_id, ) if mention_books: