Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MonstersOnParade committed Mar 16, 2021
2 parents 06c9feb + 0901858 commit b92e332
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 74 deletions.
91 changes: 43 additions & 48 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@ Used to either create or send mock events for use with local webhooks testing.

**Args**

| Argument | Description |
| ---------------------- | ---------------------------------------------------------------------------------------------------------- |
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream-change` | Stream Changed event. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |
| `add-moderator` | Channel moderator add event. |
| `remove-moderator` | Channel moderator removal event. |
| `ban` | Channel ban event. |
| `unban` | Channel unban event. |
| `hype-train-begin` | Hype Train begins event. |
| `hype-train-progress` | Hype Train progress event. |
| `hype-train-end` | Hype Train ends event. |
| Argument | Description |
|---------------------|------------------------------------------------------------------------------------------------------------|
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream-change` | Stream Changed event. |
| `streamup` | Stream online event. |
| `streamdown` | Sstream offline event. |
| `add-moderator` | Channel moderator add event. |
| `remove-moderator` | Channel moderator removal event. |
| `ban` | Channel ban event. |
| `unban` | Channel unban event. |


**Flags**

Expand All @@ -56,7 +54,7 @@ Used to either create or send mock events for use with local webhooks testing.
| `--status` | `-S` | Status of the event object, currently applies to channel points redemptions. | `-S fulfilled` | N |
| `--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
| `--description` | `-d` | Title the stream should be updated with. | `-d Awesome new title!` | N |
| `--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | N |

**Examples**

Expand Down Expand Up @@ -107,29 +105,26 @@ Allows you to test if your webserver responds to subscription requests properly.
**Args**

| Argument | Description |
| ---------------------- | ---------------------------------------------------------------------------------------------------------- |
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream_change` | Stream changed event. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |
| `add-moderator` | Channel moderator add event. |
| `remove-moderator` | Channel moderator removal event. |
| `ban` | Channel ban event. |
| `unban` | Channel unban event. |
| `hype-train-begin` | Hype Train begins event. |
| `hype-train-progress` | Hype Train progress event. |
| `hype-train-end` | Hype Train ends event. |
|---------------------|------------------------------------------------------------------------------------------------------------|
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `stream-change` | Stream Changed event. |
| `streamup` | Stream online event. |
| `streamdown` | Sstream offline event. |
| `add-moderator` | Channel moderator add event. |
| `remove-moderator` | Channel moderator removal event. |
| `ban` | Channel ban event. |
| `unban` | Channel unban event. |

**Flags**

Expand Down
1 change: 1 addition & 0 deletions internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MockEventParameters struct {
Status string
ItemID string
Cost int64
IsPermanent bool
StreamTitle string
}

Expand Down
33 changes: 15 additions & 18 deletions internal/events/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
package events

var triggerSupported = map[string]bool{
"subscribe": true,
"unsubscribe": true,
"gift": true,
"cheer": true,
"transaction": true,
"follow": true,
"add-redemption": true,
"update-redemption": true,
"add-reward": true,
"update-reward": true,
"remove-reward": true,
"add-moderator": true,
"remove-moderator": true,
"ban": true,
"unban": true,
"hype-train-begin": true,
"hype-train-progress": true,
"hype-train-end": true,
"subscribe": true,
"unsubscribe": true,
"gift": true,
"cheer": true,
"transaction": true,
"follow": true,
"add-redemption": true,
"update-redemption": true,
"add-reward": true,
"update-reward": true,
"remove-reward": true,
"add-moderator": true,
"remove-moderator": true,
"ban": true,
"unban": true,
}

var transportSupported = map[string]bool{
Expand Down
1 change: 1 addition & 0 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Fire(p TriggerParameters) (string, error) {
IsAnonymous: p.IsAnonymous,
Cost: p.Cost,
Status: p.Status,
ItemID: p.ItemID,
StreamTitle: p.StreamTitle,
}

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

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: true,
models.TransportEventSub: true,
}

var triggerSupported = []string{"ban", "unban"}

var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"ban": "moderation.user.ban",
"unban": "moderation.user.unban",
},
models.TransportEventSub: {
"ban": "channel.ban",
"unban": "channel.unban",
},
}

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:
body := *&models.EventsubResponse{
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",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.BanEventSubEvent{
UserID: params.FromUserID,
UserLogin: params.FromUserName,
UserName: params.FromUserName,
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
ModeratorUserId: util.RandomUserID(),
ModeratorUserLogin: "CLIModerator",
ModeratorUserName: "CLIModerator",
Reason: "This is a test event",
EndsAt: util.GetTimestamp().Format(time.RFC3339Nano),
IsPermanent: params.IsPermanent,
},
}

event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}

case models.TransportWebSub:
body := *&models.BanWebSubResponse{
Data: []models.BanWebSubResponseData{
{
ID: params.ID,
EventType: triggerMapping[params.Transport][params.Trigger],
EventTimestamp: util.GetTimestamp().Format(time.RFC3339),
Version: "v1",
EventData: models.BanWebSubEventData{
BroadcasterID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterName: params.ToUserName,
UserID: params.FromUserID,
UserLogin: params.FromUserName,
UserName: params.FromUserName,
ExpiresAt: util.GetTimestamp().Add(1 * time.Hour).Format(time.RFC3339),
},
},
}}

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 b92e332

Please sign in to comment.