mirror of
https://github.com/Luzifer/twitch-manager.git
synced 2024-12-21 04:11:17 +00:00
Add donation webhook
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
aa20136f63
commit
e4800c921f
3 changed files with 55 additions and 11 deletions
19
api.go
19
api.go
|
@ -18,15 +18,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
msgTypeAlert string = "alert"
|
msgTypeAlert string = "alert"
|
||||||
msgTypeBits string = "bits"
|
msgTypeBits string = "bits"
|
||||||
msgTypeCustom string = "custom"
|
msgTypeCustom string = "custom"
|
||||||
msgTypeFollow string = "follow"
|
msgTypeDonation string = "donation"
|
||||||
msgTypeHost string = "host"
|
msgTypeFollow string = "follow"
|
||||||
msgTypeRaid string = "raid"
|
msgTypeHost string = "host"
|
||||||
msgTypeStore string = "store"
|
msgTypeRaid string = "raid"
|
||||||
msgTypeSub string = "sub"
|
msgTypeStore string = "store"
|
||||||
msgTypeSubGift string = "subgift"
|
msgTypeSub string = "sub"
|
||||||
|
msgTypeSubGift string = "subgift"
|
||||||
)
|
)
|
||||||
|
|
||||||
var subscriptions = newSubscriptionStore()
|
var subscriptions = newSubscriptionStore()
|
||||||
|
|
18
storage.go
18
storage.go
|
@ -4,18 +4,26 @@ import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const storeMaxRecent = 25
|
const storeMaxRecent = 50
|
||||||
|
|
||||||
type subscriber struct {
|
type subscriber struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Months int64 `json:"months"`
|
Months int64 `json:"months"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type storedEvent struct {
|
||||||
|
Time time.Time
|
||||||
|
Type string
|
||||||
|
Message map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
type storage struct {
|
type storage struct {
|
||||||
BitDonations struct {
|
BitDonations struct {
|
||||||
LastDonator *string `json:"last_donator"`
|
LastDonator *string `json:"last_donator"`
|
||||||
|
@ -25,7 +33,6 @@ type storage struct {
|
||||||
Donations struct {
|
Donations struct {
|
||||||
LastDonator *string `json:"last_donator"`
|
LastDonator *string `json:"last_donator"`
|
||||||
LastAmount float64 `json:"last_amount"`
|
LastAmount float64 `json:"last_amount"`
|
||||||
TotalAmount float64 `json:"total_amount"`
|
|
||||||
} `json:"donations"`
|
} `json:"donations"`
|
||||||
Followers struct {
|
Followers struct {
|
||||||
Last *string `json:"last"`
|
Last *string `json:"last"`
|
||||||
|
@ -39,6 +46,8 @@ type storage struct {
|
||||||
Recent []subscriber `json:"recent"`
|
Recent []subscriber `json:"recent"`
|
||||||
} `json:"subs"`
|
} `json:"subs"`
|
||||||
|
|
||||||
|
Events []storedEvent
|
||||||
|
|
||||||
modLock sync.RWMutex
|
modLock sync.RWMutex
|
||||||
saveLock sync.Mutex
|
saveLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
@ -82,6 +91,11 @@ func (s *storage) Save(to string) error {
|
||||||
s.Subs.Recent = s.Subs.Recent[:storeMaxRecent]
|
s.Subs.Recent = s.Subs.Recent[:storeMaxRecent]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(s.Events, func(j, i int) bool { return s.Events[i].Time.Before(s.Events[j].Time) })
|
||||||
|
if len(s.Events) > storeMaxRecent {
|
||||||
|
s.Events = s.Events[:storeMaxRecent]
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Create(to)
|
f, err := os.Create(to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "create file")
|
return errors.Wrap(err, "create file")
|
||||||
|
|
29
webhook.go
29
webhook.go
|
@ -63,6 +63,35 @@ func handleWebHookPush(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch hookType {
|
switch hookType {
|
||||||
|
case "donation":
|
||||||
|
var payload struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Amount float64 `json:"amount"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(body).Decode(&payload); err != nil {
|
||||||
|
logger.WithError(err).Error("Unable to decode payload")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := map[string]interface{}{
|
||||||
|
"name": payload.Name,
|
||||||
|
"amount": payload.Amount,
|
||||||
|
"message": payload.Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := subscriptions.SendAllSockets(msgTypeDonation, fields); err != nil {
|
||||||
|
log.WithError(err).Error("Unable to send update to all sockets")
|
||||||
|
}
|
||||||
|
|
||||||
|
store.WithModLock(func() error {
|
||||||
|
store.Donations.LastAmount = payload.Amount
|
||||||
|
store.Donations.LastDonator = &payload.Name
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
case "follow":
|
case "follow":
|
||||||
var payload struct {
|
var payload struct {
|
||||||
Data []struct {
|
Data []struct {
|
||||||
|
|
Loading…
Reference in a new issue