|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package channels |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/twitchdev/twitch-cli/internal/database" |
| 10 | + "github.com/twitchdev/twitch-cli/internal/mock_api/authentication" |
| 11 | + "github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors" |
| 12 | + "github.com/twitchdev/twitch-cli/internal/models" |
| 13 | +) |
| 14 | + |
| 15 | +var followedMethodsSupported = map[string]bool{ |
| 16 | + http.MethodGet: true, |
| 17 | + http.MethodPost: false, |
| 18 | + http.MethodDelete: false, |
| 19 | + http.MethodPatch: false, |
| 20 | + http.MethodPut: false, |
| 21 | +} |
| 22 | + |
| 23 | +var followedScopesByMethod = map[string][]string{ |
| 24 | + http.MethodGet: {"user:read:follows"}, |
| 25 | + http.MethodPost: {}, |
| 26 | + http.MethodDelete: {}, |
| 27 | + http.MethodPatch: {}, |
| 28 | + http.MethodPut: {}, |
| 29 | +} |
| 30 | + |
| 31 | +type FollowedEndpoint struct{} |
| 32 | + |
| 33 | +type GetFollowedEndpointResponseData struct { |
| 34 | + BroadcasterID string `json:"broadcaster_id"` |
| 35 | + BroadcasterLogin string `json:"broadcaster_login"` |
| 36 | + BroadcasterName string `json:"broadcaster_name"` |
| 37 | + FollowedAt string `json:"followed_at"` |
| 38 | +} |
| 39 | + |
| 40 | +func (e FollowedEndpoint) Path() string { return "/channels/followed" } |
| 41 | + |
| 42 | +func (e FollowedEndpoint) GetRequiredScopes(method string) []string { |
| 43 | + return followedScopesByMethod[method] |
| 44 | +} |
| 45 | + |
| 46 | +func (e FollowedEndpoint) ValidMethod(method string) bool { |
| 47 | + return followedMethodsSupported[method] |
| 48 | +} |
| 49 | + |
| 50 | +func (e FollowedEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 51 | + db = r.Context().Value("db").(database.CLIDatabase) |
| 52 | + |
| 53 | + switch r.Method { |
| 54 | + case http.MethodGet: |
| 55 | + getFollowed(w, r) |
| 56 | + default: |
| 57 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 58 | + return |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func getFollowed(w http.ResponseWriter, r *http.Request) { |
| 63 | + user_id := r.URL.Query().Get("user_id") |
| 64 | + broadcaster_id := r.URL.Query().Get("broadcaster_id") |
| 65 | + |
| 66 | + userCtx := r.Context().Value("auth").(authentication.UserAuthentication) |
| 67 | + |
| 68 | + if user_id == "" { |
| 69 | + mock_errors.WriteBadRequest(w, "The user_id query parameter is required") |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + if user_id != userCtx.UserID { |
| 74 | + mock_errors.WriteUnauthorized(w, "user_id does not match User ID in the access token") |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + req := database.UserRequestParams{ |
| 79 | + UserID: user_id, |
| 80 | + BroadcasterID: broadcaster_id, |
| 81 | + } |
| 82 | + |
| 83 | + dbr, err := db.NewQuery(r, 100).GetFollows(req, true) |
| 84 | + if dbr == nil { |
| 85 | + mock_errors.WriteServerError(w, err.Error()) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + // Build list of who the user is following |
| 90 | + follows := []GetFollowedEndpointResponseData{} |
| 91 | + for _, f := range dbr.Data.([]database.Follow) { |
| 92 | + follows = append(follows, GetFollowedEndpointResponseData{ |
| 93 | + BroadcasterID: f.BroadcasterID, |
| 94 | + BroadcasterLogin: f.BroadcasterLogin, |
| 95 | + BroadcasterName: f.BroadcasterName, |
| 96 | + FollowedAt: f.FollowedAt, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + body := models.APIResponse{ |
| 101 | + Data: follows, |
| 102 | + Total: &dbr.Total, |
| 103 | + } |
| 104 | + if dbr != nil && dbr.Cursor != "" { |
| 105 | + body.Pagination = &models.APIPagination{ |
| 106 | + Cursor: dbr.Cursor, |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + bytes, _ := json.Marshal(body) |
| 111 | + w.Write(bytes) |
| 112 | +} |
0 commit comments