-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for eventsub triggers channel.shield_mode.begin and cha…
…nnel.shield_mode.end
- Loading branch information
Showing
6 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package shield_mode | ||
|
||
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.TransportEventSub: true, | ||
} | ||
var triggers = []string{"shield-mode-begin", "shield-mode-end"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportEventSub: { | ||
"shield-mode-begin": "channel.shield_mode.begin", | ||
"shield-mode-end": "channel.shield_mode.end", | ||
}, | ||
} | ||
|
||
type Event struct{} | ||
|
||
func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) { | ||
var event []byte | ||
var err error | ||
|
||
switch params.Transport { | ||
case models.TransportEventSub: | ||
eventBody := models.ShieldModeEventSubEvent{ | ||
BroadcasterUserID: params.ToUserID, | ||
BroadcasterUserName: params.ToUserName, | ||
BroadcasterUserLogin: params.ToUserName, | ||
ModeratorUserID: params.FromUserID, | ||
ModeratorUserName: params.FromUserName, | ||
ModeratorUserLogin: params.FromUserName, | ||
} | ||
|
||
if params.Trigger == "shield-mode-begin" { | ||
eventBody.StartedAt = util.GetTimestamp().Add(-10 * time.Minute).Format(time.RFC3339Nano) | ||
} else if params.Trigger == "shield-mode-end" { | ||
eventBody.EndedAt = util.GetTimestamp().Format(time.RFC3339Nano) | ||
} | ||
|
||
body := models.ShieldModeEventSubResponse{ | ||
Subscription: models.EventsubSubscription{ | ||
ID: params.ID, | ||
Status: params.SubscriptionStatus, | ||
Type: triggerMapping[params.Transport][params.Trigger], | ||
Version: e.SubscriptionVersion(), | ||
Condition: models.EventsubCondition{ | ||
BroadcasterUserID: params.ToUserID, | ||
ModeratorUserID: params.FromUserID, | ||
}, | ||
Transport: models.EventsubTransport{ | ||
Method: "webhook", | ||
Callback: "null", | ||
}, | ||
Cost: 0, | ||
CreatedAt: params.Timestamp, | ||
}, | ||
Event: eventBody, | ||
} | ||
|
||
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, | ||
ToUser: params.ToUserID, | ||
FromUser: params.FromUserID, | ||
}, 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) GetEventSubAlias(t string) string { | ||
// check for aliases | ||
for trigger, topic := range triggerMapping[models.TransportEventSub] { | ||
if topic == t { | ||
return trigger | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (e Event) SubscriptionVersion() string { | ||
return "beta" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package shield_mode | ||
|
||
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) | ||
|
||
beginParams := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: models.TransportEventSub, | ||
Trigger: "shield-mode-begin", | ||
SubscriptionStatus: "enabled", | ||
Cost: 0, | ||
} | ||
endParams := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: models.TransportEventSub, | ||
Trigger: "shield-mode-end", | ||
SubscriptionStatus: "enabled", | ||
Cost: 0, | ||
} | ||
|
||
r1, err := Event{}.GenerateEvent(beginParams) | ||
a.Nil(err) | ||
|
||
r2, err := Event{}.GenerateEvent(endParams) | ||
a.Nil(err) | ||
|
||
var body models.ShieldModeEventSubResponse | ||
err = json.Unmarshal(r1.JSON, &body) | ||
a.Nil(err) | ||
|
||
err = json.Unmarshal(r2.JSON, &body) | ||
a.Nil(err) | ||
} | ||
|
||
func TestFakeTransport(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
beginParams := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: "shield-mode-begin", | ||
SubscriptionStatus: "enabled", | ||
} | ||
endParams := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: "shield-mode-end", | ||
SubscriptionStatus: "enabled", | ||
} | ||
|
||
r1, err1 := Event{}.GenerateEvent(beginParams) | ||
r2, err2 := Event{}.GenerateEvent(endParams) | ||
a.Nil(err1) | ||
a.Nil(err2) | ||
a.Empty(r1) | ||
a.Empty(r2) | ||
} | ||
func TestValidTrigger(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTrigger("shield-mode-begin") | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTrigger("shield-mode-end") | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTrigger("notshield") | ||
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, "shield-mode-begin") | ||
a.NotNil(r) | ||
|
||
r = Event{}.GetTopic(models.TransportEventSub, "shield-mode-end") | ||
a.NotNil(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package models | ||
|
||
type ShieldModeEventSubResponse struct { | ||
Subscription EventsubSubscription `json:"subscription"` | ||
Event ShieldModeEventSubEvent `json:"event"` | ||
} | ||
|
||
type ShieldModeEventSubEvent struct { | ||
BroadcasterUserID string `json:"broadcaster_user_id"` | ||
BroadcasterUserName string `json:"broadcaster_user_name"` | ||
BroadcasterUserLogin string `json:"broadcaster_user_login"` | ||
ModeratorUserID string `json:"moderator_user_id"` | ||
ModeratorUserName string `json:"moderator_user_name"` | ||
ModeratorUserLogin string `json:"moderator_user_login"` | ||
StartedAt string `json:"started_at,omitempty"` | ||
EndedAt string `json:"ended_at,omitempty"` | ||
} |