mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-29 13:01:08 +00:00
Better error handling in broadcaster
This commit is contained in:
parent
fa0f37fb0f
commit
9f0bcd8152
3 changed files with 22 additions and 6 deletions
|
@ -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),
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<div>
|
||||
<h2>{{ result.username }}</h2>
|
||||
<form action="/follow/" method="post">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="user" value="{{ result.id }}"></input>
|
||||
<input type="submit" value="Follow"></input>
|
||||
</form>
|
||||
|
|
Loading…
Reference in a new issue