Skip to content

Commit

Permalink
Merge pull request #330 from BarryCarlyon/fix329
Browse files Browse the repository at this point in the history
Fixes #329
  • Loading branch information
Xemdo authored Jul 4, 2024
2 parents 89451e5 + 64a102e commit 2b10cc2
Show file tree
Hide file tree
Showing 39 changed files with 103 additions and 82 deletions.
5 changes: 3 additions & 2 deletions cmd/events/retrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func RetriggerCommand() (command *cobra.Command) {
}

command.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event (webhook only).")
command.Flags().StringVarP(&eventID, "id", "i", "", "ID of the event to be refired.")
command.Flags().StringVarP(&eventMessageID, "id", "i", "", "ID of the event to be refired.")
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().BoolVarP(&noConfig, "no-config", "D", false, "Disables the use of the configuration, if it exists.")
command.MarkFlagRequired("id")
Expand Down Expand Up @@ -49,7 +49,8 @@ func retriggerCmdRun(cmd *cobra.Command, args []string) error {
forwardAddress = defaults.ForwardAddress
}

res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{
//color.New().Add(color.FgGreen).Println(fmt.Sprintf(`Refire %v`, eventMessageID));
res, err := trigger.RefireEvent(eventMessageID, trigger.TriggerParameters{
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
Expand Down
4 changes: 2 additions & 2 deletions cmd/events/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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.")
command.Flags().StringVarP(&subscriptionID, "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.")
Expand Down Expand Up @@ -94,7 +94,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) error {
for i := 0; i < count; i++ {
res, err := trigger.Fire(trigger.TriggerParameters{
Event: args[0],
EventID: eventID,
SubscriptionID: subscriptionID,
EventMessageID: eventMessageID,
Transport: transport,
ForwardAddress: forwardAddress,
Expand Down
2 changes: 1 addition & 1 deletion cmd/events/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
fromUser string
toUser string
giftUser string
eventID string
subscriptionID string
eventMessageID string
secret string
eventStatus string
Expand Down
4 changes: 2 additions & 2 deletions cmd/events/verify_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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.")
command.Flags().StringVarP(&subscriptionID, "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.")
Expand Down Expand Up @@ -91,8 +91,8 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
EventMessageID: eventMessageID,
SubscriptionID: subscriptionID,
BroadcasterUserID: toUser,
Version: version,
})
Expand Down
3 changes: 2 additions & 1 deletion internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ package events
// MockEventParameters are used to craft the event; most of this data is prepopulated by lower services, such as the from/to users to avoid
// replicating logic across files
type MockEventParameters struct {
ID string
EventMessageID string
SubscriptionID string
Transport string
Trigger string
FromUserID string
Expand Down
2 changes: 1 addition & 1 deletion internal/events/trigger/forward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ForwardEvent(p ForwardParamters) (*http.Response, error) {

switch p.Transport {
case models.TransportWebhook:
req.Header.Set("Twitch-Eventsub-Message-Id", p.EventMessageID)
req.Header.Set("Twitch-Eventsub-Message-Id", p.ID)
req.Header.Set("Twitch-Eventsub-Subscription-Type", p.Event)
req.Header.Set("Twitch-Eventsub-Subscription-Version", p.SubscriptionVersion)
switch p.Type {
Expand Down
5 changes: 4 additions & 1 deletion internal/events/trigger/retrigger_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ func TestRefireEvent(t *testing.T) {
}))
defer ts.Close()

var eventMessageID = "testtriggereventid";

params := TriggerParameters{
Event: "gift",
EventMessageID: eventMessageID,
Transport: models.TransportWebhook,
IsAnonymous: false,
FromUser: "",
Expand All @@ -47,7 +50,7 @@ func TestRefireEvent(t *testing.T) {
err = json.Unmarshal([]byte(response), &body)
a.Nil(err)

json, err := RefireEvent(body.Subscription.ID, params)
json, err := RefireEvent(eventMessageID, params)
a.Nil(err)
a.Equal(response, json)
}
12 changes: 10 additions & 2 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type TriggerParameters struct {
GameID string
Tier string
Timestamp string
EventID string
SubscriptionID string
EventMessageID string
CharityCurrentValue int
CharityTargetValue int
Expand Down Expand Up @@ -99,10 +99,16 @@ func Fire(p TriggerParameters) (string, error) {
"Valid values are 1000, 2000 or 3000")
}

// the header twitch-eventsub-message-id
if p.EventMessageID == "" {
p.EventMessageID = util.RandomGUID()
}

// the body subscription.id
if p.SubscriptionID == "" {
p.SubscriptionID = util.RandomGUID()
}

if p.Timestamp == "" {
p.Timestamp = util.GetTimestamp().Format(time.RFC3339Nano)
} else {
Expand All @@ -117,7 +123,8 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
}

eventParamaters := events.MockEventParameters{
ID: p.EventID,
SubscriptionID: p.SubscriptionID,
EventMessageID: p.EventMessageID,
Trigger: p.Event,
Transport: p.Transport,
FromUserID: p.FromUser,
Expand Down Expand Up @@ -162,6 +169,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
return "", err
}

//color.New().Add(color.FgGreen).Println(fmt.Sprintf(`Insert into DB with %v`, resp.ID));
err = db.NewQuery(nil, 100).InsertIntoDB(database.EventCacheParameters{
ID: resp.ID,
Event: p.Event,
Expand Down
2 changes: 1 addition & 1 deletion internal/events/types/_template/_event_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/ad_break/ad_break_begin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -89,7 +89,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/authorization_grant/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook:
body := &models.AuthorizationRevokeEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -79,7 +79,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/authorization_revoke/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := &models.AuthorizationRevokeEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -87,7 +87,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/ban/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven

body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -152,7 +152,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := models.RedemptionEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand All @@ -70,7 +70,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
CreatedAt: params.Timestamp,
},
Event: models.RedemptionEventSubEvent{
ID: params.ID,
ID: util.RandomGUID(),
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Expand Down Expand Up @@ -114,7 +114,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
7 changes: 4 additions & 3 deletions internal/events/types/channel_points_reward/reward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
)

var transportsSupported = map[string]bool{
Expand Down Expand Up @@ -48,7 +49,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand All @@ -63,7 +64,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
CreatedAt: params.Timestamp,
},
Event: models.RewardEventSubEvent{
ID: params.ID,
ID: util.RandomGUID(),
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Expand Down Expand Up @@ -128,7 +129,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.ToUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/channel_update_v1/channel_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
body := &models.EventsubResponse{
// make the eventsub response (if supported)
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -99,7 +99,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/channel_update_v2/channel_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
body := &models.EventsubResponse{
// make the eventsub response (if supported)
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -102,7 +102,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/charity/charity_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Status: params.SubscriptionStatus,
Expand Down Expand Up @@ -199,7 +199,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
ToUser: params.ToUserID,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/cheer/cheer_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook, models.TransportWebSocket:
body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -97,7 +97,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
4 changes: 2 additions & 2 deletions internal/events/types/drop/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}
body := &models.DropsEntitlementEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
ID: params.SubscriptionID,
Status: params.SubscriptionStatus,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Expand Down Expand Up @@ -121,7 +121,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
}

return events.MockEventResponse{
ID: params.ID,
ID: params.EventMessageID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
Expand Down
Loading

0 comments on commit 2b10cc2

Please sign in to comment.