Skip to content

Commit

Permalink
Merge pull request #320 from twitchdev/eventsub-updates
Browse files Browse the repository at this point in the history
Eventsub updates
  • Loading branch information
Xemdo authored Apr 3, 2024
2 parents f491ba8 + 5209302 commit 538b48a
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 0 deletions.
133 changes: 133 additions & 0 deletions internal/events/types/ad_break/ad_break_begin.go
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"
}
77 changes: 77 additions & 0 deletions internal/events/types/ad_break/ad_break_begin_test.go
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)
}
4 changes: 4 additions & 0 deletions internal/events/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/events/types/ad_break"
"github.com/twitchdev/twitch-cli/internal/events/types/authorization_grant"
"github.com/twitchdev/twitch-cli/internal/events/types/authorization_revoke"
"github.com/twitchdev/twitch-cli/internal/events/types/ban"
Expand All @@ -35,12 +36,14 @@ import (
"github.com/twitchdev/twitch-cli/internal/events/types/subscribe"
"github.com/twitchdev/twitch-cli/internal/events/types/subscription_message"
"github.com/twitchdev/twitch-cli/internal/events/types/unban"
"github.com/twitchdev/twitch-cli/internal/events/types/unban_requests"
user_update "github.com/twitchdev/twitch-cli/internal/events/types/user"
"github.com/twitchdev/twitch-cli/internal/models"
)

func AllEvents() []events.MockEvent {
return []events.MockEvent{
ad_break.Event{},
authorization_grant.Event{},
authorization_revoke.Event{},
ban.Event{},
Expand All @@ -67,6 +70,7 @@ func AllEvents() []events.MockEvent {
subscribe.Event{},
subscription_message.Event{},
unban.Event{},
unban_requests.Event{},
user_update.Event{},
}
}
Expand Down
Loading

0 comments on commit 538b48a

Please sign in to comment.