@@ -109,6 +109,7 @@ module Store =
109 let private openConn () =
109 let private openConn () =
110 let conn = new SqliteConnection($"Data Source={dbPath}")
110 let conn = new SqliteConnection($"Data Source={dbPath}")
111 conn.Open()
111 conn.Open()
112+ conn.Execute("PRAGMA busy_timeout = 5000") |> ignore
114 type private ByteArrayOptionHandler() =
115 type private ByteArrayOptionHandler() =
@@ -130,6 +131,7 @@ module Store =
130 OptionTypes.register ()
131 OptionTypes.register ()
131 SqlMapper.AddTypeHandler(ByteArrayOptionHandler())
132 SqlMapper.AddTypeHandler(ByteArrayOptionHandler())
132 use conn = openConn ()
133 use conn = openConn ()
134+ conn.Execute("PRAGMA journal_mode=WAL") |> ignore
134 conn.Execute(
136 conn.Execute(
135 "CREATE TABLE IF NOT EXISTS profile (Id TEXT PRIMARY KEY, DisplayName TEXT NOT NULL DEFAULT '', Bio TEXT NOT NULL DEFAULT '', Photo BLOB)"
137 "CREATE TABLE IF NOT EXISTS profile (Id TEXT PRIMARY KEY, DisplayName TEXT NOT NULL DEFAULT '', Bio TEXT NOT NULL DEFAULT '', Photo BLOB)"
@@ -170,11 +172,9 @@ module Store =
170 log $"schema version check: {ex.Message}"
172 log $"schema version check: {ex.Message}"
173− // Future migrations:
174− // if dbVersion < 2 then
175+ if dbVersion < 2 then
175− // conn.Execute("ALTER TABLE ...") |> ignore
176+ conn.Execute("CREATE INDEX IF NOT EXISTS idx_messages_conv ON messages(ConversationId)")
179 conn.Execute(
179 conn.Execute(
180 "INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('schema_version', @v)",
180 "INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('schema_version', @v)",
@@ -288,11 +288,9 @@ module Store =
288 |> Map.ofSeq
288 |> Map.ofSeq
290 let! messageRows =
290 let! messageRows =
292− for m in messageTable do
295− |> conn.SelectAsync<MessageRow>
291+ conn.QueryAsync<MessageRow>(
292+ "SELECT Id, ConversationId, Body, TimestampUnix, IsOutgoing, Status FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ConversationId ORDER BY TimestampUnix DESC) AS rn FROM messages) WHERE rn <= 200"
296 |> Async.AwaitTask
294 |> Async.AwaitTask
298 let messages =
296 let messages =
@@ -334,6 +332,7 @@ module Store =
336 use conn = openConn ()
334 use conn = openConn ()
335+ use tx = conn.BeginTransaction()
338 for c in data.Contacts.Values do
337 for c in data.Contacts.Values do
@@ -343,7 +342,8 @@ module Store =
343 Nickname = c.Nickname
342 Nickname = c.Nickname
344 DisplayName = c.DisplayName
343 DisplayName = c.DisplayName
345 Bio = c.Bio
344 Bio = c.Bio
346− Photo = c.Photo |}
345+ Photo = c.Photo |},
348 |> Async.AwaitTask
348 |> Async.AwaitTask
349 |> Async.Ignore
349 |> Async.Ignore
@@ -364,7 +364,8 @@ module Store =
364 Body = m.Body
364 Body = m.Body
365 TimestampUnix = m.Timestamp.ToUnixTimeMilliseconds()
365 TimestampUnix = m.Timestamp.ToUnixTimeMilliseconds()
366 IsOutgoing = isOutgoing
366 IsOutgoing = isOutgoing
367− Status = status |}
367+ Status = status |},
369 |> Async.AwaitTask
370 |> Async.AwaitTask
370 |> Async.Ignore
371 |> Async.Ignore
@@ -374,10 +375,13 @@ module Store =
374 "INSERT OR REPLACE INTO settings (Id, ServerUrl, PollCursor) VALUES (@Id, @ServerUrl, @PollCursor)",
375 "INSERT OR REPLACE INTO settings (Id, ServerUrl, PollCursor) VALUES (@Id, @ServerUrl, @PollCursor)",
375 {| Id = Constants.settingsId
376 {| Id = Constants.settingsId
376 ServerUrl = data.ServerUrl
377 ServerUrl = data.ServerUrl
377− PollCursor = data.PollCursor |}
378+ PollCursor = data.PollCursor |},
379 |> Async.AwaitTask
381 |> Async.AwaitTask
380 |> Async.Ignore
382 |> Async.Ignore
381 with ex ->
385 with ex ->
382 log $"saveData error: {ex.Message}"
386 log $"saveData error: {ex.Message}"
@@ -440,3 +444,37 @@ module Store =
440 with ex ->
444 with ex ->
441 log $"dequeueOutbox error: {ex.Message}"
445 log $"dequeueOutbox error: {ex.Message}"
448+ let loadMessagesForConversation (conversationId: string) =
451+ use conn = openConn ()
454+ conn.QueryAsync<MessageRow>(
455+ "SELECT Id, ConversationId, Body, TimestampUnix, IsOutgoing, Status FROM messages WHERE ConversationId = @convId ORDER BY TimestampUnix",
456+ {| convId = conversationId |}
462+ |> Seq.map (fun m ->
465+ Timestamp = DateTimeOffset.FromUnixTimeMilliseconds m.TimestampUnix
467+ if not m.IsOutgoing then
471+ if m.Status = 1 then
472+ DeliveryStatus.Delivered
478+ log $"loadMessagesForConversation error: {ex.Message}"