Skip to content

Commit 1eb4432

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 0e30dd8 commit 1eb4432

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
@@ -1211,37 +1211,35 @@ impl ChatId {
12111211
)
12121212
.await?
12131213
} else if received {
1214-
// Received messages shouldn't mingle with just sent ones and appear somewhere in the
1215-
// middle of the chat, so we go after the newest non fresh message.
1216-
//
1217-
// But if a received outgoing message is older than some seen message, better sort the
1218-
// received message purely by timestamp. We could place it just before that seen
1219-
// message, but anyway the user may not notice it.
1214+
// Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1215+
// in the middle of the chat, so we go after the newest non fresh message. Received
1216+
// outgoing messages are allowed to mingle with seen messages though to avoid seen
1217+
// replies appearing before messages sent from another device (cases like the user
1218+
// sharing the account with others or bots are rare, so let them break sometimes).
12201219
//
12211220
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12221221
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12231222
// fresh ones, they rather mean that older incoming messages are actually seen as well.
12241223
context
12251224
.sql
12261225
.query_row_optional(
1227-
"SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1226+
"SELECT MAX(timestamp)
12281227
FROM msgs
12291228
WHERE chat_id=? AND hidden=0 AND state>?
12301229
HAVING COUNT(*) > 0",
1231-
(MessageState::InSeen, self, MessageState::InFresh),
1230+
(
1231+
self,
1232+
match incoming {
1233+
true => MessageState::InFresh,
1234+
false => MessageState::InSeen,
1235+
},
1236+
),
12321237
|row| {
12331238
let ts: i64 = row.get(0)?;
1234-
let ts_sent_seen: i64 = row.get(1)?;
1235-
Ok((ts, ts_sent_seen))
1239+
Ok(ts)
12361240
},
12371241
)
12381242
.await?
1239-
.and_then(|(ts, ts_sent_seen)| {
1240-
match incoming || ts_sent_seen <= message_timestamp {
1241-
true => Some(ts),
1242-
false => None,
1243-
}
1244-
})
12451243
} else {
12461244
None
12471245
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Single#Chat#1001: bob@example.net [KEY bob@example.net]
22
--------------------------------------------------------------------------------
3-
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
43
Msg#1002🔒: Me (Contact#Contact#Self): Test – This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
4+
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
55
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)