From 6c87aaef04e461fbfbc4e3b6460f249b8910aa67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Fafak?= Date: Tue, 24 Jan 2023 15:40:25 +0300 Subject: [PATCH] Custom site name set on root HTML title (#21106) * Site name set on HTML title 43924 * Added description meta tag. * EscapeString added, variables name changed * Update string check * Re-organized methods * Updated method * one return to readability of function * Update web/static.go Co-authored-by: Tim Scheuermann Co-authored-by: Tim Scheuermann Co-authored-by: Mattermod Co-authored-by: Mattermost Build --- web/static.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/web/static.go b/web/static.go index 5e856e267e..3b9943af75 100644 --- a/web/static.go +++ b/web/static.go @@ -4,7 +4,11 @@ package web import ( + "bytes" + "fmt" + "html" "net/http" + "os" "path" "path/filepath" "strings" @@ -73,7 +77,21 @@ func root(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") staticDir, _ := fileutils.FindDir(model.ClientDir) - http.ServeFile(w, r, filepath.Join(staticDir, "root.html")) + contents, err := os.ReadFile(filepath.Join(staticDir, "root.html")) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + titleTemplate := "%s" + originalHTML := fmt.Sprintf(titleTemplate, html.EscapeString(model.TeamSettingsDefaultSiteName)) + modifiedHTML := getOpenGraphMetaTags(c) + if originalHTML != modifiedHTML { + contents = bytes.ReplaceAll(contents, []byte(originalHTML), []byte(modifiedHTML)) + } + + w.Header().Set("Content-Type", "text/html") + w.Write(contents) } func staticFilesHandler(handler http.Handler) http.Handler { @@ -130,3 +148,27 @@ func unsupportedBrowserScriptHandler(w http.ResponseWriter, r *http.Request) { templatesDir, _ := templates.GetTemplateDirectory() http.ServeFile(w, r, filepath.Join(templatesDir, "unsupported_browser.js")) } + +func getOpenGraphMetaTags(c *Context) string { + siteName := model.TeamSettingsDefaultSiteName + customSiteName := c.App.Srv().Config().TeamSettings.SiteName + if customSiteName != nil && *customSiteName != "" { + siteName = *customSiteName + } + + siteDescription := model.TeamSettingsDefaultCustomDescriptionText + customSiteDescription := c.App.Srv().Config().TeamSettings.CustomDescriptionText + if customSiteDescription != nil && *customSiteDescription != "" { + siteDescription = *customSiteDescription + } + + titleTemplate := "%s" + titleHTML := fmt.Sprintf(titleTemplate, html.EscapeString(siteName)) + descriptionHTML := "" + if siteDescription != "" { + descriptionTemplate := "" + descriptionHTML = fmt.Sprintf(descriptionTemplate, html.EscapeString(siteDescription)) + } + + return titleHTML + descriptionHTML +}