2020-03-04 10:29:00 +00:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
2020-03-04 10:29:00 +00:00
|
|
|
)
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
var db database.Database
|
|
|
|
|
|
|
|
func SetDB(database database.Database) {
|
|
|
|
db = database.Model(&Notification{})
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
func Append(n Notifier) {
|
|
|
|
allNotifiers = append(allNotifiers, n)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
func Find(name string) (*Notification, error) {
|
2020-03-09 15:15:15 +00:00
|
|
|
for _, n := range allNotifiers {
|
2020-03-04 10:29:00 +00:00
|
|
|
notif := n.Select()
|
2020-03-06 03:03:18 +00:00
|
|
|
if notif.Name() == name || notif.Method == name {
|
2020-03-10 05:24:35 +00:00
|
|
|
return notif, nil
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("notifier not found")
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
func All() []Notifier {
|
|
|
|
return allNotifiers
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notification) Create() error {
|
2020-03-09 15:15:15 +00:00
|
|
|
var notif Notification
|
2020-03-10 05:24:35 +00:00
|
|
|
if db.Where("method = ?", n.Method).Find(¬if).RecordNotFound() {
|
|
|
|
Append(n)
|
|
|
|
return db.Create(n).Error()
|
2020-03-09 15:15:15 +00:00
|
|
|
}
|
2020-03-10 05:24:35 +00:00
|
|
|
Append(n)
|
2020-03-09 15:15:15 +00:00
|
|
|
return nil
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 03:03:18 +00:00
|
|
|
func (n *Notification) Update() error {
|
2020-03-04 10:29:00 +00:00
|
|
|
n.ResetQueue()
|
|
|
|
if n.Enabled.Bool {
|
|
|
|
n.Close()
|
|
|
|
n.Start()
|
2020-03-06 03:03:18 +00:00
|
|
|
go Queue(Notifier(n))
|
2020-03-04 10:29:00 +00:00
|
|
|
} else {
|
|
|
|
n.Close()
|
|
|
|
}
|
2020-03-10 05:24:35 +00:00
|
|
|
err := db.Update(n)
|
2020-03-06 03:03:18 +00:00
|
|
|
return err.Error()
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notification) Delete() error {
|
|
|
|
return nil
|
|
|
|
}
|