From 2d7cafcd7a0a722391b622b12d81dfbc68ddf458 Mon Sep 17 00:00:00 2001 From: megrogan Date: Fri, 13 Dec 2024 17:19:13 +0000 Subject: [PATCH] Simplification --- .../chat_events/src/chat_event_internal.rs | 2 +- backend/libraries/group_chat_core/src/lib.rs | 73 ++++++++++--------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/backend/libraries/chat_events/src/chat_event_internal.rs b/backend/libraries/chat_events/src/chat_event_internal.rs index d553d4df87..41c4dff3af 100644 --- a/backend/libraries/chat_events/src/chat_event_internal.rs +++ b/backend/libraries/chat_events/src/chat_event_internal.rs @@ -572,7 +572,7 @@ mod tests { let event_bytes = msgpack::serialize_then_unwrap(&event); let event_bytes_len = event_bytes.len(); - assert_eq!(message_bytes_len, 165); + assert_eq!(message_bytes_len, 230); assert_eq!(event_bytes_len, message_bytes_len + 18); let _deserialized: EventWrapperInternal = msgpack::deserialize_then_unwrap(&event_bytes); diff --git a/backend/libraries/group_chat_core/src/lib.rs b/backend/libraries/group_chat_core/src/lib.rs index 16b318e554..0c64a79d1e 100644 --- a/backend/libraries/group_chat_core/src/lib.rs +++ b/backend/libraries/group_chat_core/src/lib.rs @@ -642,48 +642,49 @@ impl GroupChatCore { // If there is an existing message with the same message id then this is invalid unless // a bot is finalising its unfinalised message - if let Some(reader) = self.events.events_reader(EventIndex::default(), thread_root_message_index) { - if let Some(message) = reader.message_internal(message_id.into()) { - if let Caller::BotV2(bot_now) = &caller { - if let Some(bot_message) = message.bot_context { - if bot_now.bot == message.sender - && bot_now.initiator == bot_message.initiator - && bot_now.command_text == bot_message.command_text - && bot_now.finalised - && !bot_message.finalised - { - let edit_message_args = EditMessageArgs { - sender: caller.agent(), - min_visible_event_index: EventIndex::default(), - thread_root_message_index, - message_id, - content, - block_level_markdown: Some(block_level_markdown), - finalise_bot_message: true, - now, - }; + if let Some((message, _)) = + self.events + .message_internal(EventIndex::default(), thread_root_message_index, message_id.into()) + { + if let Caller::BotV2(bot_now) = &caller { + if let Some(bot_message) = message.bot_context { + if bot_now.bot == message.sender + && bot_now.initiator == bot_message.initiator + && bot_now.command_text == bot_message.command_text + && bot_now.finalised + && !bot_message.finalised + { + let edit_message_args = EditMessageArgs { + sender: caller.agent(), + min_visible_event_index: EventIndex::default(), + thread_root_message_index, + message_id, + content, + block_level_markdown: Some(block_level_markdown), + finalise_bot_message: true, + now, + }; - match self.events.edit_message::(edit_message_args, None) { - chat_events::EditMessageResult::Success => { - let reader = self - .events - .events_reader(EventIndex::default(), thread_root_message_index) - .unwrap(); - let message_event = reader.message_event(message_id.into(), Some(caller.agent())).unwrap(); - return SendMessageResult::Success(SendMessageSuccess { - message_event, - users_to_notify: vec![], - bot_message_finalised: true, - }); - } - _ => unreachable!(), + match self.events.edit_message::(edit_message_args, None) { + chat_events::EditMessageResult::Success => { + let reader = self + .events + .events_reader(EventIndex::default(), thread_root_message_index) + .unwrap(); + let message_event = reader.message_event(message_id.into(), Some(caller.agent())).unwrap(); + return SendMessageResult::Success(SendMessageSuccess { + message_event, + users_to_notify: vec![], + bot_message_finalised: true, + }); } + _ => unreachable!(), } } } - - return SendMessageResult::MessageAlreadyExists; } + + return SendMessageResult::MessageAlreadyExists; } self.send_message(