-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating eventsub to match production
- Loading branch information
lleadbet
committed
Jul 26, 2021
1 parent
3388f14
commit 3f42430
Showing
22 changed files
with
1,332 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ twitch-cli | |
*.html | ||
|
||
# Editor configs | ||
.vsvode/ | ||
.vscode/ | ||
.idea/ | ||
|
||
# junk files | ||
|
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package drop | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"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{ | ||
models.TransportWebSub: false, | ||
models.TransportEventSub: true, | ||
} | ||
|
||
var triggerSupported = []string{"drop"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportEventSub: { | ||
"drop": "drop.entitlement.grant", | ||
}, | ||
} | ||
|
||
type Event struct{} | ||
|
||
func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { | ||
var event []byte | ||
var err error | ||
|
||
if params.ItemID == "" { | ||
params.ItemID = util.RandomGUID() | ||
} | ||
|
||
if params.Description == "" { | ||
params.Description = fmt.Sprintf("%v", util.RandomInt(1000)) | ||
} | ||
switch params.Transport { | ||
case models.TransportEventSub: | ||
body := &models.DropsEntitlementEventSubResponse{ | ||
Subscription: models.EventsubSubscription{ | ||
ID: params.ID, | ||
Status: "enabled", | ||
Type: triggerMapping[params.Transport][params.Trigger], | ||
Version: "1", | ||
Condition: models.EventsubCondition{ | ||
OrganizationID: params.FromUserID, | ||
}, | ||
Transport: models.EventsubTransport{ | ||
Method: "webhook", | ||
Callback: "null", | ||
}, | ||
Cost: 0, | ||
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), | ||
}, | ||
Events: []models.DropsEntitlementEventSubEvent{ | ||
{ | ||
ID: util.RandomGUID(), | ||
Data: models.DropsEntitlementEventSubEventData{ | ||
OrganizationID: params.FromUserID, | ||
CategoryID: params.Description, | ||
CategoryName: "", | ||
CampaignID: util.RandomGUID(), | ||
EntitlementID: util.RandomGUID(), | ||
BenefitID: params.ItemID, | ||
UserID: params.ToUserID, | ||
UserName: params.ToUserName, | ||
UserLogin: params.ToUserName, | ||
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), | ||
}, | ||
}, | ||
}, | ||
} | ||
event, err = json.Marshal(body) | ||
if err != nil { | ||
return events.MockEventResponse{}, err | ||
} | ||
default: | ||
return events.MockEventResponse{}, nil | ||
} | ||
|
||
return events.MockEventResponse{ | ||
ID: params.ID, | ||
JSON: event, | ||
FromUser: params.FromUserID, | ||
ToUser: params.ToUserID, | ||
}, nil | ||
} | ||
|
||
func (e Event) ValidTransport(t string) bool { | ||
return transportsSupported[t] | ||
} | ||
|
||
func (e Event) ValidTrigger(t string) bool { | ||
for _, ts := range triggerSupported { | ||
if ts == t { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func (e Event) GetTopic(transport string, trigger string) string { | ||
return triggerMapping[transport][trigger] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package drop | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/twitchdev/twitch-cli/internal/events" | ||
"github.com/twitchdev/twitch-cli/internal/models" | ||
"github.com/twitchdev/twitch-cli/test_setup" | ||
) | ||
|
||
var fromUser = "1234" | ||
var toUser = "4567" | ||
|
||
func TestEventSub(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
params := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: models.TransportEventSub, | ||
Trigger: "drop", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
|
||
var body models.DropsEntitlementEventSubResponse // replace with actual value | ||
err = json.Unmarshal(r.JSON, &body) | ||
a.Nil(err) | ||
|
||
a.Len(body.Events, 1) | ||
} | ||
|
||
func TestFakeTransport(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
params := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: "drop", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
a.Empty(r) | ||
} | ||
func TestValidTrigger(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTrigger("drop") | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTrigger("notdrop") | ||
a.Equal(false, r) | ||
} | ||
|
||
func TestValidTransport(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTransport(models.TransportEventSub) | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTransport("noteventsub") | ||
a.Equal(false, r) | ||
} | ||
func TestGetTopic(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
r := Event{}.GetTopic(models.TransportEventSub, "drop") | ||
a.NotNil(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package gift | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"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{ | ||
models.TransportWebSub: false, | ||
models.TransportEventSub: true, | ||
} | ||
|
||
var triggerSupported = []string{"channel-gift"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportEventSub: { | ||
"channel-gift": "channel.subscription.gift", | ||
}, | ||
} | ||
|
||
type Event struct{} | ||
|
||
func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { | ||
var event []byte | ||
var err error | ||
|
||
if params.Cost <= 0 { | ||
params.Cost = 5 | ||
} | ||
|
||
total := int(util.RandomInt(200) + params.Cost) | ||
|
||
if params.IsAnonymous { | ||
params.FromUserID = "274598607" | ||
params.FromUserName = "ananonymousgifter" | ||
} | ||
|
||
switch params.Transport { | ||
case models.TransportEventSub: | ||
body := &models.GiftEventSubResponse{ | ||
Subscription: models.EventsubSubscription{ | ||
ID: params.ID, | ||
Status: "enabled", | ||
Type: triggerMapping[params.Transport][params.Trigger], | ||
Version: "1", | ||
Condition: models.EventsubCondition{ | ||
BroadcasterUserID: params.ToUserID, | ||
}, | ||
Transport: models.EventsubTransport{ | ||
Method: "webhook", | ||
Callback: "null", | ||
}, | ||
Cost: 0, | ||
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano), | ||
}, | ||
Event: models.GiftEventSubEvent{ | ||
UserID: params.FromUserID, | ||
UserLogin: params.FromUserName, | ||
UserName: params.FromUserName, | ||
BroadcasterUserID: params.ToUserID, | ||
BroadcasterUserLogin: params.ToUserName, | ||
BroadcasterUserName: params.ToUserName, | ||
Tier: "1000", | ||
Total: int(params.Cost), | ||
CumulativeTotal: &total, | ||
IsAnonymous: params.IsAnonymous, | ||
}, | ||
} | ||
if params.IsAnonymous { | ||
body.Event.CumulativeTotal = nil | ||
} | ||
event, err = json.Marshal(body) | ||
if err != nil { | ||
return events.MockEventResponse{}, err | ||
} | ||
default: | ||
return events.MockEventResponse{}, nil | ||
} | ||
|
||
return events.MockEventResponse{ | ||
ID: params.ID, | ||
JSON: event, | ||
FromUser: params.FromUserID, | ||
ToUser: params.ToUserID, | ||
}, nil | ||
} | ||
|
||
func (e Event) ValidTransport(t string) bool { | ||
return transportsSupported[t] | ||
} | ||
|
||
func (e Event) ValidTrigger(t string) bool { | ||
for _, ts := range triggerSupported { | ||
if ts == t { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func (e Event) GetTopic(transport string, trigger string) string { | ||
return triggerMapping[transport][trigger] | ||
} |
Oops, something went wrong.