From 2626bda8a493ffa22018d310366cd0b5f4e33f25 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 20 Aug 2024 09:48:11 -0700 Subject: [PATCH] ATProto.send_chat: narrow getConvoForMembers error handling to just "recipient disabled chat" raise others, and all from sendMessage --- atproto.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/atproto.py b/atproto.py index 09ebc3a..e6e177e 100644 --- a/atproto.py +++ b/atproto.py @@ -858,8 +858,8 @@ class ATProto(User, Protocol): to_did (str) Returns: - bool: True if the report was sent successfully, False if the flag's - actor is not bridged into ATProto + bool: True if the message was sent successfully, False otherwise, eg + if the recipient has disabled chat """ assert msg['$type'] == 'chat.bsky.convo.defs#messageInput' @@ -876,19 +876,23 @@ class ATProto(User, Protocol): 'Authorization': f'Bearer {token}', }) + cli = client(lxm='chat.bsky.convo.getConvoForMembers') try: - cli = client(lxm='chat.bsky.convo.getConvoForMembers') convo = cli.chat.bsky.convo.getConvoForMembers(members=[to_did]) - - cli = client(lxm='chat.bsky.convo.sendMessage') - sent = cli.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 + if e.response is not None and e.response.status_code == 400: + body = e.response.json() + if (body.get('error') == 'InvalidRequest' + and body.get('message') == 'recipient has disabled incoming messages'): + return False + raise + + cli = client(lxm='chat.bsky.convo.sendMessage') + sent = cli.chat.bsky.convo.sendMessage({ + 'convoId': convo['convo']['id'], + 'message': msg, + }) logger.info(f'Sent chat message from {from_repo.handle} to {to_did}: {json_dumps(sent)}') return True