Files
owncast/webserver/handlers/playbackMetrics.go
Copilot 149d80a07d Inline chat moderation request returning 500s (#4412)
* Initial plan

* Initial analysis of inline chat moderation 500 error issue

Co-authored-by: gabek <414923+gabek@users.noreply.github.com>

* Fix inline chat moderation 500 errors by supporting Authorization header tokens

- Modified RequireUserModerationScopeAccesstoken middleware to check both Authorization header (Bearer tokens) and URL query parameters
- This fixes the issue where inline chat moderation from the web UI was failing with 500 errors
- Maintains backward compatibility with existing code that uses query parameter tokens
- The fix aligns the chat endpoint behavior with the integrations endpoint which already supported Bearer tokens

Addresses the wiring issue between OpenAPI spec and API implementation identified in issue comments.

Co-authored-by: gabek <414923+gabek@users.noreply.github.com>

* Fix inline chat moderation 500 errors by adding nil pointer checks

Co-authored-by: gabek <414923+gabek@users.noreply.github.com>

* Fix nil pointer vulnerabilities across all OpenAPI endpoints

Co-authored-by: gabek <414923+gabek@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: gabek <414923+gabek@users.noreply.github.com>
2025-07-04 23:57:03 -07:00

56 lines
1.7 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"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"
)
// 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 != http.MethodPost {
webutils.WriteSimpleResponse(w, false, r.Method+" not supported")
return
}
decoder := json.NewDecoder(r.Body)
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())
return
}
clientID := utils.GenerateClientIDFromRequest(r)
if request.Errors == nil {
webutils.WriteSimpleResponse(w, false, "errors field is required")
return
}
metrics.RegisterPlaybackErrorCount(clientID, *request.Errors)
if request.Bandwidth != nil && *request.Bandwidth != 0.0 {
metrics.RegisterPlayerBandwidth(clientID, *request.Bandwidth)
}
if request.Latency != nil && *request.Latency != 0.0 {
metrics.RegisterPlayerLatency(clientID, *request.Latency)
}
if request.DownloadDuration != nil && *request.DownloadDuration != 0.0 {
metrics.RegisterPlayerSegmentDownloadDuration(clientID, *request.DownloadDuration)
}
if request.QualityVariantChanges == nil {
webutils.WriteSimpleResponse(w, false, "qualityVariantChanges field is required")
return
}
metrics.RegisterQualityVariantChangesCount(clientID, *request.QualityVariantChanges)
}