@@ -922,24 +922,27 @@ impl ChatId {
922922 && old_draft. chat_id == self
923923 && old_draft. state == MessageState :: OutDraft
924924 {
925- context
926- . sql
927- . execute (
928- "UPDATE msgs
929- SET timestamp=?,type=?,txt=?,txt_normalized=?,param=?,mime_in_reply_to=?
930- WHERE id=?;" ,
931- (
932- time ( ) ,
933- msg. viewtype ,
934- & msg. text ,
935- message:: normalize_text ( & msg. text ) ,
936- msg. param . to_string ( ) ,
937- msg. in_reply_to . as_deref ( ) . unwrap_or_default ( ) ,
938- msg. id ,
939- ) ,
940- )
941- . await ?;
942- return Ok ( true ) ;
925+ let affected_rows = context
926+ . sql . execute (
927+ "UPDATE msgs
928+ SET timestamp=?1,type=?2,txt=?3,txt_normalized=?4,param=?5,mime_in_reply_to=?6
929+ WHERE id=?7
930+ AND (type <> ?2
931+ OR txt <> ?3
932+ OR txt_normalized <> ?4
933+ OR param <> ?5
934+ OR mime_in_reply_to <> ?6);" ,
935+ (
936+ time ( ) ,
937+ msg. viewtype ,
938+ & msg. text ,
939+ message:: normalize_text ( & msg. text ) ,
940+ msg. param . to_string ( ) ,
941+ msg. in_reply_to . as_deref ( ) . unwrap_or_default ( ) ,
942+ msg. id ,
943+ ) ,
944+ ) . await ?;
945+ return Ok ( affected_rows > 0 ) ;
943946 }
944947 }
945948 }
@@ -7696,4 +7699,19 @@ mod tests {
76967699
76977700 Ok ( ( ) )
76987701 }
7702+
7703+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
7704+ async fn test_do_not_overwrite_draft ( ) -> Result < ( ) > {
7705+ let mut tcm = TestContextManager :: new ( ) ;
7706+ let alice = tcm. alice ( ) . await ;
7707+ let mut msg = Message :: new_text ( "This is a draft message" . to_string ( ) ) ;
7708+ let self_chat = alice. get_self_chat ( ) . await . id ;
7709+ self_chat. set_draft ( & alice, Some ( & mut msg) ) . await . unwrap ( ) ;
7710+ let draft1 = self_chat. get_draft ( & alice) . await ?. unwrap ( ) ;
7711+ SystemTime :: shift ( Duration :: from_secs ( 1 ) ) ;
7712+ self_chat. set_draft ( & alice, Some ( & mut msg) ) . await . unwrap ( ) ;
7713+ let draft2 = self_chat. get_draft ( & alice) . await ?. unwrap ( ) ;
7714+ assert_eq ! ( draft1. timestamp_sort, draft2. timestamp_sort) ;
7715+ Ok ( ( ) )
7716+ }
76997717}
0 commit comments