mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
check for double eventsub events and drop them
This commit is contained in:
parent
d55d0bd73e
commit
13c7fd30b2
|
@ -43,6 +43,11 @@ type eventSubNotification struct {
|
||||||
Event json.RawMessage `json:"event"`
|
Event json.RawMessage `json:"event"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eventsubMessageId stores the last message id of an event sub. Twitch resends events
|
||||||
|
// if it is unsure that you have gotten them so we check if the last event has the same
|
||||||
|
// message id and if it does discard the event.
|
||||||
|
var lastEventSubSubscriptionId = ""
|
||||||
|
|
||||||
func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -70,16 +75,22 @@ func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if vals.Subscription.ID == lastEventSubSubscriptionId {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
lastEventSubSubscriptionId = vals.Subscription.ID
|
||||||
|
}
|
||||||
|
|
||||||
switch vals.Subscription.Type {
|
switch vals.Subscription.Type {
|
||||||
case helix.EventSubTypeStreamOnline:
|
case helix.EventSubTypeStreamOnline:
|
||||||
var liveEvent helix.EventSubStreamOnlineEvent
|
var liveEvent helix.EventSubStreamOnlineEvent
|
||||||
|
|
||||||
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&liveEvent)
|
err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&liveEvent)
|
||||||
log.Printf("got stream online event webhook: %s is live\n", liveEvent.BroadcasterUserName)
|
log.Printf("got stream online event webhook: %s is live\n", liveEvent.BroadcasterUserName)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
|
|
||||||
// Sleep for 5 seconds before checking querying game and title as they might not be updated.
|
time.Sleep(5 * time.Second)
|
||||||
time.Sleep(time.Duration(5) * time.Second)
|
|
||||||
|
|
||||||
game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin)
|
game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin)
|
||||||
title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin)
|
title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin)
|
||||||
|
|
Loading…
Reference in a new issue