config.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/owncast/owncast/activitypub"
  8. "github.com/owncast/owncast/config"
  9. "github.com/owncast/owncast/core/data"
  10. "github.com/owncast/owncast/models"
  11. "github.com/owncast/owncast/router/middleware"
  12. "github.com/owncast/owncast/utils"
  13. log "github.com/sirupsen/logrus"
  14. )
  15. type webConfigResponse struct {
  16. AppearanceVariables map[string]string `json:"appearanceVariables"`
  17. Name string `json:"name"`
  18. CustomStyles string `json:"customStyles"`
  19. StreamTitle string `json:"streamTitle,omitempty"` // What's going on with the current stream
  20. OfflineMessage string `json:"offlineMessage"`
  21. Logo string `json:"logo"`
  22. Version string `json:"version"`
  23. SocketHostOverride string `json:"socketHostOverride,omitempty"`
  24. ExtraPageContent string `json:"extraPageContent"`
  25. Summary string `json:"summary"`
  26. Tags []string `json:"tags"`
  27. SocialHandles []models.SocialHandle `json:"socialHandles"`
  28. ExternalActions []models.ExternalAction `json:"externalActions"`
  29. Notifications notificationsConfigResponse `json:"notifications"`
  30. Federation federationConfigResponse `json:"federation"`
  31. MaxSocketPayloadSize int `json:"maxSocketPayloadSize"`
  32. HideViewerCount bool `json:"hideViewerCount"`
  33. ChatDisabled bool `json:"chatDisabled"`
  34. NSFW bool `json:"nsfw"`
  35. Authentication authenticationConfigResponse `json:"authentication"`
  36. }
  37. type federationConfigResponse struct {
  38. Account string `json:"account,omitempty"`
  39. FollowerCount int `json:"followerCount,omitempty"`
  40. Enabled bool `json:"enabled"`
  41. }
  42. type browserNotificationsConfigResponse struct {
  43. PublicKey string `json:"publicKey,omitempty"`
  44. Enabled bool `json:"enabled"`
  45. }
  46. type notificationsConfigResponse struct {
  47. Browser browserNotificationsConfigResponse `json:"browser"`
  48. }
  49. type authenticationConfigResponse struct {
  50. IndieAuthEnabled bool `json:"indieAuthEnabled"`
  51. }
  52. // GetWebConfig gets the status of the server.
  53. func GetWebConfig(w http.ResponseWriter, r *http.Request) {
  54. middleware.EnableCors(w)
  55. middleware.DisableCache(w)
  56. w.Header().Set("Content-Type", "application/json")
  57. configuration := getConfigResponse()
  58. if err := json.NewEncoder(w).Encode(configuration); err != nil {
  59. BadRequestHandler(w, err)
  60. }
  61. }
  62. func getConfigResponse() webConfigResponse {
  63. pageContent := utils.RenderPageContentMarkdown(data.GetExtraPageBodyContent())
  64. offlineMessage := utils.RenderSimpleMarkdown(data.GetCustomOfflineMessage())
  65. socialHandles := data.GetSocialHandles()
  66. for i, handle := range socialHandles {
  67. platform := models.GetSocialHandle(handle.Platform)
  68. if platform != nil {
  69. handle.Icon = platform.Icon
  70. socialHandles[i] = handle
  71. }
  72. }
  73. serverSummary := data.GetServerSummary()
  74. var federationResponse federationConfigResponse
  75. federationEnabled := data.GetFederationEnabled()
  76. followerCount, _ := activitypub.GetFollowerCount()
  77. if federationEnabled {
  78. serverURLString := data.GetServerURL()
  79. serverURL, _ := url.Parse(serverURLString)
  80. account := fmt.Sprintf("%s@%s", data.GetDefaultFederationUsername(), serverURL.Host)
  81. federationResponse = federationConfigResponse{
  82. Enabled: federationEnabled,
  83. FollowerCount: int(followerCount),
  84. Account: account,
  85. }
  86. }
  87. browserPushEnabled := data.GetBrowserPushConfig().Enabled
  88. browserPushPublicKey, err := data.GetBrowserPushPublicKey()
  89. if err != nil {
  90. log.Errorln("unable to fetch browser push notifications public key", err)
  91. browserPushEnabled = false
  92. }
  93. notificationsResponse := notificationsConfigResponse{
  94. Browser: browserNotificationsConfigResponse{
  95. Enabled: browserPushEnabled,
  96. PublicKey: browserPushPublicKey,
  97. },
  98. }
  99. authenticationResponse := authenticationConfigResponse{
  100. IndieAuthEnabled: data.GetServerURL() != "",
  101. }
  102. return webConfigResponse{
  103. Name: data.GetServerName(),
  104. Summary: serverSummary,
  105. OfflineMessage: offlineMessage,
  106. Logo: "/logo",
  107. Tags: data.GetServerMetadataTags(),
  108. Version: config.GetReleaseString(),
  109. NSFW: data.GetNSFW(),
  110. SocketHostOverride: data.GetWebsocketOverrideHost(),
  111. ExtraPageContent: pageContent,
  112. StreamTitle: data.GetStreamTitle(),
  113. SocialHandles: socialHandles,
  114. ChatDisabled: data.GetChatDisabled(),
  115. ExternalActions: data.GetExternalActions(),
  116. CustomStyles: data.GetCustomStyles(),
  117. MaxSocketPayloadSize: config.MaxSocketPayloadSize,
  118. Federation: federationResponse,
  119. Notifications: notificationsResponse,
  120. Authentication: authenticationResponse,
  121. AppearanceVariables: data.GetCustomColorVariableValues(),
  122. HideViewerCount: data.GetHideViewerCount(),
  123. }
  124. }
  125. // GetAllSocialPlatforms will return a list of all social platform types.
  126. func GetAllSocialPlatforms(w http.ResponseWriter, r *http.Request) {
  127. middleware.EnableCors(w)
  128. w.Header().Set("Content-Type", "application/json")
  129. platforms := models.GetAllSocialHandles()
  130. if err := json.NewEncoder(w).Encode(platforms); err != nil {
  131. InternalErrorHandler(w, err)
  132. }
  133. }