From ab1583eef9db3eb6fc3c4bd0f41ef32518032bdc Mon Sep 17 00:00:00 2001 From: iequidoo Date: Tue, 7 May 2024 03:09:23 -0300 Subject: [PATCH] fix: Use ChatIdBlocked::lookup_by_contact() instead of ChatId's method when applicable `ChatId::lookup_by_contact()` returns `None` for blocked chats, so it should be only used if we need to filter out blocked chats, e.g. when building a chatlist. --- src/chat.rs | 7 +++++-- src/contact.rs | 6 ++++-- src/ephemeral.rs | 14 +++++++++----- src/message.rs | 5 +++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 494da8083..13bf5328c 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -2303,7 +2303,9 @@ async fn update_special_chat_name( contact_id: ContactId, name: String, ) -> Result<()> { - if let Some(chat_id) = ChatId::lookup_by_contact(context, contact_id).await? { + if let Some(ChatIdBlocked { id: chat_id, .. }) = + ChatIdBlocked::lookup_by_contact(context, contact_id).await? + { // the `!= name` condition avoids unneeded writes context .sql @@ -4478,9 +4480,10 @@ impl Context { } _ => (), } - ChatId::lookup_by_contact(self, contact_id) + ChatIdBlocked::lookup_by_contact(self, contact_id) .await? .with_context(|| format!("No chat for addr '{addr}'"))? + .id } SyncId::Grpid(grpid) => { if let SyncAction::CreateBroadcast(name) = action { diff --git a/src/contact.rs b/src/contact.rs index 39fd34746..2a250f9b6 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -20,7 +20,7 @@ use tokio::task; use tokio::time::{timeout, Duration}; use crate::aheader::EncryptPreference; -use crate::chat::{ChatId, ProtectionStatus}; +use crate::chat::{ChatId, ChatIdBlocked, ProtectionStatus}; use crate::color::str_to_color; use crate::config::Config; use crate::constants::{Blocked, Chattype, DC_GCL_ADD_SELF, DC_GCL_VERIFIED_ONLY}; @@ -1315,7 +1315,9 @@ impl Contact { pub async fn is_profile_verified(&self, context: &Context) -> Result { let contact_id = self.id; - if let Some(chat_id) = ChatId::lookup_by_contact(context, contact_id).await? { + if let Some(ChatIdBlocked { id: chat_id, .. }) = + ChatIdBlocked::lookup_by_contact(context, contact_id).await? + { Ok(chat_id.is_protected(context).await? == ProtectionStatus::Protected) } else { // 1:1 chat does not exist. diff --git a/src/ephemeral.rs b/src/ephemeral.rs index 7e7e9cd5a..01fe36748 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -74,7 +74,7 @@ use async_channel::Receiver; use serde::{Deserialize, Serialize}; use tokio::time::timeout; -use crate::chat::{send_msg, ChatId}; +use crate::chat::{send_msg, ChatId, ChatIdBlocked}; use crate::constants::{DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH}; use crate::contact::ContactId; use crate::context::Context; @@ -378,11 +378,13 @@ WHERE .await?; if let Some(delete_device_after) = context.get_config_delete_device_after().await? { - let self_chat_id = ChatId::lookup_by_contact(context, ContactId::SELF) + let self_chat_id = ChatIdBlocked::lookup_by_contact(context, ContactId::SELF) .await? + .map(|c| c.id) .unwrap_or_default(); - let device_chat_id = ChatId::lookup_by_contact(context, ContactId::DEVICE) + let device_chat_id = ChatIdBlocked::lookup_by_contact(context, ContactId::DEVICE) .await? + .map(|c| c.id) .unwrap_or_default(); let threshold_timestamp = now.saturating_sub(delete_device_after); @@ -490,11 +492,13 @@ pub(crate) async fn delete_expired_messages(context: &Context, now: i64) -> Resu /// `delete_device_after` setting being set. async fn next_delete_device_after_timestamp(context: &Context) -> Result> { if let Some(delete_device_after) = context.get_config_delete_device_after().await? { - let self_chat_id = ChatId::lookup_by_contact(context, ContactId::SELF) + let self_chat_id = ChatIdBlocked::lookup_by_contact(context, ContactId::SELF) .await? + .map(|c| c.id) .unwrap_or_default(); - let device_chat_id = ChatId::lookup_by_contact(context, ContactId::DEVICE) + let device_chat_id = ChatIdBlocked::lookup_by_contact(context, ContactId::DEVICE) .await? + .map(|c| c.id) .unwrap_or_default(); let oldest_message_timestamp: Option = context diff --git a/src/message.rs b/src/message.rs index 408d0a80c..880c75935 100644 --- a/src/message.rs +++ b/src/message.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use tokio::{fs, io}; use crate::blob::BlobObject; -use crate::chat::{Chat, ChatId}; +use crate::chat::{Chat, ChatId, ChatIdBlocked}; use crate::chatlist_events; use crate::config::Config; use crate::constants::{ @@ -1813,8 +1813,9 @@ pub async fn estimate_deletion_cnt( from_server: bool, seconds: i64, ) -> Result { - let self_chat_id = ChatId::lookup_by_contact(context, ContactId::SELF) + let self_chat_id = ChatIdBlocked::lookup_by_contact(context, ContactId::SELF) .await? + .map(|c| c.id) .unwrap_or_default(); let threshold_timestamp = time() - seconds;