Skip to content

Commit

Permalink
Merge pull request #322 from SputNikPlop/event-message-id
Browse files Browse the repository at this point in the history
fix: split event message id
  • Loading branch information
Xemdo authored Jun 18, 2024
2 parents fb0262e + 0160d80 commit 30cf222
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/events/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func TriggerCommand() (command *cobra.Command) {
command.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")
command.Flags().StringVarP(&gameID, "game-id", "G", "", "Sets the game/category ID for applicable events.")
command.Flags().StringVarP(&tier, "tier", "", "", "Sets the subscription tier. Valid values are 1000, 2000, and 3000.")
command.Flags().StringVarP(&eventID, "subscription-id", "u", "", "Manually set the subscription/event ID of the event itself.") // TODO: This description will need to change with https://github.com/twitchdev/twitch-cli/issues/184
command.Flags().StringVarP(&eventID, "subscription-id", "u", "", "Manually set the subscription/event ID of the event itself.")
command.Flags().StringVarP(&eventMessageID, "event-id", "I", "", "Manually set the Twitch-Eventsub-Message-Id header value for the event.")
command.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
command.Flags().IntVar(&charityCurrentValue, "charity-current-value", 0, "Only used for \"charity-*\" events. Manually set the current dollar value for charity events.")
command.Flags().IntVar(&charityTargetValue, "charity-target-value", 1500000, "Only used for \"charity-*\" events. Manually set the target dollar value for charity events.")
Expand Down Expand Up @@ -94,6 +95,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) error {
res, err := trigger.Fire(trigger.TriggerParameters{
Event: args[0],
EventID: eventID,
EventMessageID: eventMessageID,
Transport: transport,
ForwardAddress: forwardAddress,
FromUser: fromUser,
Expand Down
1 change: 1 addition & 0 deletions cmd/events/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
toUser string
giftUser string
eventID string
eventMessageID string
secret string
eventStatus string
subscriptionStatus string
Expand Down
4 changes: 3 additions & 1 deletion cmd/events/verify_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func VerifySubscriptionCommand() (command *cobra.Command) {
command.Flags().StringVarP(&transport, "transport", "T", "webhook", fmt.Sprintf("Preferred transport method for event. Defaults to EventSub.\nSupported values: %s", events.ValidTransports()))
command.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length.")
command.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
command.Flags().StringVarP(&eventID, "subscription-id", "u", "", "Manually set the subscription/event ID of the event itself.") // TODO: This description will need to change with https://github.com/twitchdev/twitch-cli/issues/184
command.Flags().StringVarP(&eventID, "subscription-id", "u", "", "Manually set the subscription/event ID of the event itself.")
command.Flags().StringVarP(&eventMessageID, "event-id", "I", "", "Manually set the Twitch-Eventsub-Message-Id header value for the event.")
command.Flags().StringVarP(&version, "version", "v", "", "Chooses the EventSub version used for a specific event. Not required for most events.")
command.Flags().BoolVarP(&noConfig, "no-config", "D", false, "Disables the use of the configuration, if it exists.")
command.Flags().StringVarP(&toUser, "broadcaster", "b", "", "User ID of the broadcaster for the verification event.")
Expand Down Expand Up @@ -91,6 +92,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
EventMessageID: eventMessageID,
BroadcasterUserID: toUser,
Version: version,
})
Expand Down
7 changes: 4 additions & 3 deletions internal/events/trigger/forward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ForwardParamters struct {
Timestamp string
Secret string
Event string
EventMessageID string
Method string
Type string
SubscriptionVersion string
Expand Down Expand Up @@ -67,7 +68,7 @@ func ForwardEvent(p ForwardParamters) (*http.Response, error) {

switch p.Transport {
case models.TransportWebhook:
req.Header.Set("Twitch-Eventsub-Message-Id", p.ID)
req.Header.Set("Twitch-Eventsub-Message-Id", p.EventMessageID)
req.Header.Set("Twitch-Eventsub-Subscription-Type", p.Event)
req.Header.Set("Twitch-Eventsub-Subscription-Version", p.SubscriptionVersion)
switch p.Type {
Expand All @@ -83,10 +84,10 @@ func ForwardEvent(p ForwardParamters) (*http.Response, error) {
// Twitch only supports IPv4 currently, so we will force this TCP connection to only use IPv4
var dialer net.Dialer
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = func (ctx context.Context, network, addr string) (net.Conn, error) {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, "tcp4", addr)
}

if p.Secret != "" {
getSignatureHeader(req, p.ID, p.Secret, p.Transport, p.Timestamp, p.JSON)
}
Expand Down
1 change: 1 addition & 0 deletions internal/events/trigger/retrigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func RefireEvent(id string, p TriggerParameters) (string, error) {
Secret: p.Secret,
JSON: []byte(res.JSON),
Event: topic,
EventMessageID: "",
Type: EventSubMessageTypeNotification,
SubscriptionVersion: e.SubscriptionVersion(),
})
Expand Down
8 changes: 5 additions & 3 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type TriggerParameters struct {
GameID string
Tier string
Timestamp string
EventID string // Also serves as subscription ID. See https://github.com/twitchdev/twitch-cli/issues/184
EventID string
EventMessageID string
CharityCurrentValue int
CharityTargetValue int
ClientID string
Expand Down Expand Up @@ -98,8 +99,8 @@ func Fire(p TriggerParameters) (string, error) {
"Valid values are 1000, 2000 or 3000")
}

if p.EventID == "" {
p.EventID = util.RandomGUID()
if p.EventMessageID == "" {
p.EventMessageID = util.RandomGUID()
}

if p.Timestamp == "" {
Expand Down Expand Up @@ -194,6 +195,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
Secret: p.Secret,
ForwardAddress: p.ForwardAddress,
Event: topic,
EventMessageID: p.EventMessageID,
Type: messageType,
SubscriptionVersion: e.SubscriptionVersion(),
})
Expand Down
5 changes: 3 additions & 2 deletions internal/events/verify/subscription_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type VerifyParameters struct {
ForwardAddress string
Secret string
EventID string
EventMessageID string
Version string
BroadcasterUserID string
}
Expand Down Expand Up @@ -52,8 +53,8 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
}
}

if p.EventID == "" {
p.EventID = util.RandomGUID()
if p.EventMessageID == "" {
p.EventMessageID = util.RandomGUID()
}

if p.BroadcasterUserID == "" {
Expand Down

0 comments on commit 30cf222

Please sign in to comment.