From 14d300162d7a945a48de1b01f1e2c6c6c633f58a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Feb 2020 17:30:23 -0800 Subject: [PATCH] Send a different review to mastodon than to fedireads --- fedireads/activitypub/__init__.py | 2 +- fedireads/activitypub/status.py | 14 +++++++++++++- fedireads/broadcast.py | 9 +++++++-- fedireads/incoming.py | 6 ++++-- ...20200217_0309.py => 0002_auto_20200219_0118.py} | 7 ++++++- fedireads/models/activity.py | 1 + fedireads/models/user.py | 1 + fedireads/outgoing.py | 12 +++++++++--- fedireads/remote_user.py | 3 ++- 9 files changed, 44 insertions(+), 11 deletions(-) rename fedireads/migrations/{0002_auto_20200217_0309.py => 0002_auto_20200219_0118.py} (90%) diff --git a/fedireads/activitypub/__init__.py b/fedireads/activitypub/__init__.py index ae6f788f9..ae9270692 100644 --- a/fedireads/activitypub/__init__.py +++ b/fedireads/activitypub/__init__.py @@ -4,4 +4,4 @@ from .collection import get_outbox, get_outbox_page, get_add, get_remove, \ get_following, get_followers from .create import get_create from .follow import get_follow_request, get_accept -from .status import get_review, get_status, get_replies +from .status import get_review, get_review_article, get_status, get_replies diff --git a/fedireads/activitypub/status.py b/fedireads/activitypub/status.py index 2f474f669..81508daf2 100644 --- a/fedireads/activitypub/status.py +++ b/fedireads/activitypub/status.py @@ -2,13 +2,25 @@ def get_review(review): ''' fedireads json for book reviews ''' status = get_status(review) - status['inReplyTo'] = review.book.absolute_id + status['inReplyToBook'] = review.book.absolute_id status['fedireadsType'] = review.status_type, status['name'] = review.name status['rating'] = review.rating return status +def get_review_article(review): + ''' a book review formatted for a non-fedireads isntance (mastodon) ''' + status = get_status(review) + name = 'Review of "%s" (%d stars): %s' % ( + review.book.data['title'], + review.rating, + review.name + ) + status['name'] = name + return status + + def get_status(status): ''' create activitypub json for a status ''' user = status.user diff --git a/fedireads/broadcast.py b/fedireads/broadcast.py index 49fbeb7f1..bc2a0da81 100644 --- a/fedireads/broadcast.py +++ b/fedireads/broadcast.py @@ -9,7 +9,7 @@ import requests from urllib.parse import urlparse -def get_recipients(user, post_privacy, direct_recipients=None): +def get_recipients(user, post_privacy, direct_recipients=None, limit=False): ''' deduplicated list of recipient inboxes ''' recipients = direct_recipients or [] if post_privacy == 'direct': @@ -17,7 +17,12 @@ def get_recipients(user, post_privacy, direct_recipients=None): return [u.inbox for u in recipients] # load all the followers of the user who is sending the message - followers = user.followers.all() + if not limit: + followers = user.followers.all() + else: + fedireads_user = limit == 'fedireads' + followers = user.followers.filter(fedireads_user=fedireads_user).all() + if post_privacy == 'public': # post to public shared inboxes shared_inboxes = set( diff --git a/fedireads/incoming.py b/fedireads/incoming.py index 9083815a7..e5b6653b5 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -205,11 +205,13 @@ def handle_incoming_create(activity): if not 'object' in activity: return HttpResponseBadRequest() + # TODO: should only create notes if they are relevent to a book, + # so, not every single thing someone posts on mastodon response = HttpResponse() content = activity['object'].get('content') if activity['object'].get('fedireadsType') == 'Review' and \ - 'inReplyTo' in activity['object']: - book = activity['object']['inReplyTo'] + 'inReplyToBook' in activity['object']: + book = activity['object']['inReplyToBook'] book = book.split('/')[-1] name = activity['object'].get('name') rating = activity['object'].get('rating') diff --git a/fedireads/migrations/0002_auto_20200217_0309.py b/fedireads/migrations/0002_auto_20200219_0118.py similarity index 90% rename from fedireads/migrations/0002_auto_20200217_0309.py rename to fedireads/migrations/0002_auto_20200219_0118.py index 1c05a22a2..ac28d9d9f 100644 --- a/fedireads/migrations/0002_auto_20200217_0309.py +++ b/fedireads/migrations/0002_auto_20200219_0118.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.3 on 2020-02-17 03:09 +# Generated by Django 3.0.3 on 2020-02-19 01:18 from django.db import migrations, models import django.utils.timezone @@ -72,4 +72,9 @@ class Migration(migrations.Migration): name='content', field=models.TextField(blank=True, null=True), ), + migrations.AddField( + model_name='user', + name='fedireads_user', + field=models.BooleanField(default=True), + ), ] diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index 087ddcf1e..8cbceea61 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -35,5 +35,6 @@ class Review(Status): def save(self, *args, **kwargs): self.status_type = 'Review' + self.activity_type = 'Article' super().save(*args, **kwargs) diff --git a/fedireads/models/user.py b/fedireads/models/user.py index 977c15532..038f93b0f 100644 --- a/fedireads/models/user.py +++ b/fedireads/models/user.py @@ -25,6 +25,7 @@ class User(AbstractUser): outbox = models.CharField(max_length=255, unique=True) summary = models.TextField(blank=True, null=True) local = models.BooleanField(default=True) + fedireads_user = models.BooleanField(default=True) localname = models.CharField( max_length=255, null=True, diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 86326f137..a026b5a1d 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -130,10 +130,16 @@ def handle_review(user, book, name, content, rating): review = create_review(user, book, name, content, rating) review_activity = activitypub.get_review(review) - create_activity = activitypub.get_create(user, review_activity) + review_create_activity = activitypub.get_create(user, review_activity) + fr_recipients = get_recipients(user, 'public', limit='fedireads') + broadcast(user, review_create_activity, fr_recipients) - recipients = get_recipients(user, 'public') - broadcast(user, create_activity, recipients) + # re-format the activity for non-fedireads servers + article_activity = activitypub.get_review_article(review) + article_create_activity = activitypub.get_create(user, article_activity) + + other_recipients = get_recipients(user, 'public', limit='other') + broadcast(user, article_create_activity, other_recipients) def handle_comment(user, review, content): diff --git a/fedireads/remote_user.py b/fedireads/remote_user.py index 80e5a53b9..013358b1c 100644 --- a/fedireads/remote_user.py +++ b/fedireads/remote_user.py @@ -41,7 +41,8 @@ def get_or_create_remote_user(actor): shared_inbox=shared_inbox, # TODO: I'm never actually using this for remote users public_key=data.get('publicKey').get('publicKeyPem'), - local=False + local=False, + fedireads_user=False, ) except KeyError: return False