Skip to content

Commit

Permalink
Merge branch 'main' into feat/eventsub-configure
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet authored Dec 13, 2023
2 parents c1aef88 + 414972f commit 5d50e42
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 178 deletions.
17 changes: 10 additions & 7 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func init() {
triggerCmd.Flags().StringVarP(&subscriptionStatus, "subscription-status", "r", "enabled", "Status of the Subscription object (.subscription.status in JSON). Defaults to \"enabled\".")
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.")
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.")
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of subscriptions, bits, or channel points redeemed/used in the event.")
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of drops, subscriptions, bits, or channel points redeemed/used in the event.")
triggerCmd.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")
triggerCmd.Flags().StringVarP(&gameID, "game-id", "G", "", "Sets the game/category ID for applicable events.")
triggerCmd.Flags().StringVarP(&tier, "tier", "", "", "Sets the subscription tier. Valid values are 1000, 2000, and 3000.")
Expand Down Expand Up @@ -189,6 +189,7 @@ func init() {
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
verifyCmd.Flags().StringVarP(&version, "version", "v", "", "Chooses the EventSub version used for a specific event. Not required for most events.")
verifyCmd.Flags().BoolVarP(&noConfig, "no-config", "D", false, "Disables the use of the configuration, if it exists.")
verifyCmd.Flags().StringVarP(&toUser, "broadcaster", "b", "", "User ID of the broadcaster for the verification event.")

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

_, err := verify.VerifyWebhookSubscription(verify.VerifyParameters{
Event: args[0],
Transport: transport,
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
Event: args[0],
Transport: transport,
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
BroadcasterUserID: toUser,
Version: version,
})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ This command takes the same arguments as [Trigger](#trigger).

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
| `--broadcaster` | `-b` | The broadcaster's user ID to be used for verification | `-b 1234` | N |
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
| `--no-config` | `-D` | Disables the use of the configuration values should they exist. | `-D` | N |
| `--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 |
Expand Down
53 changes: 36 additions & 17 deletions internal/events/types/drop/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
case models.TransportWebhook:
campaignId := util.RandomGUID()

dropEvents := []models.DropsEntitlementEventSubEvent{
{
ID: util.RandomGUID(),
Data: models.DropsEntitlementEventSubEventData{
OrganizationID: params.FromUserID,
CategoryID: params.GameID,
CategoryName: "Special Events",
CampaignID: campaignId,
EntitlementID: util.RandomGUID(),
BenefitID: params.ItemID,
UserID: params.ToUserID,
UserName: params.ToUserName,
UserLogin: params.ToUserName,
CreatedAt: params.Timestamp,
},
},
}

for i := int64(1); i < params.Cost; i++ {
// for the new events, we'll use the entitlement above except generating new users as to avoid conflicting drops
dropEvents = append(dropEvents, models.DropsEntitlementEventSubEvent{
ID: util.RandomGUID(),
Data: models.DropsEntitlementEventSubEventData{
OrganizationID: params.FromUserID,
CategoryID: params.GameID,
CategoryName: "Special Events",
CampaignID: campaignId,
EntitlementID: util.RandomGUID(),
BenefitID: params.ItemID,
UserID: util.RandomUserID(),
UserName: params.ToUserName,
UserLogin: params.ToUserName,
CreatedAt: params.Timestamp,
}})
}
body := &models.DropsEntitlementEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Expand All @@ -59,23 +94,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
Cost: 0,
CreatedAt: params.Timestamp,
},
Events: []models.DropsEntitlementEventSubEvent{
{
ID: util.RandomGUID(),
Data: models.DropsEntitlementEventSubEventData{
OrganizationID: params.FromUserID,
CategoryID: params.GameID,
CategoryName: "Special Events",
CampaignID: campaignId,
EntitlementID: util.RandomGUID(),
BenefitID: params.ItemID,
UserID: params.ToUserID,
UserName: params.ToUserName,
UserLogin: params.ToUserName,
CreatedAt: params.Timestamp,
},
},
},
Events: dropEvents,
}
event, err = json.Marshal(body)
if err != nil {
Expand Down
25 changes: 15 additions & 10 deletions internal/events/verify/subscription_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (
)

type VerifyParameters struct {
Transport string
Timestamp string
Event string
ForwardAddress string
Secret string
EventID string
Version string
Transport string
Timestamp string
Event string
ForwardAddress string
Secret string
EventID string
Version string
BroadcasterUserID string
}

type VerifyResponse struct {
Expand Down Expand Up @@ -55,7 +56,11 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
p.EventID = util.RandomGUID()
}

body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), challenge, p.ForwardAddress)
if p.BroadcasterUserID == "" {
p.BroadcasterUserID = util.RandomUserID()
}

body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), p.BroadcasterUserID, challenge, p.ForwardAddress)
if err != nil {
return VerifyResponse{}, err
}
Expand Down Expand Up @@ -133,7 +138,7 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
return r, nil
}

func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, challenge string, callback string) (trigger.TriggerResponse, error) {
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, broadcaster string, challenge string, callback string) (trigger.TriggerResponse, error) {
var res []byte
var err error
ts := util.GetTimestamp().Format(time.RFC3339Nano)
Expand All @@ -147,7 +152,7 @@ func generateWebhookSubscriptionBody(transport string, eventID string, event str
Type: event,
Version: subscriptionVersion,
Condition: models.EventsubCondition{
BroadcasterUserID: util.RandomUserID(),
BroadcasterUserID: broadcaster,
},
Transport: models.EventsubTransport{
Method: "webhook",
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/charity/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GetCharityCampaignResponse struct {
CharityDescription string `json:"charity_description"`
CharityLogo string `json:"charity_logo"`
CharityWebsite string `json:"charity_website"`
CurrentAmount CharityAmount `json:"current_ammount"`
CurrentAmount CharityAmount `json:"current_amount"`
TargetAmount CharityAmount `json:"target_amount"`
}

Expand Down
22 changes: 12 additions & 10 deletions internal/mock_api/endpoints/charity/donations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ var donationsScopesByMethod = map[string][]string{
type CharityDonations struct{}

type GetCharityDonationsResponse struct {
ID string `json:"campaign_id"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
TargetAmount CharityAmount `json:"target_amount"`
ID string `json:"id"`
CampaignID string `json:"campaign_id"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
Amount CharityAmount `json:"amount"`
}

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

for i := 0; i < first; i++ {
d := GetCharityDonationsResponse{
ID: util.RandomGUID(),
UserID: userCtx.UserID,
UserName: user.DisplayName,
UserLogin: user.UserLogin,
TargetAmount: CharityAmount{
ID: util.RandomGUID(),
CampaignID: util.RandomGUID(),
UserID: userCtx.UserID,
UserName: user.DisplayName,
UserLogin: user.UserLogin,
Amount: CharityAmount{
Value: rand.Intn(150000-300) + 300, // Between $3 and $1,500
DecimalPlaces: 2,
Currency: "USD",
Expand Down
Loading

0 comments on commit 5d50e42

Please sign in to comment.