Adds status type for app-generated statuses

This commit is contained in:
Mouse Reeve 2020-09-28 17:26:15 -07:00
parent 10d18cc633
commit 39931e2e69
6 changed files with 47 additions and 11 deletions

View file

@ -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

View file

@ -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 '''

View file

@ -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

View file

@ -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(
'<a href="%s">"%s"</a>' % (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 '''

View file

@ -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))

View file

@ -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: