diff --git a/webserver/handlers/admin/chat.go b/webserver/handlers/admin/chat.go index cf2e7b4d1..f95468cd2 100644 --- a/webserver/handlers/admin/chat.go +++ b/webserver/handlers/admin/chat.go @@ -27,11 +27,6 @@ func ExternalUpdateMessageVisibility(integration models.ExternalAPIUser, w http. // UpdateMessageVisibility updates an array of message IDs to have the same visiblity. func UpdateMessageVisibility(w http.ResponseWriter, r *http.Request) { - // type messageVisibilityUpdateRequest struct { - // IDArray []string `json:"idArray"` - // Visible bool `json:"visible"` - // } - if r.Method != http.MethodPost { // nolint:goconst webutils.WriteSimpleResponse(w, false, r.Method+" not supported") @@ -199,18 +194,13 @@ func GetDisabledUsers(w http.ResponseWriter, r *http.Request) { // UpdateUserModerator will set the moderator status for a user ID. func UpdateUserModerator(w http.ResponseWriter, r *http.Request) { - type request struct { - UserID string `json:"userId"` - IsModerator bool `json:"isModerator"` - } - if r.Method != http.MethodPost { webutils.WriteSimpleResponse(w, false, r.Method+" not supported") return } decoder := json.NewDecoder(r.Body) - var req request + var req generated.UpdateUserModeratorJSONBody if err := decoder.Decode(&req); err != nil { webutils.WriteSimpleResponse(w, false, "") @@ -220,17 +210,17 @@ func UpdateUserModerator(w http.ResponseWriter, r *http.Request) { userRepository := userrepository.Get() // Update the user object with new moderation access. - if err := userRepository.SetModerator(req.UserID, req.IsModerator); err != nil { + if err := userRepository.SetModerator(*req.UserId, *req.IsModerator); err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return } // Update the clients for this user to know about the moderator access change. - if err := chat.SendConnectedClientInfoToUser(req.UserID); err != nil { + if err := chat.SendConnectedClientInfoToUser(*req.UserId); err != nil { log.Debugln(err) } - webutils.WriteSimpleResponse(w, true, fmt.Sprintf("%s is moderator: %t", req.UserID, req.IsModerator)) + webutils.WriteSimpleResponse(w, true, fmt.Sprintf("%s is moderator: %t", *req.UserId, *req.IsModerator)) } // GetModerators will return a list of moderator users. diff --git a/webserver/handlers/admin/config.go b/webserver/handlers/admin/config.go index 56b11e8d6..bc9c0a104 100644 --- a/webserver/handlers/admin/config.go +++ b/webserver/handlers/admin/config.go @@ -17,6 +17,7 @@ import ( "github.com/owncast/owncast/core/webhooks" "github.com/owncast/owncast/models" "github.com/owncast/owncast/utils" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" log "github.com/sirupsen/logrus" "github.com/teris-io/shortid" @@ -678,18 +679,15 @@ func SetCustomJavascript(w http.ResponseWriter, r *http.Request) { // SetForbiddenUsernameList will set the list of usernames we do not allow to use. func SetForbiddenUsernameList(w http.ResponseWriter, r *http.Request) { - type forbiddenUsernameListRequest struct { - Value []string `json:"value"` - } - decoder := json.NewDecoder(r.Body) - var request forbiddenUsernameListRequest + var request generated.SetForbiddenUsernameListJSONBody + if err := decoder.Decode(&request); err != nil { webutils.WriteSimpleResponse(w, false, "unable to update forbidden usernames with provided values") return } - if err := data.SetForbiddenUsernameList(request.Value); err != nil { + if err := data.SetForbiddenUsernameList(*request.Value); err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return } @@ -699,19 +697,15 @@ func SetForbiddenUsernameList(w http.ResponseWriter, r *http.Request) { // SetSuggestedUsernameList will set the list of suggested usernames that newly registered users are assigned if it isn't inferred otherwise (i.e. through a proxy). func SetSuggestedUsernameList(w http.ResponseWriter, r *http.Request) { - type suggestedUsernameListRequest struct { - Value []string `json:"value"` - } - decoder := json.NewDecoder(r.Body) - var request suggestedUsernameListRequest + var request generated.SetSuggestedUsernameListJSONBody if err := decoder.Decode(&request); err != nil { webutils.WriteSimpleResponse(w, false, "unable to update suggested usernames with provided values") return } - if err := data.SetSuggestedUsernamesList(request.Value); err != nil { + if err := data.SetSuggestedUsernamesList(*request.Value); err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return } diff --git a/webserver/handlers/admin/externalAPIUsers.go b/webserver/handlers/admin/externalAPIUsers.go index 016d61732..28ec61967 100644 --- a/webserver/handlers/admin/externalAPIUsers.go +++ b/webserver/handlers/admin/externalAPIUsers.go @@ -10,22 +10,14 @@ import ( "github.com/owncast/owncast/models" "github.com/owncast/owncast/persistence/userrepository" "github.com/owncast/owncast/utils" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" ) -type deleteExternalAPIUserRequest struct { - Token string `json:"token"` -} - -type createExternalAPIUserRequest struct { - Name string `json:"name"` - Scopes []string `json:"scopes"` -} - // CreateExternalAPIUser will generate a 3rd party access token. func CreateExternalAPIUser(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) - var request createExternalAPIUserRequest + var request generated.CreateExternalAPIUserJSONBody if err := decoder.Decode(&request); err != nil { webutils.BadRequestHandler(w, err) return @@ -34,7 +26,7 @@ func CreateExternalAPIUser(w http.ResponseWriter, r *http.Request) { userRepository := userrepository.Get() // Verify all the scopes provided are valid - if !userRepository.HasValidScopes(request.Scopes) { + if !userRepository.HasValidScopes(*request.Scopes) { webutils.BadRequestHandler(w, errors.New("one or more invalid scopes provided")) return } @@ -47,7 +39,7 @@ func CreateExternalAPIUser(w http.ResponseWriter, r *http.Request) { color := utils.GenerateRandomDisplayColor(config.MaxUserColor) - if err := userRepository.InsertExternalAPIUser(token, request.Name, color, request.Scopes); err != nil { + if err := userRepository.InsertExternalAPIUser(token, *request.Name, color, *request.Scopes); err != nil { webutils.InternalErrorHandler(w, err) return } @@ -55,9 +47,9 @@ func CreateExternalAPIUser(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") webutils.WriteResponse(w, models.ExternalAPIUser{ AccessToken: token, - DisplayName: request.Name, + DisplayName: *request.Name, DisplayColor: color, - Scopes: request.Scopes, + Scopes: *request.Scopes, CreatedAt: time.Now(), LastUsedAt: nil, }) @@ -87,20 +79,20 @@ func DeleteExternalAPIUser(w http.ResponseWriter, r *http.Request) { } decoder := json.NewDecoder(r.Body) - var request deleteExternalAPIUserRequest + var request generated.DeleteExternalAPIUserJSONBody if err := decoder.Decode(&request); err != nil { webutils.BadRequestHandler(w, err) return } - if request.Token == "" { + if request.Token != nil && *request.Token == "" { webutils.BadRequestHandler(w, errors.New("must provide a token")) return } userRepository := userrepository.Get() - if err := userRepository.DeleteExternalAPIUser(request.Token); err != nil { + if err := userRepository.DeleteExternalAPIUser(*request.Token); err != nil { webutils.InternalErrorHandler(w, err) return } diff --git a/webserver/handlers/admin/followers.go b/webserver/handlers/admin/followers.go index e6ea0bcf0..2bea8e9cb 100644 --- a/webserver/handlers/admin/followers.go +++ b/webserver/handlers/admin/followers.go @@ -7,6 +7,7 @@ import ( "github.com/owncast/owncast/activitypub/persistence" "github.com/owncast/owncast/activitypub/requests" "github.com/owncast/owncast/core/data" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" ) @@ -16,28 +17,28 @@ func ApproveFollower(w http.ResponseWriter, r *http.Request) { return } - type approveFollowerRequest struct { - ActorIRI string `json:"actorIRI"` - Approved bool `json:"approved"` - } + // type approveFollowerRequest struct { + // ActorIRI string `json:"actorIRI"` + // Approved bool `json:"approved"` + // } decoder := json.NewDecoder(r.Body) - var approval approveFollowerRequest + var approval generated.ApproveFollowerJSONBody if err := decoder.Decode(&approval); err != nil { webutils.WriteSimpleResponse(w, false, "unable to handle follower state with provided values") return } - if approval.Approved { + if *approval.Approved { // Approve a follower - if err := persistence.ApprovePreviousFollowRequest(approval.ActorIRI); err != nil { + if err := persistence.ApprovePreviousFollowRequest(*approval.ActorIRI); err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return } localAccountName := data.GetDefaultFederationUsername() - followRequest, err := persistence.GetFollower(approval.ActorIRI) + followRequest, err := persistence.GetFollower(*approval.ActorIRI) if err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return @@ -50,7 +51,7 @@ func ApproveFollower(w http.ResponseWriter, r *http.Request) { } } else { // Remove/block a follower - if err := persistence.BlockOrRejectFollower(approval.ActorIRI); err != nil { + if err := persistence.BlockOrRejectFollower(*approval.ActorIRI); err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return } diff --git a/webserver/handlers/admin/webhooks.go b/webserver/handlers/admin/webhooks.go index 156b11299..719edc6fb 100644 --- a/webserver/handlers/admin/webhooks.go +++ b/webserver/handlers/admin/webhooks.go @@ -8,13 +8,10 @@ import ( "github.com/owncast/owncast/core/data" "github.com/owncast/owncast/models" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" ) -type deleteWebhookRequest struct { - ID int `json:"id"` -} - type createWebhookRequest struct { URL string `json:"url"` Events []models.EventType `json:"events"` @@ -69,13 +66,13 @@ func DeleteWebhook(w http.ResponseWriter, r *http.Request) { } decoder := json.NewDecoder(r.Body) - var request deleteWebhookRequest + var request generated.DeleteWebhookJSONBody if err := decoder.Decode(&request); err != nil { webutils.BadRequestHandler(w, err) return } - if err := data.DeleteWebhook(request.ID); err != nil { + if err := data.DeleteWebhook(*request.Id); err != nil { webutils.InternalErrorHandler(w, err) return } diff --git a/webserver/handlers/chat.go b/webserver/handlers/chat.go index 010fe5254..a7112f8e3 100644 --- a/webserver/handlers/chat.go +++ b/webserver/handlers/chat.go @@ -10,6 +10,7 @@ import ( "github.com/owncast/owncast/models" "github.com/owncast/owncast/persistence/userrepository" "github.com/owncast/owncast/utils" + "github.com/owncast/owncast/webserver/handlers/generated" "github.com/owncast/owncast/webserver/router/middleware" webutils "github.com/owncast/owncast/webserver/utils" log "github.com/sirupsen/logrus" @@ -64,10 +65,6 @@ func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) { return } - type registerAnonymousUserRequest struct { - DisplayName string `json:"displayName"` - } - type registerAnonymousUserResponse struct { ID string `json:"id"` AccessToken string `json:"accessToken"` @@ -75,14 +72,14 @@ func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) { } decoder := json.NewDecoder(r.Body) - var request registerAnonymousUserRequest - if err := decoder.Decode(&request); err != nil { //nolint + var request generated.RegisterAnonymousChatUserJSONBody // registerAnonymousUserRequest + if err := decoder.Decode(&request); err != nil { //nolint // this is fine. register a new user anyway. } proposedNewDisplayName := r.Header.Get("X-Forwarded-User") - if proposedNewDisplayName == "" { - proposedNewDisplayName = request.DisplayName + if proposedNewDisplayName == "" && request.DisplayName != nil { + proposedNewDisplayName = *request.DisplayName } if proposedNewDisplayName == "" { proposedNewDisplayName = generateDisplayName() diff --git a/webserver/handlers/constants.go b/webserver/handlers/constants.go deleted file mode 100644 index 52741dac6..000000000 --- a/webserver/handlers/constants.go +++ /dev/null @@ -1,7 +0,0 @@ -package handlers - -// POST is the HTTP POST method. -const POST = "POST" - -// GET is the HTTP GET method. -const GET = "GET" diff --git a/webserver/handlers/playbackMetrics.go b/webserver/handlers/playbackMetrics.go index 6cd9660ee..1bb7954bd 100644 --- a/webserver/handlers/playbackMetrics.go +++ b/webserver/handlers/playbackMetrics.go @@ -6,6 +6,7 @@ import ( "github.com/owncast/owncast/metrics" "github.com/owncast/owncast/utils" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" log "github.com/sirupsen/logrus" ) @@ -13,21 +14,13 @@ import ( // ReportPlaybackMetrics will accept playback metrics from a client and save // them for future video health reporting. func ReportPlaybackMetrics(w http.ResponseWriter, r *http.Request) { - if r.Method != POST { + if r.Method != http.MethodPost { webutils.WriteSimpleResponse(w, false, r.Method+" not supported") return } - type reportPlaybackMetricsRequest struct { - Bandwidth float64 `json:"bandwidth"` - Latency float64 `json:"latency"` - Errors float64 `json:"errors"` - DownloadDuration float64 `json:"downloadDuration"` - QualityVariantChanges float64 `json:"qualityVariantChanges"` - } - decoder := json.NewDecoder(r.Body) - var request reportPlaybackMetricsRequest + var request generated.ReportPlaybackMetricsJSONRequestBody if err := decoder.Decode(&request); err != nil { log.Errorln("error decoding playback metrics payload:", err) webutils.WriteSimpleResponse(w, false, err.Error()) @@ -36,18 +29,18 @@ func ReportPlaybackMetrics(w http.ResponseWriter, r *http.Request) { clientID := utils.GenerateClientIDFromRequest(r) - metrics.RegisterPlaybackErrorCount(clientID, request.Errors) - if request.Bandwidth != 0.0 { - metrics.RegisterPlayerBandwidth(clientID, request.Bandwidth) + metrics.RegisterPlaybackErrorCount(clientID, *request.Errors) + if *request.Bandwidth != 0.0 { + metrics.RegisterPlayerBandwidth(clientID, *request.Bandwidth) } - if request.Latency != 0.0 { - metrics.RegisterPlayerLatency(clientID, request.Latency) + if *request.Latency != 0.0 { + metrics.RegisterPlayerLatency(clientID, *request.Latency) } - if request.DownloadDuration != 0.0 { - metrics.RegisterPlayerSegmentDownloadDuration(clientID, request.DownloadDuration) + if *request.DownloadDuration != 0.0 { + metrics.RegisterPlayerSegmentDownloadDuration(clientID, *request.DownloadDuration) } - metrics.RegisterQualityVariantChangesCount(clientID, request.QualityVariantChanges) + metrics.RegisterQualityVariantChangesCount(clientID, *request.QualityVariantChanges) } diff --git a/webserver/handlers/remoteFollow.go b/webserver/handlers/remoteFollow.go index 6dbe62922..5dcd63ac1 100644 --- a/webserver/handlers/remoteFollow.go +++ b/webserver/handlers/remoteFollow.go @@ -9,27 +9,24 @@ import ( "github.com/owncast/owncast/activitypub/webfinger" "github.com/owncast/owncast/core/data" + "github.com/owncast/owncast/webserver/handlers/generated" webutils "github.com/owncast/owncast/webserver/utils" ) // RemoteFollow handles a request to begin the remote follow redirect flow. func RemoteFollow(w http.ResponseWriter, r *http.Request) { - type followRequest struct { - Account string `json:"account"` - } - type followResponse struct { RedirectURL string `json:"redirectUrl"` } - var request followRequest + var request generated.RemoteFollowJSONRequestBody decoder := json.NewDecoder(r.Body) if err := decoder.Decode(&request); err != nil { webutils.WriteSimpleResponse(w, false, "unable to parse request") return } - if request.Account == "" { + if request.Account != nil && *request.Account == "" { webutils.WriteSimpleResponse(w, false, "Remote Fediverse account is required to follow.") return } @@ -37,7 +34,7 @@ func RemoteFollow(w http.ResponseWriter, r *http.Request) { localActorPath, _ := url.Parse(data.GetServerURL()) localActorPath.Path = fmt.Sprintf("/federation/user/%s", data.GetDefaultFederationUsername()) var template string - links, err := webfinger.GetWebfingerLinks(request.Account) + links, err := webfinger.GetWebfingerLinks(*request.Account) if err != nil { webutils.WriteSimpleResponse(w, false, err.Error()) return @@ -53,7 +50,7 @@ func RemoteFollow(w http.ResponseWriter, r *http.Request) { } if localActorPath.String() == "" || template == "" { - webutils.WriteSimpleResponse(w, false, "unable to determine remote follow information for "+request.Account) + webutils.WriteSimpleResponse(w, false, "unable to determine remote follow information for "+*request.Account) return }