Skip to content

Commit d0f8e10

Browse files
committed
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 5051240 commit d0f8e10

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
@@ -1220,37 +1220,35 @@ impl ChatId {
12201220
)
12211221
.await?
12221222
} else if received {
1223-
// Received messages shouldn't mingle with just sent ones and appear somewhere in the
1224-
// middle of the chat, so we go after the newest non fresh message.
1225-
//
1226-
// But if a received outgoing message is older than some seen message, better sort the
1227-
// received message purely by timestamp. We could place it just before that seen
1228-
// message, but anyway the user may not notice it.
1223+
// Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1224+
// in the middle of the chat, so we go after the newest non fresh message. Received
1225+
// outgoing messages are allowed to mingle with seen messages though to avoid seen
1226+
// replies appearing before messages sent from another device (cases like the user
1227+
// sharing the account with others or bots are rare, so let them break sometimes).
12291228
//
12301229
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12311230
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12321231
// fresh ones, they rather mean that older incoming messages are actually seen as well.
12331232
context
12341233
.sql
12351234
.query_row_optional(
1236-
"SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1235+
"SELECT MAX(timestamp)
12371236
FROM msgs
12381237
WHERE chat_id=? AND hidden=0 AND state>?
12391238
HAVING COUNT(*) > 0",
1240-
(MessageState::InSeen, self, MessageState::InFresh),
1239+
(
1240+
self,
1241+
match incoming {
1242+
true => MessageState::InFresh,
1243+
false => MessageState::InSeen,
1244+
},
1245+
),
12411246
|row| {
12421247
let ts: i64 = row.get(0)?;
1243-
let ts_sent_seen: i64 = row.get(1)?;
1244-
Ok((ts, ts_sent_seen))
1248+
Ok(ts)
12451249
},
12461250
)
12471251
.await?
1248-
.and_then(|(ts, ts_sent_seen)| {
1249-
match incoming || ts_sent_seen <= message_timestamp {
1250-
true => Some(ts),
1251-
false => None,
1252-
}
1253-
})
12541252
} else {
12551253
None
12561254
};
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)