Skip to content

Commit

Permalink
Added unban requests events
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo committed Mar 29, 2024
1 parent 3c1437c commit 5209302
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/events/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/twitchdev/twitch-cli/internal/events/types/subscribe"
"github.com/twitchdev/twitch-cli/internal/events/types/subscription_message"
"github.com/twitchdev/twitch-cli/internal/events/types/unban"
"github.com/twitchdev/twitch-cli/internal/events/types/unban_requests"
user_update "github.com/twitchdev/twitch-cli/internal/events/types/user"
"github.com/twitchdev/twitch-cli/internal/models"
)
Expand Down Expand Up @@ -69,6 +70,7 @@ func AllEvents() []events.MockEvent {
subscribe.Event{},
subscription_message.Event{},
unban.Event{},
unban_requests.Event{},
user_update.Event{},
}
}
Expand Down
161 changes: 161 additions & 0 deletions internal/events/types/unban_requests/unban_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package unban_requests

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.TransportWebhook: true,
models.TransportWebSocket: true,
}
var triggers = []string{"unban-request-create", "unban-request-resolve"}

var triggerMapping = map[string]map[string]string{
models.TransportWebhook: {
"unban-request-create": "channel.unban_request.create",
"unban-request-resolve": "channel.unban_request.resolve",
},
models.TransportWebSocket: {
"unban-request-create": "channel.unban_request.create",
"unban-request-resolve": "channel.unban_request.resolve",
},
}

type Event struct{}

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

var unbanRequestEvent interface{}

if params.Trigger == "unban-request-create" {
unbanRequestEvent = models.UnbanRequestCreateEventSubEvent{
BroadcasterUserID: params.ToUserID,
BroadcasterUserName: params.ToUserName,
BroadcasterUserLogin: strings.ToLower(params.ToUserName),
UserID: params.FromUserID,
UserName: params.FromUserName,
UserLogin: strings.ToLower(params.FromUserName),
Text: "Please unban me!",
CreatedAt: util.GetTimestamp().Add(-30 * time.Minute).Format(time.RFC3339Nano),
}
}

if params.Trigger == "unban-request-resolve" {
mod_user := util.RandomUserID()
mod_user_lower := strings.ToLower(mod_user)
mod_user_id := util.RandomUserID()

unbanRequestEvent = models.UnbanRequestResolveEventSubEvent{
ID: util.RandomGUID(),
BroadcasterUserID: params.ToUserID,
BroadcasterUserName: params.ToUserName,
BroadcasterUserLogin: strings.ToLower(params.ToUserName),
ModeratorUserID: &mod_user_id,
ModeratorUserName: &mod_user,
ModeratorUserLogin: &mod_user_lower,
UserID: params.FromUserID,
UserName: params.FromUserName,
UserLogin: strings.ToLower(params.FromUserName),
ResolutionText: "We forgive you",
Status: "approved",
}
}

switch params.Transport {
case models.TransportWebhook, models.TransportWebSocket:
body := models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Type: triggerMapping[params.Transport][params.Trigger],
Version: e.SubscriptionVersion(),
Status: params.SubscriptionStatus,
Cost: 0,
Condition: models.EventsubCondition{
BroadcasterUserID: params.ToUserID,
ModeratorUserID: params.FromUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: params.Timestamp,
},
Event: unbanRequestEvent,
}

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,
}, 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) 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"
}
89 changes: 89 additions & 0 deletions internal/events/types/unban_requests/unban_requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package unban_requests

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 TestEventSubUnbanRequests(t *testing.T) {
testEventSubUnbanRequests(t, "unban-request-create")
testEventSubUnbanRequests(t, "unban-request-resolve")
}

func testEventSubUnbanRequests(t *testing.T, trigger string) {
a := test_setup.SetupTestEnv(t)
params := events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportWebhook,
Trigger: trigger,
SubscriptionStatus: "enabled",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err, "Error generating body.")

var body models.UnbanRequestCreateEventSubResponse

err = json.Unmarshal(r.JSON, &body)
a.Nil(err, "Error unmarshalling JSON")

a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID)
if trigger == "unban-request-create" {
a.Equal(fromUser, body.Event.UserID, "Expected from user %v, got %v", r.ToUser, body.Event.UserID)
}
}

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

params := events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: "fake_transport",
Trigger: "unban-request-create",
}

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

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

r := Event{}.ValidTrigger("unban-request-create")
a.Equal(true, r)

r = Event{}.ValidTrigger("unban-request-resolve")
a.Equal(true, r)
}

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

r := Event{}.ValidTransport(models.TransportWebhook)
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.TransportWebhook, "unban-request-create")
a.NotNil(r)

r = Event{}.GetTopic(models.TransportWebhook, "unban-request-resolve")
a.NotNil(r)
}
34 changes: 34 additions & 0 deletions internal/models/unban_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package models

type UnbanRequestCreateEventSubEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
}

type UnbanRequestCreateEventSubResponse struct {
Subscription EventsubSubscription `json:"subscription"`
Event TransactionEventSubEvent `json:"event"`
}

type UnbanRequestResolveEventSubEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
ModeratorUserID *string `json:"moderator_user_id"`
ModeratorUserLogin *string `json:"moderator_user_login"`
ModeratorUserName *string `json:"moderator_user_name"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
ResolutionText string `json:"resolution_text"`
Status string `json:"status"`
}

0 comments on commit 5209302

Please sign in to comment.