From 80579c5e7e3dbaf689162ca6db6ea46d8a018e42 Mon Sep 17 00:00:00 2001 From: nebunez <76886915+nebunez@users.noreply.github.com> Date: Sun, 21 Mar 2021 17:10:56 -0400 Subject: [PATCH] implement custom welcome message (#820) * implement custom welcome message This change adds logic for handling custom welcome messages. * trim welcome message string on POST Instead of trimming the welcome message every time we are going to send to send a welcome message, we just trim once when the message is posted from the admin. --- config/defaults.go | 22 ++++++++++++---------- controllers/admin/config.go | 20 ++++++++++++++++++++ controllers/admin/serverConfig.go | 2 ++ core/chat/server.go | 8 +++++--- core/data/config.go | 17 +++++++++++++++++ core/data/defaults.go | 1 + router/router.go | 3 +++ 7 files changed, 60 insertions(+), 13 deletions(-) diff --git a/config/defaults.go b/config/defaults.go index 9c966b300..bb5fc5635 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -4,12 +4,13 @@ import "github.com/owncast/owncast/models" // Defaults will hold default configuration values. type Defaults struct { - Name string - Title string - Summary string - Logo string - Tags []string - PageBodyContent string + Name string + Title string + Summary string + ServerWelcomeMessage string + Logo string + Tags []string + PageBodyContent string DatabaseFilePath string WebServerPort int @@ -27,10 +28,11 @@ type Defaults struct { // GetDefaults will return default configuration values. func GetDefaults() Defaults { return Defaults{ - Name: "Owncast", - Title: "My Owncast Server", - Summary: "This is brief summary of whom you are or what your stream is. You can edit this description in the admin.", - Logo: "logo.svg", + Name: "Owncast", + Title: "My Owncast Server", + Summary: "This is brief summary of whom you are or what your stream is. You can edit this description in the admin.", + ServerWelcomeMessage: "", + Logo: "logo.svg", Tags: []string{ "owncast", "streaming", diff --git a/controllers/admin/config.go b/controllers/admin/config.go index ea6d370d5..9b7f81226 100644 --- a/controllers/admin/config.go +++ b/controllers/admin/config.go @@ -6,6 +6,7 @@ import ( "net/http" "path/filepath" "reflect" + "strings" "github.com/owncast/owncast/controllers" "github.com/owncast/owncast/core" @@ -120,6 +121,25 @@ func SetServerSummary(w http.ResponseWriter, r *http.Request) { controllers.WriteSimpleResponse(w, true, "changed") } +// SetServerWelcomeMessage will handle the web config request to set the welcome message text. +func SetServerWelcomeMessage(w http.ResponseWriter, r *http.Request) { + if !requirePOST(w, r) { + return + } + + configValue, success := getValueFromRequest(w, r) + if !success { + return + } + + if err := data.SetServerWelcomeMessage(strings.TrimSpace(configValue.Value.(string))); err != nil { + controllers.WriteSimpleResponse(w, false, err.Error()) + return + } + + controllers.WriteSimpleResponse(w, true, "changed") +} + // SetExtraPageContent will handle the web config request to set the page markdown content. func SetExtraPageContent(w http.ResponseWriter, r *http.Request) { if !requirePOST(w, r) { diff --git a/controllers/admin/serverConfig.go b/controllers/admin/serverConfig.go index 6d99f001b..8ba2d8ebd 100644 --- a/controllers/admin/serverConfig.go +++ b/controllers/admin/serverConfig.go @@ -35,6 +35,7 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) { Tags: data.GetServerMetadataTags(), ExtraPageContent: data.GetExtraPageBodyContent(), StreamTitle: data.GetStreamTitle(), + WelcomeMessage: data.GetServerWelcomeMessage(), Logo: data.GetLogoPath(), SocialHandles: data.GetSocialHandles(), NSFW: data.GetNSFW(), @@ -85,6 +86,7 @@ type videoSettings struct { type webConfigResponse struct { Name string `json:"name"` Summary string `json:"summary"` + WelcomeMessage string `json:"welcomeMessage"` Logo string `json:"logo"` Tags []string `json:"tags"` Version string `json:"version"` diff --git a/core/chat/server.go b/core/chat/server.go index 9ce5c5ee8..01dfada58 100644 --- a/core/chat/server.go +++ b/core/chat/server.go @@ -182,8 +182,10 @@ func (s *server) sendWelcomeMessageToClient(c *Client) { // Add an artificial delay so people notice this message come in. time.Sleep(7 * time.Second) - initialChatMessageText := fmt.Sprintf("Welcome to %s! %s", data.GetServerName(), data.GetServerSummary()) - initialMessage := models.ChatEvent{ClientID: "owncast-server", Author: data.GetServerName(), Body: initialChatMessageText, ID: "initial-message-1", MessageType: "SYSTEM", Visible: true, Timestamp: time.Now()} - c.write(initialMessage) + welcomeMessage := data.GetServerWelcomeMessage() + if welcomeMessage != "" { + initialMessage := models.ChatEvent{ClientID: "owncast-server", Author: data.GetServerName(), Body: welcomeMessage, ID: "initial-message-1", MessageType: "SYSTEM", Visible: true, Timestamp: time.Now()} + c.write(initialMessage) + } }() } diff --git a/core/data/config.go b/core/data/config.go index 9455748ef..1655fc211 100644 --- a/core/data/config.go +++ b/core/data/config.go @@ -16,6 +16,7 @@ const streamTitleKey = "stream_title" const streamKeyKey = "stream_key" const logoPathKey = "logo_path" const serverSummaryKey = "server_summary" +const serverWelcomeMessageKey = "server_welcome_message" const serverNameKey = "server_name" const serverURLKey = "server_url" const httpPortNumberKey = "http_port_number" @@ -119,6 +120,22 @@ func SetServerSummary(summary string) error { return _datastore.SetString(serverSummaryKey, summary) } +// GetServerWelcomeMessage will return the server welcome message text. +func GetServerWelcomeMessage() string { + welcomeMessage, err := _datastore.GetString(serverWelcomeMessageKey) + if err != nil { + log.Debugln(serverWelcomeMessageKey, err) + return config.GetDefaults().ServerWelcomeMessage + } + + return welcomeMessage +} + +// SetServerWelcomeMessage will set the server welcome message text. +func SetServerWelcomeMessage(welcomeMessage string) error { + return _datastore.SetString(serverWelcomeMessageKey, welcomeMessage) +} + // GetServerName will return the server name text. func GetServerName() string { name, err := _datastore.GetString(serverNameKey) diff --git a/core/data/defaults.go b/core/data/defaults.go index 4519b6736..f52387494 100644 --- a/core/data/defaults.go +++ b/core/data/defaults.go @@ -28,6 +28,7 @@ func PopulateDefaults() { _ = SetLogoPath(defaults.Logo) _ = SetServerMetadataTags([]string{"owncast", "streaming"}) _ = SetServerSummary("Welcome to your new Owncast server! This description can be changed in the admin") + _ = SetServerWelcomeMessage("") _ = SetServerName("Owncast") _ = SetStreamKey(defaults.StreamKey) _ = SetExtraPageBodyContent("This is your page's content that can be edited in the admin.") diff --git a/router/router.go b/router/router.go index e28c48cb3..dc42bf723 100644 --- a/router/router.go +++ b/router/router.go @@ -109,6 +109,9 @@ func Start() error { // Server summary http.HandleFunc("/api/admin/config/serversummary", middleware.RequireAdminAuth(admin.SetServerSummary)) + // Server welcome message + http.HandleFunc("/api/admin/config/welcomemessage", middleware.RequireAdminAuth(admin.SetServerWelcomeMessage)) + // Disable chat http.HandleFunc("/api/admin/config/chat/disable", middleware.RequireAdminAuth(admin.SetChatDisabled))