1use anyhow::Result;
4use mail_builder::mime::MimePart;
5use serde::{Deserialize, Serialize};
6
7use crate::chat::{self, ChatId};
8use crate::config::Config;
9use crate::constants::Blocked;
10use crate::contact::ContactId;
11use crate::context::Context;
12use crate::log::LogExt;
13use crate::log::{info, warn};
14use crate::message::{Message, MsgId, Viewtype};
15use crate::mimeparser::SystemMessage;
16use crate::param::Param;
17use crate::sync::SyncData::{AddQrToken, AlterChat, DeleteQrToken};
18use crate::token::Namespace;
19use crate::tools::time;
20use crate::{message, stock_str, token};
21use std::collections::HashSet;
22
23#[derive(Debug, PartialEq)]
25pub(crate) enum Sync {
26 Nosync,
27 Sync,
28}
29
30impl From<Sync> for bool {
31 fn from(sync: Sync) -> bool {
32 match sync {
33 Sync::Nosync => false,
34 Sync::Sync => true,
35 }
36 }
37}
38
39impl From<bool> for Sync {
40 fn from(sync: bool) -> Sync {
41 match sync {
42 false => Sync::Nosync,
43 true => Sync::Sync,
44 }
45 }
46}
47
48#[derive(Debug, Serialize, Deserialize)]
49pub(crate) struct QrTokenData {
50 pub(crate) invitenumber: String,
51 pub(crate) auth: String,
52 pub(crate) grpid: Option<String>,
53}
54
55#[derive(Debug, Serialize, Deserialize)]
56pub(crate) enum SyncData {
57 AddQrToken(QrTokenData),
58 DeleteQrToken(QrTokenData),
59 AlterChat {
60 id: chat::SyncId,
61 action: chat::SyncAction,
62 },
63 Config {
64 key: Config,
65 val: String,
66 },
67 SaveMessage {
68 src: String, dest: String, },
71 DeleteMessages {
72 msgs: Vec<String>, },
74}
75
76#[derive(Debug, Serialize, Deserialize)]
77#[serde(untagged)]
78pub(crate) enum SyncDataOrUnknown {
79 SyncData(SyncData),
80 Unknown(serde_json::Value),
81}
82
83#[derive(Debug, Serialize, Deserialize)]
84pub(crate) struct SyncItem {
85 timestamp: i64,
86
87 data: SyncDataOrUnknown,
88}
89
90#[derive(Debug, Deserialize)]
91pub(crate) struct SyncItems {
92 items: Vec<SyncItem>,
93}
94
95impl From<SyncData> for SyncDataOrUnknown {
96 fn from(sync_data: SyncData) -> Self {
97 Self::SyncData(sync_data)
98 }
99}
100
101impl Context {
102 pub(crate) async fn add_sync_item(&self, data: SyncData) -> Result<()> {
107 self.add_sync_item_with_timestamp(data, time()).await
108 }
109
110 async fn add_sync_item_with_timestamp(&self, data: SyncData, timestamp: i64) -> Result<()> {
113 if !self.should_send_sync_msgs().await? {
114 return Ok(());
115 }
116
117 let item = SyncItem {
118 timestamp,
119 data: data.into(),
120 };
121 let item = serde_json::to_string(&item)?;
122 self.sql
123 .execute("INSERT INTO multi_device_sync (item) VALUES(?);", (item,))
124 .await?;
125
126 Ok(())
127 }
128
129 pub(crate) async fn sync_qr_code_tokens(&self, grpid: Option<&str>) -> Result<()> {
134 if !self.should_send_sync_msgs().await? {
135 return Ok(());
136 }
137 if let (Some(invitenumber), Some(auth)) = (
138 token::lookup(self, Namespace::InviteNumber, grpid).await?,
139 token::lookup(self, Namespace::Auth, grpid).await?,
140 ) {
141 self.add_sync_item(SyncData::AddQrToken(QrTokenData {
142 invitenumber,
143 auth,
144 grpid: grpid.map(|s| s.to_string()),
145 }))
146 .await?;
147 }
148 Ok(())
149 }
150
151 pub(crate) async fn sync_qr_code_token_deletion(
155 &self,
156 invitenumber: String,
157 auth: String,
158 ) -> Result<()> {
159 self.add_sync_item(SyncData::DeleteQrToken(QrTokenData {
160 invitenumber,
161 auth,
162 grpid: None,
163 }))
164 .await?;
165 self.scheduler.interrupt_inbox().await;
166 Ok(())
167 }
168
169 pub async fn send_sync_msg(&self) -> Result<Option<MsgId>> {
175 if let Some((json, ids)) = self.build_sync_json().await? {
176 let chat_id =
177 ChatId::create_for_contact_with_blocked(self, ContactId::SELF, Blocked::Yes)
178 .await?;
179 let mut msg = Message {
180 chat_id,
181 viewtype: Viewtype::Text,
182 text: stock_str::sync_msg_body(self).await,
183 hidden: true,
184 subject: stock_str::sync_msg_subject(self).await,
185 ..Default::default()
186 };
187 msg.param.set_cmd(SystemMessage::MultiDeviceSync);
188 msg.param.set(Param::Arg, json);
189 msg.param.set(Param::Arg2, ids);
190 msg.param.set_int(Param::GuaranteeE2ee, 1);
191 Ok(Some(chat::send_msg(self, chat_id, &mut msg).await?))
192 } else {
193 Ok(None)
194 }
195 }
196
197 pub(crate) async fn build_sync_json(&self) -> Result<Option<(String, String)>> {
200 let (ids, serialized) = self
201 .sql
202 .query_map(
203 "SELECT id, item FROM multi_device_sync ORDER BY id;",
204 (),
205 |row| Ok((row.get::<_, u32>(0)?, row.get::<_, String>(1)?)),
206 |rows| {
207 let mut ids = vec![];
208 let mut serialized = String::default();
209 for row in rows {
210 let (id, item) = row?;
211 ids.push(id);
212 if !serialized.is_empty() {
213 serialized.push_str(",\n");
214 }
215 serialized.push_str(&item);
216 }
217 Ok((ids, serialized))
218 },
219 )
220 .await?;
221
222 if ids.is_empty() {
223 Ok(None)
224 } else {
225 Ok(Some((
226 format!("{{\"items\":[\n{serialized}\n]}}"),
227 ids.iter()
228 .map(|x| x.to_string())
229 .collect::<Vec<String>>()
230 .join(","),
231 )))
232 }
233 }
234
235 pub(crate) fn build_sync_part(&self, json: String) -> MimePart<'static> {
236 MimePart::new("application/json", json).attachment("multi-device-sync.json")
237 }
238
239 pub(crate) fn parse_sync_items(&self, serialized: String) -> Result<SyncItems> {
242 let sync_items: SyncItems = serde_json::from_str(&serialized)?;
243 Ok(sync_items)
244 }
245
246 pub(crate) async fn execute_sync_items(&self, items: &SyncItems) {
257 info!(self, "executing {} sync item(s)", items.items.len());
258 for item in &items.items {
259 match &item.data {
260 SyncDataOrUnknown::SyncData(data) => match data {
261 AddQrToken(token) => self.add_qr_token(token).await,
262 DeleteQrToken(token) => self.delete_qr_token(token).await,
263 AlterChat { id, action } => self.sync_alter_chat(id, action).await,
264 SyncData::Config { key, val } => self.sync_config(key, val).await,
265 SyncData::SaveMessage { src, dest } => self.save_message(src, dest).await,
266 SyncData::DeleteMessages { msgs } => self.sync_message_deletion(msgs).await,
267 },
268 SyncDataOrUnknown::Unknown(data) => {
269 warn!(self, "Ignored unknown sync item: {data}.");
270 Ok(())
271 }
272 }
273 .log_err(self)
274 .ok();
275 }
276
277 if !items.items.is_empty() && !self.get_config_bool(Config::BccSelf).await.unwrap_or(true) {
280 self.set_config_ex(Sync::Nosync, Config::BccSelf, Some("1"))
281 .await
282 .log_err(self)
283 .ok();
284 }
285 }
286
287 async fn add_qr_token(&self, token: &QrTokenData) -> Result<()> {
288 let grpid = token.grpid.as_deref();
289 token::save(self, Namespace::InviteNumber, grpid, &token.invitenumber).await?;
290 token::save(self, Namespace::Auth, grpid, &token.auth).await?;
291 Ok(())
292 }
293
294 async fn delete_qr_token(&self, token: &QrTokenData) -> Result<()> {
295 token::delete(self, Namespace::InviteNumber, &token.invitenumber).await?;
296 token::delete(self, Namespace::Auth, &token.auth).await?;
297 Ok(())
298 }
299
300 async fn save_message(&self, src_rfc724_mid: &str, dest_rfc724_mid: &String) -> Result<()> {
301 if let Some((src_msg_id, _)) = message::rfc724_mid_exists(self, src_rfc724_mid).await? {
302 chat::save_copy_in_self_talk(self, src_msg_id, dest_rfc724_mid).await?;
303 }
304 Ok(())
305 }
306
307 async fn sync_message_deletion(&self, msgs: &Vec<String>) -> Result<()> {
308 let mut modified_chat_ids = HashSet::new();
309 let mut msg_ids = Vec::new();
310 for rfc724_mid in msgs {
311 if let Some((msg_id, _)) = message::rfc724_mid_exists(self, rfc724_mid).await? {
312 if let Some(msg) = Message::load_from_db_optional(self, msg_id).await? {
313 message::delete_msg_locally(self, &msg).await?;
314 msg_ids.push(msg.id);
315 modified_chat_ids.insert(msg.chat_id);
316 } else {
317 warn!(self, "Sync message delete: Database entry does not exist.");
318 }
319 } else {
320 warn!(self, "Sync message delete: {rfc724_mid:?} not found.");
321 }
322 }
323 message::delete_msgs_locally_done(self, &msg_ids, modified_chat_ids).await?;
324 Ok(())
325 }
326}
327
328#[cfg(test)]
329mod tests {
330 use std::time::Duration;
331
332 use anyhow::bail;
333
334 use super::*;
335 use crate::chat::{remove_contact_from_chat, Chat, ProtectionStatus};
336 use crate::chatlist::Chatlist;
337 use crate::contact::{Contact, Origin};
338 use crate::securejoin::get_securejoin_qr;
339 use crate::test_utils::{self, TestContext, TestContextManager};
340 use crate::tools::SystemTime;
341
342 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
343 async fn test_config_sync_msgs() -> Result<()> {
344 let t = TestContext::new_alice().await;
345 assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, false);
346 assert_eq!(t.get_config_bool(Config::BccSelf).await?, true);
347 assert_eq!(t.should_send_sync_msgs().await?, false);
348
349 t.set_config_bool(Config::SyncMsgs, true).await?;
350 assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, true);
351 assert_eq!(t.get_config_bool(Config::BccSelf).await?, true);
352 assert_eq!(t.should_send_sync_msgs().await?, true);
353
354 t.set_config_bool(Config::BccSelf, false).await?;
355 assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, true);
356 assert_eq!(t.get_config_bool(Config::BccSelf).await?, false);
357 assert_eq!(t.should_send_sync_msgs().await?, false);
358
359 t.set_config_bool(Config::SyncMsgs, false).await?;
360 assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, false);
361 assert_eq!(t.get_config_bool(Config::BccSelf).await?, false);
362 assert_eq!(t.should_send_sync_msgs().await?, false);
363 Ok(())
364 }
365
366 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
367 async fn test_build_sync_json() -> Result<()> {
368 let t = TestContext::new_alice().await;
369 t.set_config_bool(Config::SyncMsgs, true).await?;
370
371 assert!(t.build_sync_json().await?.is_none());
372
373 t.add_sync_item_with_timestamp(
377 SyncData::AlterChat {
378 id: chat::SyncId::ContactAddr("bob@example.net".to_string()),
379 action: chat::SyncAction::SetMuted(chat::MuteDuration::Until(
380 SystemTime::UNIX_EPOCH + Duration::from_millis(42999),
381 )),
382 },
383 1631781315,
384 )
385 .await?;
386
387 t.add_sync_item_with_timestamp(
388 SyncData::AddQrToken(QrTokenData {
389 invitenumber: "testinvite".to_string(),
390 auth: "testauth".to_string(),
391 grpid: Some("group123".to_string()),
392 }),
393 1631781316,
394 )
395 .await?;
396 t.add_sync_item_with_timestamp(
397 SyncData::DeleteQrToken(QrTokenData {
398 invitenumber: "123!?\":.;{}".to_string(),
399 auth: "456".to_string(),
400 grpid: None,
401 }),
402 1631781317,
403 )
404 .await?;
405
406 let (serialized, ids) = t.build_sync_json().await?.unwrap();
407 assert_eq!(
408 serialized,
409 r#"{"items":[
410{"timestamp":1631781315,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}},
411{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"testinvite","auth":"testauth","grpid":"group123"}}},
412{"timestamp":1631781317,"data":{"DeleteQrToken":{"invitenumber":"123!?\":.;{}","auth":"456","grpid":null}}}
413]}"#
414 );
415
416 assert!(t.build_sync_json().await?.is_some());
417 t.sql
418 .execute(
419 &format!("DELETE FROM multi_device_sync WHERE id IN ({ids})"),
420 (),
421 )
422 .await?;
423 assert!(t.build_sync_json().await?.is_none());
424
425 let sync_items = t.parse_sync_items(serialized)?;
426 assert_eq!(sync_items.items.len(), 3);
427
428 Ok(())
429 }
430
431 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
432 async fn test_build_sync_json_sync_msgs_off() -> Result<()> {
433 let t = TestContext::new_alice().await;
434 t.set_config_bool(Config::SyncMsgs, false).await?;
435 t.add_sync_item(SyncData::AddQrToken(QrTokenData {
436 invitenumber: "testinvite".to_string(),
437 auth: "testauth".to_string(),
438 grpid: Some("group123".to_string()),
439 }))
440 .await?;
441 assert!(t.build_sync_json().await?.is_none());
442 Ok(())
443 }
444
445 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
446 async fn test_parse_sync_items() -> Result<()> {
447 let t = TestContext::new_alice().await;
448
449 assert!(t.parse_sync_items(r#"{bad json}"#.to_string()).is_err());
450
451 assert!(t.parse_sync_items(r#"{"badname":[]}"#.to_string()).is_err());
452
453 for bad_item_example in [
454 r#"{"items":[{"timestamp":1631781316,"data":{"BadItem":{"invitenumber":"in","auth":"a","grpid":null}}}]}"#,
455 r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":123}}}]}"#, r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":true}}}]}"#, r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":[]}}}]}"#, r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":{}}}}]}"#, r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","grpid":null}}}]}"#, r#"{"items":[{"timestamp":1631781316,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":"Burn"}}}]}"#, ] {
462 let sync_items = t.parse_sync_items(bad_item_example.to_string()).unwrap();
463 assert_eq!(sync_items.items.len(), 1);
464 assert!(matches!(sync_items.items[0].timestamp, 1631781316));
465 assert!(matches!(
466 sync_items.items[0].data,
467 SyncDataOrUnknown::Unknown(_)
468 ));
469 }
470
471 let sync_items = t.parse_sync_items(
473 r#"{"items":[{"timestamp":1631781318,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}}]}"#.to_string(),
474 )?;
475 assert_eq!(sync_items.items.len(), 1);
476 let SyncDataOrUnknown::SyncData(AlterChat { id, action }) =
477 &sync_items.items.first().unwrap().data
478 else {
479 bail!("bad item");
480 };
481 assert_eq!(
482 *id,
483 chat::SyncId::ContactAddr("bob@example.net".to_string())
484 );
485 assert_eq!(
486 *action,
487 chat::SyncAction::SetMuted(chat::MuteDuration::Until(
488 SystemTime::UNIX_EPOCH + Duration::from_millis(42999)
489 ))
490 );
491
492 assert_eq!(
494 t.parse_sync_items(r#"{"items":[]}"#.to_string())?
495 .items
496 .len(),
497 0
498 );
499
500 let sync_items = t
502 .parse_sync_items(
503 r#"{"items":[
504{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"yip","grpid":null}}},
505{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip","additional":123,"grpid":null}}}
506]}"#
507 .to_string(),
508 )
509 ?;
510 assert_eq!(sync_items.items.len(), 2);
511
512 let sync_items = t.parse_sync_items(
513 r#"{"items":[
514{"timestamp":1631781318,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip","grpid":null}}}
515],"additional":"field"}"#
516 .to_string(),
517 )?;
518
519 assert_eq!(sync_items.items.len(), 1);
520 if let SyncDataOrUnknown::SyncData(AddQrToken(token)) =
521 &sync_items.items.first().unwrap().data
522 {
523 assert_eq!(token.invitenumber, "in");
524 assert_eq!(token.auth, "yip");
525 assert_eq!(token.grpid, None);
526 } else {
527 bail!("bad item");
528 }
529
530 let sync_items = t.parse_sync_items(
532 r#"{"items":[{"timestamp":1631781319,"data":{"AddQrToken":{"invitenumber":"in","auth":"a"}}}]}"#.to_string(),
533 )
534 ?;
535 assert_eq!(sync_items.items.len(), 1);
536
537 Ok(())
538 }
539
540 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
541 async fn test_execute_sync_items() -> Result<()> {
542 let t = TestContext::new_alice().await;
543
544 assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await?);
545
546 let sync_items = t
547 .parse_sync_items(
548 r#"{"items":[
549{"timestamp":1631781315,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":"Block"}}},
550{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"yip-in","auth":"a"}}},
551{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"delete unexistent, shall continue"}}},
552{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip-auth"}}},
553{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"foo","grpid":"non-existent"}}},
554{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"directly deleted"}}},
555{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"directly deleted"}}}
556]}"#
557 .to_string(),
558 )
559 ?;
560 t.execute_sync_items(&sync_items).await;
561
562 assert!(
563 Contact::lookup_id_by_addr(&t, "bob@example.net", Origin::Unknown)
564 .await?
565 .is_none()
566 );
567 assert!(token::exists(&t, Namespace::InviteNumber, "yip-in").await?);
568 assert!(token::exists(&t, Namespace::Auth, "yip-auth").await?);
569 assert!(!token::exists(&t, Namespace::Auth, "non-existent").await?);
570 assert!(!token::exists(&t, Namespace::Auth, "directly deleted").await?);
571
572 Ok(())
573 }
574
575 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
576 async fn test_send_sync_msg() -> Result<()> {
577 let alice = TestContext::new_alice().await;
578 alice.set_config_bool(Config::SyncMsgs, true).await?;
579 alice
580 .add_sync_item(SyncData::AddQrToken(QrTokenData {
581 invitenumber: "in".to_string(),
582 auth: "testtoken".to_string(),
583 grpid: None,
584 }))
585 .await?;
586 let msg_id = alice.send_sync_msg().await?.unwrap();
587 let msg = Message::load_from_db(&alice, msg_id).await?;
588 let chat = Chat::load_from_db(&alice, msg.chat_id).await?;
589 assert!(chat.is_self_talk());
590
591 assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
594 let chat_id = ChatId::create_for_contact(&alice, ContactId::SELF).await?;
595 let chat = Chat::load_from_db(&alice, chat_id).await?;
596 assert!(chat.is_self_talk());
597 assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 1);
598 let msgs = chat::get_chat_msgs(&alice, chat_id).await?;
599 assert_eq!(msgs.len(), 0);
600
601 let sent_msg = alice.pop_sent_sync_msg().await;
604 let alice2 = TestContext::new_alice().await;
605 alice2.set_config_bool(Config::SyncMsgs, true).await?;
606 alice2.recv_msg_trash(&sent_msg).await;
607 assert!(token::exists(&alice2, token::Namespace::Auth, "testtoken").await?);
608 assert_eq!(Chatlist::try_load(&alice2, 0, None, None).await?.len(), 0);
609
610 let self_contact = alice2.add_or_lookup_contact(&alice2).await;
612 assert!(!self_contact.is_bot());
613
614 let bob = TestContext::new_bob().await;
616 bob.recv_msg_trash(&sent_msg).await;
617 assert!(!token::exists(&bob, token::Namespace::Auth, "testtoken").await?);
618
619 Ok(())
620 }
621
622 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
623 async fn test_send_sync_msg_enables_bccself() -> Result<()> {
624 for (chatmail, sync_message_sent) in
625 [(false, false), (false, true), (true, false), (true, true)]
626 {
627 let alice1 = TestContext::new_alice().await;
628 let alice2 = TestContext::new_alice().await;
629
630 alice1.set_config_bool(Config::SyncMsgs, true).await?;
633 alice2.set_config_bool(Config::SyncMsgs, true).await?;
634
635 if chatmail {
636 alice1.set_config_bool(Config::IsChatmail, true).await?;
637 alice2.set_config_bool(Config::IsChatmail, true).await?;
638 } else {
639 alice2.set_config_bool(Config::BccSelf, false).await?;
640 }
641
642 alice1.set_config_bool(Config::BccSelf, true).await?;
643
644 let sent_msg = if sync_message_sent {
645 alice1
646 .add_sync_item(SyncData::AddQrToken(QrTokenData {
647 invitenumber: "in".to_string(),
648 auth: "testtoken".to_string(),
649 grpid: None,
650 }))
651 .await?;
652 alice1.send_sync_msg().await?.unwrap();
653 alice1.pop_sent_sync_msg().await
654 } else {
655 let chat = alice1.get_self_chat().await;
656 alice1.send_text(chat.id, "Hi").await
657 };
658
659 assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false);
664
665 alice2.recv_msg_opt(&sent_msg).await;
666 assert_eq!(
667 alice2.get_config_bool(Config::BccSelf).await?,
668 sync_message_sent
673 );
674 }
675 Ok(())
676 }
677
678 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
679 async fn test_bot_no_sync_msgs() -> Result<()> {
680 let mut tcm = TestContextManager::new();
681 let alice = &tcm.alice().await;
682 let bob = &tcm.bob().await;
683 alice.set_config_bool(Config::SyncMsgs, true).await?;
684 let chat_id = alice.create_chat(bob).await.id;
685
686 chat::send_text_msg(alice, chat_id, "hi".to_string()).await?;
687 alice
688 .set_config(Config::Displayname, Some("Alice Human"))
689 .await?;
690 alice.send_sync_msg().await?;
691 alice.pop_sent_sync_msg().await;
692 let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
693 assert_eq!(msg.text, "hi");
694
695 alice.set_config_bool(Config::Bot, true).await?;
696 chat::send_text_msg(alice, chat_id, "hi".to_string()).await?;
697 alice
698 .set_config(Config::Displayname, Some("Alice Bot"))
699 .await?;
700 let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
701 assert_eq!(msg.text, "hi");
702 Ok(())
703 }
704
705 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
706 async fn test_unpromoted_group_qr_sync() -> Result<()> {
707 let mut tcm = TestContextManager::new();
708 let alice = &tcm.alice().await;
709 alice.set_config_bool(Config::SyncMsgs, true).await?;
710 let alice_chatid =
711 chat::create_group_chat(alice, ProtectionStatus::Protected, "the chat").await?;
712 let qr = get_securejoin_qr(alice, Some(alice_chatid)).await?;
713
714 let alice2 = &tcm.alice().await;
716 alice2.set_config_bool(Config::SyncMsgs, true).await?;
717 test_utils::sync(alice, alice2).await;
718
719 let bob = &tcm.bob().await;
720 tcm.exec_securejoin_qr(bob, alice, &qr).await;
721 let msg_id = alice.send_sync_msg().await?;
722 assert!(msg_id.is_some());
726 let sent = alice.pop_sent_sync_msg().await;
727 let msg = alice.parse_msg(&sent).await;
728 let mut sync_items = msg.sync_items.unwrap().items;
729 assert_eq!(sync_items.len(), 1);
730 let data = sync_items.pop().unwrap().data;
731 let SyncDataOrUnknown::SyncData(AddQrToken(_)) = data else {
732 unreachable!();
733 };
734
735 let alice_bob_id = alice.add_or_lookup_contact(bob).await.id;
737 remove_contact_from_chat(alice, alice_chatid, alice_bob_id).await?;
738 alice.pop_sent_msg().await;
739 let sent = alice
740 .send_text(alice_chatid, "Promoting group to another device")
741 .await;
742 alice2.recv_msg(&sent).await;
743
744 let fiona = &tcm.fiona().await;
745 tcm.exec_securejoin_qr(fiona, alice2, &qr).await;
746 let msg = fiona.get_last_msg().await;
747 assert_eq!(msg.text, "Member Me added by alice@example.org.");
748 Ok(())
749 }
750}