moviewyrm/fedireads/broadcast.py

117 lines
3.8 KiB
Python
Raw Normal View History

2020-02-11 23:17:21 +00:00
''' send out activitypub messages '''
2020-01-28 19:45:27 +00: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 19:45:27 +00:00
import json
import requests
from urllib.parse import urlparse
2020-01-28 19:45:27 +00:00
2020-03-29 02:12:17 +00:00
from fedireads import models
2020-04-01 01:03:58 +00:00
from fedireads.tasks import app
2020-03-29 02:12:17 +00:00
2020-01-28 19:45:27 +00:00
def get_recipients(user, post_privacy, direct_recipients=None, limit=False):
2020-02-07 23:11:53 +00:00
''' deduplicated list of recipient inboxes '''
2020-03-29 07:05:09 +00:00
# we're always going to broadcast to any direct recipients
direct_recipients = direct_recipients or []
recipients = [u.inbox for u in direct_recipients]
# if we're federating a book, it isn't related to any user's followers, we
# just want to send it out. To whom? I'm not sure, but for now, everyone.
2020-03-29 02:12:17 +00:00
if not user:
users = models.User.objects.filter(local=False).all()
recipients += list(set(
u.shared_inbox if u.shared_inbox else u.inbox for u in users
))
return recipients
2020-02-07 23:11:53 +00:00
if post_privacy == 'direct':
2020-03-29 07:05:09 +00:00
# all we care about is direct_recipients, not followers, so we're done
return recipients
2020-01-28 19:45:27 +00:00
2020-02-07 23:11:53 +00:00
# load all the followers of the user who is sending the message
2020-03-29 07:05:09 +00:00
# "limit" refers to whether we want to send to other fedireads instances,
# or to only non-fedireads instances. this is confusing (TODO)
if not limit:
followers = user.followers.all()
else:
fedireads_user = limit == 'fedireads'
followers = user.followers.filter(fedireads_user=fedireads_user).all()
2020-04-01 00:07:35 +00:00
# we don't need to broadcast to ourself
followers = followers.filter(local=False)
2020-03-29 07:05:09 +00:00
# TODO I don't think this is actually accomplishing pubic/followers only?
2020-01-28 19:45:27 +00:00
if post_privacy == 'public':
# post to public shared inboxes
shared_inboxes = set(
u.shared_inbox for u in followers if u.shared_inbox
)
2020-01-28 19:45:27 +00:00
recipients += list(shared_inboxes)
recipients += [u.inbox for u in followers if not u.shared_inbox]
2020-03-29 07:05:09 +00:00
2020-01-28 19:45:27 +00:00
if post_privacy == 'followers':
# don't send it to the shared inboxes
inboxes = set(u.inbox for u in followers)
recipients += list(inboxes)
2020-03-29 07:05:09 +00:00
2020-01-28 19:45:27 +00:00
return recipients
2020-04-01 01:03:58 +00:00
@app.task
2020-02-07 23:11:53 +00:00
def broadcast(sender, activity, recipients):
2020-01-28 19:45:27 +00:00
''' send out an event '''
2020-04-01 01:03:58 +00:00
sender = models.User.objects.get(id=sender)
2020-01-29 23:55:48 +00:00
errors = []
2020-01-28 19:45:27 +00:00
for recipient in recipients:
2020-01-29 23:55:48 +00:00
try:
2020-02-15 21:01:42 +00:00
sign_and_send(sender, activity, recipient)
2020-01-29 23:55:48 +00:00
except requests.exceptions.HTTPError as e:
2020-02-07 23:11:53 +00:00
# TODO: maybe keep track of users who cause errors
2020-01-29 23:55:48 +00:00
errors.append({
'error': e,
'recipient': recipient,
2020-02-07 23:11:53 +00:00
'activity': activity,
2020-01-29 23:55:48 +00:00
})
return errors
2020-01-28 19:45:27 +00:00
2020-02-07 23:11:53 +00:00
def sign_and_send(sender, activity, destination):
2020-01-28 19:45:27 +00:00
''' crpyto whatever and http junk '''
inbox_parts = urlparse(destination)
now = http_date()
2020-01-30 04:56:18 +00:00
signature_headers = [
'(request-target): post %s' % inbox_parts.path,
'host: %s' % inbox_parts.netloc,
'date: %s' % now
2020-01-30 04:56:18 +00:00
]
message_to_sign = '\n'.join(signature_headers)
2020-02-07 23:11:53 +00:00
# TODO: raise an error if the user doesn't have a private key
2020-01-28 19:45:27 +00: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 23:55:48 +00:00
signature = {
'keyId': '%s#main-key' % sender.actor,
'algorithm': 'rsa-sha256',
'headers': '(request-target) host date',
2020-01-30 04:56:18 +00:00
'signature': b64encode(signed_message).decode('utf8'),
2020-01-29 23:55:48 +00:00
}
signature = ','.join('%s="%s"' % (k, v) for (k, v) in signature.items())
2020-01-28 19:45:27 +00:00
response = requests.post(
destination,
2020-02-07 23:11:53 +00:00
data=json.dumps(activity),
2020-01-28 19:45:27 +00:00
headers={
'Date': now,
'Signature': signature,
2020-02-07 21:39:48 +00:00
'Content-Type': 'application/activity+json; charset=utf-8',
2020-01-28 19:45:27 +00:00
},
)
if not response.ok:
response.raise_for_status()
return response
2020-01-28 19:45:27 +00:00