Skip to content

Commit

Permalink
ATProto.send_chat: handle 400 from getConvoForMembers, sendMessage
Browse files Browse the repository at this point in the history
for #1024, #966, etc
  • Loading branch information
snarfed committed Aug 7, 2024
1 parent c8d6ba5 commit 848313d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
19 changes: 13 additions & 6 deletions atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,17 @@ def send_chat(self, msg, from_user):
'User-Agent': USER_AGENT,
'Authorization': f'Bearer {token}',
})
convo = client.chat.bsky.convo.getConvoForMembers(members=[to_did])
output = client.chat.bsky.convo.sendMessage({
'convoId': convo['convo']['id'],
'message': msg,
})
logger.info(f'Sent chat message from {from_user.handle} to {self.handle} {to_did}: {json_dumps(output)}')

try:
convo = client.chat.bsky.convo.getConvoForMembers(members=[to_did])
sent = client.chat.bsky.convo.sendMessage({
'convoId': convo['convo']['id'],
'message': msg,
})
except RequestException as e:
# getConvoForMembers returns eg 400 if the recipient has disabled chat
util.interpret_http_exception(e)
return False

logger.info(f'Sent chat message from {from_user.handle} to {self.handle} {to_did}: {json_dumps(sent)}')
return True
18 changes: 18 additions & 0 deletions tests/test_atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,24 @@ def test_send_chat(self, mock_get, mock_post):
'Authorization': ANY,
})

# getConvoForMembers
@patch('requests.get', return_value=requests_response({
'error': 'InvalidRequest',
'message': 'recipient has disabled incoming messages',
}, status=400))
def test_send_chat_recipient_disabled(self, mock_get):
user = self.make_user_and_repo()
alice = ATProto(id='did:plc:alice')

self.assertFalse(alice.send_chat({
'$type': 'chat.bsky.convo.defs#messageInput',
'text': 'hello world',
}, from_user=user))

mock_get.assert_any_call(
'https://chat.service.local/xrpc/chat.bsky.convo.getConvoForMembers?members=did%3Aplc%3Aalice',
json=None, data=None, headers=ANY)

def test_datastore_client_get_record_datastore_object(self):
self.make_user_and_repo()
post = {
Expand Down

0 comments on commit 848313d

Please sign in to comment.