From c77ac77e78268bc2f4265a872b213d4300b3eb66 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 2 Feb 2025 19:23:50 +0200 Subject: Added support for offline notifications where the server calculates them and sends the info to the client when it connects --- internal/client/ui/core/state/state.go | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'internal/client/ui/core/state') diff --git a/internal/client/ui/core/state/state.go b/internal/client/ui/core/state/state.go index ee61a2c..ba9e233 100644 --- a/internal/client/ui/core/state/state.go +++ b/internal/client/ui/core/state/state.go @@ -3,6 +3,7 @@ package state import ( "crypto/ed25519" "encoding/json" + "log" "slices" "github.com/google/btree" @@ -59,6 +60,12 @@ var Data UserData = UserData{ var UserID *snowflake.ID = nil +var ( + ReceivedData = false + ReceivedNetworks = false + AskedForNotifs = false +) + func UpdateNetworks(info *packet.NetworksInfo) { networks := State.Networks @@ -111,6 +118,8 @@ func UpdateNetworks(info *packet.NetworksInfo) { Data: &data, User: nil, }) + + ReceivedNetworks = true } func UpdateFrequencies(info *packet.FrequenciesInfo) { @@ -215,6 +224,8 @@ func FromJsonUserData(s string) { if Data.LastReadMessage == nil { Data.LastReadMessage = map[snowflake.ID]*snowflake.ID{} } + + ReceivedData = true } func UpdateTrusteds(info *packet.TrustInfo) { @@ -246,3 +257,41 @@ func SendUserDatUpdate() { User: nil, }) } + +func UpdateNotifications(info *packet.NotificationsInfo) { + for i := 0; i < len(info.Source); i++ { + ping := info.Pings[i] + if ping != nil { + State.Notifications[info.Source[i]] = int(*ping) + } else { + delete(State.Notifications, info.Source[i]) + } + } +} + +func AskForNotifs() { + source := []snowflake.ID{} + + source = append(source, Data.Peers...) + for networkId := range State.Networks { + for _, frequency := range State.Frequencies[networkId] { + source = append(source, frequency.ID) + } + } + + lastReadId := make([]snowflake.ID, 0, len(source)) + + for _, source := range source { + id := Data.LastReadMessage[source] + if id == nil { + lastReadId = append(lastReadId, 0) + } else { + lastReadId = append(lastReadId, *id) + } + } + + gateway.SendAsync(&packet.GetNotifications{ + Source: source, + LastReadId: lastReadId, + }) +} -- cgit v1.3.1