60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
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) {
|
|
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
|
|
}
|