Skip to content

Commit bf361d1

Browse files
iequidoolink2xt
authored andcommitted
feat: calc_sort_timestamp(): Don't look at existing messages' timestamp_sent
This makes `calc_sort_timestamp()` a continuous function of the message timestamp, simplifies the SQL query and prepares for creation of a db index for it so that it's fast. Currently it doesn't uses indexes effectively; if a chat has many messages, it's slow, i.e. O(n).
1 parent 268a5cf commit bf361d1

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/chat.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,37 +1218,35 @@ impl ChatId {
12181218
)
12191219
.await?
12201220
} else if received {
1221-
// Received messages shouldn't mingle with just sent ones and appear somewhere in the
1222-
// middle of the chat, so we go after the newest non fresh message.
1223-
//
1224-
// But if a received outgoing message is older than some seen message, better sort the
1225-
// received message purely by timestamp. We could place it just before that seen
1226-
// message, but anyway the user may not notice it.
1221+
// Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1222+
// in the middle of the chat, so we go after the newest non fresh message. Received
1223+
// outgoing messages are allowed to mingle with seen messages though to avoid seen
1224+
// replies appearing before messages sent from another device (cases like the user
1225+
// sharing the account with others or bots are rare, so let them break sometimes).
12271226
//
12281227
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12291228
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12301229
// fresh ones, they rather mean that older incoming messages are actually seen as well.
12311230
context
12321231
.sql
12331232
.query_row_optional(
1234-
"SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1233+
"SELECT MAX(timestamp)
12351234
FROM msgs
12361235
WHERE chat_id=? AND hidden=0 AND state>?
12371236
HAVING COUNT(*) > 0",
1238-
(MessageState::InSeen, self, MessageState::InFresh),
1237+
(
1238+
self,
1239+
match incoming {
1240+
true => MessageState::InFresh,
1241+
false => MessageState::InSeen,
1242+
},
1243+
),
12391244
|row| {
12401245
let ts: i64 = row.get(0)?;
1241-
let ts_sent_seen: i64 = row.get(1)?;
1242-
Ok((ts, ts_sent_seen))
1246+
Ok(ts)
12431247
},
12441248
)
12451249
.await?
1246-
.and_then(|(ts, ts_sent_seen)| {
1247-
match incoming || ts_sent_seen <= message_timestamp {
1248-
true => Some(ts),
1249-
false => None,
1250-
}
1251-
})
12521250
} else {
12531251
None
12541252
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Single#Chat#10: bob@example.net [KEY bob@example.net]
22
--------------------------------------------------------------------------------
3-
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
43
Msg#11🔒: Me (Contact#Contact#Self): Test – This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
4+
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
55
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)