From 9f0bcd8152bd2bfcd35ba77bde36f9fbf26f33cd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 29 Jan 2020 15:55:48 -0800 Subject: [PATCH] Better error handling in broadcaster --- fedireads/api.py | 23 ++++++++++++++++++----- fedireads/outgoing.py | 4 +++- fedireads/templates/user_results.html | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/fedireads/api.py b/fedireads/api.py index 76a56b979..dc012f774 100644 --- a/fedireads/api.py +++ b/fedireads/api.py @@ -61,9 +61,17 @@ def get_recipients(user, post_privacy, direct_recipients=None): def broadcast(sender, action, recipients): ''' send out an event ''' + errors = [] for recipient in recipients: - # TODO: error handling - sign_and_send(sender, action, recipient) + try: + 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): @@ -76,9 +84,14 @@ date: %s''' % (inbox_fragment, DOMAIN, now) signer = pkcs1_15.new(RSA.import_key(sender.private_key)) signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8'))) - signature = 'keyId="%s",' % sender.localname - signature += 'headers="(request-target) host date",' - signature += 'signature="%s"' % b64encode(signed_message) + signature = { + 'keyId': '%s#main-key' % sender.actor, + '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( destination, data=json.dumps(action), diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index f645b7f00..d13bfd7fc 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -62,7 +62,9 @@ def handle_outgoing_follow(user, to_follow): '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): diff --git a/fedireads/templates/user_results.html b/fedireads/templates/user_results.html index 5f2d41354..97d0d7ff8 100644 --- a/fedireads/templates/user_results.html +++ b/fedireads/templates/user_results.html @@ -5,6 +5,7 @@

{{ result.username }}

+ {% csrf_token %}