diff options
Diffstat (limited to 'internal/client/ui')
| -rw-r--r-- | internal/client/ui/core/core.go | 15 | ||||
| -rw-r--r-- | internal/client/ui/core/state/state.go | 49 |
2 files changed, 64 insertions, 0 deletions
diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go index 36c2b0f..c7b3ca4 100644 --- a/internal/client/ui/core/core.go +++ b/internal/client/ui/core/core.go @@ -189,6 +189,9 @@ func (m *Model) updateNotConnected(msg tea.Msg) tea.Cmd { switch msg := msg.(type) { case gateway.ConnectionEstablished: state.UserID = (*snowflake.ID)(&msg) + state.ReceivedData = false + state.ReceivedNetworks = false + state.AskedForNotifs = false m.connected = true m.timeout = initialTimeout @@ -257,6 +260,11 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { m.memberList.SetWidth(sidebarWidth) m.chat.SetWidth(chatWidth) + if state.ReceivedData && state.ReceivedNetworks && !state.AskedForNotifs { + state.AskedForNotifs = true + state.AskForNotifs() + } + switch msg := msg.(type) { case ui.QuitMsg: data := state.JsonUserData() @@ -335,6 +343,9 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { case *packet.TrustInfo: state.UpdateTrusteds(msg) + case *packet.NotificationsInfo: + state.UpdateNotifications(msg) + case ui.BanReasonPopupMsg: popup := banreason.New(msg.User, msg.Network) m.banReasonPopup = &popup @@ -693,6 +704,10 @@ func (m *Model) HasPopup() bool { func calculateNotifications() { for networkId := range state.State.Networks { for _, frequency := range state.State.Frequencies[networkId] { + if _, ok := state.State.Messages[frequency.ID]; !ok { + continue + } + pings, hasNotif := getFrequencyNotification(networkId, frequency.ID) if hasNotif { state.State.Notifications[frequency.ID] = pings 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, + }) +} |
