Skip to content

Commit

Permalink
Merge branch 'dnsge:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont authored Mar 10, 2024
2 parents 3c04e66 + e4555a0 commit 6de474a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dnsge/twitch-eventsub-framework
go 1.17

require (
github.com/dnsge/twitch-eventsub-bindings v1.1.0
github.com/dnsge/twitch-eventsub-bindings v1.2.1
github.com/mozillazg/go-httpheader v0.3.0
github.com/stretchr/testify v1.8.2
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnsge/twitch-eventsub-bindings v1.1.0 h1:11MBREP4tdcxfGNxDRSu86ASxlnMsSCo0YnLV01T0Ws=
github.com/dnsge/twitch-eventsub-bindings v1.1.0/go.mod h1:Zbj+TpgcdNu4Gj6+6KG/+A7EvWMDbzQ+UGm35P918pc=
github.com/dnsge/twitch-eventsub-bindings v1.2.1 h1:PTn3o8ctiH9rdzXPGqSG5iIqIvGKV9EZ9pU54zDUjp4=
github.com/dnsge/twitch-eventsub-bindings v1.2.1/go.mod h1:Zbj+TpgcdNu4Gj6+6KG/+A7EvWMDbzQ+UGm35P918pc=
github.com/mozillazg/go-httpheader v0.3.0 h1:3brX5z8HTH+0RrNA1362Rc3HsaxyWEKtGY45YrhuINM=
github.com/mozillazg/go-httpheader v0.3.0/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
164 changes: 140 additions & 24 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package eventsub_framework

import (
"encoding/json"
esb "github.com/dnsge/twitch-eventsub-bindings"
"github.com/mozillazg/go-httpheader"
"io"
"net/http"

esb "github.com/dnsge/twitch-eventsub-bindings"
"github.com/mozillazg/go-httpheader"
)

const (
Expand Down Expand Up @@ -35,35 +36,74 @@ type SubHandler struct {
HandleChannelFollow func(h *esb.ResponseHeaders, event *esb.EventChannelFollow)
HandleUserUpdate func(h *esb.ResponseHeaders, event *esb.EventUserUpdate)

HandleChannelSubscribe func(h *esb.ResponseHeaders, event *esb.EventChannelSubscribe)
HandleChannelSubscriptionEnd func(h *esb.ResponseHeaders, event *esb.EventChannelSubscriptionEnd)
HandleChannelSubscriptionGift func(h *esb.ResponseHeaders, event *esb.EventChannelSubscriptionGift)
HandleChannelSubscriptionMessage func(h *esb.ResponseHeaders, event *esb.EventChannelSubscriptionMessage)
HandleChannelCheer func(h *esb.ResponseHeaders, event *esb.EventChannelCheer)
HandleChannelRaid func(h *esb.ResponseHeaders, event *esb.EventChannelRaid)
HandleChannelSubscribe func(h *esb.ResponseHeaders, event *esb.EventChannelSubscribe)
HandleChannelSubscriptionEnd func(
h *esb.ResponseHeaders,
event *esb.EventChannelSubscriptionEnd,
)
HandleChannelSubscriptionGift func(
h *esb.ResponseHeaders,
event *esb.EventChannelSubscriptionGift,
)
HandleChannelSubscriptionMessage func(
h *esb.ResponseHeaders,
event *esb.EventChannelSubscriptionMessage,
)
HandleChannelCheer func(h *esb.ResponseHeaders, event *esb.EventChannelCheer)
HandleChannelRaid func(h *esb.ResponseHeaders, event *esb.EventChannelRaid)

HandleChannelBan func(h *esb.ResponseHeaders, event *esb.EventChannelBan)
HandleChannelUnban func(h *esb.ResponseHeaders, event *esb.EventChannelUnban)
HandleChannelModeratorAdd func(h *esb.ResponseHeaders, event *esb.EventChannelModeratorAdd)
HandleChannelModeratorRemove func(h *esb.ResponseHeaders, event *esb.EventChannelModeratorRemove)

HandleChannelPointsRewardAdd func(h *esb.ResponseHeaders, event *esb.EventChannelPointsRewardAdd)
HandleChannelPointsRewardUpdate func(h *esb.ResponseHeaders, event *esb.EventChannelPointsRewardUpdate)
HandleChannelPointsRewardRemove func(h *esb.ResponseHeaders, event *esb.EventChannelPointsRewardRemove)
HandleChannelPointsRewardRedemptionAdd func(h *esb.ResponseHeaders, event *esb.EventChannelPointsRewardRedemptionAdd)
HandleChannelPointsRewardRedemptionUpdate func(h *esb.ResponseHeaders, event *esb.EventChannelPointsRewardRedemptionUpdate)
HandleChannelPointsRewardAdd func(
h *esb.ResponseHeaders,
event *esb.EventChannelPointsRewardAdd,
)
HandleChannelPointsRewardUpdate func(
h *esb.ResponseHeaders,
event *esb.EventChannelPointsRewardUpdate,
)
HandleChannelPointsRewardRemove func(
h *esb.ResponseHeaders,
event *esb.EventChannelPointsRewardRemove,
)
HandleChannelPointsRewardRedemptionAdd func(
h *esb.ResponseHeaders,
event *esb.EventChannelPointsRewardRedemptionAdd,
)
HandleChannelPointsRewardRedemptionUpdate func(
h *esb.ResponseHeaders,
event *esb.EventChannelPointsRewardRedemptionUpdate,
)

HandleChannelPollBegin func(h *esb.ResponseHeaders, event *esb.EventChannelPollBegin)
HandleChannelPollProgress func(h *esb.ResponseHeaders, event *esb.EventChannelPollProgress)
HandleChannelPollEnd func(h *esb.ResponseHeaders, event *esb.EventChannelPollEnd)

HandleChannelPredictionBegin func(h *esb.ResponseHeaders, event *esb.EventChannelPredictionBegin)
HandleChannelPredictionProgress func(h *esb.ResponseHeaders, event *esb.EventChannelPredictionProgress)
HandleChannelPredictionLock func(h *esb.ResponseHeaders, event *esb.EventChannelPredictionLock)
HandleChannelPredictionEnd func(h *esb.ResponseHeaders, event *esb.EventChannelPredictionEnd)

HandleDropEntitlementGrant func(h *esb.ResponseHeaders, event *esb.EventDropEntitlementGrant)
HandleExtensionBitsTransactionCreate func(h *esb.ResponseHeaders, event *esb.EventBitsTransactionCreate)
HandleChannelPredictionBegin func(
h *esb.ResponseHeaders,
event *esb.EventChannelPredictionBegin,
)
HandleChannelPredictionProgress func(
h *esb.ResponseHeaders,
event *esb.EventChannelPredictionProgress,
)
HandleChannelPredictionLock func(
h *esb.ResponseHeaders,
event *esb.EventChannelPredictionLock,
)
HandleChannelPredictionEnd func(h *esb.ResponseHeaders, event *esb.EventChannelPredictionEnd)

HandleDropEntitlementGrant func(
h *esb.ResponseHeaders,
event *esb.EventDropEntitlementGrant,
)
HandleExtensionBitsTransactionCreate func(
h *esb.ResponseHeaders,
event *esb.EventBitsTransactionCreate,
)

HandleGoalBegin func(h *esb.ResponseHeaders, event *esb.EventGoals)
HandleGoalProgress func(h *esb.ResponseHeaders, event *esb.EventGoals)
Expand All @@ -77,7 +117,26 @@ type SubHandler struct {
HandleStreamOffline func(h *esb.ResponseHeaders, event *esb.EventStreamOffline)

HandleUserAuthorizationGrant func(h *esb.ResponseHeaders, event *esb.EventUserAuthorizationGrant)
HandleUserAuthorizationRevoke func(h *esb.ResponseHeaders, event *esb.EventUserAuthorizationRevoke)
HandleUserAuthorizationRevoke func(
h *esb.ResponseHeaders,
event *esb.EventUserAuthorizationRevoke,
)

HandleChannelChatMessage func(h *esb.ResponseHeaders, event *esb.EventChannelChatMessage)
HandleChannelChatClear func(h *esb.ResponseHeaders, event *esb.EventChannelChatClear)
HandleChannelChatClearUserMessages func(
h *esb.ResponseHeaders,
event *esb.EventChannelChatClearUserMessages,
)
HandleChannelChatMessageDelete func(
h *esb.ResponseHeaders,
event *esb.EventChannelChatMessageDelete,
)

HandleChannelChatNotification func(
h *esb.ResponseHeaders,
event *esb.EventChannelChatNotification,
)
}

func NewSubHandler(doSignatureVerification bool, secret []byte) *SubHandler {
Expand Down Expand Up @@ -147,7 +206,11 @@ func (s *SubHandler) handlePost(w http.ResponseWriter, r *http.Request) {
// checkIfDuplicate returns whether the IDTracker reports this notification is
// a duplicate. If it is a duplicate, it writes a 2xx response and returns true.
// Otherwise, it returns false.
func (s *SubHandler) checkIfDuplicate(w http.ResponseWriter, r *http.Request, h *esb.ResponseHeaders) (bool, error) {
func (s *SubHandler) checkIfDuplicate(
w http.ResponseWriter,
r *http.Request,
h *esb.ResponseHeaders,
) (bool, error) {
if s.IDTracker != nil {
duplicate, err := s.IDTracker.AddAndCheckIfDuplicate(r.Context(), h.MessageID)
if err != nil {
Expand All @@ -166,7 +229,11 @@ func (s *SubHandler) checkIfDuplicate(w http.ResponseWriter, r *http.Request, h
return false, nil
}

func (s *SubHandler) handleVerification(w http.ResponseWriter, bodyBytes []byte, headers *esb.ResponseHeaders) {
func (s *SubHandler) handleVerification(
w http.ResponseWriter,
bodyBytes []byte,
headers *esb.ResponseHeaders,
) {
var data esb.SubscriptionChallenge
if err := json.Unmarshal(bodyBytes, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
Expand All @@ -182,7 +249,11 @@ func (s *SubHandler) handleVerification(w http.ResponseWriter, bodyBytes []byte,
}
}

func (s *SubHandler) handleNotification(w http.ResponseWriter, bodyBytes []byte, h *esb.ResponseHeaders) {
func (s *SubHandler) handleNotification(
w http.ResponseWriter,
bodyBytes []byte,
h *esb.ResponseHeaders,
) {
var notification esb.EventNotification
if err := json.Unmarshal(bodyBytes, &notification); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
Expand Down Expand Up @@ -524,6 +595,51 @@ func (s *SubHandler) handleNotification(w http.ResponseWriter, bodyBytes []byte,
if s.HandleUserUpdate != nil {
go s.HandleUserUpdate(h, &data)
}
case "channel.chat.message":
var data esb.EventChannelChatMessage
if err := json.Unmarshal(event, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
if s.HandleChannelChatMessage != nil {
go s.HandleChannelChatMessage(h, &data)
}
case "channel.chat.clear":
var data esb.EventChannelChatClear
if err := json.Unmarshal(event, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
if s.HandleChannelChatClear != nil {
go s.HandleChannelChatClear(h, &data)
}
case "channel.chat.clear_user_messages":
var data esb.EventChannelChatClearUserMessages
if err := json.Unmarshal(event, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
if s.HandleChannelChatClearUserMessages != nil {
go s.HandleChannelChatClearUserMessages(h, &data)
}
case "channel.chat.message_delete":
var data esb.EventChannelChatMessageDelete
if err := json.Unmarshal(event, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
if s.HandleChannelChatMessageDelete != nil {
go s.HandleChannelChatMessageDelete(h, &data)
}
case "channel.chat.notification":
var data esb.EventChannelChatNotification
if err := json.Unmarshal(event, &data); err != nil {
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
return
}
if s.HandleChannelChatNotification != nil {
go s.HandleChannelChatNotification(h, &data)
}
default:
http.Error(w, "Unknown notification type", http.StatusBadRequest)
return
Expand Down

0 comments on commit 6de474a

Please sign in to comment.