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 <noxer@users.noreply.github.com>

Co-authored-by: Tim Scheuermann <noxer@users.noreply.github.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
şafak
2023-01-24 15:40:25 +03:00
коммит произвёл GitHub
родитель 7f5ac299ce
Коммит 6c87aaef04

Просмотреть файл

@@ -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 := "<title>%s</title>"
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 := "<title>%s</title>"
titleHTML := fmt.Sprintf(titleTemplate, html.EscapeString(siteName))
descriptionHTML := ""
if siteDescription != "" {
descriptionTemplate := "<meta property=\"og:description\" content=\"%s\" />"
descriptionHTML = fmt.Sprintf(descriptionTemplate, html.EscapeString(siteDescription))
}
return titleHTML + descriptionHTML
}