-
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 'channelBan' of https://github.com/twitchdev/twitch-cli …
…into channelBan fast-forwarding to current main .
- Loading branch information
Showing
14 changed files
with
402 additions
and
6 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
Oops, something went wrong.