From 030233fb1a7623b0e6b0bf7712b6561b3b1074f3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 5 Apr 2020 17:29:25 -0700 Subject: [PATCH] Serialize and broadcast ratings --- fedireads/activitypub/__init__.py | 1 + fedireads/activitypub/status.py | 32 ++++++++++++++++++++++--------- fedireads/outgoing.py | 14 ++++++++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) 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):