Send a different review to mastodon than to fedireads

This commit is contained in:
Mouse Reeve 2020-02-18 17:30:23 -08:00
parent 0447fef279
commit 14d300162d
9 changed files with 44 additions and 11 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -35,5 +35,6 @@ class Review(Status):
def save(self, *args, **kwargs):
self.status_type = 'Review'
self.activity_type = 'Article'
super().save(*args, **kwargs)

View file

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

View file

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

View file

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