chat.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/owncast/owncast/config"
  6. "github.com/owncast/owncast/core/chat"
  7. "github.com/owncast/owncast/core/user"
  8. "github.com/owncast/owncast/router/middleware"
  9. "github.com/owncast/owncast/utils"
  10. log "github.com/sirupsen/logrus"
  11. )
  12. // ExternalGetChatMessages gets all of the chat messages.
  13. func ExternalGetChatMessages(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
  14. middleware.EnableCors(w)
  15. getChatMessages(w, r)
  16. }
  17. // GetChatMessages gets all of the chat messages.
  18. func GetChatMessages(u user.User, w http.ResponseWriter, r *http.Request) {
  19. middleware.EnableCors(w)
  20. getChatMessages(w, r)
  21. }
  22. func getChatMessages(w http.ResponseWriter, r *http.Request) {
  23. w.Header().Set("Content-Type", "application/json")
  24. switch r.Method {
  25. case http.MethodGet:
  26. messages := chat.GetChatHistory()
  27. if err := json.NewEncoder(w).Encode(messages); err != nil {
  28. log.Debugln(err)
  29. }
  30. default:
  31. w.WriteHeader(http.StatusNotImplemented)
  32. if err := json.NewEncoder(w).Encode(j{"error": "method not implemented (PRs are accepted)"}); err != nil {
  33. InternalErrorHandler(w, err)
  34. }
  35. }
  36. }
  37. // RegisterAnonymousChatUser will register a new user.
  38. func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) {
  39. middleware.EnableCors(w)
  40. if r.Method == "OPTIONS" {
  41. // All OPTIONS requests should have a wildcard CORS header.
  42. w.Header().Set("Access-Control-Allow-Origin", "*")
  43. w.WriteHeader(http.StatusNoContent)
  44. return
  45. }
  46. if r.Method != http.MethodPost {
  47. // nolint:goconst
  48. WriteSimpleResponse(w, false, r.Method+" not supported")
  49. return
  50. }
  51. type registerAnonymousUserRequest struct {
  52. DisplayName string `json:"displayName"`
  53. }
  54. type registerAnonymousUserResponse struct {
  55. ID string `json:"id"`
  56. AccessToken string `json:"accessToken"`
  57. DisplayName string `json:"displayName"`
  58. }
  59. decoder := json.NewDecoder(r.Body)
  60. var request registerAnonymousUserRequest
  61. if err := decoder.Decode(&request); err != nil { //nolint
  62. // this is fine. register a new user anyway.
  63. }
  64. if request.DisplayName == "" {
  65. request.DisplayName = r.Header.Get("X-Forwarded-User")
  66. }
  67. proposedNewDisplayName := utils.MakeSafeStringOfLength(request.DisplayName, config.MaxChatDisplayNameLength)
  68. newUser, accessToken, err := user.CreateAnonymousUser(proposedNewDisplayName)
  69. if err != nil {
  70. WriteSimpleResponse(w, false, err.Error())
  71. return
  72. }
  73. response := registerAnonymousUserResponse{
  74. ID: newUser.ID,
  75. AccessToken: accessToken,
  76. DisplayName: newUser.DisplayName,
  77. }
  78. w.Header().Set("Content-Type", "application/json")
  79. middleware.DisableCache(w)
  80. WriteResponse(w, response)
  81. }