Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat badge endpoints update #218

Merged
merged 3 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var prettyPrint bool
var autoPaginate int = 0
var port int

var generateCount int

var apiCmd = &cobra.Command{
Use: "api",
Short: "Used to interface with the Twitch API",
Expand Down Expand Up @@ -100,7 +102,7 @@ func init() {

startCmd.Flags().IntVarP(&port, "port", "p", 8080, "Defines the port that the mock API will run on.")

generateCmd.Flags().IntVarP(&count, "count", "c", 10, "Defines the number of fake users to generate.")
generateCmd.Flags().IntVarP(&generateCount, "count", "c", 25, "Defines the number of fake users to generate.")
}

func cmdRun(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -142,5 +144,5 @@ func mockStartRun(cmd *cobra.Command, args []string) {
}

func generateMockRun(cmd *cobra.Command, args []string) {
generate.Generate(count)
generate.Generate(generateCount)
}
43 changes: 31 additions & 12 deletions internal/mock_api/endpoints/chat/channel_badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"

"github.com/twitchdev/twitch-cli/internal/database"
"github.com/twitchdev/twitch-cli/internal/mock_api/authentication"
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
Expand All @@ -34,7 +33,23 @@ type ChannelBadges struct{}

func (e ChannelBadges) Path() string { return "/chat/badges" }

var defaultChannelBadgeVersions = []string{"0", "12", "2", "3", "6", "9"}
var defaultChannelSubscriberBadgeVersions = [][]string{
{"0", "Subscriber"}, // Tier 1
{"3", "3-Month Subscriber"}, // Tier 1 - 3 months
{"6", "6-Month Subscriber"}, // Tier 1 - 6 months
{"9", "9-Month Subscriber"}, // Tier 1 - 9 months
{"12", "1-Year Subscriber"}, // Tier 1 - 12 months
{"2000", "Subscriber"}, // Tier 2
{"2003", "3-Month Subscriber"}, // Tier 2 - 3 months
{"2006", "6-Month Subscriber"}, // Tier 2 - 6 months
{"2009", "9-Month Subscriber"}, // Tier 2 - 9 months
{"2012", "1-Year Subscriber"}, // Tier 2 - 12 months
{"3000", "Subscriber"}, // Tier 3
{"3003", "3-Month Subscriber"}, // Tier 3 - 3 months
{"3006", "6-Month Subscriber"}, // Tier 3 - 6 months
{"3009", "9-Month Subscriber"}, // Tier 3 - 9 months
{"3012", "1-Year Subscriber"}, // Tier 3 - 12 months
}

func (e ChannelBadges) GetRequiredScopes(method string) []string {
return channelBadgesScopesByMethod[method]
Expand All @@ -56,26 +71,30 @@ func (e ChannelBadges) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func getChannelBadges(w http.ResponseWriter, r *http.Request) {
badges := []BadgesResponse{}
userCtx := r.Context().Value("auth").(authentication.UserAuthentication)

if !userCtx.MatchesBroadcasterIDParam(r) {
mock_errors.WriteUnauthorized(w, "Broadcaster ID does not match token.")
broadcasterID := r.URL.Query().Get("broadcaster_id")
if broadcasterID == "" {
mock_errors.WriteBadRequest(w, "Missing required parameter broadcaster_id")
return
}

badges := []BadgesResponse{}

b := BadgesResponse{
SetID: "subscriber",
Versions: []BadgesVersion{},
}

for _, v := range defaultChannelBadgeVersions {
for _, v := range defaultChannelSubscriberBadgeVersions {
id := util.RandomGUID()
b.Versions = append(b.Versions, BadgesVersion{
ID: v,
ImageURL1X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/1", id),
ImageURL2X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/2", id),
ImageURL4X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/3", id),
ID: v[0],
ImageURL1X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/1", id),
ImageURL2X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/2", id),
ImageURL4X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/3", id),
Title: v[1],
Description: v[1],
ClickAction: ptr("subscribe_to_channel"),
ClickURL: nil,
})
}
badges = append(badges, b)
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestChannelBadges(t *testing.T) {
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
a.Nil(err)
a.Equal(401, resp.StatusCode)
a.Equal(400, resp.StatusCode)

q.Set("broadcaster_id", "1")
req.URL.RawQuery = q.Encode()
Expand Down
102 changes: 91 additions & 11 deletions internal/mock_api/endpoints/chat/global_badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,87 @@ var globalBadgesScopesByMethod = map[string][]string{

type GlobalBadges struct{}

var defaultGlobalSets = []string{"clip-champ", "extension", "hype-train", "moderator", "premium", "vip"}
var defaultGlobalSets = []BadgesResponse{
{
SetID: "clip-champ",
Versions: []BadgesVersion{
{
ID: "1",
Title: "Power Clipper",
Description: "Power Clipper",
ClickAction: ptr("visit_url"),
ClickURL: ptr("https://help.twitch.tv/customer/portal/articles/2918323-clip-champs-guide"),
},
},
},
{
SetID: "extension",
Versions: []BadgesVersion{
{
ID: "1",
Title: "Extension",
Description: "Extension",
ClickAction: nil,
ClickURL: nil,
},
},
},
{
SetID: "hype-train",
Versions: []BadgesVersion{
{
ID: "1",
Title: "Current Hype Train Conductor",
Description: "Top supporter during the most recent hype train",
ClickAction: ptr("visit_url"),
ClickURL: ptr("https://help.twitch.tv/s/article/hype-train-guide"),
},
{
ID: "2",
Title: "Former Hype Train Conductor",
Description: "Top supporter during prior hype trains",
ClickAction: ptr("visit_url"),
ClickURL: ptr("https://help.twitch.tv/s/article/hype-train-guide"),
},
},
},
{
SetID: "moderator",
Versions: []BadgesVersion{
{
ID: "1",
Title: "Moderator",
Description: "Moderator",
ClickAction: nil,
ClickURL: nil,
},
},
},
{
SetID: "premium",
Versions: []BadgesVersion{
{
ID: "1",
Title: "Prime Gaming",
Description: "Prime Gaming",
ClickAction: ptr("visit_url"),
ClickURL: ptr("https://gaming.amazon.com"),
},
},
},
{
SetID: "vip",
Versions: []BadgesVersion{
{
ID: "1",
Title: "VIP",
Description: "VIP",
ClickAction: ptr("visit_url"),
ClickURL: ptr("https://help.twitch.tv/customer/en/portal/articles/659115-twitch-chat-badges-guide"),
},
},
},
}

func (e GlobalBadges) Path() string { return "/chat/badges/global" }

Expand Down Expand Up @@ -58,17 +138,17 @@ func getGlobalBadges(w http.ResponseWriter, r *http.Request) {
badges := []BadgesResponse{}

for _, set := range defaultGlobalSets {
id := util.RandomGUID()
versions := set.Versions
for i := range versions {
uuid := util.RandomGUID()
versions[i].ImageURL1X = fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/1", uuid)
versions[i].ImageURL2X = fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/2", uuid)
versions[i].ImageURL4X = fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/3", uuid)
}

badges = append(badges, BadgesResponse{
SetID: set,
Versions: []BadgesVersion{
{
ID: "1",
ImageURL1X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/1", id),
ImageURL2X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/2", id),
ImageURL4X: fmt.Sprintf("https://static-cdn.jtvnw.net/badges/v1/%v/3", id),
},
},
SetID: set.SetID,
Versions: versions,
})
}

Expand Down
16 changes: 12 additions & 4 deletions internal/mock_api/endpoints/chat/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ type BadgesResponse struct {
}

type BadgesVersion struct {
ID string `json:"id"`
ImageURL1X string `json:"image_url_1x"`
ImageURL2X string `json:"image_url_2x"`
ImageURL4X string `json:"image_url_4x"`
ID string `json:"id"`
ImageURL1X string `json:"image_url_1x"`
ImageURL2X string `json:"image_url_2x"`
ImageURL4X string `json:"image_url_4x"`
Title string `json:"title"`
Description string `json:"description"`
ClickAction *string `json:"click_action"`
ClickURL *string `json:"click_url"`
}

type EmotesResponse struct {
Expand All @@ -39,3 +43,7 @@ type EmotesImages struct {
ImageURL2X string `json:"url_2x"`
ImageURL4X string `json:"url_4x"`
}

func ptr(str string) *string {
return &str
}