Skip to content

Commit

Permalink
Merge pull request #70 from Khukharr/main
Browse files Browse the repository at this point in the history
Adding support for transaction events with eventsub transport.
  • Loading branch information
lleadbet authored Jul 13, 2021
2 parents 6d1d730 + e4c0dce commit fccc850
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/events/trigger/trigger_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func TestFire(t *testing.T) {
Count: 0,
}
res, err = Fire(params)
a.NotNil(err)
a.Nil(err)
a.NotEmpty(res)

params = *&TriggerParameters{
Event: "add-reward",
Expand Down
52 changes: 49 additions & 3 deletions internal/events/types/extension_transaction/transaction_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ package extension_transaction

import (
"encoding/json"
"errors"
"time"

"github.com/spf13/viper"
"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: false,
models.TransportEventSub: true,
}

var triggerSupported = []string{"transaction"}
Expand All @@ -23,6 +23,9 @@ var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"transaction": "transaction",
},
models.TransportEventSub: {
"transaction": "extension.bits_transaction.create",
},
}

type Event struct{}
Expand All @@ -31,12 +34,55 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
var event []byte
var err error

clientID := viper.GetString("clientId")

// if not configured, generate a random one
if clientID == "" {
clientID = util.RandomClientID()
}

if params.Cost == 0 {
params.Cost = 100
}
switch params.Transport {
case models.TransportEventSub:
return events.MockEventResponse{}, errors.New("Extension transactions are unsupported on Eventsub")
body := &models.TransactionEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
ExtensionClientID: clientID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
Cost: 1,
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.TransactionEventSubEvent{
ID: params.ID,
ExtensionClientID: clientID,
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: "testBroadcaster",
BroadcasterUserName: "testBroadcaster",
UserName: "testUser",
UserLogin: "testUser",
UserID: params.FromUserID,
Product: models.TransactionEventSubProduct{
Name: "Test Trigger Item from CLI",
Sku: "testItemSku",
Bits: params.Cost,
InDevelopment: true,
},
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
case models.TransportWebSub:
body := *&models.TransactionWebSubResponse{
Data: []models.TransactionWebsubEvent{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"encoding/json"
"testing"

"github.com/spf13/viper"
"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"
var clientId = "7890"

func TestWebusbTransaction(t *testing.T) {
a := test_setup.SetupTestEnv(t)
Expand Down Expand Up @@ -46,8 +48,18 @@ func TestEventsubTransaction(t *testing.T) {
Trigger: "transaction",
}

_, err := Event{}.GenerateEvent(params)
a.NotNil(err)
viper.Set("clientId", clientId)

r, err := Event{}.GenerateEvent(params)
a.Nil(err)

var body models.TransactionEventSubResponse
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", fromUser, body.Event.UserID)
a.Equal(clientId, body.Event.ExtensionClientID, "Expected client id %v, got %v", clientId, body.Event.ExtensionClientID)
}

func TestFakeTransport(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/models/eventsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type EventsubCondition struct {
ToBroadcasterUserID string `json:"to_broadcaster_user_id,omitempty"`
FromBroadcasterUserID string `json:"from_broadcaster_user_id,omitempty"`
ClientID string `json:"client_id,omitempty"`
ExtensionClientID string `json:"extension_client_id,omitempty"`
}

type EventsubResponse struct {
Expand Down
24 changes: 24 additions & 0 deletions internal/models/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
// SPDX-License-Identifier: Apache-2.0
package models

type TransactionEventSubEvent struct {
ID string `json:"id"`
ExtensionClientID string `json:"extension_client_id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
UserName string `json:"user_name"`
UserLogin string `json:"user_login"`
UserID string `json:"user_id"`
Product TransactionEventSubProduct `json:"product"`
}

type TransactionEventSubProduct struct {
Name string `json:"name"`
Sku string `json:"sku"`
Bits int64 `json:"bits"`
InDevelopment bool `json:"in_development"`
}

type TransactionEventSubResponse struct {
Subscription EventsubSubscription `json:"subscription"`
Event TransactionEventSubEvent `json:"event"`
}

type TransactionWebsubEvent struct {
ID string `json:"id"`
Timestamp string `json:"timestamp"`
Expand Down

0 comments on commit fccc850

Please sign in to comment.