puerta/internal/push/push.go

41 lines
758 B
Go
Raw Normal View History

2023-04-16 21:17:36 +00:00
package push
import (
"git.rob.mx/nidito/puerta/internal/user"
webpush "github.com/SherClockHolmes/webpush-go"
)
type VAPIDKey struct {
2023-04-16 22:19:40 +00:00
Private string `yaml:"private"`
Public string `yaml:"public"`
2023-04-16 21:17:36 +00:00
}
type Config struct {
2023-04-16 22:19:40 +00:00
Key *VAPIDKey `yaml:"key"`
2023-04-16 21:17:36 +00:00
}
type Notifier struct {
cfg *Config
}
var self *Notifier
func Notify(message string, subscriber *user.Subscription) error {
resp, err := webpush.SendNotification([]byte(message), subscriber.AsWebPush(), &webpush.Options{
Subscriber: subscriber.ID(),
VAPIDPublicKey: self.cfg.Key.Public,
VAPIDPrivateKey: self.cfg.Key.Private,
TTL: 30,
})
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func Initialize(cfg *Config) {
self = &Notifier{cfg}
}