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