From 0659bdaf81583c4aa7437fd23703f14aa9c4f6e9 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sat, 1 Feb 2025 16:51:10 +0200 Subject: Added a way to display notifications for networks --- internal/client/ui/core/core.go | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'internal/client/ui/core/core.go') diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go index 1ba0069..36c2b0f 100644 --- a/internal/client/ui/core/core.go +++ b/internal/client/ui/core/core.go @@ -551,6 +551,8 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { return cmd } + calculateNotifications() + var cmds []tea.Cmd var cmd tea.Cmd @@ -687,3 +689,53 @@ func (m *Model) HasPopup() bool { m.banViewPopup != nil || m.peerAddPopup != nil } + +func calculateNotifications() { + for networkId := range state.State.Networks { + for _, frequency := range state.State.Frequencies[networkId] { + pings, hasNotif := getFrequencyNotification(networkId, frequency.ID) + if hasNotif { + state.State.Notifications[frequency.ID] = pings + } else { + delete(state.State.Notifications, frequency.ID) + } + } + } +} + +func getFrequencyNotification(networkId, frequencyId snowflake.ID) (_ int, _ bool) { + lastReadMsg := state.Data.LastReadMessage[frequencyId] + if lastReadMsg == nil { + return 0, false + } + + btree := state.State.Messages[frequencyId] + if btree == nil { + return 0, false + } + + pings := 0 + hasNotif := false + + isAdmin := state.State.Members[networkId][*state.UserID].IsAdmin + + btree.AscendGreaterOrEqual(data.Message{ID: *lastReadMsg + 1}, func(item data.Message) bool { + hasNotif = true + + if item.Ping == nil { + return true + } + + if *item.Ping == packet.PingEveryone { + pings++ + } else if *item.Ping == packet.PingAdmins && isAdmin { + pings++ + } else if *item.Ping == *state.UserID { + pings++ + } + + return pings <= 10 + }) + + return pings, hasNotif +} -- cgit v1.3.1