forked from mirrors/bookwyrm
Send a different review to mastodon than to fedireads
This commit is contained in:
parent
0447fef279
commit
14d300162d
9 changed files with 44 additions and 11 deletions
|
@ -4,4 +4,4 @@ from .collection import get_outbox, get_outbox_page, get_add, get_remove, \
|
||||||
get_following, get_followers
|
get_following, get_followers
|
||||||
from .create import get_create
|
from .create import get_create
|
||||||
from .follow import get_follow_request, get_accept
|
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
|
||||||
|
|
|
@ -2,13 +2,25 @@
|
||||||
def get_review(review):
|
def get_review(review):
|
||||||
''' fedireads json for book reviews '''
|
''' fedireads json for book reviews '''
|
||||||
status = get_status(review)
|
status = get_status(review)
|
||||||
status['inReplyTo'] = review.book.absolute_id
|
status['inReplyToBook'] = review.book.absolute_id
|
||||||
status['fedireadsType'] = review.status_type,
|
status['fedireadsType'] = review.status_type,
|
||||||
status['name'] = review.name
|
status['name'] = review.name
|
||||||
status['rating'] = review.rating
|
status['rating'] = review.rating
|
||||||
return status
|
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):
|
def get_status(status):
|
||||||
''' create activitypub json for a status '''
|
''' create activitypub json for a status '''
|
||||||
user = status.user
|
user = status.user
|
||||||
|
|
|
@ -9,7 +9,7 @@ import requests
|
||||||
from urllib.parse import urlparse
|
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 '''
|
''' deduplicated list of recipient inboxes '''
|
||||||
recipients = direct_recipients or []
|
recipients = direct_recipients or []
|
||||||
if post_privacy == 'direct':
|
if post_privacy == 'direct':
|
||||||
|
@ -17,7 +17,12 @@ def get_recipients(user, post_privacy, direct_recipients=None):
|
||||||
return [u.inbox for u in recipients]
|
return [u.inbox for u in recipients]
|
||||||
|
|
||||||
# load all the followers of the user who is sending the message
|
# load all the followers of the user who is sending the message
|
||||||
|
if not limit:
|
||||||
followers = user.followers.all()
|
followers = user.followers.all()
|
||||||
|
else:
|
||||||
|
fedireads_user = limit == 'fedireads'
|
||||||
|
followers = user.followers.filter(fedireads_user=fedireads_user).all()
|
||||||
|
|
||||||
if post_privacy == 'public':
|
if post_privacy == 'public':
|
||||||
# post to public shared inboxes
|
# post to public shared inboxes
|
||||||
shared_inboxes = set(
|
shared_inboxes = set(
|
||||||
|
|
|
@ -205,11 +205,13 @@ def handle_incoming_create(activity):
|
||||||
if not 'object' in activity:
|
if not 'object' in activity:
|
||||||
return HttpResponseBadRequest()
|
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()
|
response = HttpResponse()
|
||||||
content = activity['object'].get('content')
|
content = activity['object'].get('content')
|
||||||
if activity['object'].get('fedireadsType') == 'Review' and \
|
if activity['object'].get('fedireadsType') == 'Review' and \
|
||||||
'inReplyTo' in activity['object']:
|
'inReplyToBook' in activity['object']:
|
||||||
book = activity['object']['inReplyTo']
|
book = activity['object']['inReplyToBook']
|
||||||
book = book.split('/')[-1]
|
book = book.split('/')[-1]
|
||||||
name = activity['object'].get('name')
|
name = activity['object'].get('name')
|
||||||
rating = activity['object'].get('rating')
|
rating = activity['object'].get('rating')
|
||||||
|
|
|
@ -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
|
from django.db import migrations, models
|
||||||
import django.utils.timezone
|
import django.utils.timezone
|
||||||
|
@ -72,4 +72,9 @@ class Migration(migrations.Migration):
|
||||||
name='content',
|
name='content',
|
||||||
field=models.TextField(blank=True, null=True),
|
field=models.TextField(blank=True, null=True),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='fedireads_user',
|
||||||
|
field=models.BooleanField(default=True),
|
||||||
|
),
|
||||||
]
|
]
|
|
@ -35,5 +35,6 @@ class Review(Status):
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
self.status_type = 'Review'
|
self.status_type = 'Review'
|
||||||
|
self.activity_type = 'Article'
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ class User(AbstractUser):
|
||||||
outbox = models.CharField(max_length=255, unique=True)
|
outbox = models.CharField(max_length=255, unique=True)
|
||||||
summary = models.TextField(blank=True, null=True)
|
summary = models.TextField(blank=True, null=True)
|
||||||
local = models.BooleanField(default=True)
|
local = models.BooleanField(default=True)
|
||||||
|
fedireads_user = models.BooleanField(default=True)
|
||||||
localname = models.CharField(
|
localname = models.CharField(
|
||||||
max_length=255,
|
max_length=255,
|
||||||
null=True,
|
null=True,
|
||||||
|
|
|
@ -130,10 +130,16 @@ def handle_review(user, book, name, content, rating):
|
||||||
review = create_review(user, book, name, content, rating)
|
review = create_review(user, book, name, content, rating)
|
||||||
|
|
||||||
review_activity = activitypub.get_review(review)
|
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')
|
# re-format the activity for non-fedireads servers
|
||||||
broadcast(user, create_activity, recipients)
|
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):
|
def handle_comment(user, review, content):
|
||||||
|
|
|
@ -41,7 +41,8 @@ def get_or_create_remote_user(actor):
|
||||||
shared_inbox=shared_inbox,
|
shared_inbox=shared_inbox,
|
||||||
# TODO: I'm never actually using this for remote users
|
# TODO: I'm never actually using this for remote users
|
||||||
public_key=data.get('publicKey').get('publicKeyPem'),
|
public_key=data.get('publicKey').get('publicKeyPem'),
|
||||||
local=False
|
local=False,
|
||||||
|
fedireads_user=False,
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in a new issue