-
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 #41 from twitchdev/moderator-add-remove
Moderator change events on WebSub
- Loading branch information
Showing
6 changed files
with
206 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
87 changes: 87 additions & 0 deletions
87
internal/events/types/moderator_change/moderator_change_event.go
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,87 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package moderator_change | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"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: false, | ||
} | ||
|
||
var triggerSupported = []string{"add-moderator", "remove-moderator"} | ||
|
||
var triggerMapping = map[string]map[string]string{ | ||
models.TransportWebSub: { | ||
"add-moderator": "moderation.moderator.add", | ||
"remove-moderator": "moderation.moderator.remove", | ||
}, | ||
} | ||
|
||
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: | ||
return events.MockEventResponse{}, errors.New("Moderator change events are currently in beta on EventSub") | ||
case models.TransportWebSub: | ||
body := *&models.ModeratorChangeWebSubResponse{ | ||
Data: []models.ModeratorChangeWebSubEvent{ | ||
{ | ||
ID: params.ID, | ||
EventType: triggerMapping[params.Transport][params.Trigger], | ||
EventTimestamp: util.GetTimestamp().Format(time.RFC3339), | ||
Version: "v1", | ||
EventData: models.ModeratorChangeEventData{ | ||
BroadcasterID: params.ToUserID, | ||
BroadcasterName: params.ToUserName, | ||
UserID: params.FromUserID, | ||
UserName: params.FromUserName, | ||
}, | ||
}, | ||
}, | ||
} | ||
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] | ||
} |
89 changes: 89 additions & 0 deletions
89
internal/events/types/moderator_change/moderator_change_event_test.go
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,89 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package moderator_change | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/twitchdev/twitch-cli/internal/events" | ||
"github.com/twitchdev/twitch-cli/internal/models" | ||
"github.com/twitchdev/twitch-cli/internal/util" | ||
) | ||
|
||
var fromUser = "1234" | ||
var toUser = "4567" | ||
|
||
func TestWebSub(t *testing.T) { | ||
a := util.SetupTestEnv(t) | ||
|
||
params := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: models.TransportWebSub, | ||
Trigger: "add-moderator", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
|
||
var body models.ModeratorChangeWebSubResponse | ||
err = json.Unmarshal(r.JSON, &body) | ||
a.Nil(err) | ||
|
||
a.Equal("moderation.moderator.add", body.Data[0].EventType, "Expected event type %v, got %v", "moderation.moderator.add", body.Data[0].EventType) | ||
a.Equal(toUser, body.Data[0].EventData.BroadcasterID, "Expected to user %v, got %v", toUser, body.Data[0].EventData.BroadcasterID) | ||
a.Equal(fromUser, body.Data[0].EventData.UserID, "Expected from user %v, got %v", fromUser, body.Data[0].EventData.UserID) | ||
|
||
params = *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: models.TransportWebSub, | ||
} | ||
} | ||
func TestFakeTransport(t *testing.T) { | ||
a := util.SetupTestEnv(t) | ||
|
||
params := *&events.MockEventParameters{ | ||
FromUserID: fromUser, | ||
ToUserID: toUser, | ||
Transport: "fake_transport", | ||
Trigger: "add-moderator", | ||
} | ||
|
||
r, err := Event{}.GenerateEvent(params) | ||
a.Nil(err) | ||
a.Empty(r) | ||
} | ||
func TestValidTrigger(t *testing.T) { | ||
a := util.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTrigger("add-moderator") | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTrigger("remove-moderator") | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTrigger("update-moderator") | ||
a.Equal(false, r) | ||
} | ||
|
||
func TestValidTransport(t *testing.T) { | ||
a := util.SetupTestEnv(t) | ||
|
||
r := Event{}.ValidTransport(models.TransportWebSub) | ||
a.Equal(true, r) | ||
|
||
r = Event{}.ValidTransport(models.TransportEventSub) | ||
a.Equal(false, r) | ||
} | ||
|
||
func TestGetTopic(t *testing.T) { | ||
a := util.SetupTestEnv(t) | ||
|
||
r := Event{}.GetTopic(models.TransportWebSub, "add-moderator") | ||
a.Equal("moderation.moderator.add", r, "Expected %v, got %v", "moderation.moderator.add", r) | ||
|
||
r = Event{}.GetTopic(models.TransportWebSub, "remove-moderator") | ||
a.Equal("moderation.moderator.remove", r, "Expected %v, got %v", "moderation.moderator.remove", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package models | ||
|
||
type ModeratorChangeWebSubEvent struct { | ||
ID string `json:"id"` | ||
EventType string `json:"event_type"` | ||
EventTimestamp string `json:"event_timestamp"` | ||
Version string `json:"version"` | ||
EventData ModeratorChangeEventData `json:"event_data"` | ||
} | ||
|
||
type ModeratorChangeEventData struct { | ||
BroadcasterID string `json:"broadcaster_id"` | ||
BroadcasterName string `json:"broadcaster_name"` | ||
UserID string `json:"user_id"` | ||
UserName string `json:"user_name"` | ||
} | ||
|
||
type ModeratorChangeWebSubResponse struct { | ||
Data []ModeratorChangeWebSubEvent `json:"data"` | ||
} |