Skip to content

Commit 62c1237

Browse files
committed
refactor: Move calc_{protection_msg_,}sort_timestamp() to impl ChatId
1 parent 8d41d02 commit 62c1237

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

src/chat.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::mimefactory::MimeFactory;
3636
use crate::mimeparser::SystemMessage;
3737
use crate::param::{Param, Params};
3838
use crate::peerstate::Peerstate;
39-
use crate::receive_imf::{calc_sort_timestamp, ReceivedMsg};
39+
use crate::receive_imf::ReceivedMsg;
4040
use crate::smtp::send_msg_to_smtp;
4141
use crate::sql;
4242
use crate::stock_str;
@@ -611,7 +611,9 @@ impl ChatId {
611611
contact_id: Option<ContactId>,
612612
) -> Result<()> {
613613
let sort_to_bottom = true;
614-
let ts = calc_sort_timestamp(context, timestamp_sent, self, sort_to_bottom, false).await?;
614+
let ts = self
615+
.calc_sort_timestamp(context, timestamp_sent, sort_to_bottom, false)
616+
.await?;
615617
self.set_protection_for_timestamp_sort(context, protect, ts, contact_id)
616618
.await
617619
}
@@ -1335,6 +1337,64 @@ impl ChatId {
13351337
.unwrap_or_default();
13361338
Ok(protection_status)
13371339
}
1340+
1341+
/// Returns the sort timestamp for a new message in the chat.
1342+
///
1343+
/// `message_timestamp` should be either the message "sent" timestamp or a timestamp of the
1344+
/// corresponding event in case of a system message (usually the current system time).
1345+
/// `always_sort_to_bottom` makes this ajust the returned timestamp up so that the message goes
1346+
/// to the chat bottom.
1347+
/// `incoming` -- whether the message is incoming.
1348+
pub(crate) async fn calc_sort_timestamp(
1349+
self,
1350+
context: &Context,
1351+
message_timestamp: i64,
1352+
always_sort_to_bottom: bool,
1353+
incoming: bool,
1354+
) -> Result<i64> {
1355+
let mut sort_timestamp = cmp::min(message_timestamp, smeared_time(context));
1356+
1357+
let last_msg_time: Option<i64> = if always_sort_to_bottom {
1358+
// get newest message for this chat
1359+
1360+
// Let hidden messages also be ordered with protection messages because hidden messages
1361+
// also can be or not be verified, so let's preserve this information -- even it's not
1362+
// used currently, it can be useful in the future versions.
1363+
context
1364+
.sql
1365+
.query_get_value(
1366+
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND state!=?",
1367+
(self, MessageState::OutDraft),
1368+
)
1369+
.await?
1370+
} else if incoming {
1371+
// get newest non fresh message for this chat.
1372+
1373+
// If a user hasn't been online for some time, the Inbox is fetched first and then the
1374+
// Sentbox. In order for Inbox and Sent messages to be allowed to mingle, outgoing
1375+
// messages are purely sorted by their sent timestamp. NB: The Inbox must be fetched
1376+
// first otherwise Inbox messages would be always below old Sentbox messages. We could
1377+
// take in the query below only incoming messages, but then new incoming messages would
1378+
// mingle with just sent outgoing ones and apear somewhere in the middle of the chat.
1379+
context
1380+
.sql
1381+
.query_get_value(
1382+
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0 AND state>?",
1383+
(self, MessageState::InFresh),
1384+
)
1385+
.await?
1386+
} else {
1387+
None
1388+
};
1389+
1390+
if let Some(last_msg_time) = last_msg_time {
1391+
if last_msg_time > sort_timestamp {
1392+
sort_timestamp = last_msg_time;
1393+
}
1394+
}
1395+
1396+
Ok(sort_timestamp)
1397+
}
13381398
}
13391399

13401400
impl std::fmt::Display for ChatId {

src/receive_imf.rs

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Internet Message Format reception pipeline.
22
3-
use std::cmp::min;
43
use std::collections::HashSet;
54
use std::convert::TryFrom;
65

@@ -39,9 +38,7 @@ use crate::simplify;
3938
use crate::sql;
4039
use crate::stock_str;
4140
use crate::sync::Sync::*;
42-
use crate::tools::{
43-
buf_compress, extract_grpid_from_rfc724_mid, smeared_time, strip_rtlo_characters,
44-
};
41+
use crate::tools::{buf_compress, extract_grpid_from_rfc724_mid, strip_rtlo_characters};
4542
use crate::{contact, imap};
4643

4744
/// This is the struct that is returned after receiving one email (aka MIME message).
@@ -1017,14 +1014,15 @@ async fn add_parts(
10171014
};
10181015

10191016
let in_fresh = state == MessageState::InFresh;
1020-
let sort_timestamp = calc_sort_timestamp(
1021-
context,
1022-
mime_parser.timestamp_sent,
1023-
chat_id,
1024-
false,
1025-
incoming,
1026-
)
1027-
.await?;
1017+
let sort_to_bottom = false;
1018+
let sort_timestamp = chat_id
1019+
.calc_sort_timestamp(
1020+
context,
1021+
mime_parser.timestamp_sent,
1022+
sort_to_bottom,
1023+
incoming,
1024+
)
1025+
.await?;
10281026

10291027
// Apply ephemeral timer changes to the chat.
10301028
//
@@ -1487,53 +1485,6 @@ async fn save_locations(
14871485
Ok(())
14881486
}
14891487

1490-
pub(crate) async fn calc_sort_timestamp(
1491-
context: &Context,
1492-
message_timestamp: i64,
1493-
chat_id: ChatId,
1494-
always_sort_to_bottom: bool,
1495-
incoming: bool,
1496-
) -> Result<i64> {
1497-
let mut sort_timestamp = min(message_timestamp, smeared_time(context));
1498-
1499-
let last_msg_time: Option<i64> = if always_sort_to_bottom {
1500-
// get newest message for this chat
1501-
context
1502-
.sql
1503-
.query_get_value(
1504-
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND state!=?",
1505-
(chat_id, MessageState::OutDraft),
1506-
)
1507-
.await?
1508-
} else if incoming {
1509-
// get newest non fresh message for this chat.
1510-
1511-
// If a user hasn't been online for some time, the Inbox is fetched first and then the
1512-
// Sentbox. In order for Inbox and Sent messages to be allowed to mingle, outgoing messages
1513-
// are purely sorted by their sent timestamp. NB: The Inbox must be fetched first otherwise
1514-
// Inbox messages would be always below old Sentbox messages. We could take in the query
1515-
// below only incoming messages, but then new incoming messages would mingle with just sent
1516-
// outgoing ones and apear somewhere in the middle of the chat.
1517-
context
1518-
.sql
1519-
.query_get_value(
1520-
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0 AND state>?",
1521-
(chat_id, MessageState::InFresh),
1522-
)
1523-
.await?
1524-
} else {
1525-
None
1526-
};
1527-
1528-
if let Some(last_msg_time) = last_msg_time {
1529-
if last_msg_time > sort_timestamp {
1530-
sort_timestamp = last_msg_time;
1531-
}
1532-
}
1533-
1534-
Ok(sort_timestamp)
1535-
}
1536-
15371488
async fn lookup_chat_by_reply(
15381489
context: &Context,
15391490
mime_parser: &MimeMessage,

0 commit comments

Comments
 (0)