-
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 pull request #253 from twitchdev/ccl-updates
Content Classification Label updates
- Loading branch information
Showing
13 changed files
with
548 additions
and
50 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
2 changes: 1 addition & 1 deletion
2
...stream_change/stream_change_event_test.go → .../channel_update_v1/channel_update_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
147 changes: 147 additions & 0 deletions
147
internal/events/types/channel_update_v2/channel_update.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,147 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package channel_update_v2 | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/twitchdev/twitch-cli/internal/events" | ||
"github.com/twitchdev/twitch-cli/internal/models" | ||
) | ||
|
||
var transportsSupported = map[string]bool{ | ||
models.TransportWebhook: true, | ||
models.TransportWebSocket: true, | ||
} | ||
|
||
var triggerSupported = []string{"stream-change"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportWebhook: { | ||
"stream-change": "channel.update", | ||
}, | ||
models.TransportWebSocket: { | ||
"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.Description == "" { | ||
params.Description = "Example title from the CLI!" | ||
} | ||
if params.ItemID == "" && params.GameID == "" { | ||
params.GameID = "509658" | ||
} else if params.ItemID != "" && params.GameID == "" { | ||
params.GameID = params.ItemID | ||
} | ||
if params.ItemName == "" { | ||
params.ItemName = "Just Chatting" | ||
} | ||
|
||
switch params.Transport { | ||
case models.TransportWebhook, models.TransportWebSocket: | ||
body := &models.EventsubResponse{ | ||
// make the eventsub response (if supported) | ||
Subscription: models.EventsubSubscription{ | ||
ID: params.ID, | ||
Status: params.SubscriptionStatus, | ||
Type: triggerMapping[params.Transport][params.Trigger], | ||
Version: e.SubscriptionVersion(), | ||
Condition: models.EventsubCondition{ | ||
BroadcasterUserID: params.ToUserID, | ||
}, | ||
Transport: models.EventsubTransport{ | ||
Method: "webhook", | ||
Callback: "null", | ||
}, | ||
Cost: 0, | ||
CreatedAt: params.Timestamp, | ||
}, | ||
Event: models.ChannelUpdateEventSubEvent{ | ||
BroadcasterUserID: params.ToUserID, | ||
BroadcasterUserLogin: params.ToUserName, | ||
BroadcasterUserName: params.ToUserName, | ||
StreamTitle: params.Description, | ||
StreamLanguage: "en", | ||
StreamCategoryID: params.GameID, | ||
StreamCategoryName: params.ItemName, | ||
ContentClassificationLabels: []string{ | ||
"MatureGame", | ||
"ViolentGraphic", | ||
}, | ||
}, | ||
} | ||
event, err = json.Marshal(body) | ||
if err != nil { | ||
return events.MockEventResponse{}, err | ||
} | ||
|
||
// Delete event info if Subscription.Status is not set to "enabled" | ||
if !strings.EqualFold(params.SubscriptionStatus, "enabled") { | ||
var i interface{} | ||
if err := json.Unmarshal([]byte(event), &i); err != nil { | ||
return events.MockEventResponse{}, err | ||
} | ||
if m, ok := i.(map[string]interface{}); ok { | ||
delete(m, "event") // Matches JSON key defined in body variable above | ||
} | ||
|
||
event, err = json.Marshal(i) | ||
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] | ||
} | ||
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.TransportWebhook] { | ||
if topic == t { | ||
return trigger | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (e Event) SubscriptionVersion() string { | ||
return "beta" | ||
} |
98 changes: 98 additions & 0 deletions
98
internal/events/types/channel_update_v2/channel_update_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,98 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package channel_update_v2 | ||
|
||
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.TransportWebhook, | ||
Trigger: "stream-change", | ||
SubscriptionStatus: "enabled", | ||
} | ||
|
||
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.TransportWebhook, | ||
Trigger: "stream_change", | ||
SubscriptionStatus: "enabled", | ||
GameID: "1234", | ||
} | ||
|
||
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) | ||
a.Equal("1234", body.Event.StreamCategoryID) | ||
} | ||
|
||
func TestFakeTransport(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
params := events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: "stream-change", | ||
SubscriptionStatus: "enabled", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
a.Empty(r) | ||
} | ||
func TestValidTrigger(t *testing.T) { | ||
a := test_setup.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 := test_setup.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTransport(models.TransportWebhook) | ||
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.TransportWebhook, "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
Oops, something went wrong.