bookwyrm-mastodon/fedireads/broadcast.py

96 lines
3.0 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 urllib.parse import urlparse
2020-01-28 14:45:27 -05:00
from base64 import b64encode
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from django.utils.http import http_date
2020-01-28 14:45:27 -05:00
import requests
2020-03-28 22:12:17 -04:00
from fedireads import models
2020-03-31 21:03:58 -04:00
from fedireads.tasks import app
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(fedireads_user=(software == 'fedireads'))
2020-03-29 03:05:09 -04:00
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-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-04-20 20:43:42 -04:00
recipients = [u.inbox for u in direct_recipients or []]
# TODO: other kinds of privacy
if privacy == 'public':
recipients = get_public_recipients(sender, software=software)
2020-04-01 13:16:20 -04:00
broadcast_task.delay(sender.id, activity, recipients)
@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({
'error': e,
'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
2020-02-07 18:11:53 -05:00
def sign_and_send(sender, activity, destination):
2020-01-28 14:45:27 -05:00
''' crpyto whatever and http junk '''
inbox_parts = urlparse(destination)
now = http_date()
2020-01-29 23:56:18 -05:00
signature_headers = [
'(request-target): post %s' % inbox_parts.path,
'host: %s' % inbox_parts.netloc,
'date: %s' % now
2020-01-29 23:56:18 -05:00
]
message_to_sign = '\n'.join(signature_headers)
2020-02-07 18:11:53 -05:00
# TODO: raise an error if the user doesn't have a private key
2020-01-28 14:45:27 -05:00
signer = pkcs1_15.new(RSA.import_key(sender.private_key))
signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8')))
2020-01-29 18:55:48 -05:00
signature = {
'keyId': '%s#main-key' % sender.actor,
'algorithm': 'rsa-sha256',
'headers': '(request-target) host date',
2020-01-29 23:56:18 -05:00
'signature': b64encode(signed_message).decode('utf8'),
2020-01-29 18:55:48 -05:00
}
signature = ','.join('%s="%s"' % (k, v) for (k, v) in signature.items())
2020-01-28 14:45:27 -05:00
response = requests.post(
destination,
2020-02-07 18:11:53 -05:00
data=json.dumps(activity),
2020-01-28 14:45:27 -05:00
headers={
'Date': now,
'Signature': signature,
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