2024-07-02 07:44:37 +03:00
|
|
|
package handlers
|
2020-06-23 23:11:01 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2022-12-29 08:30:06 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/core/chat"
|
2024-07-02 04:58:50 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2024-11-16 06:20:58 +03:00
|
|
|
"github.com/owncast/owncast/persistence/configrepository"
|
2024-07-02 04:58:50 +03:00
|
|
|
"github.com/owncast/owncast/persistence/userrepository"
|
2022-12-29 08:30:06 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2024-07-03 06:37:58 +03:00
|
|
|
"github.com/owncast/owncast/webserver/handlers/generated"
|
2024-07-02 06:12:08 +03:00
|
|
|
"github.com/owncast/owncast/webserver/router/middleware"
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils "github.com/owncast/owncast/webserver/utils"
|
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.
|
2024-07-02 04:58:50 +03:00
|
|
|
func ExternalGetChatMessages(integration models.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.
|
2024-07-02 04:58:50 +03:00
|
|
|
func GetChatMessages(u models.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)
|
2024-07-02 07:44:37 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(webutils.J{"error": "method not implemented (PRs are accepted)"}); err != nil {
|
|
|
|
webutils.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)
|
|
|
|
|
2024-07-02 04:58:50 +03:00
|
|
|
userRepository := userrepository.Get()
|
|
|
|
|
|
|
|
if r.Method == http.MethodOptions {
|
2022-05-03 03:45:22 +03:00
|
|
|
// 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 {
|
2023-10-21 19:46:45 +03:00
|
|
|
// nolint:goconst
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, r.Method+" not supported")
|
2021-07-20 05:22:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-07-03 06:37:58 +03:00
|
|
|
var request generated.RegisterAnonymousChatUserJSONBody // registerAnonymousUserRequest
|
|
|
|
if err := decoder.Decode(&request); err != nil { //nolint
|
2021-07-20 05:22:29 +03:00
|
|
|
// this is fine. register a new user anyway.
|
|
|
|
}
|
|
|
|
|
2024-07-02 04:58:50 +03:00
|
|
|
proposedNewDisplayName := r.Header.Get("X-Forwarded-User")
|
2024-07-03 06:37:58 +03:00
|
|
|
if proposedNewDisplayName == "" && request.DisplayName != nil {
|
|
|
|
proposedNewDisplayName = *request.DisplayName
|
2024-07-02 04:58:50 +03:00
|
|
|
}
|
|
|
|
if proposedNewDisplayName == "" {
|
|
|
|
proposedNewDisplayName = generateDisplayName()
|
2021-12-31 08:33:45 +03:00
|
|
|
}
|
|
|
|
|
2024-07-02 04:58:50 +03:00
|
|
|
proposedNewDisplayName = utils.MakeSafeStringOfLength(proposedNewDisplayName, config.MaxChatDisplayNameLength)
|
|
|
|
newUser, accessToken, err := userRepository.CreateAnonymousUser(proposedNewDisplayName)
|
2021-07-20 05:22:29 +03:00
|
|
|
if err != nil {
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, err.Error())
|
2021-07-20 05:22:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := registerAnonymousUserResponse{
|
2021-09-12 10:18:15 +03:00
|
|
|
ID: newUser.ID,
|
2022-04-22 00:55:26 +03:00
|
|
|
AccessToken: accessToken,
|
2022-12-30 01:52:15 +03:00
|
|
|
DisplayName: newUser.DisplayName,
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:58:03 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
middleware.DisableCache(w)
|
|
|
|
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteResponse(w, response)
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
2024-07-02 04:58:50 +03:00
|
|
|
|
|
|
|
func generateDisplayName() string {
|
2024-11-16 06:20:58 +03:00
|
|
|
configRepository := configrepository.Get()
|
|
|
|
suggestedUsernamesList := configRepository.GetSuggestedUsernamesList()
|
2024-07-02 04:58:50 +03:00
|
|
|
minSuggestedUsernamePoolLength := 10
|
|
|
|
|
|
|
|
if len(suggestedUsernamesList) >= minSuggestedUsernamePoolLength {
|
|
|
|
index := utils.RandomIndex(len(suggestedUsernamesList))
|
|
|
|
return suggestedUsernamesList[index]
|
|
|
|
} else {
|
|
|
|
return utils.GeneratePhrase()
|
|
|
|
}
|
|
|
|
}
|