@@ -5,14 +5,16 @@ use anyhow::{Context as _, Result};
55use super :: HandshakeMessage ;
66use super :: qrinvite:: QrInvite ;
77use crate :: chat:: { self , ChatId , is_contact_in_chat} ;
8+ use crate :: chatlist_events;
89use crate :: constants:: { Blocked , Chattype } ;
910use crate :: contact:: Origin ;
1011use crate :: context:: Context ;
1112use crate :: events:: EventType ;
1213use crate :: key:: self_fingerprint;
13- use crate :: message:: { Message , Viewtype } ;
14+ use crate :: log:: LogExt ;
15+ use crate :: message:: { Message , MsgId , Viewtype } ;
1416use crate :: mimeparser:: { MimeMessage , SystemMessage } ;
15- use crate :: param:: Param ;
17+ use crate :: param:: { Param , Params } ;
1618use crate :: securejoin:: { ContactId , encrypted_and_signed, verify_sender_by_fingerprint} ;
1719use crate :: stock_str;
1820use crate :: sync:: Sync :: * ;
@@ -48,16 +50,16 @@ pub(super) async fn start_protocol(context: &Context, invite: QrInvite) -> Resul
4850 ContactId :: scaleup_origin ( context, & [ invite. contact_id ( ) ] , Origin :: SecurejoinJoined ) . await ?;
4951 context. emit_event ( EventType :: ContactsChanged ( None ) ) ;
5052
53+ let has_key = context
54+ . sql
55+ . exists (
56+ "SELECT COUNT(*) FROM public_keys WHERE fingerprint=?" ,
57+ ( invite. fingerprint ( ) . hex ( ) , ) ,
58+ )
59+ . await ?;
60+
5161 // Now start the protocol and initialise the state.
5262 {
53- let has_key = context
54- . sql
55- . exists (
56- "SELECT COUNT(*) FROM public_keys WHERE fingerprint=?" ,
57- ( invite. fingerprint ( ) . hex ( ) , ) ,
58- )
59- . await ?;
60-
6163 // `joining_chat_id` is `Some` if group chat
6264 // already exists and we are in the chat.
6365 let joining_chat_id = match invite {
@@ -142,20 +144,22 @@ pub(super) async fn start_protocol(context: &Context, invite: QrInvite) -> Resul
142144 Ok ( joining_chat_id)
143145 }
144146 QrInvite :: Contact { .. } => {
145- // For setup-contact the BobState already ensured the 1:1 chat exists because it
146- // uses it to send the handshake messages.
147- chat:: add_info_msg_with_cmd (
148- context,
149- private_chat_id,
150- & stock_str:: securejoin_wait ( context) . await ,
151- SystemMessage :: SecurejoinWait ,
152- None ,
153- time ( ) ,
154- None ,
155- None ,
156- None ,
157- )
158- . await ?;
147+ // For setup-contact the BobState already ensured the 1:1 chat exists because it is
148+ // used to send the handshake messages.
149+ if !has_key {
150+ chat:: add_info_msg_with_cmd (
151+ context,
152+ private_chat_id,
153+ & stock_str:: securejoin_wait ( context) . await ,
154+ SystemMessage :: SecurejoinWait ,
155+ None ,
156+ time ( ) ,
157+ None ,
158+ None ,
159+ None ,
160+ )
161+ . await ?;
162+ }
159163 Ok ( private_chat_id)
160164 }
161165 }
@@ -177,6 +181,38 @@ async fn insert_new_db_entry(context: &Context, invite: QrInvite, chat_id: ChatI
177181 . await
178182}
179183
184+ async fn delete_securejoin_wait_msg ( context : & Context , chat_id : ChatId ) -> Result < ( ) > {
185+ if let Some ( ( msg_id, param) ) = context
186+ . sql
187+ . query_row_optional (
188+ "
189+ SELECT id, param FROM msgs
190+ WHERE timestamp=(SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0)
191+ AND chat_id=? AND hidden=0
192+ LIMIT 1
193+ " ,
194+ ( chat_id, chat_id) ,
195+ |row| {
196+ let id: MsgId = row. get ( 0 ) ?;
197+ let param: String = row. get ( 1 ) ?;
198+ let param: Params = param. parse ( ) . unwrap_or_default ( ) ;
199+ Ok ( ( id, param) )
200+ } ,
201+ )
202+ . await ?
203+ && param. get_cmd ( ) == SystemMessage :: SecurejoinWait
204+ {
205+ let on_server = false ;
206+ msg_id. trash ( context, on_server) . await ?;
207+ context. emit_event ( EventType :: MsgDeleted { chat_id, msg_id } ) ;
208+ context. emit_msgs_changed_without_msg_id ( chat_id) ;
209+ chatlist_events:: emit_chatlist_item_changed ( context, chat_id) ;
210+ context. emit_msgs_changed_without_ids ( ) ;
211+ chatlist_events:: emit_chatlist_changed ( context) ;
212+ }
213+ Ok ( ( ) )
214+ }
215+
180216/// Handles `vc-auth-required` and `vg-auth-required` handshake messages.
181217///
182218/// # Bob - the joiner's side
@@ -213,6 +249,11 @@ pub(super) async fn handle_auth_required(
213249
214250 info ! ( context, "Fingerprint verified." , ) ;
215251 let chat_id = private_chat_id ( context, & invite) . await ?;
252+ delete_securejoin_wait_msg ( context, chat_id)
253+ . await
254+ . context ( "delete_securejoin_wait_msg" )
255+ . log_err ( context)
256+ . ok ( ) ;
216257 send_handshake_message ( context, & invite, chat_id, BobHandshakeMsg :: RequestWithAuth ) . await ?;
217258 context
218259 . sql
0 commit comments