Skip to content

Commit e4c0dce

Browse files
committed
Adding support for transaction events with eventsub transport.
1 parent 6d1d730 commit e4c0dce

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

internal/events/trigger/trigger_event_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func TestFire(t *testing.T) {
172172
Count: 0,
173173
}
174174
res, err = Fire(params)
175-
a.NotNil(err)
175+
a.Nil(err)
176+
a.NotEmpty(res)
176177

177178
params = *&TriggerParameters{
178179
Event: "add-reward",

internal/events/types/extension_transaction/transaction_event.go

+49-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ package extension_transaction
44

55
import (
66
"encoding/json"
7-
"errors"
87
"time"
98

9+
"github.com/spf13/viper"
1010
"github.com/twitchdev/twitch-cli/internal/events"
1111
"github.com/twitchdev/twitch-cli/internal/models"
1212
"github.com/twitchdev/twitch-cli/internal/util"
1313
)
1414

1515
var transportsSupported = map[string]bool{
1616
models.TransportWebSub: true,
17-
models.TransportEventSub: false,
17+
models.TransportEventSub: true,
1818
}
1919

2020
var triggerSupported = []string{"transaction"}
@@ -23,6 +23,9 @@ var triggerMapping = map[string]map[string]string{
2323
models.TransportWebSub: {
2424
"transaction": "transaction",
2525
},
26+
models.TransportEventSub: {
27+
"transaction": "extension.bits_transaction.create",
28+
},
2629
}
2730

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

37+
clientID := viper.GetString("clientId")
38+
39+
// if not configured, generate a random one
40+
if clientID == "" {
41+
clientID = util.RandomClientID()
42+
}
43+
3444
if params.Cost == 0 {
3545
params.Cost = 100
3646
}
3747
switch params.Transport {
3848
case models.TransportEventSub:
39-
return events.MockEventResponse{}, errors.New("Extension transactions are unsupported on Eventsub")
49+
body := &models.TransactionEventSubResponse{
50+
Subscription: models.EventsubSubscription{
51+
ID: params.ID,
52+
Status: "enabled",
53+
Type: triggerMapping[params.Transport][params.Trigger],
54+
Version: "1",
55+
Condition: models.EventsubCondition{
56+
ExtensionClientID: clientID,
57+
},
58+
Transport: models.EventsubTransport{
59+
Method: "webhook",
60+
Callback: "null",
61+
},
62+
Cost: 1,
63+
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
64+
},
65+
Event: models.TransactionEventSubEvent{
66+
ID: params.ID,
67+
ExtensionClientID: clientID,
68+
BroadcasterUserID: params.ToUserID,
69+
BroadcasterUserLogin: "testBroadcaster",
70+
BroadcasterUserName: "testBroadcaster",
71+
UserName: "testUser",
72+
UserLogin: "testUser",
73+
UserID: params.FromUserID,
74+
Product: models.TransactionEventSubProduct{
75+
Name: "Test Trigger Item from CLI",
76+
Sku: "testItemSku",
77+
Bits: params.Cost,
78+
InDevelopment: true,
79+
},
80+
},
81+
}
82+
event, err = json.Marshal(body)
83+
if err != nil {
84+
return events.MockEventResponse{}, err
85+
}
4086
case models.TransportWebSub:
4187
body := *&models.TransactionWebSubResponse{
4288
Data: []models.TransactionWebsubEvent{

internal/events/types/extension_transaction/transaction_event_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"encoding/json"
77
"testing"
88

9+
"github.com/spf13/viper"
910
"github.com/twitchdev/twitch-cli/internal/events"
1011
"github.com/twitchdev/twitch-cli/internal/models"
1112
"github.com/twitchdev/twitch-cli/test_setup"
1213
)
1314

1415
var fromUser = "1234"
1516
var toUser = "4567"
17+
var clientId = "7890"
1618

1719
func TestWebusbTransaction(t *testing.T) {
1820
a := test_setup.SetupTestEnv(t)
@@ -46,8 +48,18 @@ func TestEventsubTransaction(t *testing.T) {
4648
Trigger: "transaction",
4749
}
4850

49-
_, err := Event{}.GenerateEvent(params)
50-
a.NotNil(err)
51+
viper.Set("clientId", clientId)
52+
53+
r, err := Event{}.GenerateEvent(params)
54+
a.Nil(err)
55+
56+
var body models.TransactionEventSubResponse
57+
err = json.Unmarshal(r.JSON, &body)
58+
a.Nil(err)
59+
60+
a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID)
61+
a.Equal(fromUser, body.Event.UserID, "Expected from user %v, got %v", fromUser, body.Event.UserID)
62+
a.Equal(clientId, body.Event.ExtensionClientID, "Expected client id %v, got %v", clientId, body.Event.ExtensionClientID)
5163
}
5264

5365
func TestFakeTransport(t *testing.T) {

internal/models/eventsub.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type EventsubCondition struct {
2323
ToBroadcasterUserID string `json:"to_broadcaster_user_id,omitempty"`
2424
FromBroadcasterUserID string `json:"from_broadcaster_user_id,omitempty"`
2525
ClientID string `json:"client_id,omitempty"`
26+
ExtensionClientID string `json:"extension_client_id,omitempty"`
2627
}
2728

2829
type EventsubResponse struct {

internal/models/transactions.go

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
// SPDX-License-Identifier: Apache-2.0
33
package models
44

5+
type TransactionEventSubEvent struct {
6+
ID string `json:"id"`
7+
ExtensionClientID string `json:"extension_client_id"`
8+
BroadcasterUserID string `json:"broadcaster_user_id"`
9+
BroadcasterUserLogin string `json:"broadcaster_user_login"`
10+
BroadcasterUserName string `json:"broadcaster_user_name"`
11+
UserName string `json:"user_name"`
12+
UserLogin string `json:"user_login"`
13+
UserID string `json:"user_id"`
14+
Product TransactionEventSubProduct `json:"product"`
15+
}
16+
17+
type TransactionEventSubProduct struct {
18+
Name string `json:"name"`
19+
Sku string `json:"sku"`
20+
Bits int64 `json:"bits"`
21+
InDevelopment bool `json:"in_development"`
22+
}
23+
24+
type TransactionEventSubResponse struct {
25+
Subscription EventsubSubscription `json:"subscription"`
26+
Event TransactionEventSubEvent `json:"event"`
27+
}
28+
529
type TransactionWebsubEvent struct {
630
ID string `json:"id"`
731
Timestamp string `json:"timestamp"`

0 commit comments

Comments
 (0)