Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions deltachat-ffi/src/dc_array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::chat::ChatItem;
use crate::constants::DC_MSG_ID_DAYMARKER;
use crate::contact::ContactId;
use crate::location::Location;
use crate::message::MsgId;
Expand All @@ -21,7 +20,7 @@ impl dc_array_t {
Self::ContactIds(array) => array[index].to_u32(),
Self::Chat(array) => match array[index] {
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::Uint(array) => array[index],
Expand Down
14 changes: 6 additions & 8 deletions deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::time::{Duration, SystemTime};

use anyhow::Context as _;
use deltachat::chat::{ChatId, ChatVisibility, MessageListOptions, MuteDuration};
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
use deltachat::contact::{Contact, ContactId, Origin};
use deltachat::context::{Context, ContextBuilder};
use deltachat::ephemeral::Timer as EphemeralTimer;
Expand Down Expand Up @@ -1774,8 +1773,7 @@ pub unsafe extern "C" fn dc_set_chat_name(
chat_id: u32,
name: *const libc::c_char,
) -> libc::c_int {
if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() || name.is_null()
{
if context.is_null() || chat_id <= ChatId::LAST_SPECIAL.to_u32() || name.is_null() {
eprintln!("ignoring careless call to dc_set_chat_name()");
return 0;
}
Expand All @@ -1796,7 +1794,7 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
chat_id: u32,
image: *const libc::c_char,
) -> libc::c_int {
if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() {
if context.is_null() || chat_id <= ChatId::LAST_SPECIAL.to_u32() {
eprintln!("ignoring careless call to dc_set_chat_profile_image()");
return 0;
}
Expand Down Expand Up @@ -1958,7 +1956,7 @@ pub unsafe extern "C" fn dc_forward_msgs(
if context.is_null()
|| msg_ids.is_null()
|| msg_cnt <= 0
|| chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32()
|| chat_id <= ChatId::LAST_SPECIAL.to_u32()
{
eprintln!("ignoring careless call to dc_forward_msgs()");
return;
Expand Down Expand Up @@ -2039,7 +2037,7 @@ pub unsafe extern "C" fn dc_get_msg(context: *mut dc_context_t, msg_id: u32) ->
{
Ok(msg) => msg,
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
message::Message::new(Viewtype::default())
} else {
Expand Down Expand Up @@ -2462,7 +2460,7 @@ pub unsafe extern "C" fn dc_send_locations_to_chat(
chat_id: u32,
seconds: libc::c_int,
) {
if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() || seconds < 0 {
if context.is_null() || chat_id <= ChatId::LAST_SPECIAL.to_u32() || seconds < 0 {
eprintln!("ignoring careless call to dc_send_locations_to_chat()");
return;
}
Expand Down Expand Up @@ -4408,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 msg_ids: Vec<MsgId> = ids
.iter()
.filter(|id| **id > DC_MSG_ID_LAST_SPECIAL)
.filter(|id| **id > MsgId::LAST_SPECIAL.to_u32())
.map(|id| MsgId::new(*id))
.collect();

Expand Down
5 changes: 2 additions & 3 deletions deltachat-jsonrpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use deltachat::chat::{
};
use deltachat::chatlist::Chatlist;
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::context::get_info;
use deltachat::ephemeral::Timer;
Expand Down Expand Up @@ -1382,7 +1381,7 @@ impl CommandApi {
///
/// * chat_id The chat ID of which the messages IDs should be queried.
/// * _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
/// corresponding (following) day in the local timezone.
async fn get_message_ids(
Expand All @@ -1404,7 +1403,7 @@ impl CommandApi {
.map(|chat_item| -> u32 {
match chat_item {
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())
Expand Down
4 changes: 2 additions & 2 deletions deltachat-repl/src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
let mut lines_out = 0;
for &msg_id in msglist {
if msg_id == MsgId::new(DC_MSG_ID_DAYMARKER) {
if msg_id == MsgId::DAYMARKER {
println!(
"--------------------------------------------------------------------------------"
);
Expand Down Expand Up @@ -630,7 +630,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
.into_iter()
.map(|x| match x {
ChatItem::Message { msg_id } => msg_id,
ChatItem::DayMarker { .. } => MsgId::new(DC_MSG_ID_DAYMARKER),
ChatItem::DayMarker { .. } => MsgId::DAYMARKER,
})
.collect();

Expand Down
3 changes: 1 addition & 2 deletions src/calls/calls_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate::chat::forward_msgs;
use crate::config::Config;
use crate::constants::DC_CHAT_ID_TRASH;
use crate::message::MessageState;
use crate::receive_imf::receive_imf;
use crate::test_utils;
Expand Down Expand Up @@ -678,7 +677,7 @@ async fn test_end_text_call() -> Result<()> {
.await?
.unwrap();
assert_eq!(received2.msg_ids.len(), 1);
assert_eq!(received2.chat_id, DC_CHAT_ID_TRASH);
assert_eq!(received2.chat_id, ChatId::TRASH);
alice.assert_warn("does not refer to a call message").await;

Ok(())
Expand Down
41 changes: 24 additions & 17 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use crate::chatlist_events;
use crate::color::str_to_color;
use crate::config::Config;
use crate::constants::{
self, Blocked, Chattype, DC_CHAT_ID_ALLDONE_HINT, DC_CHAT_ID_ARCHIVED_LINK,
DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH, DC_RESEND_USER_AVATAR_DAYS, EDITED_PREFIX,
TIMESTAMP_SENT_TOLERANCE,
self, Blocked, Chattype, DC_RESEND_USER_AVATAR_DAYS, EDITED_PREFIX, TIMESTAMP_SENT_TOLERANCE,
};
use crate::contact::{self, Contact, ContactId, Origin};
use crate::context::Context;
Expand Down Expand Up @@ -135,6 +133,15 @@ impl fmt::Display for CantSendReason {
pub struct ChatId(u32);

impl ChatId {
/// messages that should be deleted get this chat_id; the messages are deleted from the working thread later then. This is also needed as rfc724_mid should be preset as long as the message is not deleted on the server (otherwise it is downloaded again)
pub const TRASH: ChatId = ChatId::new(3);
/// only an indicator in a chatlist
pub const ARCHIVED_LINK: ChatId = ChatId::new(6);
/// only an indicator in a chatlist
pub const ALLDONE_HINT: ChatId = ChatId::new(7);
/// larger chat IDs are "real" chats, their messages are "real" messages.
pub const LAST_SPECIAL: ChatId = ChatId::new(9);

/// Create a new [ChatId].
pub const fn new(id: u32) -> ChatId {
ChatId(id)
Expand All @@ -151,7 +158,7 @@ impl ChatId {
///
/// This kind of chat ID can not be used for real chats.
pub fn is_special(self) -> bool {
(0..=DC_CHAT_ID_LAST_SPECIAL.0).contains(&self.0)
(0..=Self::LAST_SPECIAL.0).contains(&self.0)
}

/// Chat ID for messages which need to be deleted.
Expand All @@ -161,7 +168,7 @@ impl ChatId {
/// as they are not deleted on the server so that their rfc724_mid
/// remains known and downloading them again can be avoided.
pub fn is_trash(self) -> bool {
self == DC_CHAT_ID_TRASH
self == Self::TRASH
}

/// Chat ID signifying there are **any** number of archived chats.
Expand All @@ -171,7 +178,7 @@ impl ChatId {
///
/// [`Chatlist`]: crate::chatlist::Chatlist
pub fn is_archived_link(self) -> bool {
self == DC_CHAT_ID_ARCHIVED_LINK
self == Self::ARCHIVED_LINK
}

/// Virtual chat ID signalling there are **only** archived chats.
Expand All @@ -183,12 +190,12 @@ impl ChatId {
/// [`DC_GCL_ADD_ALLDONE_HINT`]: crate::constants::DC_GCL_ADD_ALLDONE_HINT
/// [`Chatlist`]: crate::chatlist::Chatlist
pub fn is_alldone_hint(self) -> bool {
self == DC_CHAT_ID_ALLDONE_HINT
self == Self::ALLDONE_HINT
}

/// Returns [`ChatId`] of a chat that `msg` belongs to.
pub(crate) fn lookup_by_message(msg: &Message) -> Option<Self> {
if msg.chat_id == DC_CHAT_ID_TRASH {
if msg.chat_id == Self::TRASH {
return None;
}
if msg.download_state == DownloadState::Undecipherable {
Expand Down Expand Up @@ -578,7 +585,7 @@ impl ChatId {
/// `msg_state` is the state of the message. Matters only for incoming messages currently. For
/// multiple outgoing messages the function may be called once with MessageState::Undefined.
/// Sending an appropriate event is up to the caller.
/// Also emits DC_EVENT_MSGS_CHANGED for DC_CHAT_ID_ARCHIVED_LINK when the number of archived
/// Also emits DC_EVENT_MSGS_CHANGED for ChatId::ARCHIVED_LINK when the number of archived
/// chats with unread messages increases (which is possible if the chat is muted).
pub async fn unarchive_if_not_muted(
self,
Expand Down Expand Up @@ -614,7 +621,7 @@ impl ChatId {
.await?;
if unread_cnt == 1 {
// Added the first unread message in the chat.
context.emit_msgs_changed_without_msg_id(DC_CHAT_ID_ARCHIVED_LINK);
context.emit_msgs_changed_without_msg_id(ChatId::ARCHIVED_LINK);
}
return Ok(());
}
Expand Down Expand Up @@ -680,7 +687,7 @@ impl ChatId {
INSERT OR REPLACE INTO msgs (id, rfc724_mid, pre_rfc724_mid, timestamp, chat_id, deleted)
SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=?
",
(DC_CHAT_ID_TRASH, self),
(ChatId::TRASH, self),
)?;
transaction.execute("DELETE FROM chats_contacts WHERE chat_id=?", (self,))?;
transaction.execute("DELETE FROM chats WHERE id=?", (self,))?;
Expand Down Expand Up @@ -1011,7 +1018,7 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=?
AND y.chat_id<>x.chat_id
AND y.chat_id>?
GROUP BY y.chat_id",
(self, DC_CHAT_ID_LAST_SPECIAL),
(self, ChatId::LAST_SPECIAL),
|row| {
let chat_id: ChatId = row.get(0)?;
let intersection: f64 = row.get(1)?;
Expand All @@ -1029,7 +1036,7 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=?
WHERE contact_id > ? AND chat_id > ?
AND add_timestamp >= remove_timestamp
GROUP BY chat_id",
(ContactId::LAST_SPECIAL, DC_CHAT_ID_LAST_SPECIAL),
(ContactId::LAST_SPECIAL, ChatId::LAST_SPECIAL),
|row| {
let chat_id: ChatId = row.get(0)?;
let size: f64 = row.get(1)?;
Expand Down Expand Up @@ -2369,7 +2376,7 @@ impl ChatIdBlocked {
INNER JOIN chats_contacts j
ON c.id=j.chat_id
WHERE c.type=100 -- 100 = Chattype::Single
AND c.id>9 -- 9 = DC_CHAT_ID_LAST_SPECIAL
AND c.id>9 -- 9 = ChatId::LAST_SPECIAL
AND j.contact_id=?;",
(contact_id,),
|row| {
Expand Down Expand Up @@ -3489,7 +3496,7 @@ pub async fn get_chat_media(
(
chat_id.is_none(),
chat_id.unwrap_or_else(|| ChatId::new(0)),
DC_CHAT_ID_TRASH,
ChatId::TRASH,
Viewtype::Webxdc,
),
|row| {
Expand All @@ -3512,7 +3519,7 @@ pub async fn get_chat_media(
(
chat_id.is_none(),
chat_id.unwrap_or_else(|| ChatId::new(0)),
DC_CHAT_ID_TRASH,
ChatId::TRASH,
msg_type,
if msg_type2 != Viewtype::Unknown {
msg_type2
Expand Down Expand Up @@ -5384,7 +5391,7 @@ impl Context {
/// a noticed chat is archived. Emitting events should be cheap, a false-positive `MsgsChanged`
/// is ok.
pub(crate) fn on_archived_chats_maybe_noticed(&self) {
self.emit_msgs_changed_without_msg_id(DC_CHAT_ID_ARCHIVED_LINK);
self.emit_msgs_changed_without_msg_id(ChatId::ARCHIVED_LINK);
}
}

Expand Down
Loading