deltachat/
sync.rs

1//! # Synchronize items between devices.
2
3use 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::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/// Whether to send device sync messages. Aimed for usage in the internal API.
24#[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,  // RFC724 id (i.e. "Message-Id" header)
69        dest: String, // RFC724 id (i.e. "Message-Id" header)
70    },
71    DeleteMessages {
72        msgs: Vec<String>, // RFC724 id (i.e. "Message-Id" header)
73    },
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    /// Adds an item to the list of items that should be synchronized to other devices.
103    ///
104    /// NB: Private and `pub(crate)` functions shouldn't call this unless `Sync::Sync` is explicitly
105    /// passed to them. This way it's always clear whether the code performs synchronisation.
106    pub(crate) async fn add_sync_item(&self, data: SyncData) -> Result<()> {
107        self.add_sync_item_with_timestamp(data, time()).await
108    }
109
110    /// Adds item and timestamp to the list of items that should be synchronized to other devices.
111    /// If device synchronization is disabled, the function does nothing.
112    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    /// Adds most recent qr-code tokens for the given group or self-contact to the list of items to
130    /// be synced. If device synchronization is disabled,
131    /// no tokens exist or the chat is unpromoted, the function does nothing.
132    /// The caller should call `SchedulerState::interrupt_inbox()` on its own to trigger sending.
133    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    /// Adds deleted qr-code token to the list of items to be synced
152    /// so that the token also gets deleted on the other devices.
153    /// This interrupts SMTP on its own.
154    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    /// Sends out a self-sent message with items to be synchronized, if any.
170    ///
171    /// Mustn't be called from multiple tasks in parallel to avoid sending the same sync items twice
172    /// because sync items are removed from the db only after successful sending. We guarantee this
173    /// by calling `send_sync_msg()` only from the inbox loop.
174    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    /// Copies all sync items to a JSON string and clears the sync-table.
198    /// Returns the JSON string and a comma-separated string of the IDs used.
199    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| {
206                    let id: u32 = row.get(0)?;
207                    let item: String = row.get(1)?;
208                    Ok((id, item))
209                },
210                |rows| {
211                    let mut ids = vec![];
212                    let mut serialized = String::default();
213                    for row in rows {
214                        let (id, item) = row?;
215                        ids.push(id);
216                        if !serialized.is_empty() {
217                            serialized.push_str(",\n");
218                        }
219                        serialized.push_str(&item);
220                    }
221                    Ok((ids, serialized))
222                },
223            )
224            .await?;
225
226        if ids.is_empty() {
227            Ok(None)
228        } else {
229            Ok(Some((
230                format!("{{\"items\":[\n{serialized}\n]}}"),
231                ids.iter()
232                    .map(|x| x.to_string())
233                    .collect::<Vec<String>>()
234                    .join(","),
235            )))
236        }
237    }
238
239    pub(crate) fn build_sync_part(&self, json: String) -> MimePart<'static> {
240        MimePart::new("application/json", json).attachment("multi-device-sync.json")
241    }
242
243    /// Takes a JSON string created by `build_sync_json()`
244    /// and construct `SyncItems` from it.
245    pub(crate) fn parse_sync_items(&self, serialized: String) -> Result<SyncItems> {
246        let sync_items: SyncItems = serde_json::from_str(&serialized)?;
247        Ok(sync_items)
248    }
249
250    /// Executes sync items sent by other device.
251    ///
252    /// CAVE: When changing the code to handle other sync items,
253    /// take care that does not result in calls to `add_sync_item()`
254    /// as otherwise we would add in a dead-loop between two devices
255    /// sending message back and forth.
256    ///
257    /// If an error is returned, the caller shall not try over because some sync items could be
258    /// already executed. Sync items are considered independent and executed in the given order but
259    /// regardless of whether executing of the previous items succeeded.
260    pub(crate) async fn execute_sync_items(&self, items: &SyncItems, timestamp_sent: i64) {
261        info!(self, "executing {} sync item(s)", items.items.len());
262        for item in &items.items {
263            // Limit the timestamp to ensure it is not in the future.
264            //
265            // `sent_timestamp` should be already corrected
266            // if the `Date` header is in the future.
267            let timestamp = std::cmp::min(item.timestamp, timestamp_sent);
268
269            match &item.data {
270                SyncDataOrUnknown::SyncData(data) => match data {
271                    AddQrToken(token) => self.add_qr_token(token, timestamp).await,
272                    DeleteQrToken(token) => self.delete_qr_token(token, timestamp).await,
273                    AlterChat { id, action } => self.sync_alter_chat(id, action).await,
274                    SyncData::Config { key, val } => self.sync_config(key, val).await,
275                    SyncData::SaveMessage { src, dest } => self.save_message(src, dest).await,
276                    SyncData::DeleteMessages { msgs } => self.sync_message_deletion(msgs).await,
277                },
278                SyncDataOrUnknown::Unknown(data) => {
279                    warn!(self, "Ignored unknown sync item: {data}.");
280                    Ok(())
281                }
282            }
283            .log_err(self)
284            .ok();
285        }
286
287        // Since there was a sync message, we know that there is a second device.
288        // Set BccSelf to true if it isn't already.
289        if !items.items.is_empty() && !self.get_config_bool(Config::BccSelf).await.unwrap_or(true) {
290            self.set_config_ex(Sync::Nosync, Config::BccSelf, Some("1"))
291                .await
292                .log_err(self)
293                .ok();
294        }
295    }
296
297    async fn add_qr_token(&self, token: &QrTokenData, timestamp: i64) -> Result<()> {
298        let grpid = token.grpid.as_deref();
299        token::save(
300            self,
301            Namespace::InviteNumber,
302            grpid,
303            &token.invitenumber,
304            timestamp,
305        )
306        .await?;
307        token::save(self, Namespace::Auth, grpid, &token.auth, timestamp).await?;
308        Ok(())
309    }
310
311    async fn delete_qr_token(&self, token: &QrTokenData, timestamp: i64) -> Result<()> {
312        self.sql
313            .execute(
314                "DELETE FROM tokens
315                 WHERE foreign_key IN
316                 (SELECT foreign_key FROM tokens
317                  WHERE token=? OR token=? AND timestamp <= ?)",
318                (&token.invitenumber, &token.auth, timestamp),
319            )
320            .await?;
321        Ok(())
322    }
323
324    async fn save_message(&self, src_rfc724_mid: &str, dest_rfc724_mid: &String) -> Result<()> {
325        if let Some(src_msg_id) = message::rfc724_mid_exists(self, src_rfc724_mid).await? {
326            chat::save_copy_in_self_talk(self, src_msg_id, dest_rfc724_mid).await?;
327        }
328        Ok(())
329    }
330
331    async fn sync_message_deletion(&self, msgs: &Vec<String>) -> Result<()> {
332        let mut modified_chat_ids = HashSet::new();
333        let mut msg_ids = Vec::new();
334        for rfc724_mid in msgs {
335            if let Some(msg_id) = message::rfc724_mid_exists(self, rfc724_mid).await? {
336                if let Some(msg) = Message::load_from_db_optional(self, msg_id).await? {
337                    message::delete_msg_locally(self, &msg).await?;
338                    msg_ids.push(msg.id);
339                    modified_chat_ids.insert(msg.chat_id);
340                } else {
341                    warn!(self, "Sync message delete: Database entry does not exist.");
342                }
343            } else {
344                warn!(self, "Sync message delete: {rfc724_mid:?} not found.");
345            }
346        }
347        message::delete_msgs_locally_done(self, &msg_ids, modified_chat_ids).await?;
348        Ok(())
349    }
350}
351
352#[cfg(test)]
353mod tests {
354    use std::time::Duration;
355
356    use anyhow::bail;
357
358    use super::*;
359    use crate::chat::{Chat, remove_contact_from_chat};
360    use crate::chatlist::Chatlist;
361    use crate::contact::{Contact, Origin};
362    use crate::securejoin::get_securejoin_qr;
363    use crate::test_utils::{self, TestContext, TestContextManager};
364    use crate::tools::SystemTime;
365
366    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
367    async fn test_config_sync_msgs() -> Result<()> {
368        let t = TestContext::new_alice().await;
369        assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, false);
370        assert_eq!(t.get_config_bool(Config::BccSelf).await?, true);
371        assert_eq!(t.should_send_sync_msgs().await?, false);
372
373        t.set_config_bool(Config::SyncMsgs, true).await?;
374        assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, true);
375        assert_eq!(t.get_config_bool(Config::BccSelf).await?, true);
376        assert_eq!(t.should_send_sync_msgs().await?, true);
377
378        t.set_config_bool(Config::BccSelf, false).await?;
379        assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, true);
380        assert_eq!(t.get_config_bool(Config::BccSelf).await?, false);
381        assert_eq!(t.should_send_sync_msgs().await?, false);
382
383        t.set_config_bool(Config::SyncMsgs, false).await?;
384        assert_eq!(t.get_config_bool(Config::SyncMsgs).await?, false);
385        assert_eq!(t.get_config_bool(Config::BccSelf).await?, false);
386        assert_eq!(t.should_send_sync_msgs().await?, false);
387        Ok(())
388    }
389
390    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
391    async fn test_build_sync_json() -> Result<()> {
392        let t = TestContext::new_alice().await;
393        t.set_config_bool(Config::SyncMsgs, true).await?;
394
395        assert!(t.build_sync_json().await?.is_none());
396
397        // Having one test on `SyncData::AlterChat` is sufficient here as
398        // `chat::SyncAction::SetMuted` introduces enums inside items and `SystemTime`. Let's avoid
399        // in-depth testing of the serialiser here which is an external crate.
400        t.add_sync_item_with_timestamp(
401            SyncData::AlterChat {
402                id: chat::SyncId::ContactAddr("bob@example.net".to_string()),
403                action: chat::SyncAction::SetMuted(chat::MuteDuration::Until(
404                    SystemTime::UNIX_EPOCH + Duration::from_millis(42999),
405                )),
406            },
407            1631781315,
408        )
409        .await?;
410
411        t.add_sync_item_with_timestamp(
412            SyncData::AddQrToken(QrTokenData {
413                invitenumber: "testinvite".to_string(),
414                auth: "testauth".to_string(),
415                grpid: Some("group123".to_string()),
416            }),
417            1631781316,
418        )
419        .await?;
420        t.add_sync_item_with_timestamp(
421            SyncData::DeleteQrToken(QrTokenData {
422                invitenumber: "123!?\":.;{}".to_string(),
423                auth: "456".to_string(),
424                grpid: None,
425            }),
426            1631781317,
427        )
428        .await?;
429
430        let (serialized, ids) = t.build_sync_json().await?.unwrap();
431        assert_eq!(
432            serialized,
433            r#"{"items":[
434{"timestamp":1631781315,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}},
435{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"testinvite","auth":"testauth","grpid":"group123"}}},
436{"timestamp":1631781317,"data":{"DeleteQrToken":{"invitenumber":"123!?\":.;{}","auth":"456","grpid":null}}}
437]}"#
438        );
439
440        assert!(t.build_sync_json().await?.is_some());
441        t.sql
442            .execute(
443                &format!("DELETE FROM multi_device_sync WHERE id IN ({ids})"),
444                (),
445            )
446            .await?;
447        assert!(t.build_sync_json().await?.is_none());
448
449        let sync_items = t.parse_sync_items(serialized)?;
450        assert_eq!(sync_items.items.len(), 3);
451
452        Ok(())
453    }
454
455    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
456    async fn test_build_sync_json_sync_msgs_off() -> Result<()> {
457        let t = TestContext::new_alice().await;
458        t.set_config_bool(Config::SyncMsgs, false).await?;
459        t.add_sync_item(SyncData::AddQrToken(QrTokenData {
460            invitenumber: "testinvite".to_string(),
461            auth: "testauth".to_string(),
462            grpid: Some("group123".to_string()),
463        }))
464        .await?;
465        assert!(t.build_sync_json().await?.is_none());
466        Ok(())
467    }
468
469    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
470    async fn test_parse_sync_items() -> Result<()> {
471        let t = TestContext::new_alice().await;
472
473        assert!(t.parse_sync_items(r#"{bad json}"#.to_string()).is_err());
474
475        assert!(t.parse_sync_items(r#"{"badname":[]}"#.to_string()).is_err());
476
477        for bad_item_example in [
478            r#"{"items":[{"timestamp":1631781316,"data":{"BadItem":{"invitenumber":"in","auth":"a","grpid":null}}}]}"#,
479            r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":123}}}]}"#, // `123` is invalid for `String`
480            r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":true}}}]}"#, // `true` is invalid for `String`
481            r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":[]}}}]}"#, // `[]` is invalid for `String`
482            r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":{}}}}]}"#, // `{}` is invalid for `String`
483            r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","grpid":null}}}]}"#, // missing field
484            r#"{"items":[{"timestamp":1631781316,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":"Burn"}}}]}"#, // Unknown enum value
485        ] {
486            let sync_items = t.parse_sync_items(bad_item_example.to_string()).unwrap();
487            assert_eq!(sync_items.items.len(), 1);
488            assert!(matches!(sync_items.items[0].timestamp, 1631781316));
489            assert!(matches!(
490                sync_items.items[0].data,
491                SyncDataOrUnknown::Unknown(_)
492            ));
493        }
494
495        // Test enums inside items and SystemTime
496        let sync_items = t.parse_sync_items(
497            r#"{"items":[{"timestamp":1631781318,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}}]}"#.to_string(),
498        )?;
499        assert_eq!(sync_items.items.len(), 1);
500        let SyncDataOrUnknown::SyncData(AlterChat { id, action }) =
501            &sync_items.items.first().unwrap().data
502        else {
503            bail!("bad item");
504        };
505        assert_eq!(
506            *id,
507            chat::SyncId::ContactAddr("bob@example.net".to_string())
508        );
509        assert_eq!(
510            *action,
511            chat::SyncAction::SetMuted(chat::MuteDuration::Until(
512                SystemTime::UNIX_EPOCH + Duration::from_millis(42999)
513            ))
514        );
515
516        // empty item list is okay
517        assert_eq!(
518            t.parse_sync_items(r#"{"items":[]}"#.to_string())?
519                .items
520                .len(),
521            0
522        );
523
524        // to allow forward compatibility, additional fields should not break parsing
525        let sync_items = t
526            .parse_sync_items(
527                r#"{"items":[
528{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"yip","grpid":null}}},
529{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip","additional":123,"grpid":null}}}
530]}"#
531                .to_string(),
532            )
533            ?;
534        assert_eq!(sync_items.items.len(), 2);
535
536        let sync_items = t.parse_sync_items(
537            r#"{"items":[
538{"timestamp":1631781318,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip","grpid":null}}}
539],"additional":"field"}"#
540                .to_string(),
541        )?;
542
543        assert_eq!(sync_items.items.len(), 1);
544        if let SyncDataOrUnknown::SyncData(AddQrToken(token)) =
545            &sync_items.items.first().unwrap().data
546        {
547            assert_eq!(token.invitenumber, "in");
548            assert_eq!(token.auth, "yip");
549            assert_eq!(token.grpid, None);
550        } else {
551            bail!("bad item");
552        }
553
554        // to allow backward compatibility, missing `Option<>` should not break parsing
555        let sync_items = t.parse_sync_items(
556               r#"{"items":[{"timestamp":1631781319,"data":{"AddQrToken":{"invitenumber":"in","auth":"a"}}}]}"#.to_string(),
557           )
558           ?;
559        assert_eq!(sync_items.items.len(), 1);
560
561        Ok(())
562    }
563
564    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
565    async fn test_execute_sync_items() -> Result<()> {
566        let t = TestContext::new_alice().await;
567
568        assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await?);
569
570        let timestamp_sent = time();
571        let sync_items = t
572            .parse_sync_items(
573                r#"{"items":[
574{"timestamp":1631781315,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":"Block"}}},
575{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"yip-in","auth":"a"}}},
576{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"delete unexistent, shall continue"}}},
577{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"yip-auth"}}},
578{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"foo","grpid":"non-existent"}}},
579{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":"directly deleted"}}},
580{"timestamp":1631781316,"data":{"DeleteQrToken":{"invitenumber":"in","auth":"directly deleted"}}}
581]}"#
582                .to_string(),
583            )
584            ?;
585        t.execute_sync_items(&sync_items, timestamp_sent).await;
586
587        assert!(
588            Contact::lookup_id_by_addr(&t, "bob@example.net", Origin::Unknown)
589                .await?
590                .is_none()
591        );
592        assert!(!token::exists(&t, Namespace::InviteNumber, "yip-in").await?);
593        assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await?);
594        assert!(!token::exists(&t, Namespace::Auth, "non-existent").await?);
595        assert!(!token::exists(&t, Namespace::Auth, "directly deleted").await?);
596
597        Ok(())
598    }
599
600    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
601    async fn test_send_sync_msg() -> Result<()> {
602        let alice = TestContext::new_alice().await;
603        alice.set_config_bool(Config::SyncMsgs, true).await?;
604        alice
605            .add_sync_item(SyncData::AddQrToken(QrTokenData {
606                invitenumber: "in".to_string(),
607                auth: "testtoken".to_string(),
608                grpid: None,
609            }))
610            .await?;
611        let msg_id = alice.send_sync_msg().await?.unwrap();
612        let msg = Message::load_from_db(&alice, msg_id).await?;
613        let chat = Chat::load_from_db(&alice, msg.chat_id).await?;
614        assert!(chat.is_self_talk());
615
616        // check that the used self-talk is not visible to the user
617        // but that creation will still work (in this case, the chat is empty)
618        assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
619        let chat_id = ChatId::create_for_contact(&alice, ContactId::SELF).await?;
620        let chat = Chat::load_from_db(&alice, chat_id).await?;
621        assert!(chat.is_self_talk());
622        assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 1);
623        let msgs = chat::get_chat_msgs(&alice, chat_id).await?;
624        assert_eq!(msgs.len(), 0);
625
626        // let alice's other device receive and execute the sync message,
627        // also here, self-talk should stay hidden
628        let sent_msg = alice.pop_sent_sync_msg().await;
629        let alice2 = TestContext::new_alice().await;
630        alice2.set_config_bool(Config::SyncMsgs, true).await?;
631        alice2.recv_msg_trash(&sent_msg).await;
632        assert!(token::exists(&alice2, token::Namespace::Auth, "testtoken").await?);
633        assert_eq!(Chatlist::try_load(&alice2, 0, None, None).await?.len(), 0);
634
635        // Sync messages are "auto-generated", but they mustn't make the self-contact a bot.
636        let self_contact = alice2.add_or_lookup_contact(&alice2).await;
637        assert!(!self_contact.is_bot());
638
639        // the same sync message sent to bob must not be executed
640        let bob = TestContext::new_bob().await;
641        bob.recv_msg_trash(&sent_msg).await;
642        assert!(!token::exists(&bob, token::Namespace::Auth, "testtoken").await?);
643
644        Ok(())
645    }
646
647    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
648    async fn test_send_sync_msg_enables_bccself() -> Result<()> {
649        for (chatmail, sync_message_sent) in
650            [(false, false), (false, true), (true, false), (true, true)]
651        {
652            let alice1 = TestContext::new_alice().await;
653            let alice2 = TestContext::new_alice().await;
654
655            // SyncMsgs defaults to true on real devices, but in tests it defaults to false,
656            // so we need to enable it
657            alice1.set_config_bool(Config::SyncMsgs, true).await?;
658            alice2.set_config_bool(Config::SyncMsgs, true).await?;
659
660            alice1.set_config_bool(Config::IsChatmail, chatmail).await?;
661            alice2.set_config_bool(Config::IsChatmail, chatmail).await?;
662
663            alice1.set_config_bool(Config::BccSelf, true).await?;
664            alice2.set_config_bool(Config::BccSelf, false).await?;
665
666            let sent_msg = if sync_message_sent {
667                alice1
668                    .add_sync_item(SyncData::AddQrToken(QrTokenData {
669                        invitenumber: "in".to_string(),
670                        auth: "testtoken".to_string(),
671                        grpid: None,
672                    }))
673                    .await?;
674                alice1.send_sync_msg().await?.unwrap();
675                alice1.pop_sent_sync_msg().await
676            } else {
677                let chat = alice1.get_self_chat().await;
678                alice1.send_text(chat.id, "Hi").await
679            };
680
681            // On chatmail accounts, BccSelf defaults to false.
682            // When receiving a sync message from another device,
683            // there obviously is a multi-device-setup, and BccSelf
684            // should be enabled.
685            assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false);
686
687            alice2.recv_msg_opt(&sent_msg).await;
688            assert_eq!(
689                alice2.get_config_bool(Config::BccSelf).await?,
690                // BccSelf should be enabled when receiving a sync message,
691                // but not when receiving another outgoing message
692                // because we might have forgotten it and it then it might have been forwarded to us again
693                // (though of course this is very unlikely).
694                sync_message_sent
695            );
696        }
697        Ok(())
698    }
699
700    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
701    async fn test_bot_no_sync_msgs() -> Result<()> {
702        let mut tcm = TestContextManager::new();
703        let alice = &tcm.alice().await;
704        let bob = &tcm.bob().await;
705        alice.set_config_bool(Config::SyncMsgs, true).await?;
706        let chat_id = alice.create_chat(bob).await.id;
707
708        chat::send_text_msg(alice, chat_id, "hi".to_string()).await?;
709        alice
710            .set_config(Config::Displayname, Some("Alice Human"))
711            .await?;
712        alice.send_sync_msg().await?;
713        alice.pop_sent_sync_msg().await;
714        let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
715        assert_eq!(msg.text, "hi");
716
717        alice.set_config_bool(Config::Bot, true).await?;
718        chat::send_text_msg(alice, chat_id, "hi".to_string()).await?;
719        alice
720            .set_config(Config::Displayname, Some("Alice Bot"))
721            .await?;
722        let msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
723        assert_eq!(msg.text, "hi");
724        Ok(())
725    }
726
727    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
728    async fn test_unpromoted_group_qr_sync() -> Result<()> {
729        let mut tcm = TestContextManager::new();
730        let alice = &tcm.alice().await;
731        alice.set_config_bool(Config::SyncMsgs, true).await?;
732        let alice_chatid = chat::create_group(alice, "the chat").await?;
733        let qr = get_securejoin_qr(alice, Some(alice_chatid)).await?;
734
735        // alice2 syncs the QR code token.
736        let alice2 = &tcm.alice().await;
737        alice2.set_config_bool(Config::SyncMsgs, true).await?;
738        test_utils::sync(alice, alice2).await;
739
740        let bob = &tcm.bob().await;
741        tcm.exec_securejoin_qr(bob, alice, &qr).await;
742        let msg_id = alice.send_sync_msg().await?;
743        // Core <= v1.143 doesn't sync QR code tokens immediately, so current Core does that when a
744        // group is promoted for compatibility (because the group could be created by older Core).
745        // TODO: assert!(msg_id.is_none());
746        assert!(msg_id.is_some());
747        let sent = alice.pop_sent_sync_msg().await;
748        let msg = alice.parse_msg(&sent).await;
749        let mut sync_items = msg.sync_items.unwrap().items;
750        assert_eq!(sync_items.len(), 1);
751        let data = sync_items.pop().unwrap().data;
752        let SyncDataOrUnknown::SyncData(AddQrToken(_)) = data else {
753            unreachable!();
754        };
755
756        // Remove Bob because alice2 doesn't have their key.
757        let alice_bob_id = alice.add_or_lookup_contact(bob).await.id;
758        remove_contact_from_chat(alice, alice_chatid, alice_bob_id).await?;
759        alice.pop_sent_msg().await;
760        let sent = alice
761            .send_text(alice_chatid, "Promoting group to another device")
762            .await;
763        alice2.recv_msg(&sent).await;
764
765        let fiona = &tcm.fiona().await;
766        tcm.exec_securejoin_qr(fiona, alice2, &qr).await;
767        let msg = fiona.get_last_msg().await;
768        assert_eq!(msg.text, "Member Me added by alice@example.org.");
769        Ok(())
770    }
771}