From 07e4762f71a8c0252ca74f1d816b2ab72d394785 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Thu, 15 Oct 2020 17:43:22 +0200 Subject: [PATCH] Call update_device_chats automatically during configure (#1957) update_device_chats() takes about 2 seconds on a modern device (Android) because the welcome image file has to be written to the disk as a blob. The problem was that this was done after the progress bar had vanished and before anything else happened so that I thought that something had gone wrong multiple times. The UIs have to remove update_device_chats(), too.. --- CHANGELOG.md | 6 ++++++ deltachat-ffi/deltachat.h | 19 +------------------ deltachat-ffi/src/lib.rs | 15 --------------- examples/repl/cmdline.rs | 3 --- python/src/deltachat/__init__.py | 1 + python/src/deltachat/account.py | 3 +++ python/src/deltachat/testplugin.py | 2 ++ src/config.rs | 2 ++ src/configure/mod.rs | 4 ++++ src/stock.rs | 13 +++++-------- 10 files changed, 24 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb447bd9de..3a26d41459 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +- breaking change: `dc_update_device_chats()` was removed. This is now done automatically during configure. + +- Added a `bot` config. Currently, it only prevents filling the device chats automatically. + ## 1.46.0 - breaking change: `dc_configure()` report errors in diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 753cb8cdd2..6073c78c64 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -349,6 +349,7 @@ char* dc_get_blobdir (const dc_context_t* context); * https://github.com/cracker0dks/basicwebrtc which some UIs have native support for. * The type `jitsi:` may be handled by external apps. * If no type is prefixed, the videochat is handled completely in a browser. + * - `bot` = Set to "1" if this is a bot. E.g. prevents adding the "Device messages" and "Saved messages" chats. * * If you want to retrieve a value, use dc_get_config(). * @@ -984,24 +985,6 @@ void dc_set_draft (dc_context_t* context, uint32_t ch */ uint32_t dc_add_device_msg (dc_context_t* context, const char* label, dc_msg_t* msg); - -/** - * Init device-messages and saved-messages chat. - * This function adds the device-chat and saved-messages chat - * and adds one or more welcome or update-messages. - * The ui can add messages on its own using dc_add_device_msg() - - * for ordering, either before or after or even without calling this function. - * - * Chat and message creation is done only once. - * So if the user has manually deleted things, they won't be re-created - * (however, not seen device messages are added and may re-create the device-chat). - * - * @memberof dc_context_t - * @param context The context object. - */ -void dc_update_device_chats (dc_context_t* context); - - /** * Check if a device-message with a given label was ever added. * Device-messages can be added dc_add_device_msg(). diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index d9c617af1c..c8a3ce7166 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -807,21 +807,6 @@ pub unsafe extern "C" fn dc_add_device_msg( .to_u32() } -#[no_mangle] -pub unsafe extern "C" fn dc_update_device_chats(context: *mut dc_context_t) { - if context.is_null() { - eprintln!("ignoring careless call to dc_update_device_chats()"); - return; - } - let ctx = &mut *context; - - block_on(async move { - ctx.update_device_chats() - .await - .unwrap_or_log_default(&ctx, "Failed to add device message") - }) -} - #[no_mangle] pub unsafe extern "C" fn dc_was_device_msg_ever_added( context: *mut dc_context_t, diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index e949a34e2e..c89126957e 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -881,9 +881,6 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu msg.set_text(Some(arg1.to_string())); chat::add_device_msg(&context, None, Some(&mut msg)).await?; } - "updatedevicechats" => { - context.update_device_chats().await?; - } "listmedia" => { ensure!(sel_chat.is_some(), "No chat selected."); diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index 6240dfed19..dad8e121d8 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -77,6 +77,7 @@ def run_cmdline(argv=None, account_plugins=None): ac.set_config("mvbox_move", "0") ac.set_config("mvbox_watch", "0") ac.set_config("sentbox_watch", "0") + ac.set_config("bot", "1") configtracker = ac.configure() configtracker.wait_finish() diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 0c910909c7..e756866a1a 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -336,6 +336,9 @@ def get_chats(self): def get_deaddrop_chat(self): return Chat(self, const.DC_CHAT_ID_DEADDROP) + def get_device_chat(self): + return Contact(self, const.DC_CONTACT_ID_DEVICE).create_chat() + def get_message_by_id(self, msg_id): """ return Message instance. :param msg_id: integer id of this message. diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index f83a417a90..8e2f639370 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -354,6 +354,8 @@ def wait_configure_and_start_io(self): for acc in self._accounts: if hasattr(acc, "_configtracker"): acc._configtracker.wait_finish() + acc._evtracker.consume_events() + acc.get_device_chat().mark_noticed() del acc._configtracker acc.set_config("bcc_self", "0") if acc.is_configured() and not acc.is_started(): diff --git a/src/config.rs b/src/config.rs index 150718eede..15973e8f7a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -121,6 +121,8 @@ pub enum Config { #[strum(serialize = "sys.config_keys")] SysConfigKeys, + Bot, + /// Whether we send a warning if the password is wrong (set to false when we send a warning /// because we do not want to send a second warning) #[strum(props(default = "0"))] diff --git a/src/configure/mod.rs b/src/configure/mod.rs index 670ecc86cd..55c9833a0d 100644 --- a/src/configure/mod.rs +++ b/src/configure/mod.rs @@ -163,6 +163,9 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> { DC_LP_AUTH_NORMAL as i32 }; + let ctx2 = ctx.clone(); + let update_device_chats_handle = task::spawn(async move { ctx2.update_device_chats().await }); + // Step 1: Load the parameters and check email-address and password if oauth2 { @@ -357,6 +360,7 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> { .await; progress!(ctx, 940); + update_device_chats_handle.await?; Ok(()) } diff --git a/src/stock.rs b/src/stock.rs index d9876207a5..29ee70c350 100644 --- a/src/stock.rs +++ b/src/stock.rs @@ -5,7 +5,6 @@ use std::borrow::Cow; use strum::EnumProperty; use strum_macros::EnumProperty; -use crate::blob::BlobObject; use crate::chat; use crate::chat::ProtectionStatus; use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; @@ -15,6 +14,7 @@ use crate::error::{bail, Error}; use crate::message::Message; use crate::param::Param; use crate::stock::StockMessage::{DeviceMessagesHint, WelcomeMessage}; +use crate::{blob::BlobObject, config::Config}; /// Stock strings /// @@ -423,16 +423,13 @@ impl Context { .await } - pub async fn update_device_chats(&self) -> Result<(), Error> { - // check for the LAST added device message - if it is present, we can skip message creation. - // this is worthwhile as this function is typically called - // by the UI on every program start or even on every opening of the chatlist. - if chat::was_device_msg_ever_added(&self, "core-welcome").await? { + pub(crate) async fn update_device_chats(&self) -> Result<(), Error> { + if self.get_config_bool(Config::Bot).await { return Ok(()); } - // create saved-messages chat; - // we do this only once, if the user has deleted the chat, he can recreate it manually. + // create saved-messages chat; we do this only once, if the user has deleted the chat, + // he can recreate it manually (make sure we do not re-add it when configure() was called a second time) if !self.sql.get_raw_config_bool(&self, "self-chat-added").await { self.sql .set_raw_config_bool(&self, "self-chat-added", true)