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