mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-30 02:40:33 +00:00
103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
|
package alerts
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"text/template"
|
||
|
|
||
|
"git.sr.ht/~ashkeel/strimertul/twitch/eventsub"
|
||
|
|
||
|
template2 "git.sr.ht/~ashkeel/strimertul/twitch/template"
|
||
|
|
||
|
jsoniter "github.com/json-iterator/go"
|
||
|
"go.uber.org/zap"
|
||
|
|
||
|
"git.sr.ht/~ashkeel/strimertul/database"
|
||
|
)
|
||
|
|
||
|
var json = jsoniter.ConfigFastest
|
||
|
|
||
|
type (
|
||
|
templateCache map[string]*template.Template
|
||
|
templateCacheMap map[templateType]templateCache
|
||
|
)
|
||
|
|
||
|
type templateType string
|
||
|
|
||
|
const (
|
||
|
templateTypeSubscription templateType = "subscription"
|
||
|
templateTypeFollow templateType = "follow"
|
||
|
templateTypeRaid templateType = "raid"
|
||
|
templateTypeCheer templateType = "cheer"
|
||
|
templateTypeGift templateType = "gift"
|
||
|
)
|
||
|
|
||
|
type Module struct {
|
||
|
Config Config
|
||
|
|
||
|
db database.Database
|
||
|
logger *zap.Logger
|
||
|
templater template2.Engine
|
||
|
templates templateCacheMap
|
||
|
|
||
|
cancelAlertSub database.CancelFunc
|
||
|
cancelTwitchEventSub database.CancelFunc
|
||
|
|
||
|
pendingMux sync.Mutex
|
||
|
pendingSubs map[string]subMixedEvent
|
||
|
}
|
||
|
|
||
|
func Setup(db database.Database, logger *zap.Logger, templater template2.Engine) *Module {
|
||
|
mod := &Module{
|
||
|
db: db,
|
||
|
logger: logger,
|
||
|
templater: templater,
|
||
|
pendingMux: sync.Mutex{},
|
||
|
pendingSubs: make(map[string]subMixedEvent),
|
||
|
}
|
||
|
|
||
|
// Load config from database
|
||
|
err := db.GetJSON(ConfigKey, &mod.Config)
|
||
|
if err != nil {
|
||
|
logger.Debug("Config load error", zap.Error(err))
|
||
|
mod.Config = Config{}
|
||
|
// Save empty config
|
||
|
err = db.PutJSON(ConfigKey, mod.Config)
|
||
|
if err != nil {
|
||
|
logger.Warn("Could not save default config for bot alerts", zap.Error(err))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mod.compileTemplates()
|
||
|
|
||
|
mod.cancelAlertSub, err = db.SubscribeKey(ConfigKey, func(value string) {
|
||
|
err := json.UnmarshalFromString(value, &mod.Config)
|
||
|
if err != nil {
|
||
|
logger.Warn("Error loading alert config", zap.Error(err))
|
||
|
} else {
|
||
|
logger.Info("Reloaded alert config")
|
||
|
}
|
||
|
mod.compileTemplates()
|
||
|
})
|
||
|
if err != nil {
|
||
|
logger.Error("Could not set-up bot alert reload subscription", zap.Error(err))
|
||
|
}
|
||
|
|
||
|
mod.cancelTwitchEventSub, err = db.SubscribePrefix(mod.onEventSubEvent, eventsub.EventKeyPrefix)
|
||
|
if err != nil {
|
||
|
logger.Error("Could not setup twitch alert subscription", zap.Error(err))
|
||
|
}
|
||
|
|
||
|
logger.Debug("Loaded bot alerts")
|
||
|
|
||
|
return mod
|
||
|
}
|
||
|
|
||
|
func (m *Module) Close() {
|
||
|
if m.cancelAlertSub != nil {
|
||
|
m.cancelAlertSub()
|
||
|
}
|
||
|
if m.cancelTwitchEventSub != nil {
|
||
|
m.cancelTwitchEventSub()
|
||
|
}
|
||
|
}
|