Skip to content

Commit

Permalink
Merge branch 'main' into mock-api-jan-23
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo authored Feb 15, 2023
2 parents 907ff67 + 28810f9 commit 2957aa8
Show file tree
Hide file tree
Showing 34 changed files with 225 additions and 77 deletions.
12 changes: 8 additions & 4 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/events/mock_wss_server"
"github.com/twitchdev/twitch-cli/internal/events/trigger"
"github.com/twitchdev/twitch-cli/internal/events/types"
"github.com/twitchdev/twitch-cli/internal/events/verify"
"github.com/twitchdev/twitch-cli/internal/util"
)
Expand All @@ -36,6 +37,7 @@ var (
count int
description string
gameID string
tier string
timestamp string
charityCurrentValue int
charityTargetValue int
Expand All @@ -54,9 +56,9 @@ var triggerCmd = &cobra.Command{
Short: "Creates mock events that can be forwarded to a local webserver for event testing.",
Long: fmt.Sprintf(`Creates mock events that can be forwarded to a local webserver for event testing.
Supported:
%s`, events.ValidTriggers()),
%s`, types.AllEventTopics()),
Args: cobra.MaximumNArgs(1),
ValidArgs: events.ValidTriggers(),
ValidArgs: types.AllEventTopics(),
Run: triggerCmdRun,
Example: `twitch event trigger subscribe`,
Aliases: []string{
Expand All @@ -69,9 +71,9 @@ var verifyCmd = &cobra.Command{
Short: "Mocks the subscription verification event. Can be forwarded to a local webserver for testing.",
Long: fmt.Sprintf(`Mocks the subscription verification event that can be forwarded to a local webserver for testing.
Supported:
%s`, events.ValidTriggers()),
%s`, types.AllEventTopics()),
Args: cobra.MaximumNArgs(1),
ValidArgs: events.ValidTriggers(),
ValidArgs: types.AllEventTopics(),
Run: verifyCmdRun,
Example: `twitch event verify-subscription subscribe`,
Aliases: []string{
Expand Down Expand Up @@ -121,6 +123,7 @@ func init() {
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of 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.")
triggerCmd.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
triggerCmd.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
triggerCmd.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 @@ -189,6 +192,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
Description: description,
ItemName: itemName,
GameID: gameID,
Tier: tier,
SubscriptionStatus: subscriptionStatus,
Timestamp: timestamp,
CharityCurrentValue: charityCurrentValue,
Expand Down
1 change: 1 addition & 0 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Used to either create or send mock events for use with local webhooks testing.
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
| `--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | N |
| `--game-id` | `-G` | Game ID for Drop or other relevant events. | `-G 1234` | N |
| `--tier` | | Tier of the subscription. | `--tier 3000` | N |


```sh
Expand Down
17 changes: 3 additions & 14 deletions internal/events/cmd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
package events

import "sort"

func ValidTriggers() []string {
names := []string{}

for name, enabled := range triggerSupported {
if enabled == true {
names = append(names, name)
}
}
sort.Strings(names)

return names
}
import (
"sort"
)

func ValidTransports() []string {
names := []string{}
Expand Down
7 changes: 0 additions & 7 deletions internal/events/cmd_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import (
"github.com/twitchdev/twitch-cli/test_setup"
)

func TestValidTriggers(t *testing.T) {
a := test_setup.SetupTestEnv(t)

t1 := ValidTriggers()
a.NotEmpty(t1)
}

func TestValidTransports(t *testing.T) {
a := test_setup.SetupTestEnv(t)

Expand Down
4 changes: 4 additions & 0 deletions internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type MockEventParameters struct {
IsPermanent bool
Description string
GameID string
Tier string
Timestamp string
CharityCurrentValue int
CharityTargetValue int
Expand All @@ -46,6 +47,9 @@ type MockEvent interface {
// Returns whether a given event supports a supplied transport
ValidTransport(transport string) bool

// Returns
GetAllTopicsByTransport(transport string) []string

// Returns the string of the topic
GetTopic(transport string, trigger string) string

Expand Down
47 changes: 0 additions & 47 deletions internal/events/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
package events

var triggerSupported = map[string]bool{
"add-moderator": true,
"add-redemption": true,
"add-reward": true,
"ban": true,
"charity-donate": true,
"charity-progress": true,
"charity-start": true,
"charity-stop": true,
"cheer": true,
"drop": true,
"follow": true,
"gift": true,
"goal-begin": true,
"goal-end": true,
"goal-progress": true,
"grant": true,
"hype-train-begin": true,
"hype-train-end": true,
"hype-train-progress": true,
"poll-begin": true,
"poll-progress": true,
"poll-end": true,
"prediction-begin": true,
"prediction-progress": true,
"prediction-lock": true,
"prediction-end": true,
"raid": true,
"remove-moderator": true,
"remove-reward": true,
"revoke": true,
"shield-mode-begin": true,
"shield-mode-end": true,
"shoutout-create": true,
"shoutout-received": true,
"stream-change": true,
"streamdown": true,
"streamup": true,
"subscribe": true,
"transaction": true,
"unban": true,
"unsubscribe": true,
"update-redemption": true,
"update-reward": true,
"user-update": true,
}

var transportSupported = map[string]bool{
"websub": false,
"eventsub": true,
Expand Down
13 changes: 13 additions & 0 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type TriggerParameters struct {
Description string
ItemName string
GameID string
Tier string
Timestamp string
EventID string // Also serves as subscription ID. See https://github.com/twitchdev/twitch-cli/issues/184
CharityCurrentValue int
Expand Down Expand Up @@ -66,6 +67,17 @@ func Fire(p TriggerParameters) (string, error) {
p.GameID = fmt.Sprint(util.RandomInt(10 * 1000))
}

switch p.Tier {
case "":
p.Tier = "1000"
case "1000", "2000", "3000":
// do nothing, these are valid values
default:
return "", fmt.Errorf(
`Discarding event: Invalid tier provided.
Valid values are 1000, 2000 or 3000`)
}

if p.EventID == "" {
p.EventID = util.RandomGUID()
}
Expand Down Expand Up @@ -98,6 +110,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
Description: p.Description,
ItemName: p.ItemName,
GameID: p.GameID,
Tier: p.Tier,
SubscriptionStatus: p.SubscriptionStatus,
Timestamp: p.Timestamp,
CharityCurrentValue: p.CharityCurrentValue,
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/ban/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/channel_points_reward/reward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/charity/charity_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ func (e Event) ValidTrigger(trigger string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/cheer/cheer_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/drop/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/follow/follow_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func (e Event) ValidTrigger(trigger string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/gift/channel_gift.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
8 changes: 8 additions & 0 deletions internal/events/types/goal/goal_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}

func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}

func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
7 changes: 7 additions & 0 deletions internal/events/types/hype_train/hype_train_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func (e Event) ValidTrigger(t string) bool {
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
func (e Event) GetAllTopicsByTransport(transport string) []string {
allTopics := []string{}
for _, topic := range triggerMapping[transport] {
allTopics = append(allTopics, topic)
}
return allTopics
}
func (e Event) GetEventSubAlias(t string) string {
// check for aliases
for trigger, topic := range triggerMapping[models.TransportEventSub] {
Expand Down
Loading

0 comments on commit 2957aa8

Please sign in to comment.