2020-02-11 23:17:21 +00:00
|
|
|
''' send out activitypub messages '''
|
2020-04-22 13:53:22 +00:00
|
|
|
import json
|
2020-02-15 03:55:33 +00:00
|
|
|
from django.utils.http import http_date
|
2020-01-28 19:45:27 +00:00
|
|
|
import requests
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.activitypub import ActivityEncoder
|
|
|
|
from bookwyrm.tasks import app
|
|
|
|
from bookwyrm.signatures import make_signature, make_digest
|
2020-03-29 02:12:17 +00:00
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-04-21 00:43:42 +00:00
|
|
|
def get_public_recipients(user, software=None):
|
|
|
|
''' everybody and their public inboxes '''
|
|
|
|
followers = user.followers.filter(local=False)
|
|
|
|
if software:
|
2020-09-21 15:10:37 +00:00
|
|
|
followers = followers.filter(bookwyrm_user=(software == 'bookwyrm'))
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-05-09 21:26:27 +00:00
|
|
|
# we want shared inboxes when available
|
2020-04-21 00:43:42 +00:00
|
|
|
shared = followers.filter(
|
|
|
|
shared_inbox__isnull=False
|
|
|
|
).values_list('shared_inbox', flat=True).distinct()
|
2020-03-29 02:12:17 +00:00
|
|
|
|
2020-05-09 21:26:27 +00:00
|
|
|
# if a user doesn't have a shared inbox, we need their personal inbox
|
|
|
|
# iirc pixelfed doesn't have shared inboxes
|
2020-04-21 00:43:42 +00:00
|
|
|
inboxes = followers.filter(
|
|
|
|
shared_inbox__isnull=True
|
|
|
|
).values_list('inbox', flat=True)
|
2020-01-28 19:45:27 +00:00
|
|
|
|
2020-04-21 00:43:42 +00:00
|
|
|
return list(shared) + list(inboxes)
|
2020-02-19 01:30:23 +00:00
|
|
|
|
2020-04-01 00:07:35 +00:00
|
|
|
|
2020-04-21 00:43:42 +00:00
|
|
|
def broadcast(sender, activity, software=None, \
|
|
|
|
privacy='public', direct_recipients=None):
|
2020-01-28 19:45:27 +00:00
|
|
|
''' send out an event '''
|
2020-05-09 21:26:27 +00:00
|
|
|
# start with parsing the direct recipients
|
2020-04-21 00:43:42 +00:00
|
|
|
recipients = [u.inbox for u in direct_recipients or []]
|
2020-05-09 21:26:27 +00:00
|
|
|
# and then add any other recipients
|
2020-04-21 00:43:42 +00:00
|
|
|
if privacy == 'public':
|
2020-05-04 21:15:16 +00:00
|
|
|
recipients += get_public_recipients(sender, software=software)
|
2020-09-17 20:02:52 +00:00
|
|
|
broadcast_task.delay(
|
|
|
|
sender.id,
|
|
|
|
json.dumps(activity, cls=ActivityEncoder),
|
|
|
|
recipients
|
|
|
|
)
|
2020-04-01 17:16:20 +00: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 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:
|
|
|
|
errors.append({
|
2020-09-30 04:08:04 +00:00
|
|
|
'error': str(e),
|
2020-01-29 23:55:48 +00:00
|
|
|
'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-11-02 23:19:58 +00:00
|
|
|
def sign_and_send(sender, data, destination):
|
2020-05-13 10:08:52 +00:00
|
|
|
''' crpyto whatever and http junk '''
|
|
|
|
now = http_date()
|
|
|
|
|
2020-12-01 03:33:50 +00:00
|
|
|
if not sender.key_pair.private_key:
|
2020-05-13 10:08:52 +00:00
|
|
|
# this shouldn't happen. it would be bad if it happened.
|
|
|
|
raise ValueError('No private key found for sender')
|
2020-01-29 23:55:48 +00:00
|
|
|
|
2020-05-19 20:33:47 +00:00
|
|
|
digest = make_digest(data)
|
|
|
|
|
2020-01-28 19:45:27 +00:00
|
|
|
response = requests.post(
|
|
|
|
destination,
|
2020-05-19 20:33:47 +00:00
|
|
|
data=data,
|
2020-01-28 19:45:27 +00:00
|
|
|
headers={
|
|
|
|
'Date': now,
|
2020-05-19 20:33:47 +00:00
|
|
|
'Digest': digest,
|
|
|
|
'Signature': make_signature(sender, destination, now, digest),
|
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()
|
2020-02-15 19:13:49 +00:00
|
|
|
return response
|