@@ -3,7 +3,6 @@ namespace Skrepka
33open System
44open System.IO
55open System.Text.Json
6−open System.Text.Json.Serialization
76open Fabulous
87open Fabulous.Maui
98open Microsoft.Maui.Graphics
@@ -66,8 +65,7 @@ module App =
6665 // ── Msg ──
6766
6867 type StateLoadResult =
69− | IdentityAndData of Identity * Data * Profile option
70− | IdentityOnly of Identity * Profile option
68+ | Loaded of Identity * Data option * Profile option
7169 | NothingSaved
7270
7371 type Msg =
@@ -340,7 +338,7 @@ module App =
340338 | None -> () ]
341339 | _ -> model, []
342340
343− | StateLoaded(IdentityAndData(identity, data, profile)) ->
341+ | StateLoaded(Loaded(identity, Some data, profile)) ->
344342 let model' =
345343 { model with
346344 Auth = Identified(identity, Connecting)
@@ -351,7 +349,7 @@ module App =
351349 Profile = profile
352350 Page = Conversations }
353351 model', [ CmdConnect(model'.ServerUrl, identity) ]
354− | StateLoaded(IdentityOnly(identity, profile)) ->
352+ | StateLoaded(Loaded(identity, None, profile)) ->
355353 { model with Auth = Identified(identity, Offline); Profile = profile; Page = Settings }, []
356354 | StateLoaded NothingSaved -> model, []
357355
@@ -367,16 +365,16 @@ module App =
367365
368366 type private Envelope =
369367 | TextMessage of id: string * body: string
370− | DeliveryAck of id: string * ackIds: string list
371− | ProfileMessage of id: string * profile: Profile
368+ | DeliveryAck of ackIds: string list
369+ | ProfileMessage of profile: Profile
372370
373371 let private serializeEnvelope = function
374372 | TextMessage(id, body) ->
375373 JsonSerializer.Serialize({| ``type`` = "text"; id = id; body = body |})
376− | DeliveryAck(id, ackIds) ->
377− JsonSerializer.Serialize({| ``type`` = "delivery.ack"; id = id; ack_ids = ackIds |})
378− | ProfileMessage(id, profile) ->
379− JsonSerializer.Serialize({| ``type`` = "profile"; id = id; timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); display_name = profile.DisplayName; bio = profile.Bio; photo = profile.PhotoBase64 |})
374+ | DeliveryAck ackIds ->
375+ JsonSerializer.Serialize({| ``type`` = "delivery.ack"; id = Guid.NewGuid().ToString(); ack_ids = ackIds |})
376+ | ProfileMessage profile ->
377+ JsonSerializer.Serialize({| ``type`` = "profile"; id = Guid.NewGuid().ToString(); timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); display_name = profile.DisplayName; bio = profile.Bio; photo = profile.PhotoBase64 |})
380378
381379 let private sendEnvelope (session: Session) (recipientHex: string) (envelope: Envelope) =
382380 async {
@@ -399,12 +397,12 @@ module App =
399397 Some(TextMessage(root.GetProperty("id").GetString(), root.GetProperty("body").GetString()))
400398 | "delivery.ack" ->
401399 let ackIds = root.GetProperty("ack_ids").EnumerateArray() |> Seq.map _.GetString() |> Seq.toList
402− Some(DeliveryAck(root.GetProperty("id").GetString(), ackIds))
400+ Some(DeliveryAck ackIds)
403401 | "profile" ->
404402 let displayName = root.GetProperty("display_name").GetString()
405403 let bio = tryGetJsonString root "bio" |> Option.defaultValue ""
406404 let photo = tryGetJsonString root "photo" |> Option.defaultValue ""
407− Some(ProfileMessage(root.GetProperty("id").GetString(), { DisplayName = displayName; Bio = bio; PhotoBase64 = photo }))
405+ Some(ProfileMessage { DisplayName = displayName; Bio = bio; PhotoBase64 = photo })
408406 | _ -> None
409407
410408 let private decryptEvent (privKey: byte[]) (payload: EventPayload) =
@@ -420,9 +418,9 @@ module App =
420418 Timestamp = DateTimeOffset.FromUnixTimeSeconds(payload.Timestamp)
421419 IsOutgoing = false
422420 Status = DeliveryStatus.Delivered }))
423− | Some(DeliveryAck(_, ackIds)) ->
421+ | Some(DeliveryAck ackIds) ->
424422 Ok(IncomingDeliveryAck(senderHex, ackIds))
425− | Some(ProfileMessage(_, profile)) ->
423+ | Some(ProfileMessage profile) ->
426424 Ok(IncomingProfile(senderHex, profile))
427425 | None -> Error "unknown envelope type"
428426 | None -> Error "decrypt failed"
@@ -438,7 +436,7 @@ module App =
438436 |> List.map (fun (sender, pairs) -> sender, List.map snd pairs)
439437
440438 for sender, msgIds in chatsBySender do
441− try do! sendEnvelope session sender (DeliveryAck(Guid.NewGuid().ToString(), msgIds)) |> Async.Ignore
439+ try do! sendEnvelope session sender (DeliveryAck msgIds) |> Async.Ignore
442440 with _ -> ()
443441 }
444442
@@ -506,11 +504,7 @@ module App =
506504 asyncCmd (async {
507505 match! Store.loadIdentity () with
508506 | Some id ->
509− let profile = Store.loadProfile ()
510− return StateLoaded(
511− match Store.loadData () with
512− | Some data -> IdentityAndData(id, data, profile)
513− | None -> IdentityOnly(id, profile))
507+ return StateLoaded(Loaded(id, Store.loadData (), Store.loadProfile ()))
514508 | None -> return StateLoaded NothingSaved
515509 })
516510
@@ -542,7 +536,7 @@ module App =
542536 Cmd.ofEffect (fun _ ->
543537 async {
544538 for recipientHex in recipientHexes do
545− try do! sendEnvelope session recipientHex (ProfileMessage(Guid.NewGuid().ToString(), profile)) |> Async.Ignore
539+ try do! sendEnvelope session recipientHex (ProfileMessage profile) |> Async.Ignore
546540 with _ -> ()
547541 } |> Async.Start)
548542