Add support for active viewer details API. Closes #1477 (#1747)

This commit is contained in:
Gabe Kangas
2022-03-06 17:31:47 -08:00
committed by GitHub
parent 92041c4c23
commit 98fce01b52
7 changed files with 96 additions and 23 deletions

View File

@@ -4,7 +4,11 @@ import (
"encoding/json"
"net/http"
"github.com/owncast/owncast/controllers"
"github.com/owncast/owncast/core"
"github.com/owncast/owncast/core/user"
"github.com/owncast/owncast/metrics"
"github.com/owncast/owncast/models"
log "github.com/sirupsen/logrus"
)
@@ -17,3 +21,23 @@ func GetViewersOverTime(w http.ResponseWriter, r *http.Request) {
log.Errorln(err)
}
}
// GetActiveViewers returns currently connected clients.
func GetActiveViewers(w http.ResponseWriter, r *http.Request) {
c := core.GetActiveViewers()
viewers := []models.Viewer{}
for _, v := range c {
viewers = append(viewers, *v)
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(viewers); err != nil {
controllers.InternalErrorHandler(w, err)
}
}
// ExternalGetActiveViewers returns currently connected clients.
func ExternalGetActiveViewers(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
GetConnectedChatClients(w, r)
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/router/middleware"
"github.com/owncast/owncast/utils"
)
@@ -42,8 +43,8 @@ func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-mpegURL")
// Use this as an opportunity to mark this viewer as active.
id := utils.GenerateClientIDFromRequest(r)
core.SetViewerIDActive(id)
viewer := models.GenerateViewerFromRequest(r)
core.SetViewerActive(&viewer)
} else {
cacheTime := utils.GetCacheDurationSecondsForPath(relativePath)
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(cacheTime))

View File

@@ -4,11 +4,11 @@ import (
"net/http"
"github.com/owncast/owncast/core"
"github.com/owncast/owncast/utils"
"github.com/owncast/owncast/models"
)
// Ping is fired by a client to show they are still an active viewer.
func Ping(w http.ResponseWriter, r *http.Request) {
id := utils.GenerateClientIDFromRequest(r)
core.SetViewerIDActive(id)
viewer := models.GenerateViewerFromRequest(r)
core.SetViewerActive(&viewer)
}