From a85a1ea3d18b2d9dad97459f7652fcfc854b5783 Mon Sep 17 00:00:00 2001 From: Sergey Mankovsky Date: Fri, 6 Sep 2024 18:10:38 +0200 Subject: [PATCH] Send message if the original is missing --- server/reflector/views/transcripts.py | 12 ++++++++++-- server/reflector/zulip.py | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/server/reflector/views/transcripts.py b/server/reflector/views/transcripts.py index fe67970a..c9551eef 100644 --- a/server/reflector/views/transcripts.py +++ b/server/reflector/views/transcripts.py @@ -17,6 +17,7 @@ from reflector.processors.types import Transcript as ProcessorTranscript from reflector.processors.types import Word from reflector.settings import settings from reflector.zulip import ( + InvalidMessageError, get_zulip_message, send_message_to_zulip, update_zulip_message, @@ -346,9 +347,16 @@ async def transcript_post_to_zulip( raise HTTPException(status_code=404, detail="Transcript not found") content = get_zulip_message(transcript, include_topics) + + message_updated = False if transcript.zulip_message_id: - update_zulip_message(transcript.zulip_message_id, stream, topic, content) - else: + try: + update_zulip_message(transcript.zulip_message_id, stream, topic, content) + message_updated = True + except InvalidMessageError: + pass + + if not message_updated: response = send_message_to_zulip(stream, topic, content) await transcripts_controller.update( transcript, {"zulip_message_id": response["id"]} diff --git a/server/reflector/zulip.py b/server/reflector/zulip.py index df417558..ee35fe13 100644 --- a/server/reflector/zulip.py +++ b/server/reflector/zulip.py @@ -6,6 +6,10 @@ from reflector.db.transcripts import Transcript from reflector.settings import settings +class InvalidMessageError(Exception): + pass + + def send_message_to_zulip(stream: str, topic: str, content: str): try: response = requests.post( @@ -32,8 +36,6 @@ def update_zulip_message(message_id: int, stream: str, topic: str, content: str) response = requests.patch( f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}", data={ - "type": "stream", - "to": stream, "topic": topic, "content": content, }, @@ -41,6 +43,12 @@ def update_zulip_message(message_id: int, stream: str, topic: str, content: str) headers={"Content-Type": "application/x-www-form-urlencoded"}, ) + if ( + response.status_code == 400 + and response.json()["msg"] == "Invalid message(s)" + ): + raise InvalidMessageError(f"There is no message with id: {message_id}") + response.raise_for_status() return response.json()