Skip to content

Commit 5d50e42

Browse files
authored
Merge branch 'main' into feat/eventsub-configure
2 parents c1aef88 + 414972f commit 5d50e42

7 files changed

Lines changed: 208 additions & 178 deletions

File tree

cmd/events.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func init() {
160160
triggerCmd.Flags().StringVarP(&subscriptionStatus, "subscription-status", "r", "enabled", "Status of the Subscription object (.subscription.status in JSON). Defaults to \"enabled\".")
161161
triggerCmd.Flags().StringVarP(&itemID, "item-id", "i", "", "Manually set the ID of the event payload item (for example the reward ID in redemption events). For stream events, this is the game ID.")
162162
triggerCmd.Flags().StringVarP(&itemName, "item-name", "n", "", "Manually set the name of the event payload item (for example the reward ID in redemption events). For stream events, this is the game title.")
163-
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of subscriptions, bits, or channel points redeemed/used in the event.")
163+
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of drops, subscriptions, bits, or channel points redeemed/used in the event.")
164164
triggerCmd.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")
165165
triggerCmd.Flags().StringVarP(&gameID, "game-id", "G", "", "Sets the game/category ID for applicable events.")
166166
triggerCmd.Flags().StringVarP(&tier, "tier", "", "", "Sets the subscription tier. Valid values are 1000, 2000, and 3000.")
@@ -189,6 +189,7 @@ func init() {
189189
verifyCmd.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
190190
verifyCmd.Flags().StringVarP(&version, "version", "v", "", "Chooses the EventSub version used for a specific event. Not required for most events.")
191191
verifyCmd.Flags().BoolVarP(&noConfig, "no-config", "D", false, "Disables the use of the configuration, if it exists.")
192+
verifyCmd.Flags().StringVarP(&toUser, "broadcaster", "b", "", "User ID of the broadcaster for the verification event.")
192193

193194
// websocket flags
194195
/// flags for start-server
@@ -358,12 +359,14 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
358359
}
359360

360361
_, err := verify.VerifyWebhookSubscription(verify.VerifyParameters{
361-
Event: args[0],
362-
Transport: transport,
363-
ForwardAddress: forwardAddress,
364-
Secret: secret,
365-
Timestamp: timestamp,
366-
EventID: eventID,
362+
Event: args[0],
363+
Transport: transport,
364+
ForwardAddress: forwardAddress,
365+
Secret: secret,
366+
Timestamp: timestamp,
367+
EventID: eventID,
368+
BroadcasterUserID: toUser,
369+
Version: version,
367370
})
368371

369372
if err != nil {

docs/event.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ This command takes the same arguments as [Trigger](#trigger).
171171

172172
| Flag | Shorthand | Description | Example | Required? (Y/N) |
173173
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
174+
| `--broadcaster` | `-b` | The broadcaster's user ID to be used for verification | `-b 1234` | N |
174175
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
175176
| `--no-config` | `-D` | Disables the use of the configuration values should they exist. | `-D` | N |
176177
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length. | `-s testsecret` | N |

internal/events/types/drop/drop.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
4141
case models.TransportWebhook:
4242
campaignId := util.RandomGUID()
4343

44+
dropEvents := []models.DropsEntitlementEventSubEvent{
45+
{
46+
ID: util.RandomGUID(),
47+
Data: models.DropsEntitlementEventSubEventData{
48+
OrganizationID: params.FromUserID,
49+
CategoryID: params.GameID,
50+
CategoryName: "Special Events",
51+
CampaignID: campaignId,
52+
EntitlementID: util.RandomGUID(),
53+
BenefitID: params.ItemID,
54+
UserID: params.ToUserID,
55+
UserName: params.ToUserName,
56+
UserLogin: params.ToUserName,
57+
CreatedAt: params.Timestamp,
58+
},
59+
},
60+
}
61+
62+
for i := int64(1); i < params.Cost; i++ {
63+
// for the new events, we'll use the entitlement above except generating new users as to avoid conflicting drops
64+
dropEvents = append(dropEvents, models.DropsEntitlementEventSubEvent{
65+
ID: util.RandomGUID(),
66+
Data: models.DropsEntitlementEventSubEventData{
67+
OrganizationID: params.FromUserID,
68+
CategoryID: params.GameID,
69+
CategoryName: "Special Events",
70+
CampaignID: campaignId,
71+
EntitlementID: util.RandomGUID(),
72+
BenefitID: params.ItemID,
73+
UserID: util.RandomUserID(),
74+
UserName: params.ToUserName,
75+
UserLogin: params.ToUserName,
76+
CreatedAt: params.Timestamp,
77+
}})
78+
}
4479
body := &models.DropsEntitlementEventSubResponse{
4580
Subscription: models.EventsubSubscription{
4681
ID: params.ID,
@@ -59,23 +94,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
5994
Cost: 0,
6095
CreatedAt: params.Timestamp,
6196
},
62-
Events: []models.DropsEntitlementEventSubEvent{
63-
{
64-
ID: util.RandomGUID(),
65-
Data: models.DropsEntitlementEventSubEventData{
66-
OrganizationID: params.FromUserID,
67-
CategoryID: params.GameID,
68-
CategoryName: "Special Events",
69-
CampaignID: campaignId,
70-
EntitlementID: util.RandomGUID(),
71-
BenefitID: params.ItemID,
72-
UserID: params.ToUserID,
73-
UserName: params.ToUserName,
74-
UserLogin: params.ToUserName,
75-
CreatedAt: params.Timestamp,
76-
},
77-
},
78-
},
97+
Events: dropEvents,
7998
}
8099
event, err = json.Marshal(body)
81100
if err != nil {

internal/events/verify/subscription_verify.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
)
2020

2121
type VerifyParameters struct {
22-
Transport string
23-
Timestamp string
24-
Event string
25-
ForwardAddress string
26-
Secret string
27-
EventID string
28-
Version string
22+
Transport string
23+
Timestamp string
24+
Event string
25+
ForwardAddress string
26+
Secret string
27+
EventID string
28+
Version string
29+
BroadcasterUserID string
2930
}
3031

3132
type VerifyResponse struct {
@@ -55,7 +56,11 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
5556
p.EventID = util.RandomGUID()
5657
}
5758

58-
body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), challenge, p.ForwardAddress)
59+
if p.BroadcasterUserID == "" {
60+
p.BroadcasterUserID = util.RandomUserID()
61+
}
62+
63+
body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), p.BroadcasterUserID, challenge, p.ForwardAddress)
5964
if err != nil {
6065
return VerifyResponse{}, err
6166
}
@@ -133,7 +138,7 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
133138
return r, nil
134139
}
135140

136-
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, challenge string, callback string) (trigger.TriggerResponse, error) {
141+
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, broadcaster string, challenge string, callback string) (trigger.TriggerResponse, error) {
137142
var res []byte
138143
var err error
139144
ts := util.GetTimestamp().Format(time.RFC3339Nano)
@@ -147,7 +152,7 @@ func generateWebhookSubscriptionBody(transport string, eventID string, event str
147152
Type: event,
148153
Version: subscriptionVersion,
149154
Condition: models.EventsubCondition{
150-
BroadcasterUserID: util.RandomUserID(),
155+
BroadcasterUserID: broadcaster,
151156
},
152157
Transport: models.EventsubTransport{
153158
Method: "webhook",

internal/mock_api/endpoints/charity/campaigns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type GetCharityCampaignResponse struct {
4242
CharityDescription string `json:"charity_description"`
4343
CharityLogo string `json:"charity_logo"`
4444
CharityWebsite string `json:"charity_website"`
45-
CurrentAmount CharityAmount `json:"current_ammount"`
45+
CurrentAmount CharityAmount `json:"current_amount"`
4646
TargetAmount CharityAmount `json:"target_amount"`
4747
}
4848

internal/mock_api/endpoints/charity/donations.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ var donationsScopesByMethod = map[string][]string{
3434
type CharityDonations struct{}
3535

3636
type GetCharityDonationsResponse struct {
37-
ID string `json:"campaign_id"`
38-
UserID string `json:"user_id"`
39-
UserLogin string `json:"user_login"`
40-
UserName string `json:"user_name"`
41-
TargetAmount CharityAmount `json:"target_amount"`
37+
ID string `json:"id"`
38+
CampaignID string `json:"campaign_id"`
39+
UserID string `json:"user_id"`
40+
UserLogin string `json:"user_login"`
41+
UserName string `json:"user_name"`
42+
Amount CharityAmount `json:"amount"`
4243
}
4344

4445
func (e CharityDonations) Path() string { return "/charity/donations" }
@@ -98,11 +99,12 @@ func getCharityDonations(w http.ResponseWriter, r *http.Request) {
9899

99100
for i := 0; i < first; i++ {
100101
d := GetCharityDonationsResponse{
101-
ID: util.RandomGUID(),
102-
UserID: userCtx.UserID,
103-
UserName: user.DisplayName,
104-
UserLogin: user.UserLogin,
105-
TargetAmount: CharityAmount{
102+
ID: util.RandomGUID(),
103+
CampaignID: util.RandomGUID(),
104+
UserID: userCtx.UserID,
105+
UserName: user.DisplayName,
106+
UserLogin: user.UserLogin,
107+
Amount: CharityAmount{
106108
Value: rand.Intn(150000-300) + 300, // Between $3 and $1,500
107109
DecimalPlaces: 2,
108110
Currency: "USD",

0 commit comments

Comments
 (0)