Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/fix-channel-points-id
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet authored Mar 10, 2021
2 parents f8d810c + 7215694 commit 7a775fd
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
itemID string
cost int64
count int
streamTitle string
)

var eventCmd = &cobra.Command{
Expand Down Expand Up @@ -83,6 +84,7 @@ func init() {
triggerCmd.Flags().StringVarP(&status, "status", "S", "", "Status of the event object, currently applies to channel points redemptions.")
triggerCmd.Flags().StringVarP(&itemID, "item-id", "i", "", "Manually set the ID of the event payload item (for example the reward ID in redemption events).")
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of bits or channel points redeemed/used in the event.")
triggerCmd.Flags().StringVarP(&streamTitle, "description", "d", "", "Title the stream should be updated with.")

// retrigger flags
retriggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
Expand Down Expand Up @@ -125,6 +127,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
Status: status,
ItemID: itemID,
Cost: cost,
StreamTitle: streamTitle,
})

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Used to either create or send mock events for use with local webhooks testing.
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream-change` | Stream Changed event. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |
| `add-moderator` | Channel moderator add event. |
Expand All @@ -50,6 +51,7 @@ Used to either create or send mock events for use with local webhooks testing.
| `--status` | `-S` | Status of the event object, currently applies to channel points redemptions. | `-S fulfilled` | N |
| `--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
| `--description` | `-d` | Title the stream should be updated with. | `-d Awesome new title!` | N |

**Examples**

Expand Down Expand Up @@ -113,6 +115,7 @@ Allows you to test if your webserver responds to subscription requests properly.
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream_change` | Stream changed event. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |
| `add-moderator` | Channel moderator add event. |
Expand Down
1 change: 1 addition & 0 deletions internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MockEventParameters struct {
Status string
ItemID string
Cost int64
StreamTitle string
}

type MockEventResponse struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type TriggerParameters struct {
Secret string
Verbose bool
Count int
StreamTitle string
}

type TriggerResponse struct {
Expand Down Expand Up @@ -61,6 +62,7 @@ func Fire(p TriggerParameters) (string, error) {
Cost: p.Cost,
Status: p.Status,
ItemID: p.ItemID,
StreamTitle: p.StreamTitle,
}

e, err := types.GetByTriggerAndTransport(p.Event, p.Transport)
Expand Down
125 changes: 125 additions & 0 deletions internal/events/types/stream_change/stream_change_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package stream_change

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: true,
models.TransportEventSub: true,
}

var triggerSupported = []string{"stream-change"}

var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"stream_change": "streams",
},
models.TransportEventSub: {
"stream_change": "channel.update",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error

if params.StreamTitle == "" {
params.StreamTitle = "Example title from the CLI!"
}

switch params.Transport {
case models.TransportEventSub:
body := &models.EventsubResponse{
// make the eventsub response (if supported)
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: "channel.update",
Version: "1",
Condition: models.EventsubCondition{
BroadcasterUserID: params.ToUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.ChannelUpdateEventSubEvent{
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
StreamTitle: params.StreamTitle,
StreamLanguage: "en",
StreamCategoryID: "509658",
StreamCategoryName: "Just Chatting",
IsMature: "true",
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
case models.TransportWebSub:
body := models.StreamChangeWebSubResponse{
Data: []models.StreamChangeWebSubResponseData{
{
WebsubID: params.ID,
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
StreamCategoryID: "509658",
StreamCategoryName: "Just Chatting",
StreamType: "live",
StreamTitle: params.StreamTitle,
StreamViewerCount: 9848,
StreamStartedAt: util.GetTimestamp().Format(time.RFC3339),
StreamLanguage: "en",
StreamThumbnailURL: "https://static-cdn.jtvnw.net/previews-ttv/live_twitch_user-{width}x{height}.jpg",
TagIDs: make([]string, 0),
},
},
}
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]
}
117 changes: 117 additions & 0 deletions internal/events/types/stream_change/stream_change_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package stream_change

import (
"encoding/json"
"testing"

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

var fromUser = "1234"
var toUser = "4567"

func TestEventSub(t *testing.T) {
a := util.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportEventSub,
Trigger: "stream-change",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)

var body models.ChannelUpdateEventSubResponse
err = json.Unmarshal(r.JSON, &body)
a.Nil(err, "Error unmarshalling JSON")

// write actual tests here (making sure you set appropriate values and the like) for eventsub
a.Equal(toUser, body.Event.BroadcasterUserID, "Expected Stream Channel %v, got %v", toUser, body.Event.BroadcasterUserID)

// test for changing a title
params = events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportEventSub,
Trigger: "stream_change",
}

r, err = Event{}.GenerateEvent(params)
a.Nil(err)

err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

a.Equal(toUser, body.Event.BroadcasterUserID, "Expected Stream Channel %v, got %v", toUser, body.Event.BroadcasterUserID)
a.Equal("Example title from the CLI!", body.Event.StreamTitle, "Expected new stream title, got %v", body.Event.StreamTitle)
}

func TestWebSubStreamChange(t *testing.T) {
a := util.SetupTestEnv(t)

newStreamTitle := "Awesome new title from the CLI!"

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportWebSub,
Trigger: "stream-change",
StreamTitle: newStreamTitle,
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)

var body models.StreamChangeWebSubResponse
err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

// write tests here for websub
a.Equal(toUser, body.Data[0].BroadcasterUserID, "Expected Stream Channel %v, got %v", toUser, body.Data[0].BroadcasterUserID)
a.Equal(newStreamTitle, body.Data[0].StreamTitle, "Expected new stream title, got %v", body.Data[0].StreamTitle)
}
func TestFakeTransport(t *testing.T) {
a := util.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: "fake_transport",
Trigger: "stream-change",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)
a.Empty(r)
}
func TestValidTrigger(t *testing.T) {
a := util.SetupTestEnv(t)

r := Event{}.ValidTrigger("stream-change")
a.Equal(true, r)

r = Event{}.ValidTrigger("not_trigger_keyword")
a.Equal(false, r)
}

func TestValidTransport(t *testing.T) {
a := util.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 := util.SetupTestEnv(t)

r := Event{}.GetTopic(models.TransportEventSub, "stream-change")
a.NotNil(r)
}
2 changes: 2 additions & 0 deletions internal/events/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/twitchdev/twitch-cli/internal/events/types/follow"
"github.com/twitchdev/twitch-cli/internal/events/types/moderator_change"
"github.com/twitchdev/twitch-cli/internal/events/types/raid"
"github.com/twitchdev/twitch-cli/internal/events/types/stream_change"
"github.com/twitchdev/twitch-cli/internal/events/types/streamdown"
"github.com/twitchdev/twitch-cli/internal/events/types/streamup"
"github.com/twitchdev/twitch-cli/internal/events/types/subscribe"
Expand All @@ -29,6 +30,7 @@ func All() []events.MockEvent {
follow.Event{},
raid.Event{},
subscribe.Event{},
stream_change.Event{},
streamup.Event{},
streamdown.Event{},
moderator_change.Event{},
Expand Down
39 changes: 39 additions & 0 deletions internal/models/stream_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package models

type ChannelUpdateEventSubEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
StreamTitle string `json:"title"`
StreamLanguage string `json:"language"`
StreamCategoryID string `json:"category_id"`
StreamCategoryName string `json:"category_name"`
IsMature string `json:"is_mature"`
}

type StreamChangeWebSubResponse struct {
Data []StreamChangeWebSubResponseData `json:"data"`
}

type StreamChangeWebSubResponseData struct {
WebsubID string `json:"id"`
BroadcasterUserID string `json:"user_id"`
BroadcasterUserLogin string `json:"user_login"`
BroadcasterUserName string `json:"user_name"`
StreamCategoryID string `json:"game_id"`
StreamCategoryName string `json:"game_name"`
StreamType string `json:"type"`
StreamTitle string `json:"title"`
StreamViewerCount int `json:"viewer_count"`
StreamStartedAt string `json:"started_at"`
StreamLanguage string `json:"language"`
StreamThumbnailURL string `json:"thumbnail_url"`
TagIDs []string `json:"tag_ids"`
}

type ChannelUpdateEventSubResponse struct {
Subscription EventsubSubscription `json:"subscription"`
Event ChannelUpdateEventSubEvent `json:"event"`
}

0 comments on commit 7a775fd

Please sign in to comment.