mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
refactor: Move calc_{protection_msg_,}sort_timestamp() to impl ChatId
This commit is contained in:
64
src/chat.rs
64
src/chat.rs
@@ -36,7 +36,7 @@ use crate::mimefactory::MimeFactory;
|
|||||||
use crate::mimeparser::SystemMessage;
|
use crate::mimeparser::SystemMessage;
|
||||||
use crate::param::{Param, Params};
|
use crate::param::{Param, Params};
|
||||||
use crate::peerstate::Peerstate;
|
use crate::peerstate::Peerstate;
|
||||||
use crate::receive_imf::{calc_sort_timestamp, ReceivedMsg};
|
use crate::receive_imf::ReceivedMsg;
|
||||||
use crate::smtp::send_msg_to_smtp;
|
use crate::smtp::send_msg_to_smtp;
|
||||||
use crate::sql;
|
use crate::sql;
|
||||||
use crate::stock_str;
|
use crate::stock_str;
|
||||||
@@ -611,7 +611,9 @@ impl ChatId {
|
|||||||
contact_id: Option<ContactId>,
|
contact_id: Option<ContactId>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let sort_to_bottom = true;
|
let sort_to_bottom = true;
|
||||||
let ts = calc_sort_timestamp(context, timestamp_sent, self, sort_to_bottom, false).await?;
|
let ts = self
|
||||||
|
.calc_sort_timestamp(context, timestamp_sent, sort_to_bottom, false)
|
||||||
|
.await?;
|
||||||
self.set_protection_for_timestamp_sort(context, protect, ts, contact_id)
|
self.set_protection_for_timestamp_sort(context, protect, ts, contact_id)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
@@ -1335,6 +1337,64 @@ impl ChatId {
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
Ok(protection_status)
|
Ok(protection_status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the sort timestamp for a new message in the chat.
|
||||||
|
///
|
||||||
|
/// `message_timestamp` should be either the message "sent" timestamp or a timestamp of the
|
||||||
|
/// corresponding event in case of a system message (usually the current system time).
|
||||||
|
/// `always_sort_to_bottom` makes this ajust the returned timestamp up so that the message goes
|
||||||
|
/// to the chat bottom.
|
||||||
|
/// `incoming` -- whether the message is incoming.
|
||||||
|
pub(crate) async fn calc_sort_timestamp(
|
||||||
|
self,
|
||||||
|
context: &Context,
|
||||||
|
message_timestamp: i64,
|
||||||
|
always_sort_to_bottom: bool,
|
||||||
|
incoming: bool,
|
||||||
|
) -> Result<i64> {
|
||||||
|
let mut sort_timestamp = cmp::min(message_timestamp, smeared_time(context));
|
||||||
|
|
||||||
|
let last_msg_time: Option<i64> = if always_sort_to_bottom {
|
||||||
|
// get newest message for this chat
|
||||||
|
|
||||||
|
// Let hidden messages also be ordered with protection messages because hidden messages
|
||||||
|
// also can be or not be verified, so let's preserve this information -- even it's not
|
||||||
|
// used currently, it can be useful in the future versions.
|
||||||
|
context
|
||||||
|
.sql
|
||||||
|
.query_get_value(
|
||||||
|
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND state!=?",
|
||||||
|
(self, MessageState::OutDraft),
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else if incoming {
|
||||||
|
// get newest non fresh message for this chat.
|
||||||
|
|
||||||
|
// If a user hasn't been online for some time, the Inbox is fetched first and then the
|
||||||
|
// Sentbox. In order for Inbox and Sent messages to be allowed to mingle, outgoing
|
||||||
|
// messages are purely sorted by their sent timestamp. NB: The Inbox must be fetched
|
||||||
|
// first otherwise Inbox messages would be always below old Sentbox messages. We could
|
||||||
|
// take in the query below only incoming messages, but then new incoming messages would
|
||||||
|
// mingle with just sent outgoing ones and apear somewhere in the middle of the chat.
|
||||||
|
context
|
||||||
|
.sql
|
||||||
|
.query_get_value(
|
||||||
|
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0 AND state>?",
|
||||||
|
(self, MessageState::InFresh),
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(last_msg_time) = last_msg_time {
|
||||||
|
if last_msg_time > sort_timestamp {
|
||||||
|
sort_timestamp = last_msg_time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(sort_timestamp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for ChatId {
|
impl std::fmt::Display for ChatId {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
//! Internet Message Format reception pipeline.
|
//! Internet Message Format reception pipeline.
|
||||||
|
|
||||||
use std::cmp::min;
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
@@ -39,9 +38,7 @@ use crate::simplify;
|
|||||||
use crate::sql;
|
use crate::sql;
|
||||||
use crate::stock_str;
|
use crate::stock_str;
|
||||||
use crate::sync::Sync::*;
|
use crate::sync::Sync::*;
|
||||||
use crate::tools::{
|
use crate::tools::{buf_compress, extract_grpid_from_rfc724_mid, strip_rtlo_characters};
|
||||||
buf_compress, extract_grpid_from_rfc724_mid, smeared_time, strip_rtlo_characters,
|
|
||||||
};
|
|
||||||
use crate::{contact, imap};
|
use crate::{contact, imap};
|
||||||
|
|
||||||
/// This is the struct that is returned after receiving one email (aka MIME message).
|
/// This is the struct that is returned after receiving one email (aka MIME message).
|
||||||
@@ -1017,14 +1014,15 @@ async fn add_parts(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let in_fresh = state == MessageState::InFresh;
|
let in_fresh = state == MessageState::InFresh;
|
||||||
let sort_timestamp = calc_sort_timestamp(
|
let sort_to_bottom = false;
|
||||||
context,
|
let sort_timestamp = chat_id
|
||||||
mime_parser.timestamp_sent,
|
.calc_sort_timestamp(
|
||||||
chat_id,
|
context,
|
||||||
false,
|
mime_parser.timestamp_sent,
|
||||||
incoming,
|
sort_to_bottom,
|
||||||
)
|
incoming,
|
||||||
.await?;
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Apply ephemeral timer changes to the chat.
|
// Apply ephemeral timer changes to the chat.
|
||||||
//
|
//
|
||||||
@@ -1487,53 +1485,6 @@ async fn save_locations(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn calc_sort_timestamp(
|
|
||||||
context: &Context,
|
|
||||||
message_timestamp: i64,
|
|
||||||
chat_id: ChatId,
|
|
||||||
always_sort_to_bottom: bool,
|
|
||||||
incoming: bool,
|
|
||||||
) -> Result<i64> {
|
|
||||||
let mut sort_timestamp = min(message_timestamp, smeared_time(context));
|
|
||||||
|
|
||||||
let last_msg_time: Option<i64> = if always_sort_to_bottom {
|
|
||||||
// get newest message for this chat
|
|
||||||
context
|
|
||||||
.sql
|
|
||||||
.query_get_value(
|
|
||||||
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND state!=?",
|
|
||||||
(chat_id, MessageState::OutDraft),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if incoming {
|
|
||||||
// get newest non fresh message for this chat.
|
|
||||||
|
|
||||||
// If a user hasn't been online for some time, the Inbox is fetched first and then the
|
|
||||||
// Sentbox. In order for Inbox and Sent messages to be allowed to mingle, outgoing messages
|
|
||||||
// are purely sorted by their sent timestamp. NB: The Inbox must be fetched first otherwise
|
|
||||||
// Inbox messages would be always below old Sentbox messages. We could take in the query
|
|
||||||
// below only incoming messages, but then new incoming messages would mingle with just sent
|
|
||||||
// outgoing ones and apear somewhere in the middle of the chat.
|
|
||||||
context
|
|
||||||
.sql
|
|
||||||
.query_get_value(
|
|
||||||
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0 AND state>?",
|
|
||||||
(chat_id, MessageState::InFresh),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(last_msg_time) = last_msg_time {
|
|
||||||
if last_msg_time > sort_timestamp {
|
|
||||||
sort_timestamp = last_msg_time;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(sort_timestamp)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn lookup_chat_by_reply(
|
async fn lookup_chat_by_reply(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
mime_parser: &MimeMessage,
|
mime_parser: &MimeMessage,
|
||||||
|
|||||||
Reference in New Issue
Block a user