0

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.
This commit is contained in:
nebunez 2021-03-21 17:10:56 -04:00 committed by GitHub
parent 0024ee2fb3
commit 80579c5e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 13 deletions

View File

@ -7,6 +7,7 @@ type Defaults struct {
Name string Name string
Title string Title string
Summary string Summary string
ServerWelcomeMessage string
Logo string Logo string
Tags []string Tags []string
PageBodyContent string PageBodyContent string
@ -30,6 +31,7 @@ func GetDefaults() Defaults {
Name: "Owncast", Name: "Owncast",
Title: "My Owncast Server", 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.", 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", Logo: "logo.svg",
Tags: []string{ Tags: []string{
"owncast", "owncast",

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"github.com/owncast/owncast/controllers" "github.com/owncast/owncast/controllers"
"github.com/owncast/owncast/core" "github.com/owncast/owncast/core"
@ -120,6 +121,25 @@ func SetServerSummary(w http.ResponseWriter, r *http.Request) {
controllers.WriteSimpleResponse(w, true, "changed") 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. // SetExtraPageContent will handle the web config request to set the page markdown content.
func SetExtraPageContent(w http.ResponseWriter, r *http.Request) { func SetExtraPageContent(w http.ResponseWriter, r *http.Request) {
if !requirePOST(w, r) { if !requirePOST(w, r) {

View File

@ -35,6 +35,7 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) {
Tags: data.GetServerMetadataTags(), Tags: data.GetServerMetadataTags(),
ExtraPageContent: data.GetExtraPageBodyContent(), ExtraPageContent: data.GetExtraPageBodyContent(),
StreamTitle: data.GetStreamTitle(), StreamTitle: data.GetStreamTitle(),
WelcomeMessage: data.GetServerWelcomeMessage(),
Logo: data.GetLogoPath(), Logo: data.GetLogoPath(),
SocialHandles: data.GetSocialHandles(), SocialHandles: data.GetSocialHandles(),
NSFW: data.GetNSFW(), NSFW: data.GetNSFW(),
@ -85,6 +86,7 @@ type videoSettings struct {
type webConfigResponse struct { type webConfigResponse struct {
Name string `json:"name"` Name string `json:"name"`
Summary string `json:"summary"` Summary string `json:"summary"`
WelcomeMessage string `json:"welcomeMessage"`
Logo string `json:"logo"` Logo string `json:"logo"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
Version string `json:"version"` Version string `json:"version"`

View File

@ -182,8 +182,10 @@ func (s *server) sendWelcomeMessageToClient(c *Client) {
// Add an artificial delay so people notice this message come in. // Add an artificial delay so people notice this message come in.
time.Sleep(7 * time.Second) time.Sleep(7 * time.Second)
initialChatMessageText := fmt.Sprintf("Welcome to %s! %s", data.GetServerName(), data.GetServerSummary()) welcomeMessage := data.GetServerWelcomeMessage()
initialMessage := models.ChatEvent{ClientID: "owncast-server", Author: data.GetServerName(), Body: initialChatMessageText, ID: "initial-message-1", MessageType: "SYSTEM", Visible: true, Timestamp: time.Now()} 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) c.write(initialMessage)
}
}() }()
} }

View File

@ -16,6 +16,7 @@ const streamTitleKey = "stream_title"
const streamKeyKey = "stream_key" const streamKeyKey = "stream_key"
const logoPathKey = "logo_path" const logoPathKey = "logo_path"
const serverSummaryKey = "server_summary" const serverSummaryKey = "server_summary"
const serverWelcomeMessageKey = "server_welcome_message"
const serverNameKey = "server_name" const serverNameKey = "server_name"
const serverURLKey = "server_url" const serverURLKey = "server_url"
const httpPortNumberKey = "http_port_number" const httpPortNumberKey = "http_port_number"
@ -119,6 +120,22 @@ func SetServerSummary(summary string) error {
return _datastore.SetString(serverSummaryKey, summary) 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. // GetServerName will return the server name text.
func GetServerName() string { func GetServerName() string {
name, err := _datastore.GetString(serverNameKey) name, err := _datastore.GetString(serverNameKey)

View File

@ -28,6 +28,7 @@ func PopulateDefaults() {
_ = SetLogoPath(defaults.Logo) _ = SetLogoPath(defaults.Logo)
_ = SetServerMetadataTags([]string{"owncast", "streaming"}) _ = SetServerMetadataTags([]string{"owncast", "streaming"})
_ = SetServerSummary("Welcome to your new Owncast server! This description can be changed in the admin") _ = SetServerSummary("Welcome to your new Owncast server! This description can be changed in the admin")
_ = SetServerWelcomeMessage("")
_ = SetServerName("Owncast") _ = SetServerName("Owncast")
_ = SetStreamKey(defaults.StreamKey) _ = SetStreamKey(defaults.StreamKey)
_ = SetExtraPageBodyContent("This is your page's content that can be edited in the admin.") _ = SetExtraPageBodyContent("This is your page's content that can be edited in the admin.")

View File

@ -109,6 +109,9 @@ func Start() error {
// Server summary // Server summary
http.HandleFunc("/api/admin/config/serversummary", middleware.RequireAdminAuth(admin.SetServerSummary)) 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 // Disable chat
http.HandleFunc("/api/admin/config/chat/disable", middleware.RequireAdminAuth(admin.SetChatDisabled)) http.HandleFunc("/api/admin/config/chat/disable", middleware.RequireAdminAuth(admin.SetChatDisabled))