Skip to content

Commit

Permalink
updating eventsub to match production
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Jul 26, 2021
1 parent 3388f14 commit 3f42430
Show file tree
Hide file tree
Showing 22 changed files with 1,332 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ twitch-cli
*.html

# Editor configs
.vsvode/
.vscode/
.idea/

# junk files
Expand Down
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

126 changes: 75 additions & 51 deletions docs/event.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package authorization_revoke
package authorization

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

"github.com/spf13/viper"
Expand All @@ -18,11 +17,12 @@ var transportsSupported = map[string]bool{
models.TransportEventSub: true,
}

var triggerSupported = []string{"revoke"}
var triggerSupported = []string{"revoke", "grant"}

var triggerMapping = map[string]map[string]string{
models.TransportEventSub: {
"revoke": "user.authorization.revoke",
"grant": "user.authorization.grant",
},
}

Expand Down Expand Up @@ -66,8 +66,6 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
if err != nil {
return events.MockEventResponse{}, err
}
case models.TransportWebSub:
return events.MockEventResponse{}, errors.New("Websub is unsupported for authorization revoke events")
default:
return events.MockEventResponse{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package authorization_revoke
package authorization

import (
"encoding/json"
Expand Down Expand Up @@ -29,29 +29,14 @@ func TestEventSub(t *testing.T) {
r, err := Event{}.GenerateEvent(params)
a.Nil(err)

var body models.AuthorizationRevokeEventSubResponse // replace with actual value
var body models.AuthorizationRevokeEventSubResponse
err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

a.NotEmpty(body.Event.ClientID)
a.Equal(body.Event.ClientID, body.Subscription.Condition.ClientID)
a.Equal("1234", body.Event.ClientID)
}
func TestWebSub(t *testing.T) {
a := test_setup.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportWebSub,
Trigger: "revoke",
}

_, err := Event{}.GenerateEvent(params)
a.NotNil(err)

// write tests here for websub
}
func TestFakeTransport(t *testing.T) {
a := test_setup.SetupTestEnv(t)

Expand Down
107 changes: 107 additions & 0 deletions internal/events/types/drop/drop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package drop

import (
"encoding/json"
"fmt"
"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.TransportWebSub: false,
models.TransportEventSub: true,
}

var triggerSupported = []string{"drop"}

var triggerMapping = map[string]map[string]string{
models.TransportEventSub: {
"drop": "drop.entitlement.grant",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error

if params.ItemID == "" {
params.ItemID = util.RandomGUID()
}

if params.Description == "" {
params.Description = fmt.Sprintf("%v", util.RandomInt(1000))
}
switch params.Transport {
case models.TransportEventSub:
body := &models.DropsEntitlementEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
OrganizationID: params.FromUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
Cost: 0,
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Events: []models.DropsEntitlementEventSubEvent{
{
ID: util.RandomGUID(),
Data: models.DropsEntitlementEventSubEventData{
OrganizationID: params.FromUserID,
CategoryID: params.Description,
CategoryName: "",
CampaignID: util.RandomGUID(),
EntitlementID: util.RandomGUID(),
BenefitID: params.ItemID,
UserID: params.ToUserID,
UserName: params.ToUserName,
UserLogin: params.ToUserName,
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
},
},
}
event, err = json.Marshal(body)
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(t string) bool {
return transportsSupported[t]
}

func (e Event) ValidTrigger(t string) bool {
for _, ts := range triggerSupported {
if ts == t {
return true
}
}
return false
}
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
75 changes: 75 additions & 0 deletions internal/events/types/drop/drop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package drop

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.TransportEventSub,
Trigger: "drop",
}

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

var body models.DropsEntitlementEventSubResponse // replace with actual value
err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

a.Len(body.Events, 1)
}

func TestFakeTransport(t *testing.T) {
a := test_setup.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: "fake_transport",
Trigger: "drop",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)
a.Empty(r)
}
func TestValidTrigger(t *testing.T) {
a := test_setup.SetupTestEnv(t)

r := Event{}.ValidTrigger("drop")
a.Equal(true, r)

r = Event{}.ValidTrigger("notdrop")
a.Equal(false, r)
}

func TestValidTransport(t *testing.T) {
a := test_setup.SetupTestEnv(t)

r := Event{}.ValidTransport(models.TransportEventSub)
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.TransportEventSub, "drop")
a.NotNil(r)
}
108 changes: 108 additions & 0 deletions internal/events/types/gift/channel_gift.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package gift

import (
"encoding/json"
"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.TransportWebSub: false,
models.TransportEventSub: true,
}

var triggerSupported = []string{"channel-gift"}

var triggerMapping = map[string]map[string]string{
models.TransportEventSub: {
"channel-gift": "channel.subscription.gift",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error

if params.Cost <= 0 {
params.Cost = 5
}

total := int(util.RandomInt(200) + params.Cost)

if params.IsAnonymous {
params.FromUserID = "274598607"
params.FromUserName = "ananonymousgifter"
}

switch params.Transport {
case models.TransportEventSub:
body := &models.GiftEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
BroadcasterUserID: params.ToUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
Cost: 0,
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.GiftEventSubEvent{
UserID: params.FromUserID,
UserLogin: params.FromUserName,
UserName: params.FromUserName,
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Tier: "1000",
Total: int(params.Cost),
CumulativeTotal: &total,
IsAnonymous: params.IsAnonymous,
},
}
if params.IsAnonymous {
body.Event.CumulativeTotal = nil
}
event, err = json.Marshal(body)
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(t string) bool {
return transportsSupported[t]
}

func (e Event) ValidTrigger(t string) bool {
for _, ts := range triggerSupported {
if ts == t {
return true
}
}
return false
}
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
Loading

0 comments on commit 3f42430

Please sign in to comment.