mirror of
https://github.com/owncast/owncast.git
synced 2024-11-24 05:38:58 +03:00
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/owncast/owncast/config"
|
|
"github.com/owncast/owncast/core/chat"
|
|
"github.com/owncast/owncast/core/user"
|
|
"github.com/owncast/owncast/router/middleware"
|
|
"github.com/owncast/owncast/utils"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// ExternalGetChatMessages gets all of the chat messages.
|
|
func ExternalGetChatMessages(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
|
|
middleware.EnableCors(w)
|
|
getChatMessages(w, r)
|
|
}
|
|
|
|
// GetChatMessages gets all of the chat messages.
|
|
func GetChatMessages(u user.User, w http.ResponseWriter, r *http.Request) {
|
|
middleware.EnableCors(w)
|
|
getChatMessages(w, r)
|
|
}
|
|
|
|
func getChatMessages(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
messages := chat.GetChatHistory()
|
|
|
|
if err := json.NewEncoder(w).Encode(messages); err != nil {
|
|
log.Debugln(err)
|
|
}
|
|
default:
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
if err := json.NewEncoder(w).Encode(j{"error": "method not implemented (PRs are accepted)"}); err != nil {
|
|
InternalErrorHandler(w, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// RegisterAnonymousChatUser will register a new user.
|
|
func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) {
|
|
middleware.EnableCors(w)
|
|
|
|
if r.Method == "OPTIONS" {
|
|
// All OPTIONS requests should have a wildcard CORS header.
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
if r.Method != http.MethodPost {
|
|
WriteSimpleResponse(w, false, r.Method+" not supported")
|
|
return
|
|
}
|
|
|
|
type registerAnonymousUserRequest struct {
|
|
DisplayName string `json:"displayName"`
|
|
}
|
|
|
|
type registerAnonymousUserResponse struct {
|
|
ID string `json:"id"`
|
|
AccessToken string `json:"accessToken"`
|
|
DisplayName string `json:"displayName"`
|
|
}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
var request registerAnonymousUserRequest
|
|
if err := decoder.Decode(&request); err != nil { //nolint
|
|
// this is fine. register a new user anyway.
|
|
}
|
|
|
|
if request.DisplayName == "" {
|
|
request.DisplayName = r.Header.Get("X-Forwarded-User")
|
|
}
|
|
|
|
proposedNewDisplayName := utils.MakeSafeStringOfLength(request.DisplayName, config.MaxChatDisplayNameLength)
|
|
newUser, accessToken, err := user.CreateAnonymousUser(proposedNewDisplayName)
|
|
if err != nil {
|
|
WriteSimpleResponse(w, false, err.Error())
|
|
return
|
|
}
|
|
|
|
response := registerAnonymousUserResponse{
|
|
ID: newUser.ID,
|
|
AccessToken: accessToken,
|
|
DisplayName: newUser.DisplayName,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
middleware.DisableCache(w)
|
|
|
|
WriteResponse(w, response)
|
|
}
|