diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index e44f6d868..92e95d461 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -17,7 +17,7 @@ use deltachat::download::DownloadState; use deltachat::imex::*; use deltachat::location; use deltachat::log::LogExt; -use deltachat::message::{self, Message, MessageState, MsgId}; +use deltachat::message::{self, Message, MessageState, MsgId, Viewtype}; use deltachat::peerstate::*; use deltachat::qr::*; use deltachat::sql; diff --git a/src/blob.rs b/src/blob.rs index 1770af2a6..d652add20 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -19,13 +19,13 @@ use thiserror::Error; use crate::config::Config; use crate::constants::{ - MediaQuality, Viewtype, BALANCED_AVATAR_SIZE, BALANCED_IMAGE_SIZE, WORSE_AVATAR_SIZE, - WORSE_IMAGE_SIZE, + MediaQuality, BALANCED_AVATAR_SIZE, BALANCED_IMAGE_SIZE, WORSE_AVATAR_SIZE, WORSE_IMAGE_SIZE, }; use crate::context::Context; use crate::events::EventType; use crate::log::LogExt; use crate::message; +use crate::message::Viewtype; /// Represents a file in the blob directory. /// diff --git a/src/chat.rs b/src/chat.rs index 5a56fd804..2d6a7317d 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -15,10 +15,9 @@ use crate::blob::{BlobError, BlobObject}; use crate::color::str_to_color; use crate::config::Config; use crate::constants::{ - Blocked, Chattype, Viewtype, DC_CHAT_ID_ALLDONE_HINT, DC_CHAT_ID_ARCHIVED_LINK, - DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH, DC_CONTACT_ID_DEVICE, DC_CONTACT_ID_INFO, - DC_CONTACT_ID_LAST_SPECIAL, DC_CONTACT_ID_SELF, DC_GCM_ADDDAYMARKER, DC_GCM_INFO_ONLY, - DC_RESEND_USER_AVATAR_DAYS, + Blocked, Chattype, DC_CHAT_ID_ALLDONE_HINT, DC_CHAT_ID_ARCHIVED_LINK, DC_CHAT_ID_LAST_SPECIAL, + DC_CHAT_ID_TRASH, DC_CONTACT_ID_DEVICE, DC_CONTACT_ID_INFO, DC_CONTACT_ID_LAST_SPECIAL, + DC_CONTACT_ID_SELF, DC_GCM_ADDDAYMARKER, DC_GCM_INFO_ONLY, DC_RESEND_USER_AVATAR_DAYS, }; use crate::contact::{addr_cmp, Contact, ContactId, Origin, VerifiedStatus}; use crate::context::Context; @@ -33,7 +32,7 @@ use crate::events::EventType; use crate::html::new_html_mimepart; use crate::job::{self, Action}; use crate::location; -use crate::message::{self, Message, MessageState, MsgId}; +use crate::message::{self, Message, MessageState, MsgId, Viewtype}; use crate::mimefactory::MimeFactory; use crate::mimeparser::SystemMessage; use crate::param::{Param, Params}; @@ -1808,26 +1807,10 @@ pub async fn prepare_msg(context: &Context, chat_id: ChatId, msg: &mut Message) Ok(msg_id) } -pub(crate) fn msgtype_has_file(msgtype: Viewtype) -> bool { - match msgtype { - Viewtype::Unknown => false, - Viewtype::Text => false, - Viewtype::Image => true, - Viewtype::Gif => true, - Viewtype::Sticker => true, - Viewtype::Audio => true, - Viewtype::Voice => true, - Viewtype::Video => true, - Viewtype::File => true, - Viewtype::VideochatInvitation => false, - Viewtype::Webxdc => true, - } -} - async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> { if msg.viewtype == Viewtype::Text || msg.viewtype == Viewtype::VideochatInvitation { // the caller should check if the message text is empty - } else if msgtype_has_file(msg.viewtype) { + } else if msg.viewtype.has_file() { let blob = msg .param .get_blob(Param::File, context, !msg.is_increation()) diff --git a/src/chatlist.rs b/src/chatlist.rs index bae795bbd..11582cf65 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -375,8 +375,8 @@ mod tests { use super::*; use crate::chat::{create_group_chat, get_chat_contacts, ProtectionStatus}; - use crate::constants::Viewtype; use crate::dc_receive_imf::dc_receive_imf; + use crate::message::Viewtype; use crate::stock_str::StockMessage; use crate::test_utils::TestContext; diff --git a/src/configure.rs b/src/configure.rs index 8adb30a65..4cb61d35e 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -11,21 +11,20 @@ use async_std::task; use job::Action; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; -use crate::dc_tools::EmailAddress; +use crate::config::Config; +use crate::constants::{DC_LP_AUTH_FLAGS, DC_LP_AUTH_NORMAL, DC_LP_AUTH_OAUTH2}; +use crate::context::Context; +use crate::dc_tools::{time, EmailAddress}; use crate::imap::Imap; +use crate::job; use crate::login_param::{CertificateChecks, LoginParam, ServerLoginParam, Socks5Config}; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::oauth2::dc_get_oauth2_addr; +use crate::param::Params; use crate::provider::{Protocol, Socket, UsernamePattern}; use crate::smtp::Smtp; use crate::stock_str; use crate::{chat, e2ee, provider}; -use crate::{config::Config, dc_tools::time}; -use crate::{ - constants::{Viewtype, DC_LP_AUTH_FLAGS, DC_LP_AUTH_NORMAL, DC_LP_AUTH_OAUTH2}, - job, -}; -use crate::{context::Context, param::Params}; use auto_mozilla::moz_autoconfigure; use auto_outlook::outlk_autodiscover; diff --git a/src/constants.rs b/src/constants.rs index 294718e1a..08c5f7ca0 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -231,82 +231,6 @@ pub const DC_FOLDERS_CONFIGURED_VERSION: i32 = 3; // can be overwritten by the setting `max_smtp_rcpt_to` in provider-db. pub const DEFAULT_MAX_SMTP_RCPT_TO: usize = 50; -#[derive( - Debug, - Display, - Clone, - Copy, - PartialEq, - Eq, - FromPrimitive, - ToPrimitive, - FromSql, - ToSql, - Serialize, - Deserialize, -)] -#[repr(u32)] -pub enum Viewtype { - Unknown = 0, - - /// Text message. - /// The text of the message is set using dc_msg_set_text() - /// and retrieved with dc_msg_get_text(). - Text = 10, - - /// Image message. - /// If the image is an animated GIF, the type DC_MSG_GIF should be used. - /// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension - /// and retrieved via dc_msg_set_file(), dc_msg_set_dimension(). - Image = 20, - - /// Animated GIF message. - /// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension() - /// and retrieved via dc_msg_get_file(), dc_msg_get_width(), dc_msg_get_height(). - Gif = 21, - - /// Message containing a sticker, similar to image. - /// If possible, the ui should display the image without borders in a transparent way. - /// A click on a sticker will offer to install the sticker set in some future. - Sticker = 23, - - /// Message containing an Audio file. - /// File and duration are set via dc_msg_set_file(), dc_msg_set_duration() - /// and retrieved via dc_msg_get_file(), dc_msg_get_duration(). - Audio = 40, - - /// A voice message that was directly recorded by the user. - /// For all other audio messages, the type #DC_MSG_AUDIO should be used. - /// File and duration are set via dc_msg_set_file(), dc_msg_set_duration() - /// and retrieved via dc_msg_get_file(), dc_msg_get_duration() - Voice = 41, - - /// Video messages. - /// File, width, height and durarion - /// are set via dc_msg_set_file(), dc_msg_set_dimension(), dc_msg_set_duration() - /// and retrieved via - /// dc_msg_get_file(), dc_msg_get_width(), - /// dc_msg_get_height(), dc_msg_get_duration(). - Video = 50, - - /// Message containing any file, eg. a PDF. - /// The file is set via dc_msg_set_file() - /// and retrieved via dc_msg_get_file(). - File = 60, - - /// Message is an invitation to a videochat. - VideochatInvitation = 70, - - /// Message is an webxdc instance. - Webxdc = 80, -} - -impl Default for Viewtype { - fn default() -> Self { - Viewtype::Unknown - } -} - pub const DC_JOB_DELETE_MSG_ON_IMAP: i32 = 110; #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)] @@ -318,33 +242,9 @@ pub enum KeyType { #[cfg(test)] mod tests { - use super::*; use num_traits::FromPrimitive; - #[test] - fn test_derive_display_works_as_expected() { - assert_eq!(format!("{}", Viewtype::Audio), "Audio"); - } - - #[test] - fn test_viewtype_values() { - // values may be written to disk and must not change - assert_eq!(Viewtype::Unknown, Viewtype::default()); - assert_eq!(Viewtype::Unknown, Viewtype::from_i32(0).unwrap()); - assert_eq!(Viewtype::Text, Viewtype::from_i32(10).unwrap()); - assert_eq!(Viewtype::Image, Viewtype::from_i32(20).unwrap()); - assert_eq!(Viewtype::Gif, Viewtype::from_i32(21).unwrap()); - assert_eq!(Viewtype::Sticker, Viewtype::from_i32(23).unwrap()); - assert_eq!(Viewtype::Audio, Viewtype::from_i32(40).unwrap()); - assert_eq!(Viewtype::Voice, Viewtype::from_i32(41).unwrap()); - assert_eq!(Viewtype::Video, Viewtype::from_i32(50).unwrap()); - assert_eq!(Viewtype::File, Viewtype::from_i32(60).unwrap()); - assert_eq!( - Viewtype::VideochatInvitation, - Viewtype::from_i32(70).unwrap() - ); - assert_eq!(Viewtype::Webxdc, Viewtype::from_i32(80).unwrap()); - } + use super::*; #[test] fn test_chattype_values() { diff --git a/src/context.rs b/src/context.rs index bac653ac1..be11e9464 100644 --- a/src/context.rs +++ b/src/context.rs @@ -670,10 +670,10 @@ mod tests { use crate::chat::{ get_chat_contacts, get_chat_msgs, send_msg, set_muted, Chat, ChatId, MuteDuration, }; - use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; + use crate::constants::DC_CONTACT_ID_SELF; use crate::dc_receive_imf::dc_receive_imf; use crate::dc_tools::dc_create_outgoing_rfc724_mid; - use crate::message::Message; + use crate::message::{Message, Viewtype}; use crate::test_utils::TestContext; use anyhow::Context as _; use std::time::Duration; diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 4079ed81e..8b359812e 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -14,8 +14,7 @@ use sha2::{Digest, Sha256}; use crate::chat::{self, Chat, ChatId, ChatIdBlocked, ProtectionStatus}; use crate::config::Config; use crate::constants::{ - Blocked, Chattype, ShowEmails, Viewtype, DC_CHAT_ID_TRASH, DC_CONTACT_ID_LAST_SPECIAL, - DC_CONTACT_ID_SELF, + Blocked, Chattype, ShowEmails, DC_CHAT_ID_TRASH, DC_CONTACT_ID_LAST_SPECIAL, DC_CONTACT_ID_SELF, }; use crate::contact::{ addr_cmp, may_be_valid_addr, normalize_name, Contact, ContactId, Origin, VerifiedStatus, @@ -28,7 +27,9 @@ use crate::events::EventType; use crate::headerdef::{HeaderDef, HeaderDefMap}; use crate::job::{self, Action}; use crate::log::LogExt; -use crate::message::{self, rfc724_mid_exists, Message, MessageState, MessengerMessage, MsgId}; +use crate::message::{ + self, rfc724_mid_exists, Message, MessageState, MessengerMessage, MsgId, Viewtype, +}; use crate::mimeparser::{ parse_message_id, parse_message_ids, AvatarAction, MailinglistType, MimeMessage, SystemMessage, }; diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 0b61a4523..ca0af00b0 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -21,10 +21,10 @@ use mailparse::MailHeaderMap; use rand::{thread_rng, Rng}; use crate::chat::{add_device_msg, add_device_msg_with_importance}; -use crate::constants::{Viewtype, DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS}; +use crate::constants::{DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS}; use crate::context::Context; use crate::events::EventType; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::provider::get_provider_update_timestamp; use crate::stock_str; diff --git a/src/download.rs b/src/download.rs index bf7ba55f5..79c6c7402 100644 --- a/src/download.rs +++ b/src/download.rs @@ -6,12 +6,11 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use crate::config::Config; -use crate::constants::Viewtype; use crate::context::Context; use crate::dc_tools::time; use crate::imap::{Imap, ImapActionResult}; use crate::job::{self, Action, Job, Status}; -use crate::message::{Message, MsgId}; +use crate::message::{Message, MsgId, Viewtype}; use crate::mimeparser::{MimeMessage, Part}; use crate::param::Params; use crate::{job_try, stock_str, EventType}; @@ -255,13 +254,15 @@ impl MimeMessage { #[cfg(test)] mod tests { - use super::*; + use num_traits::FromPrimitive; + use crate::chat::send_msg; - use crate::constants::Viewtype; use crate::dc_receive_imf::dc_receive_imf_inner; use crate::ephemeral::Timer; + use crate::message::Viewtype; use crate::test_utils::TestContext; - use num_traits::FromPrimitive; + + use super::*; #[test] fn test_downloadstate_values() { diff --git a/src/e2ee.rs b/src/e2ee.rs index 278eefd03..4fe3d2ebc 100644 --- a/src/e2ee.rs +++ b/src/e2ee.rs @@ -400,15 +400,14 @@ pub async fn ensure_secret_key_exists(context: &Context) -> Result { #[cfg(test)] mod tests { - use super::*; - use crate::chat; - use crate::constants::Viewtype; - use crate::message::Message; + use crate::message::{Message, Viewtype}; use crate::param::Param; use crate::peerstate::ToSave; use crate::test_utils::{bob_keypair, TestContext}; + use super::*; + mod ensure_secret_key_exists { use super::*; diff --git a/src/ephemeral.rs b/src/ephemeral.rs index d42e6cc43..668407e76 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -67,14 +67,14 @@ use serde::{Deserialize, Serialize}; use crate::chat::{send_msg, ChatId}; use crate::constants::{ - Viewtype, DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH, DC_CONTACT_ID_DEVICE, DC_CONTACT_ID_SELF, + DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH, DC_CONTACT_ID_DEVICE, DC_CONTACT_ID_SELF, }; use crate::contact::ContactId; use crate::context::Context; use crate::dc_tools::time; use crate::download::MIN_DELETE_SERVER_AFTER; use crate::events::EventType; -use crate::message::{Message, MessageState, MsgId}; +use crate::message::{Message, MessageState, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::stock_str; use std::cmp::max; diff --git a/src/html.rs b/src/html.rs index a7d0e5191..639ca56ca 100644 --- a/src/html.rs +++ b/src/html.rs @@ -279,9 +279,9 @@ mod tests { use crate::chat; use crate::chat::forward_msgs; use crate::config::Config; - use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; + use crate::constants::DC_CONTACT_ID_SELF; use crate::dc_receive_imf::dc_receive_imf; - use crate::message::MessengerMessage; + use crate::message::{MessengerMessage, Viewtype}; use crate::test_utils::TestContext; #[async_std::test] diff --git a/src/imap.rs b/src/imap.rs index eb4762f94..4081a5dd4 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -17,11 +17,10 @@ use async_std::channel::Receiver; use async_std::prelude::*; use num_traits::FromPrimitive; -use crate::chat; -use crate::chat::ChatId; -use crate::chat::ChatIdBlocked; +use crate::chat::{self, ChatId, ChatIdBlocked}; +use crate::config::Config; use crate::constants::{ - Blocked, Chattype, ShowEmails, Viewtype, DC_CONTACT_ID_SELF, DC_FETCH_EXISTING_MSGS_COUNT, + Blocked, Chattype, ShowEmails, DC_CONTACT_ID_SELF, DC_FETCH_EXISTING_MSGS_COUNT, DC_FOLDERS_CONFIGURED_VERSION, DC_LP_AUTH_OAUTH2, }; use crate::context::Context; @@ -32,16 +31,17 @@ use crate::dc_tools::dc_create_id; use crate::events::EventType; use crate::headerdef::{HeaderDef, HeaderDefMap}; use crate::job::{self, Action}; -use crate::login_param::{CertificateChecks, LoginParam, ServerLoginParam}; -use crate::login_param::{ServerAddress, Socks5Config}; -use crate::message::{self, Message, MessageState, MessengerMessage, MsgId}; +use crate::login_param::{ + CertificateChecks, LoginParam, ServerAddress, ServerLoginParam, Socks5Config, +}; +use crate::message::{self, Message, MessageState, MessengerMessage, MsgId, Viewtype}; use crate::mimeparser; use crate::oauth2::dc_get_oauth2_access_token; use crate::param::Params; use crate::provider::Socket; +use crate::scheduler::connectivity::ConnectivityStore; use crate::scheduler::InterruptInfo; use crate::stock_str; -use crate::{config::Config, scheduler::connectivity::ConnectivityStore}; mod client; mod idle; diff --git a/src/imex.rs b/src/imex.rs index 95920b769..cb06a802c 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -16,7 +16,7 @@ use rand::{thread_rng, Rng}; use crate::blob::BlobObject; use crate::chat::{self, delete_and_reset_all_device_msgs, ChatId}; use crate::config::Config; -use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; +use crate::constants::DC_CONTACT_ID_SELF; use crate::context::Context; use crate::dc_tools::{ dc_create_folder, dc_delete_file, dc_delete_files_in_dir, dc_get_filesuffix_lc, @@ -26,7 +26,7 @@ use crate::e2ee; use crate::events::EventType; use crate::key::{self, DcKey, DcSecretKey, SignedPublicKey, SignedSecretKey}; use crate::log::LogExt; -use crate::message::{Message, MsgId}; +use crate::message::{Message, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::pgp; diff --git a/src/location.rs b/src/location.rs index b092c750a..47685f648 100644 --- a/src/location.rs +++ b/src/location.rs @@ -7,13 +7,13 @@ use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use crate::chat::{self, ChatId}; use crate::config::Config; -use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; +use crate::constants::DC_CONTACT_ID_SELF; use crate::contact::ContactId; use crate::context::Context; use crate::dc_tools::time; use crate::events::EventType; use crate::job::{self, Job}; -use crate::message::{Message, MsgId}; +use crate::message::{Message, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Params; use crate::stock_str; diff --git a/src/message.rs b/src/message.rs index 736e5f2bb..8ab6552df 100644 --- a/src/message.rs +++ b/src/message.rs @@ -10,8 +10,8 @@ use serde::{Deserialize, Serialize}; use crate::chat::{self, Chat, ChatId}; use crate::constants::{ - Blocked, Chattype, VideochatType, Viewtype, DC_CHAT_ID_TRASH, DC_CONTACT_ID_INFO, - DC_CONTACT_ID_SELF, DC_DESIRED_TEXT_LEN, DC_MSG_ID_LAST_SPECIAL, + Blocked, Chattype, VideochatType, DC_CHAT_ID_TRASH, DC_CONTACT_ID_INFO, DC_CONTACT_ID_SELF, + DC_DESIRED_TEXT_LEN, DC_MSG_ID_LAST_SPECIAL, }; use crate::contact::{Contact, ContactId, Origin}; use crate::context::Context; @@ -386,7 +386,7 @@ impl Message { } pub async fn try_calc_and_set_dimensions(&mut self, context: &Context) -> Result<()> { - if chat::msgtype_has_file(self.viewtype) { + if self.viewtype.has_file() { let file_param = self.param.get_path(Param::File, context)?; if let Some(path_and_filename) = file_param { if (self.viewtype == Viewtype::Image || self.viewtype == Viewtype::Gif) @@ -619,7 +619,7 @@ impl Message { /// copied to the blobdir. Thus those attachments need to be /// created immediately in the blobdir with a valid filename. pub fn is_increation(&self) -> bool { - chat::msgtype_has_file(self.viewtype) && self.state == MessageState::OutPreparing + self.viewtype.has_file() && self.state == MessageState::OutPreparing } pub fn is_setupmessage(&self) -> bool { @@ -1700,9 +1700,106 @@ pub(crate) async fn rfc724_mid_exists( Ok(res) } +/// How a message is primarily displayed. +#[derive( + Debug, + Display, + Clone, + Copy, + PartialEq, + Eq, + FromPrimitive, + ToPrimitive, + FromSql, + ToSql, + Serialize, + Deserialize, +)] +#[repr(u32)] +pub enum Viewtype { + Unknown = 0, + + /// Text message. + /// The text of the message is set using dc_msg_set_text() + /// and retrieved with dc_msg_get_text(). + Text = 10, + + /// Image message. + /// If the image is an animated GIF, the type DC_MSG_GIF should be used. + /// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension + /// and retrieved via dc_msg_set_file(), dc_msg_set_dimension(). + Image = 20, + + /// Animated GIF message. + /// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension() + /// and retrieved via dc_msg_get_file(), dc_msg_get_width(), dc_msg_get_height(). + Gif = 21, + + /// Message containing a sticker, similar to image. + /// If possible, the ui should display the image without borders in a transparent way. + /// A click on a sticker will offer to install the sticker set in some future. + Sticker = 23, + + /// Message containing an Audio file. + /// File and duration are set via dc_msg_set_file(), dc_msg_set_duration() + /// and retrieved via dc_msg_get_file(), dc_msg_get_duration(). + Audio = 40, + + /// A voice message that was directly recorded by the user. + /// For all other audio messages, the type #DC_MSG_AUDIO should be used. + /// File and duration are set via dc_msg_set_file(), dc_msg_set_duration() + /// and retrieved via dc_msg_get_file(), dc_msg_get_duration() + Voice = 41, + + /// Video messages. + /// File, width, height and durarion + /// are set via dc_msg_set_file(), dc_msg_set_dimension(), dc_msg_set_duration() + /// and retrieved via + /// dc_msg_get_file(), dc_msg_get_width(), + /// dc_msg_get_height(), dc_msg_get_duration(). + Video = 50, + + /// Message containing any file, eg. a PDF. + /// The file is set via dc_msg_set_file() + /// and retrieved via dc_msg_get_file(). + File = 60, + + /// Message is an invitation to a videochat. + VideochatInvitation = 70, + + /// Message is an webxdc instance. + Webxdc = 80, +} + +impl Default for Viewtype { + fn default() -> Self { + Viewtype::Unknown + } +} + +impl Viewtype { + /// Whether a message with this [`Viewtype`] should have a file attachment. + pub fn has_file(&self) -> bool { + match self { + Viewtype::Unknown => false, + Viewtype::Text => false, + Viewtype::Image => true, + Viewtype::Gif => true, + Viewtype::Sticker => true, + Viewtype::Audio => true, + Viewtype::Voice => true, + Viewtype::Video => true, + Viewtype::File => true, + Viewtype::VideochatInvitation => false, + Viewtype::Webxdc => true, + } + } +} + #[cfg(test)] mod tests { - use super::*; + use num_traits::FromPrimitive; + use crate::chat::{marknoticed_chat, ChatItem}; use crate::chatlist::Chatlist; use crate::constants::DC_CONTACT_ID_DEVICE; @@ -1710,6 +1807,8 @@ mod tests { use crate::test_utils as test; use crate::test_utils::TestContext; + use super::*; + #[test] fn test_guess_msgtype_from_suffix() { assert_eq!( @@ -2154,4 +2253,29 @@ mod tests { Ok(()) } + + #[test] + fn test_viewtype_derive_display_works_as_expected() { + assert_eq!(format!("{}", Viewtype::Audio), "Audio"); + } + + #[test] + fn test_viewtype_values() { + // values may be written to disk and must not change + assert_eq!(Viewtype::Unknown, Viewtype::default()); + assert_eq!(Viewtype::Unknown, Viewtype::from_i32(0).unwrap()); + assert_eq!(Viewtype::Text, Viewtype::from_i32(10).unwrap()); + assert_eq!(Viewtype::Image, Viewtype::from_i32(20).unwrap()); + assert_eq!(Viewtype::Gif, Viewtype::from_i32(21).unwrap()); + assert_eq!(Viewtype::Sticker, Viewtype::from_i32(23).unwrap()); + assert_eq!(Viewtype::Audio, Viewtype::from_i32(40).unwrap()); + assert_eq!(Viewtype::Voice, Viewtype::from_i32(41).unwrap()); + assert_eq!(Viewtype::Video, Viewtype::from_i32(50).unwrap()); + assert_eq!(Viewtype::File, Viewtype::from_i32(60).unwrap()); + assert_eq!( + Viewtype::VideochatInvitation, + Viewtype::from_i32(70).unwrap() + ); + assert_eq!(Viewtype::Webxdc, Viewtype::from_i32(80).unwrap()); + } } diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 7499bfc7d..675005ade 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -7,9 +7,9 @@ use chrono::TimeZone; use lettre_email::{mime, Address, Header, MimeMultipartType, PartBuilder}; use crate::blob::BlobObject; -use crate::chat::{self, Chat}; +use crate::chat::Chat; use crate::config::Config; -use crate::constants::{Chattype, Viewtype, DC_FROM_HANDSHAKE}; +use crate::constants::{Chattype, DC_FROM_HANDSHAKE}; use crate::contact::Contact; use crate::context::{get_version_str, Context}; use crate::dc_tools::IsNoneOrEmpty; @@ -22,7 +22,7 @@ use crate::ephemeral::Timer as EphemeralTimer; use crate::format_flowed::{format_flowed, format_flowed_quote}; use crate::html::new_html_mimepart; use crate::location; -use crate::message::{self, Message, MsgId}; +use crate::message::{self, Message, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::peerstate::{Peerstate, PeerstateVerifiedStatus}; @@ -1140,7 +1140,7 @@ impl<'a> MimeFactory<'a> { } // add attachment part - if chat::msgtype_has_file(self.msg.viewtype) { + if self.msg.viewtype.has_file() { if !is_file_size_okay(context, self.msg).await? { bail!( "Message exceeds the recommended {} MB.", @@ -1452,12 +1452,13 @@ fn maybe_encode_words(words: &str) -> String { #[cfg(test)] mod tests { - use super::*; + use async_std::fs::File; use async_std::prelude::*; + use mailparse::{addrparse_header, MailHeaderMap}; use crate::chat::ChatId; use crate::chat::{ - add_contact_to_chat, create_group_chat, remove_contact_from_chat, send_text_msg, + self, add_contact_to_chat, create_group_chat, remove_contact_from_chat, send_text_msg, ProtectionStatus, }; use crate::chatlist::Chatlist; @@ -1466,9 +1467,7 @@ mod tests { use crate::mimeparser::MimeMessage; use crate::test_utils::{get_chat_msg, TestContext}; - use async_std::fs::File; - use mailparse::{addrparse_header, MailHeaderMap}; - + use super::*; #[test] fn test_render_email_address() { let display_name = "รค space"; diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 9fd126586..9a2996631 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -12,7 +12,7 @@ use once_cell::sync::Lazy; use crate::aheader::Aheader; use crate::blob::BlobObject; -use crate::constants::{Viewtype, DC_DESIRED_TEXT_LEN, DC_ELLIPSIS}; +use crate::constants::{DC_DESIRED_TEXT_LEN, DC_ELLIPSIS}; use crate::contact::{addr_normalize, ContactId}; use crate::context::Context; use crate::dc_tools::{dc_get_filemeta, dc_truncate, parse_receive_headers}; @@ -23,7 +23,7 @@ use crate::format_flowed::unformat_flowed; use crate::headerdef::{HeaderDef, HeaderDefMap}; use crate::key::Fingerprint; use crate::location; -use crate::message; +use crate::message::{self, Viewtype}; use crate::param::{Param, Params}; use crate::peerstate::Peerstate; use crate::simplify::simplify; diff --git a/src/quota.rs b/src/quota.rs index 133db6595..1820e6e2f 100644 --- a/src/quota.rs +++ b/src/quota.rs @@ -6,13 +6,12 @@ use std::collections::BTreeMap; use crate::chat::add_device_msg_with_importance; use crate::config::Config; -use crate::constants::Viewtype; use crate::context::Context; use crate::dc_tools::time; use crate::imap::scan_folders::get_watched_folders; use crate::imap::Imap; use crate::job::{Action, Status}; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::param::Params; use crate::{job, stock_str, EventType}; diff --git a/src/securejoin.rs b/src/securejoin.rs index 0c2e821a0..9e385fe95 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -8,7 +8,7 @@ use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC}; use crate::aheader::EncryptPreference; use crate::chat::{self, Chat, ChatId, ChatIdBlocked}; use crate::config::Config; -use crate::constants::{Blocked, Viewtype, DC_CONTACT_ID_LAST_SPECIAL}; +use crate::constants::{Blocked, DC_CONTACT_ID_LAST_SPECIAL}; use crate::contact::{Contact, ContactId, Origin, VerifiedStatus}; use crate::context::Context; use crate::dc_tools::time; @@ -16,7 +16,7 @@ use crate::e2ee::ensure_secret_key_exists; use crate::events::EventType; use crate::headerdef::HeaderDef; use crate::key::{DcKey, Fingerprint, SignedPublicKey}; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::mimeparser::{MimeMessage, SystemMessage}; use crate::param::Param; use crate::peerstate::{Peerstate, PeerstateKeyType, PeerstateVerifiedStatus, ToSave}; diff --git a/src/securejoin/bobstate.rs b/src/securejoin/bobstate.rs index fe0038f8d..66b9bc58b 100644 --- a/src/securejoin/bobstate.rs +++ b/src/securejoin/bobstate.rs @@ -11,13 +11,12 @@ use anyhow::{Error, Result}; use rusqlite::Connection; use crate::chat::{self, ChatId}; -use crate::constants::Viewtype; use crate::contact::{Contact, Origin}; use crate::context::Context; use crate::events::EventType; use crate::headerdef::HeaderDef; use crate::key::{DcKey, SignedPublicKey}; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::mimeparser::{MimeMessage, SystemMessage}; use crate::param::Param; use crate::sql::Sql; diff --git a/src/sql.rs b/src/sql.rs index ac7bbe2e2..89375b452 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -15,11 +15,11 @@ use rusqlite::{config::DbConfig, Connection, OpenFlags}; use crate::blob::BlobObject; use crate::chat::{add_device_msg, update_device_icon, update_saved_messages_icon}; use crate::config::Config; -use crate::constants::{Viewtype, DC_CHAT_ID_TRASH}; +use crate::constants::DC_CHAT_ID_TRASH; use crate::context::Context; use crate::dc_tools::{dc_delete_file, time}; use crate::ephemeral::start_ephemeral_timers; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::param::{Param, Params}; use crate::peerstate::{deduplicate_peerstates, Peerstate}; use crate::stock_str; diff --git a/src/stock_str.rs b/src/stock_str.rs index c6299b377..e3e3a421f 100644 --- a/src/stock_str.rs +++ b/src/stock_str.rs @@ -10,11 +10,11 @@ use strum_macros::EnumProperty; use crate::blob::BlobObject; use crate::chat::{self, Chat, ChatId, ProtectionStatus}; use crate::config::Config; -use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; +use crate::constants::DC_CONTACT_ID_SELF; use crate::contact::{Contact, ContactId, Origin}; use crate::context::Context; use crate::dc_tools::dc_timestamp_to_str; -use crate::message::Message; +use crate::message::{Message, Viewtype}; use crate::param::Param; use humansize::{file_size_opts, FileSize}; diff --git a/src/summary.rs b/src/summary.rs index d26c89377..7801b108e 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -1,11 +1,11 @@ //! # Message summary for chatlist. use crate::chat::Chat; -use crate::constants::{Chattype, Viewtype, DC_CONTACT_ID_SELF}; +use crate::constants::{Chattype, DC_CONTACT_ID_SELF}; use crate::contact::Contact; use crate::context::Context; use crate::dc_tools::dc_truncate; -use crate::message::{Message, MessageState}; +use crate::message::{Message, MessageState, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::stock_str; diff --git a/src/sync.rs b/src/sync.rs index 3054f4a70..fbecedfb9 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -2,10 +2,10 @@ use crate::chat::{Chat, ChatId}; use crate::config::Config; -use crate::constants::{Blocked, Viewtype, DC_CONTACT_ID_SELF}; +use crate::constants::{Blocked, DC_CONTACT_ID_SELF}; use crate::context::Context; use crate::dc_tools::time; -use crate::message::{Message, MsgId}; +use crate::message::{Message, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::sync::SyncData::{AddQrToken, DeleteQrToken}; diff --git a/src/test_utils.rs b/src/test_utils.rs index feeaf6be0..029bb9c83 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -22,14 +22,14 @@ use crate::chat::{self, Chat, ChatId}; use crate::chatlist::Chatlist; use crate::config::Config; use crate::constants::Chattype; -use crate::constants::{Viewtype, DC_CONTACT_ID_SELF, DC_MSG_ID_DAYMARKER, DC_MSG_ID_MARKER1}; +use crate::constants::{DC_CONTACT_ID_SELF, DC_MSG_ID_DAYMARKER, DC_MSG_ID_MARKER1}; use crate::contact::{Contact, Modifier, Origin}; use crate::context::Context; use crate::dc_receive_imf::dc_receive_imf; use crate::dc_tools::EmailAddress; use crate::events::{Event, EventType}; use crate::key::{self, DcKey, KeyPair, KeyPairUse}; -use crate::message::{update_msg_state, Message, MessageState, MsgId}; +use crate::message::{update_msg_state, Message, MessageState, MsgId, Viewtype}; use crate::mimeparser::MimeMessage; #[allow(non_upper_case_globals)] diff --git a/src/webxdc.rs b/src/webxdc.rs index fbf15154c..c3d1920be 100644 --- a/src/webxdc.rs +++ b/src/webxdc.rs @@ -1,10 +1,9 @@ //! # Handle webxdc messages. use crate::chat::Chat; -use crate::constants::Viewtype; use crate::context::Context; use crate::dc_tools::{dc_create_smeared_timestamp, dc_open_file_std}; -use crate::message::{Message, MessageState, MsgId}; +use crate::message::{Message, MessageState, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::{chat, EventType};