Skip to content

Commit

Permalink
Added channel.ad_break.begin
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo committed Mar 29, 2024
1 parent f491ba8 commit a4ea3e1
Show file tree
Hide file tree
Showing 4 changed files with 233 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"
}
78 changes: 78 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,78 @@
// 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)
a.Equal(fromUser, body.Event.UserID, "Expected from user %v, got %v", r.ToUser, body.Event.UserID)
}

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("follow")
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, "follow")
a.NotNil(r)
}
2 changes: 2 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 Down Expand Up @@ -41,6 +42,7 @@ import (

func AllEvents() []events.MockEvent {
return []events.MockEvent{
ad_break.Event{},
authorization_grant.Event{},
authorization_revoke.Event{},
ban.Event{},
Expand Down
20 changes: 20 additions & 0 deletions internal/models/ad_break.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package models

type AdBreakBeginEventSubEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
RequesterUserID string `json:"requester_user_id"`
RequesterUserLogin string `json:"requester_user_login"`
RequesterUserName string `json:"requester_user_name"`
Duration int `json:"duration_seconds"`
IsAutomatic bool `json:"is_automatic"`
StartedAt string `json:"started_at"`
}

type AdBreakBeginEventSubResponse struct {
Subscription EventsubSubscription `json:"subscription"`
Event AdBreakBeginEventSubEvent `json:"event"`
}

0 comments on commit a4ea3e1

Please sign in to comment.