-
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.
Merge pull request #257 from twitchdev/fix-195
Allow perma ban testing for channel.ban
- Loading branch information
Showing
10 changed files
with
306 additions
and
45 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
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
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,136 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package unban | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"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.TransportWebhook: true, | ||
models.TransportWebSocket: true, | ||
} | ||
|
||
var triggerSupported = []string{"unban"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportWebhook: { | ||
"unban": "channel.unban", | ||
}, | ||
models.TransportWebSocket: { | ||
"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.TransportWebhook, models.TransportWebSocket: | ||
ban := models.UnbanEventSubEvent{ | ||
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", | ||
} | ||
|
||
body := *&models.EventsubResponse{ | ||
Subscription: models.EventsubSubscription{ | ||
ID: params.ID, | ||
Status: params.SubscriptionStatus, | ||
Type: triggerMapping[params.Transport][params.Trigger], | ||
Version: e.SubscriptionVersion(), | ||
Condition: models.EventsubCondition{ | ||
BroadcasterUserID: params.ToUserID, | ||
}, | ||
Transport: models.EventsubTransport{ | ||
Method: "webhook", | ||
Callback: "null", | ||
}, | ||
Cost: 0, | ||
CreatedAt: params.Timestamp, | ||
}, | ||
Event: ban, | ||
} | ||
|
||
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, | ||
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] | ||
} | ||
func (e Event) GetAllTopicsByTransport(transport string) []string { | ||
allTopics := []string{} | ||
for _, topic := range triggerMapping[transport] { | ||
allTopics = append(allTopics, topic) | ||
} | ||
return allTopics | ||
} | ||
func (e Event) GetEventSubAlias(t string) string { | ||
// check for aliases | ||
for trigger, topic := range triggerMapping[models.TransportWebhook] { | ||
if topic == t { | ||
return trigger | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (e Event) SubscriptionVersion() string { | ||
return "1" | ||
} |
Oops, something went wrong.