bookwyrm-mastodon/bookwyrm/broadcast.py

91 lines
2.8 KiB
Python
Raw Normal View History

2020-02-11 18:17:21 -05:00
''' send out activitypub messages '''
2020-04-22 09:53:22 -04:00
import json
from django.utils.http import http_date
2020-01-28 14:45:27 -05:00
import requests
from bookwyrm import models
from bookwyrm.activitypub import ActivityEncoder
from bookwyrm.tasks import app
from bookwyrm.signatures import make_signature, make_digest
2020-03-28 22:12:17 -04:00
2020-01-28 14:45:27 -05:00
2020-04-20 20:43:42 -04:00
def get_public_recipients(user, software=None):
''' everybody and their public inboxes '''
followers = user.followers.filter(local=False)
if software:
# TODO: eventually we may want to handle particular software differently
followers = followers.filter(bookwyrm_user=(software == 'bookwyrm'))
2020-03-29 03:05:09 -04:00
2020-05-09 17:26:27 -04:00
# we want shared inboxes when available
2020-04-20 20:43:42 -04:00
shared = followers.filter(
shared_inbox__isnull=False
).values_list('shared_inbox', flat=True).distinct()
2020-03-28 22:12:17 -04:00
2020-05-09 17:26:27 -04:00
# if a user doesn't have a shared inbox, we need their personal inbox
# iirc pixelfed doesn't have shared inboxes
2020-04-20 20:43:42 -04:00
inboxes = followers.filter(
shared_inbox__isnull=True
).values_list('inbox', flat=True)
2020-01-28 14:45:27 -05:00
2020-04-20 20:43:42 -04:00
return list(shared) + list(inboxes)
2020-03-31 20:07:35 -04:00
2020-04-20 20:43:42 -04:00
def broadcast(sender, activity, software=None, \
privacy='public', direct_recipients=None):
2020-01-28 14:45:27 -05:00
''' send out an event '''
2020-05-09 17:26:27 -04:00
# start with parsing the direct recipients
2020-04-20 20:43:42 -04:00
recipients = [u.inbox for u in direct_recipients or []]
2020-05-09 17:26:27 -04:00
# and then add any other recipients
2020-04-20 20:43:42 -04:00
# TODO: other kinds of privacy
if privacy == 'public':
2020-05-04 17:15:16 -04:00
recipients += get_public_recipients(sender, software=software)
broadcast_task.delay(
sender.id,
json.dumps(activity, cls=ActivityEncoder),
recipients
)
2020-04-01 13:16:20 -04:00
@app.task
def broadcast_task(sender_id, activity, recipients):
''' the celery task for broadcast '''
sender = models.User.objects.get(id=sender_id)
2020-01-29 18:55:48 -05:00
errors = []
2020-01-28 14:45:27 -05:00
for recipient in recipients:
2020-01-29 18:55:48 -05:00
try:
2020-02-15 16:01:42 -05:00
sign_and_send(sender, activity, recipient)
2020-01-29 18:55:48 -05:00
except requests.exceptions.HTTPError as e:
2020-02-07 18:11:53 -05:00
# TODO: maybe keep track of users who cause errors
2020-01-29 18:55:48 -05:00
errors.append({
2020-09-30 00:08:04 -04:00
'error': str(e),
2020-01-29 18:55:48 -05:00
'recipient': recipient,
2020-02-07 18:11:53 -05:00
'activity': activity,
2020-01-29 18:55:48 -05:00
})
return errors
2020-01-28 14:45:27 -05:00
def sign_and_send(sender, activity, destination):
''' crpyto whatever and http junk '''
now = http_date()
if not sender.private_key:
# this shouldn't happen. it would be bad if it happened.
raise ValueError('No private key found for sender')
2020-01-29 18:55:48 -05:00
data = json.dumps(activity).encode('utf-8')
digest = make_digest(data)
2020-01-28 14:45:27 -05:00
response = requests.post(
destination,
data=data,
2020-01-28 14:45:27 -05:00
headers={
'Date': now,
'Digest': digest,
'Signature': make_signature(sender, destination, now, digest),
2020-02-07 16:39:48 -05:00
'Content-Type': 'application/activity+json; charset=utf-8',
2020-01-28 14:45:27 -05:00
},
)
if not response.ok:
response.raise_for_status()
return response