2020-06-23 22:11:01 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2022-12-29 06:30:06 +01:00
|
|
|
"github.com/owncast/owncast/config"
|
2021-07-20 04:22:29 +02:00
|
|
|
"github.com/owncast/owncast/core/chat"
|
|
|
|
"github.com/owncast/owncast/core/user"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/router/middleware"
|
2022-12-29 06:30:06 +01:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-11-15 03:39:53 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 22:11:01 +02:00
|
|
|
)
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
// ExternalGetChatMessages gets all of the chat messages.
|
|
|
|
func ExternalGetChatMessages(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
|
2021-10-01 21:40:13 +02:00
|
|
|
middleware.EnableCors(w)
|
2022-04-21 23:55:26 +02:00
|
|
|
getChatMessages(w, r)
|
2021-07-20 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// GetChatMessages gets all of the chat messages.
|
2022-04-21 23:55:26 +02:00
|
|
|
func GetChatMessages(u user.User, w http.ResponseWriter, r *http.Request) {
|
2022-05-03 02:45:22 +02:00
|
|
|
middleware.EnableCors(w)
|
2022-04-21 23:55:26 +02:00
|
|
|
getChatMessages(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getChatMessages(w http.ResponseWriter, r *http.Request) {
|
2020-11-20 00:26:43 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-06-23 22:11:01 +02:00
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2021-07-20 04:22:29 +02:00
|
|
|
messages := chat.GetChatHistory()
|
2020-06-23 22:11:01 +02:00
|
|
|
|
2021-07-09 20:16:44 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(messages); err != nil {
|
2021-07-27 02:30:28 +02:00
|
|
|
log.Debugln(err)
|
2020-11-15 03:39:53 +01:00
|
|
|
}
|
2020-06-23 22:11:01 +02:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusNotImplemented)
|
2020-11-15 03:39:53 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(j{"error": "method not implemented (PRs are accepted)"}); err != nil {
|
2021-02-19 08:05:52 +01:00
|
|
|
InternalErrorHandler(w, err)
|
2020-11-15 03:39:53 +01:00
|
|
|
}
|
2020-06-23 22:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-20 04:22:29 +02:00
|
|
|
|
|
|
|
// RegisterAnonymousChatUser will register a new user.
|
|
|
|
func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) {
|
2022-05-03 02:45:22 +02:00
|
|
|
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 {
|
2021-07-20 04:22:29 +02:00
|
|
|
WriteSimpleResponse(w, false, r.Method+" not supported")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type registerAnonymousUserRequest struct {
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type registerAnonymousUserResponse struct {
|
2021-09-12 09:18:15 +02:00
|
|
|
ID string `json:"id"`
|
2021-07-20 04:22:29 +02:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2021-12-31 06:33:45 +01:00
|
|
|
if request.DisplayName == "" {
|
|
|
|
request.DisplayName = r.Header.Get("X-Forwarded-User")
|
|
|
|
}
|
|
|
|
|
2022-12-29 06:30:06 +01:00
|
|
|
proposedNewDisplayName := utils.MakeSafeStringOfLength(request.DisplayName, config.MaxChatDisplayNameLength)
|
|
|
|
newUser, accessToken, err := user.CreateAnonymousUser(proposedNewDisplayName)
|
2021-07-20 04:22:29 +02:00
|
|
|
if err != nil {
|
|
|
|
WriteSimpleResponse(w, false, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := registerAnonymousUserResponse{
|
2021-09-12 09:18:15 +02:00
|
|
|
ID: newUser.ID,
|
2022-04-21 23:55:26 +02:00
|
|
|
AccessToken: accessToken,
|
2022-12-29 23:52:15 +01:00
|
|
|
DisplayName: newUser.DisplayName,
|
2021-07-20 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:58:03 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
middleware.DisableCache(w)
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
WriteResponse(w, response)
|
|
|
|
}
|