on master help sign in
Simplify server code: reuse removeOffline result for appliedOffline, remove unused PruneResult type
0c01684iko
client/ApiClient.fs+4−5
@@ -12,7 +12,7 @@ module ApiClient =
12 [<CLIMutable>]
12 [<CLIMutable>]
13 type EventPayload = { EncryptedBlob: string; Timestamp: int64 }
13 type EventPayload = { EncryptedBlob: string; Timestamp: int64 }
14
14
15− type EventType = Message | UnknownEvent of string
15+ type EventType = Message | Ack | UnknownEvent of string
16
16
17 [<CLIMutable>]
17 [<CLIMutable>]
18 type PollEvent = { Id: string; EventType: EventType; Payload: EventPayload }
18 type PollEvent = { Id: string; EventType: EventType; Payload: EventPayload }
@@ -66,11 +66,13 @@ module ApiClient =
66 override _.Read(reader, _, _) =
66 override _.Read(reader, _, _) =
67 match reader.GetString() with
67 match reader.GetString() with
68 | "message" -> Message
68 | "message" -> Message
69+ | "ack" -> Ack
69 | s -> UnknownEvent s
70 | s -> UnknownEvent s
70 override _.Write(writer, value, _) =
71 override _.Write(writer, value, _) =
71 writer.WriteStringValue(
72 writer.WriteStringValue(
72 match value with
73 match value with
73 | Message -> "message"
74 | Message -> "message"
75+ | Ack -> "ack"
74 | UnknownEvent s -> s)
76 | UnknownEvent s -> s)
75
77
76 let private jsonOpts =
78 let private jsonOpts =
@@ -128,10 +130,7 @@ module ApiClient =
128
130
129 let poll (serverUrl: string) (token: string) (cursor: int64) =
131 let poll (serverUrl: string) (token: string) (cursor: int64) =
130 let body = JsonSerializer.Serialize({| cursor = cursor; timeout = 30000 |})
132 let body = JsonSerializer.Serialize({| cursor = cursor; timeout = 30000 |})
131− async {
132− let! response = postJson<PollResponse> pollClient $"{serverUrl}/poll" body (Some token)
133+ postJson<PollResponse> pollClient $"{serverUrl}/poll" body (Some token)
133− return { response with Events = response.Events |> Array.filter (fun e -> e.EventType = Message) }
134− }
135
134
136 let ackMessages (serverUrl: string) (token: string) (messageIds: string list) =
135 let ackMessages (serverUrl: string) (token: string) (messageIds: string list) =
137 let body = JsonSerializer.Serialize({| messageIds = messageIds |})
136 let body = JsonSerializer.Serialize({| messageIds = messageIds |})
client/App.fs+11−8
@@ -137,7 +137,8 @@ module App =
137
137
138 let private tryParsePubkey (input: string) =
138 let private tryParsePubkey (input: string) =
139 let input = input.Trim()
139 let input = input.Trim()
140+ Phonemic.fromOb input
140− (Phonemic.fromOb input |> Option.orElseWith (fun () -> Crypto.tryFromHex input))
141+ |> Option.orElseWith (fun () -> Crypto.tryFromHex input)
141 |> Option.filter (fun bytes -> bytes.Length = 32)
142 |> Option.filter (fun bytes -> bytes.Length = 32)
142
143
143 let private contactName (contacts: Contact list) (pk: string) =
144 let private contactName (contacts: Contact list) (pk: string) =
@@ -287,10 +288,9 @@ module App =
287 | _ -> model, []
288 | _ -> model, []
288
289
289 | CopyPubKey ->
290 | CopyPubKey ->
290− model,
291− [ match model.Auth with
291+ match model.Auth with
292− | Identified(id, _) -> CmdCopyToClipboard(hexToOb id.PubKeyHex)
292+ | Identified(id, _) -> model, [ CmdCopyToClipboard(hexToOb id.PubKeyHex) ]
293− | NoIdentity -> () ]
293+ | NoIdentity -> model, []
294
294
295 | DismissError -> { model with Error = None }, []
295 | DismissError -> { model with Error = None }, []
296
296
@@ -381,9 +381,12 @@ module App =
381 let messages, errors =
381 let messages, errors =
382 Array.foldBack
382 Array.foldBack
383 (fun (evt: PollEvent) (msgs, errs) ->
383 (fun (evt: PollEvent) (msgs, errs) ->
384+ match evt.EventType with
385+ | Message ->
384− match decryptEvent session.Identity.PrivKey evt.Payload with
386+ match decryptEvent session.Identity.PrivKey evt.Payload with
385− | Ok msg -> (msg :: msgs, errs)
387+ | Ok msg -> (msg :: msgs, errs)
386− | Error err -> (msgs, err :: errs))
388+ | Error err -> (msgs, err :: errs)
389+ | Ack | UnknownEvent _ -> (msgs, errs))
387 response.Events
390 response.Events
388 ([], [])
391 ([], [])
389
392
client/bin/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.dll

Binary file not shown.

client/bin/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.pdb

Binary file not shown.

client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.AssemblyInfo.fs+1−1
@@ -10,7 +10,7 @@ open System.Reflection
10[<assembly: System.Reflection.AssemblyCompanyAttribute("Skrepka")>]
10[<assembly: System.Reflection.AssemblyCompanyAttribute("Skrepka")>]
11[<assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")>]
11[<assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")>]
12[<assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")>]
12[<assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")>]
13−[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0+6b1d74aecd0ed0fb71c97a95acff19b5fb9bf149")>]
13+[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0+a6088f11f8b9f333baecd765cfde004d371fea16")>]
14[<assembly: System.Reflection.AssemblyProductAttribute("Skrepka")>]
14[<assembly: System.Reflection.AssemblyProductAttribute("Skrepka")>]
15[<assembly: System.Reflection.AssemblyTitleAttribute("Skrepka")>]
15[<assembly: System.Reflection.AssemblyTitleAttribute("Skrepka")>]
16[<assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")>]
16[<assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")>]
client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.AssemblyInfoInputs.cache+1−1
@@ -1 +1 @@
1−da506a85f5b894cd8a51d8e06d0587022d2d4a29f8441cc3975ed09d776ba256
1+c792c6af7fcf4a9f3bc9ffab40c693a5335abf2b4bd4c1fd73018e15acd8228b
client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.dll

Binary file not shown.

client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.pdb

Binary file not shown.

client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/Skrepka.sourcelink.json+1−1
@@ -1 +1 @@
1−{"documents":{"/Users/iko/Developer/skrepka/*":"https://raw.githubusercontent.com/ilyakooo0/skrepka/6b1d74aecd0ed0fb71c97a95acff19b5fb9bf149/*"}}
1+{"documents":{"/Users/iko/Developer/skrepka/*":"https://raw.githubusercontent.com/ilyakooo0/skrepka/a6088f11f8b9f333baecd765cfde004d371fea16/*"}}
client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/ref/Skrepka.dll

Binary file not shown.

client/obj/Debug/net10.0-maccatalyst/maccatalyst-arm64/refint/Skrepka.dll

Binary file not shown.

server.knot+2−9
@@ -115,7 +115,6 @@ type PresenceEntry = {pubkey: PubkeyHex, server: AnyServerName, expiresAt: Times
115type ForwardEntry = {messageId: MessageIdHex, toServer: ServerName, createdAt: Timestamp, toKey: PubkeyHex, fromKey: PubkeyHex, encryptedBlob: BlobHex, timestamp: Timestamp}
115type ForwardEntry = {messageId: MessageIdHex, toServer: ServerName, createdAt: Timestamp, toKey: PubkeyHex, fromKey: PubkeyHex, encryptedBlob: BlobHex, timestamp: Timestamp}
116type AckedEntry = {messageId: MessageIdHex, toKey: PubkeyHex, ackedAt: Timestamp, originServer: ServerName}
116type AckedEntry = {messageId: MessageIdHex, toKey: PubkeyHex, ackedAt: Timestamp, originServer: ServerName}
117type AuthResult = Authorized {pubkey: PubkeyHex} | Unauthorized {}
117type AuthResult = Authorized {pubkey: PubkeyHex} | Unauthorized {}
118−type PruneResult = {expiredLocal: [PresenceEntry], peerServers: [ServerName]}
119type SendResult = SendUnauthorized {} | SendRejected {} | SendDone {status: MessageStatus, remoteServers: [ServerName], fromKey: PubkeyHex}
118type SendResult = SendUnauthorized {} | SendRejected {} | SendDone {status: MessageStatus, remoteServers: [ServerName], fromKey: PubkeyHex}
120
119
121
120
@@ -943,10 +942,7 @@ serve = \req -> case req of
943 e <- offlineEntries
942 e <- offlineEntries
944 where none onlineEntries (\o -> samePubkeyServer o e && o.timestamp > e.timestamp)
943 where none onlineEntries (\o -> samePubkeyServer o e && o.timestamp > e.timestamp)
945 yield e
944 yield e
946− let trustedOffline = do
945+ let trustedOffline = do e <- offlineEntries; where e.server == fromServer; yield e
947− e <- allValidEntries
948− where not (isGossipOnline e.eventType) && e.server == fromServer
949− yield e
950 let deduped = mergeByPubkeyServer [] filteredOnline
946 let deduped = mergeByPubkeyServer [] filteredOnline
951 gossipResult <- atomic do
947 gossipResult <- atomic do
952 pr <- maybePrune t
948 pr <- maybePrune t
@@ -964,11 +960,8 @@ serve = \req -> case req of
964 e <- allValidEntries
960 e <- allValidEntries
965 where none trustedDeduped (\d -> samePubkeyServer d e)
961 where none trustedDeduped (\d -> samePubkeyServer d e)
966 yield e
962 yield e
967− let appliedOffline = do
968− k <- offlineOnlyKeys
969− where any presence (\p -> samePubkeyServer p k && any trustedOffline (\o -> samePubkeyServer o k && (if o.timestamp > t then t else o.timestamp) > p.timestamp))
970− yield {pubkey: k.pubkey, server: k.server}
971 let afterOffline = removeOffline merged offlineOnlyKeys trustedOffline t
963 let afterOffline = removeOffline merged offlineOnlyKeys trustedOffline t
964+ let appliedOffline = do p <- diff merged afterOffline; yield {pubkey: p.pubkey, server: p.server}
972 let capResult = capGossipPresence afterOffline appliedOnline trustedDeduped presence offlineOnlyKeys trustedOffline t
965 let capResult = capGossipPresence afterOffline appliedOnline trustedDeduped presence offlineOnlyKeys trustedOffline t
973 set *presence = capResult.finalPresence
966 set *presence = capResult.finalPresence
974 let gossipPeerServers = collectPeerServers capResult.finalPresence (\p -> p.server)
967 let gossipPeerServers = collectPeerServers capResult.finalPresence (\p -> p.server)