Notes/notification/pkg.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-03-29 08:06:23 +01:00
package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard
_ "fmt"
"slices"
)
type Service interface {
GetPrio() int
Send(string, []byte) error
}
type Manager struct {
services map[int][]Service
}
func NewManager() (nm Manager) {
nm.services = make(map[int][]Service, 32)
return
}
func (nm *Manager) AddService(userID int, service Service) {
var services []Service
var found bool
if services, found = nm.services[userID]; !found {
services = []Service{}
}
services = append(services, service)
slices.SortFunc(services, func(a, b Service) int {
if a.GetPrio() < b.GetPrio() {
return -1
}
if a.GetPrio() > b.GetPrio() {
return 1
}
return 0
})
nm.services[userID] = services
}
func (nm *Manager) Send(userID int, uuid string, msg []byte) (err error) {
2024-03-29 20:54:42 +01:00
services, found := nm.services[userID]
if !found {
return werr.New("No notification services defined for user ID %d", userID).WithCode("002-0008")
}
for _, service := range services {
if err = service.Send(uuid, msg); err == nil {
break
}
}
return
2024-03-29 08:06:23 +01:00
}