-
Notifications
You must be signed in to change notification settings - Fork 72
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 #320 from twitchdev/eventsub-updates
Eventsub updates
- Loading branch information
Showing
7 changed files
with
518 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package ad_break | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"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.TransportWebhook: true, | ||
models.TransportWebSocket: true, | ||
} | ||
var triggers = []string{"ad-begin"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportWebhook: { | ||
"ad-begin": "channel.ad_break.begin", | ||
}, | ||
models.TransportWebSocket: { | ||
"ad-begin": "channel.ad_break.begin", | ||
}, | ||
} | ||
|
||
type Event struct{} | ||
|
||
func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { | ||
var event []byte | ||
var err error | ||
|
||
switch params.Transport { | ||
case models.TransportWebhook, models.TransportWebSocket: | ||
body := models.EventsubResponse{ | ||
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.AdBreakBeginEventSubEvent{ | ||
RequesterUserID: params.FromUserID, | ||
RequesterUserLogin: params.FromUserName, | ||
RequesterUserName: params.FromUserName, | ||
BroadcasterUserID: params.ToUserID, | ||
BroadcasterUserLogin: params.ToUserName, | ||
BroadcasterUserName: params.ToUserName, | ||
Duration: 60, | ||
IsAutomatic: false, | ||
StartedAt: util.GetTimestamp().Format(time.RFC3339Nano), | ||
}, | ||
} | ||
|
||
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(transport string) bool { | ||
return transportsSupported[transport] | ||
} | ||
|
||
func (e Event) ValidTrigger(trigger string) bool { | ||
for _, t := range triggers { | ||
if t == trigger { | ||
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 "1" | ||
} |
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,77 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package ad_break | ||
|
||
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: "subscribe", | ||
SubscriptionStatus: "enabled", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
|
||
var body models.SubEventSubResponse | ||
err = json.Unmarshal(r.JSON, &body) | ||
a.Nil(err) | ||
|
||
a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) | ||
} | ||
|
||
func TestFakeTransport(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
params := events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: triggers[0], | ||
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("notreal") | ||
a.Equal(false, r) | ||
|
||
r = Event{}.ValidTrigger("ad-begin") | ||
a.Equal(true, 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, "ad-begin") | ||
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.