refactor: turn DC_MSG_ID_* into MsgId::* associated constants

This commit is contained in:
link2xt
2026-08-22 20:21:20 +00:00
committed by l
parent fe25e93d6c
commit 6c47e86380
6 changed files with 18 additions and 29 deletions
+1 -2
View File
@@ -1,5 +1,4 @@
use crate::chat::ChatItem; use crate::chat::ChatItem;
use crate::constants::DC_MSG_ID_DAYMARKER;
use crate::contact::ContactId; use crate::contact::ContactId;
use crate::location::Location; use crate::location::Location;
use crate::message::MsgId; use crate::message::MsgId;
@@ -21,7 +20,7 @@ impl dc_array_t {
Self::ContactIds(array) => array[index].to_u32(), Self::ContactIds(array) => array[index].to_u32(),
Self::Chat(array) => match array[index] { Self::Chat(array) => match array[index] {
ChatItem::Message { msg_id } => msg_id.to_u32(), ChatItem::Message { msg_id } => msg_id.to_u32(),
ChatItem::DayMarker { .. } => DC_MSG_ID_DAYMARKER, ChatItem::DayMarker { .. } => MsgId::DAYMARKER.to_u32(),
}, },
Self::Locations(array) => array[index].location_id, Self::Locations(array) => array[index].location_id,
Self::Uint(array) => array[index], Self::Uint(array) => array[index],
+2 -3
View File
@@ -23,7 +23,6 @@ use std::time::{Duration, SystemTime};
use anyhow::Context as _; use anyhow::Context as _;
use deltachat::chat::{ChatId, ChatVisibility, MessageListOptions, MuteDuration}; use deltachat::chat::{ChatId, ChatVisibility, MessageListOptions, MuteDuration};
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
use deltachat::contact::{Contact, ContactId, Origin}; use deltachat::contact::{Contact, ContactId, Origin};
use deltachat::context::{Context, ContextBuilder}; use deltachat::context::{Context, ContextBuilder};
use deltachat::ephemeral::Timer as EphemeralTimer; use deltachat::ephemeral::Timer as EphemeralTimer;
@@ -2038,7 +2037,7 @@ pub unsafe extern "C" fn dc_get_msg(context: *mut dc_context_t, msg_id: u32) ->
{ {
Ok(msg) => msg, Ok(msg) => msg,
Err(_) => { Err(_) => {
if msg_id <= constants::DC_MSG_ID_LAST_SPECIAL { if MsgId::new(msg_id).is_special() {
// C-core API returns empty messages, do the same // C-core API returns empty messages, do the same
message::Message::new(Viewtype::default()) message::Message::new(Viewtype::default())
} else { } else {
@@ -4407,7 +4406,7 @@ fn convert_and_prune_message_ids(msg_ids: *const u32, msg_cnt: libc::c_int) -> V
let ids = unsafe { std::slice::from_raw_parts(msg_ids, msg_cnt as usize) }; let ids = unsafe { std::slice::from_raw_parts(msg_ids, msg_cnt as usize) };
let msg_ids: Vec<MsgId> = ids let msg_ids: Vec<MsgId> = ids
.iter() .iter()
.filter(|id| **id > DC_MSG_ID_LAST_SPECIAL) .filter(|id| **id > MsgId::LAST_SPECIAL.to_u32())
.map(|id| MsgId::new(*id)) .map(|id| MsgId::new(*id))
.collect(); .collect();
+2 -3
View File
@@ -17,7 +17,6 @@ use deltachat::chat::{
}; };
use deltachat::chatlist::Chatlist; use deltachat::chatlist::Chatlist;
use deltachat::config::{Config, get_all_ui_config_keys}; use deltachat::config::{Config, get_all_ui_config_keys};
use deltachat::constants::DC_MSG_ID_DAYMARKER;
use deltachat::contact::{Contact, ContactId, Origin, may_be_valid_addr}; use deltachat::contact::{Contact, ContactId, Origin, may_be_valid_addr};
use deltachat::context::get_info; use deltachat::context::get_info;
use deltachat::ephemeral::Timer; use deltachat::ephemeral::Timer;
@@ -1382,7 +1381,7 @@ impl CommandApi {
/// ///
/// * chat_id The chat ID of which the messages IDs should be queried. /// * chat_id The chat ID of which the messages IDs should be queried.
/// * _info_only: Deprecated, pass `false` here. /// * _info_only: Deprecated, pass `false` here.
/// * `add_daymarker` - If `true`, add day markers as `DC_MSG_ID_DAYMARKER` to the result, /// * `add_daymarker` - If `true`, add day markers as `MsgId::DAYMARKER` to the result,
/// e.g. [1234, 1237, 9, 1239]. The day marker timestamp is the midnight one for the /// e.g. [1234, 1237, 9, 1239]. The day marker timestamp is the midnight one for the
/// corresponding (following) day in the local timezone. /// corresponding (following) day in the local timezone.
async fn get_message_ids( async fn get_message_ids(
@@ -1404,7 +1403,7 @@ impl CommandApi {
.map(|chat_item| -> u32 { .map(|chat_item| -> u32 {
match chat_item { match chat_item {
deltachat::chat::ChatItem::Message { msg_id } => msg_id.to_u32(), deltachat::chat::ChatItem::Message { msg_id } => msg_id.to_u32(),
deltachat::chat::ChatItem::DayMarker { .. } => DC_MSG_ID_DAYMARKER, deltachat::chat::ChatItem::DayMarker { .. } => MsgId::DAYMARKER.to_u32(),
} }
}) })
.collect()) .collect())
+2 -2
View File
@@ -228,7 +228,7 @@ async fn log_msg(context: &Context, prefix: impl AsRef<str>, msg: &Message) {
async fn log_msglist(context: &Context, msglist: &[MsgId]) -> Result<()> { async fn log_msglist(context: &Context, msglist: &[MsgId]) -> Result<()> {
let mut lines_out = 0; let mut lines_out = 0;
for &msg_id in msglist { for &msg_id in msglist {
if msg_id == MsgId::new(DC_MSG_ID_DAYMARKER) { if msg_id == MsgId::DAYMARKER {
println!( println!(
"--------------------------------------------------------------------------------" "--------------------------------------------------------------------------------"
); );
@@ -630,7 +630,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
.into_iter() .into_iter()
.map(|x| match x { .map(|x| match x {
ChatItem::Message { msg_id } => msg_id, ChatItem::Message { msg_id } => msg_id,
ChatItem::DayMarker { .. } => MsgId::new(DC_MSG_ID_DAYMARKER), ChatItem::DayMarker { .. } => MsgId::DAYMARKER,
}) })
.collect(); .collect();
-3
View File
@@ -130,9 +130,6 @@ pub enum Chattype {
InBroadcast = 165, InBroadcast = 165,
} }
pub const DC_MSG_ID_DAYMARKER: u32 = 9;
pub const DC_MSG_ID_LAST_SPECIAL: u32 = 9;
/// String that indicates that something is left out or truncated. /// String that indicates that something is left out or truncated.
pub(crate) const DC_ELLIPSIS: &str = "[...]"; pub(crate) const DC_ELLIPSIS: &str = "[...]";
// how many lines desktop can display when fullscreen (fullscreen at zoomlevel 1x) // how many lines desktop can display when fullscreen (fullscreen at zoomlevel 1x)
+11 -16
View File
@@ -4,7 +4,7 @@ use std::collections::BTreeSet;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str; use std::str;
use anyhow::{Context as _, Result, ensure, format_err}; use anyhow::{Context as _, Result, ensure};
use deltachat_contact_tools::{VcardContact, parse_vcard}; use deltachat_contact_tools::{VcardContact, parse_vcard};
use deltachat_derive::{FromSql, ToSql}; use deltachat_derive::{FromSql, ToSql};
use humansize::BINARY; use humansize::BINARY;
@@ -17,7 +17,7 @@ use crate::blob::BlobObject;
use crate::chat::{Chat, ChatId, ChatIdBlocked, ChatVisibility, send_msg}; use crate::chat::{Chat, ChatId, ChatIdBlocked, ChatVisibility, send_msg};
use crate::chatlist_events; use crate::chatlist_events;
use crate::config::Config; use crate::config::Config;
use crate::constants::{Blocked, Chattype, DC_MSG_ID_LAST_SPECIAL}; use crate::constants::{Blocked, Chattype};
use crate::contact::{self, Contact, ContactId}; use crate::contact::{self, Contact, ContactId};
use crate::context::Context; use crate::context::Context;
use crate::debug_logging::set_debug_logging_xdc; use crate::debug_logging::set_debug_logging_xdc;
@@ -49,13 +49,18 @@ use crate::tools::{
pub struct MsgId(u32); pub struct MsgId(u32);
impl MsgId { impl MsgId {
/// Markers added before each day in a local timezone.
pub const DAYMARKER: MsgId = MsgId::new(9);
/// Largest reserved message ID.
pub const LAST_SPECIAL: MsgId = MsgId::new(9);
/// Create a new [MsgId]. /// Create a new [MsgId].
pub fn new(id: u32) -> MsgId { pub const fn new(id: u32) -> MsgId {
MsgId(id) MsgId(id)
} }
/// Create a new unset [MsgId]. /// Create a new unset [MsgId].
pub fn new_unset() -> MsgId { pub const fn new_unset() -> MsgId {
MsgId(0) MsgId(0)
} }
@@ -63,7 +68,7 @@ impl MsgId {
/// ///
/// This kind of message ID can not be used for real messages. /// This kind of message ID can not be used for real messages.
pub fn is_special(self) -> bool { pub fn is_special(self) -> bool {
self.0 <= DC_MSG_ID_LAST_SPECIAL (0..=Self::LAST_SPECIAL.0).contains(&self.0)
} }
/// Whether the message ID is unset. /// Whether the message ID is unset.
@@ -355,18 +360,8 @@ impl std::fmt::Display for MsgId {
/// Allow converting [MsgId] to an SQLite type. /// Allow converting [MsgId] to an SQLite type.
/// ///
/// This allows you to directly store [MsgId] into the database. /// This allows you to directly store [MsgId] into the database.
///
/// # Errors
///
/// This **does** ensure that no special message IDs are written into
/// the database and the conversion will fail if this is not the case.
impl rusqlite::types::ToSql for MsgId { impl rusqlite::types::ToSql for MsgId {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> { fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
if self.0 <= DC_MSG_ID_LAST_SPECIAL {
return Err(rusqlite::Error::ToSqlConversionFailure(
format_err!("Invalid MsgId {}", self.0).into(),
));
}
let val = rusqlite::types::Value::Integer(i64::from(self.0)); let val = rusqlite::types::Value::Integer(i64::from(self.0));
let out = rusqlite::types::ToSqlOutput::Owned(val); let out = rusqlite::types::ToSqlOutput::Owned(val);
Ok(out) Ok(out)
@@ -2106,7 +2101,7 @@ pub async fn estimate_deletion_cnt(
AND chat_id != ?3 AND chat_id != ?3
AND chat_id != ?4 AND hidden = 0;", AND chat_id != ?4 AND hidden = 0;",
( (
DC_MSG_ID_LAST_SPECIAL, MsgId::LAST_SPECIAL,
threshold_timestamp, threshold_timestamp,
self_chat_id, self_chat_id,
ChatId::TRASH, ChatId::TRASH,