|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package chat |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/twitchdev/twitch-cli/internal/database" |
| 11 | + "github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors" |
| 12 | + "github.com/twitchdev/twitch-cli/internal/models" |
| 13 | + "github.com/twitchdev/twitch-cli/internal/util" |
| 14 | +) |
| 15 | + |
| 16 | +var channelEmotesMethodsSupported = map[string]bool{ |
| 17 | + http.MethodGet: true, |
| 18 | + http.MethodPost: false, |
| 19 | + http.MethodDelete: false, |
| 20 | + http.MethodPatch: false, |
| 21 | + http.MethodPut: false, |
| 22 | +} |
| 23 | + |
| 24 | +var channelEmotesScopesByMethod = map[string][]string{ |
| 25 | + http.MethodGet: {}, |
| 26 | + http.MethodPost: {}, |
| 27 | + http.MethodDelete: {}, |
| 28 | + http.MethodPatch: {}, |
| 29 | + http.MethodPut: {}, |
| 30 | +} |
| 31 | + |
| 32 | +type ChannelEmotes struct{} |
| 33 | + |
| 34 | +func (e ChannelEmotes) Path() string { return "/chat/emotes/channel" } |
| 35 | + |
| 36 | +func (e ChannelEmotes) GetRequiredScopes(method string) []string { |
| 37 | + return channelEmotesScopesByMethod[method] |
| 38 | +} |
| 39 | + |
| 40 | +func (e ChannelEmotes) ValidMethod(method string) bool { |
| 41 | + return channelEmotesMethodsSupported[method] |
| 42 | +} |
| 43 | + |
| 44 | +func (e ChannelEmotes) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 45 | + db = r.Context().Value("db").(database.CLIDatabase) |
| 46 | + |
| 47 | + switch r.Method { |
| 48 | + case http.MethodGet: |
| 49 | + getChannelEmotes(w, r) |
| 50 | + break |
| 51 | + default: |
| 52 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 53 | + } |
| 54 | +} |
| 55 | +func getChannelEmotes(w http.ResponseWriter, r *http.Request) { |
| 56 | + emotes := []EmotesResponse{} |
| 57 | + broadcaster := r.URL.Query().Get("broadcaster_id") |
| 58 | + if broadcaster == "" { |
| 59 | + mock_errors.WriteBadRequest(w, "Missing required parameter broadcaster_id") |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + setID := fmt.Sprint(util.RandomInt(10 * 1000)) |
| 64 | + ownerID := util.RandomUserID() |
| 65 | + for _, v := range defaultEmoteTypes { |
| 66 | + emoteType := v |
| 67 | + for i := 0; i < 5; i++ { |
| 68 | + id := util.RandomInt(10 * 1000) |
| 69 | + name := util.RandomGUID() |
| 70 | + er := EmotesResponse{ |
| 71 | + ID: fmt.Sprint(id), |
| 72 | + Name: name, |
| 73 | + Images: EmotesImages{ |
| 74 | + ImageURL1X: fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%v/1.0", id), |
| 75 | + ImageURL2X: fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%v/2.0", id), |
| 76 | + ImageURL4X: fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%v/4.0", id), |
| 77 | + }, |
| 78 | + EmoteType: &emoteType, |
| 79 | + EmoteSetID: &setID, |
| 80 | + OwnerID: &ownerID, |
| 81 | + } |
| 82 | + if emoteType == "subscription" { |
| 83 | + thousand := "1000" |
| 84 | + er.Tier = &thousand |
| 85 | + } else { |
| 86 | + es := "" |
| 87 | + er.Tier = &es |
| 88 | + } |
| 89 | + |
| 90 | + emotes = append(emotes, er) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + bytes, _ := json.Marshal(models.APIResponse{Data: emotes}) |
| 95 | + w.Write(bytes) |
| 96 | +} |
0 commit comments