config.go 5.3 KB

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