diff --git a/fedireads/activitypub/__init__.py b/fedireads/activitypub/__init__.py index d934ae89c..c8eed0150 100644 --- a/fedireads/activitypub/__init__.py +++ b/fedireads/activitypub/__init__.py @@ -7,6 +7,7 @@ from .follow import get_follow_request, get_unfollow, get_accept, get_reject from .outbox import get_outbox, get_outbox_page from .shelve import get_add, get_remove from .status import get_review, get_review_article +from .status import get_rating, get_rating_note from .status import get_comment, get_comment_article from .status import get_status, get_replies, get_replies_page from .status import get_favorite, get_unfavorite diff --git a/fedireads/activitypub/status.py b/fedireads/activitypub/status.py index 318253c08..3a4f32c6a 100644 --- a/fedireads/activitypub/status.py +++ b/fedireads/activitypub/status.py @@ -4,6 +4,16 @@ from uuid import uuid4 from fedireads.settings import DOMAIN +def get_rating(review): + ''' activitypub serialize rating activity ''' + status = get_status(review) + status['inReplyToBook'] = review.book.absolute_id + status['fedireadsType'] = review.status_type + status['rating'] = review.rating + status['content'] = '%d star rating of "%s"' % ( + review.rating, review.book.title) + return status + def get_review(review): ''' fedireads json for book reviews ''' status = get_status(review) @@ -22,27 +32,31 @@ def get_comment(comment): return status +def get_rating_note(review): + ''' simple rating, send it as a note not an artciel ''' + status = get_status(review) + status['content'] = 'Rated "%s": %d stars' % ( + review.book.title, + review.rating, + ) + status['type'] = 'Note' + return status + def get_review_article(review): ''' a book review formatted for a non-fedireads isntance (mastodon) ''' status = get_status(review) - if review.rating and review.name: - name = 'Review of "%s" (%d stars): %s' % ( + if review.rating: + status['name'] = 'Review of "%s" (%d stars): %s' % ( review.book.title, review.rating, review.name ) - elif review.rating: - name = 'Rated "%s" (%d stars)' % ( - review.book.title, - review.rating, - ) else: - name = 'Review of "%s": %s' % ( + status['name'] = 'Review of "%s": %s' % ( review.book.title, review.name ) - status['name'] = name return status diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 641512d29..43d3d43c4 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -190,8 +190,18 @@ def handle_import_books(user, items): def handle_rate(user, book, rating): ''' a review that's just a rating ''' - review = create_rating(user, book, rating) - # TODO: serialize and broadcast + rating = create_rating(user, book, rating) + rating_activity = activitypub.get_rating(rating) + rating_create_activity = activitypub.get_create(user, rating_activity) + fr_recipients = get_recipients(user, 'public', limit='fedireads') + broadcast(user, rating_create_activity, fr_recipients) + + # re-format the activity for non-fedireads servers + note_activity = activitypub.get_rating_note(rating) + note_create_activity = activitypub.get_create(user, note_activity) + + other_recipients = get_recipients(user, 'public', limit='other') + broadcast(user, note_create_activity, other_recipients) def handle_review(user, book, name, content, rating):