Skip to content

Commit

Permalink
Updated Get Channel Chat Badges and Get Global Chat Badges to include…
Browse files Browse the repository at this point in the history
… title, description, click_action, and click_url
  • Loading branch information
Xemdo committed Apr 1, 2023
1 parent b53c296 commit 092dabf
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 27 deletions.
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
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
}

0 comments on commit 092dabf

Please sign in to comment.