Better error handling in broadcaster

This commit is contained in:
Mouse Reeve 2020-01-29 15:55:48 -08:00
parent fa0f37fb0f
commit 9f0bcd8152
3 changed files with 22 additions and 6 deletions

View file

@ -61,9 +61,17 @@ def get_recipients(user, post_privacy, direct_recipients=None):
def broadcast(sender, action, recipients): def broadcast(sender, action, recipients):
''' send out an event ''' ''' send out an event '''
errors = []
for recipient in recipients: for recipient in recipients:
# TODO: error handling try:
sign_and_send(sender, action, recipient) sign_and_send(sender, action, recipient)
except requests.exceptions.HTTPError as e:
errors.append({
'error': e,
'recipient': recipient,
'activity': action,
})
return errors
def sign_and_send(sender, action, destination): def sign_and_send(sender, action, destination):
@ -76,9 +84,14 @@ date: %s''' % (inbox_fragment, DOMAIN, now)
signer = pkcs1_15.new(RSA.import_key(sender.private_key)) signer = pkcs1_15.new(RSA.import_key(sender.private_key))
signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8'))) signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8')))
signature = 'keyId="%s",' % sender.localname signature = {
signature += 'headers="(request-target) host date",' 'keyId': '%s#main-key' % sender.actor,
signature += 'signature="%s"' % b64encode(signed_message) 'algorithm': 'rsa-sha256',
'headers': '(request-target) host date',
'signature': b64encode(signed_message),
}
signature = ','.join('%s="%s"' % (k, v) for (k, v) in signature.items())
response = requests.post( response = requests.post(
destination, destination,
data=json.dumps(action), data=json.dumps(action),

View file

@ -62,7 +62,9 @@ def handle_outgoing_follow(user, to_follow):
'object': to_follow.actor, 'object': to_follow.actor,
} }
broadcast(user, activity, [to_follow.inbox]) errors = broadcast(user, activity, [to_follow.inbox])
for error in errors:
raise(error['error'])
def handle_shelve(user, book, shelf): def handle_shelve(user, book, shelf):

View file

@ -5,6 +5,7 @@
<div> <div>
<h2>{{ result.username }}</h2> <h2>{{ result.username }}</h2>
<form action="/follow/" method="post"> <form action="/follow/" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ result.id }}"></input> <input type="hidden" name="user" value="{{ result.id }}"></input>
<input type="submit" value="Follow"></input> <input type="submit" value="Follow"></input>
</form> </form>