Add sub events

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-12-30 15:26:01 +01:00
parent 42749b2501
commit 5293fc8ea2
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 84 additions and 10 deletions

1
api.go
View file

@ -21,6 +21,7 @@ const (
msgTypeHost string = "host" msgTypeHost string = "host"
msgTypeRaid string = "raid" msgTypeRaid string = "raid"
msgTypeStore string = "store" msgTypeStore string = "store"
msgTypeSub string = "sub"
) )
var subscriptions = newSubscriptionStore() var subscriptions = newSubscriptionStore()

93
irc.go
View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/go-irc/irc" "github.com/go-irc/irc"
@ -185,24 +186,96 @@ func (ircHandler) handleTwitchUsernotice(m *irc.Message) {
"trailing": m.Trailing, "trailing": m.Trailing,
}).Debug("IRC USERNOTICE event") }).Debug("IRC USERNOTICE event")
displayName, ok := m.Tags["msg-param-displayName"]
if !ok {
displayName = m.Tags["login"]
}
switch m.Tags["msg-id"] { switch m.Tags["msg-id"] {
case "": case "":
// Notices SHOULD have msg-id tags... // Notices SHOULD have msg-id tags...
log.WithField("msg", m).Warn("Received usernotice without msg-id") log.WithField("msg", m).Warn("Received usernotice without msg-id")
case "raid": case "raid":
log.WithFields(log.Fields{ fields := map[string]interface{}{
"from": m.Tags["login"], "from": displayName,
"viewercount": m.Tags["msg-param-viewerCount"],
}).Info("Incoming raid")
subscriptions.SendAllSockets(msgTypeRaid, map[string]interface{}{
"from": m.Tags["msg-param-displayName"],
"viewerCount": m.Tags["msg-param-viewerCount"], "viewerCount": m.Tags["msg-param-viewerCount"],
}) }
case "resub": log.WithFields(log.Fields(fields)).Info("Incoming raid")
// FIXME: Fill in later subscriptions.SendAllSockets(msgTypeRaid, fields)
case "sub", "resub":
fields := map[string]interface{}{
"from": displayName,
"is_resub": m.Tags["msg-id"] == "resub",
"paid_for": m.Tags["msg-param-multimonth-duration"],
"streak": m.Tags["msg-param-streak-months"],
"tier": m.Tags["msg-param-sub-plan"],
"total": m.Tags["msg-param-cumulative-months"],
}
// Update store
strDisplayName := string(displayName)
var duration int64
if v, err := strconv.ParseInt(string(m.Tags["msg-param-cumulative-months"]), 10, 64); err == nil {
duration = v
}
store.Subs.Last = &strDisplayName
store.Subs.LastDuration = duration
// Send update to sockets
log.WithFields(log.Fields(fields)).Info("New subscriber")
subscriptions.SendAllSockets(msgTypeSub, fields)
// Execute store save
if err := store.Save(cfg.StoreFile); err != nil {
log.WithError(err).Error("Unable to update persistent store")
}
if err := subscriptions.SendAllSockets(msgTypeStore, store); err != nil {
log.WithError(err).Error("Unable to send update to all sockets")
}
case "subgift", "anonsubgift":
toName, ok := m.Tags["msg-param-recipient-display-name"]
if !ok {
toName = m.Tags["msg-param-recipient-user-name"]
}
fields := map[string]interface{}{
"from": displayName,
"is_anon": m.Tags["msg-id"] == "anonsubgift",
"gift_to": toName,
"paid_for": m.Tags["msg-param-gift-months"],
"streak": m.Tags["msg-param-streak-months"],
"tier": m.Tags["msg-param-sub-plan"],
"total": m.Tags["msg-param-months"],
}
// Update store
strDisplayName := string(displayName)
var duration int64
if v, err := strconv.ParseInt(string(m.Tags["msg-param-months"]), 10, 64); err == nil {
duration = v
}
store.Subs.Last = &strDisplayName
store.Subs.LastDuration = duration
// Send update to sockets
log.WithFields(log.Fields(fields)).Info("New sub-gift")
subscriptions.SendAllSockets(msgTypeSub, fields)
// Execute store save
if err := store.Save(cfg.StoreFile); err != nil {
log.WithError(err).Error("Unable to update persistent store")
}
if err := subscriptions.SendAllSockets(msgTypeStore, store); err != nil {
log.WithError(err).Error("Unable to send update to all sockets")
}
} }
} }