-
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.
Merge branch 'main' into bugfix/fix-channel-points-id
- Loading branch information
Showing
8 changed files
with
292 additions
and
0 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
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
125 changes: 125 additions & 0 deletions
125
internal/events/types/stream_change/stream_change_event.go
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,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
117
internal/events/types/stream_change/stream_change_event_test.go
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,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) | ||
} |
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,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"` | ||
} |