gotosocial/internal/router/template.go

185 lines
5 KiB
Go
Raw Normal View History

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2021-09-01 21:12:31 +01:00
package router
import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"time"
"github.com/gin-gonic/gin"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/text"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
const (
justTime = "15:04"
dateYear = "Jan 02, 2006"
dateTime = "Jan 02, 15:04"
dateYearTime = "Jan 02, 2006, 15:04"
monthYear = "Jan, 2006"
badTimestamp = "bad timestamp"
)
// LoadTemplates loads html templates for use by the given engine
func LoadTemplates(engine *gin.Engine) error {
[chore] Global server configuration overhaul (#575) * move config flag names and usage to config package, rewrite config package to use global Configuration{} struct Signed-off-by: kim <grufwub@gmail.com> * improved code comment Signed-off-by: kim <grufwub@gmail.com> * linter Signed-off-by: kim <grufwub@gmail.com> * fix unmarshaling Signed-off-by: kim <grufwub@gmail.com> * remove kim's custom go compiler changes Signed-off-by: kim <grufwub@gmail.com> * generate setter and flag-name functions, implement these in codebase Signed-off-by: kim <grufwub@gmail.com> * update deps Signed-off-by: kim <grufwub@gmail.com> * small change Signed-off-by: kim <grufwub@gmail.com> * appease the linter... Signed-off-by: kim <grufwub@gmail.com> * move configuration into ConfigState structure, ensure reloading to/from viper settings to keep in sync Signed-off-by: kim <grufwub@gmail.com> * lint Signed-off-by: kim <grufwub@gmail.com> * update code comments Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * improved version string (removes time + go version) Signed-off-by: kim <grufwub@gmail.com> * fix version string build to pass test script + consolidate logic in func Signed-off-by: kim <grufwub@gmail.com> * add license text, update config.Defaults comment Signed-off-by: kim <grufwub@gmail.com> * add license text to generated config helpers file Signed-off-by: kim <grufwub@gmail.com> * defer unlock on config.Set___(), to ensure unlocked on panic Signed-off-by: kim <grufwub@gmail.com> * make it more obvious which cmd flags are being attached Signed-off-by: kim <grufwub@gmail.com>
2022-05-30 13:41:24 +01:00
templateBaseDir := config.GetWebTemplateBaseDir()
if templateBaseDir == "" {
[chore] Global server configuration overhaul (#575) * move config flag names and usage to config package, rewrite config package to use global Configuration{} struct Signed-off-by: kim <grufwub@gmail.com> * improved code comment Signed-off-by: kim <grufwub@gmail.com> * linter Signed-off-by: kim <grufwub@gmail.com> * fix unmarshaling Signed-off-by: kim <grufwub@gmail.com> * remove kim's custom go compiler changes Signed-off-by: kim <grufwub@gmail.com> * generate setter and flag-name functions, implement these in codebase Signed-off-by: kim <grufwub@gmail.com> * update deps Signed-off-by: kim <grufwub@gmail.com> * small change Signed-off-by: kim <grufwub@gmail.com> * appease the linter... Signed-off-by: kim <grufwub@gmail.com> * move configuration into ConfigState structure, ensure reloading to/from viper settings to keep in sync Signed-off-by: kim <grufwub@gmail.com> * lint Signed-off-by: kim <grufwub@gmail.com> * update code comments Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * improved version string (removes time + go version) Signed-off-by: kim <grufwub@gmail.com> * fix version string build to pass test script + consolidate logic in func Signed-off-by: kim <grufwub@gmail.com> * add license text, update config.Defaults comment Signed-off-by: kim <grufwub@gmail.com> * add license text to generated config helpers file Signed-off-by: kim <grufwub@gmail.com> * defer unlock on config.Set___(), to ensure unlocked on panic Signed-off-by: kim <grufwub@gmail.com> * make it more obvious which cmd flags are being attached Signed-off-by: kim <grufwub@gmail.com>
2022-05-30 13:41:24 +01:00
return fmt.Errorf("%s cannot be empty and must be a relative or absolute path", config.WebTemplateBaseDirFlag())
}
templateBaseDir, err := filepath.Abs(templateBaseDir)
if err != nil {
return fmt.Errorf("error getting absolute path of %s: %s", templateBaseDir, err)
}
if _, err := os.Stat(filepath.Join(templateBaseDir, "index.tmpl")); err != nil {
return fmt.Errorf("%s doesn't seem to contain the templates; index.tmpl is missing: %w", templateBaseDir, err)
}
engine.LoadHTMLGlob(filepath.Join(templateBaseDir, "*"))
return nil
}
func oddOrEven(n int) string {
if n%2 == 0 {
return "even"
}
2021-09-30 10:16:23 +01:00
return "odd"
}
func escape(str string) template.HTML {
/* #nosec G203 */
return template.HTML(template.HTMLEscapeString(str))
}
func noescape(str string) template.HTML {
/* #nosec G203 */
return template.HTML(str)
}
func noescapeAttr(str string) template.HTMLAttr {
/* #nosec G203 */
return template.HTMLAttr(str)
}
func timestamp(stamp string) string {
t, err := util.ParseISO8601(stamp)
if err != nil {
log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err)
return badTimestamp
}
t = t.Local()
tYear, tMonth, tDay := t.Date()
now := time.Now()
currentYear, currentMonth, currentDay := now.Date()
switch {
case tYear == currentYear && tMonth == currentMonth && tDay == currentDay:
return "Today, " + t.Format(justTime)
case tYear == currentYear:
return t.Format(dateTime)
default:
return t.Format(dateYear)
}
}
func timestampPrecise(stamp string) string {
t, err := util.ParseISO8601(stamp)
if err != nil {
log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err)
return badTimestamp
}
return t.Local().Format(dateYearTime)
}
func timestampVague(stamp string) string {
t, err := util.ParseISO8601(stamp)
if err != nil {
log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err)
return badTimestamp
}
return t.Format(monthYear)
}
2021-09-30 10:16:23 +01:00
type iconWithLabel struct {
faIcon string
label string
}
func visibilityIcon(visibility apimodel.Visibility) template.HTML {
2021-09-30 10:16:23 +01:00
var icon iconWithLabel
switch visibility {
case apimodel.VisibilityPublic:
2021-09-30 10:16:23 +01:00
icon = iconWithLabel{"globe", "public"}
case apimodel.VisibilityUnlisted:
2021-09-30 10:16:23 +01:00
icon = iconWithLabel{"unlock", "unlisted"}
case apimodel.VisibilityPrivate:
2021-09-30 10:16:23 +01:00
icon = iconWithLabel{"lock", "private"}
case apimodel.VisibilityMutualsOnly:
2021-09-30 10:16:23 +01:00
icon = iconWithLabel{"handshake-o", "mutuals only"}
case apimodel.VisibilityDirect:
2021-09-30 10:16:23 +01:00
icon = iconWithLabel{"envelope", "direct"}
}
/* #nosec G203 */
return template.HTML(fmt.Sprintf(`<i aria-label="Visibility: %v" class="fa fa-%v"></i>`, icon.label, icon.faIcon))
}
// text is a template.HTML to affirm that the input of this function is already escaped
func emojify(emojis []apimodel.Emoji, inputText template.HTML) template.HTML {
out := text.Emojify(emojis, string(inputText))
/* #nosec G203 */
// (this is escaped above)
return template.HTML(out)
}
func acctInstance(acct string) string {
parts := strings.Split(acct, "@")
if len(parts) > 1 {
return "@" + parts[1]
}
return ""
}
func LoadTemplateFunctions(engine *gin.Engine) {
engine.SetFuncMap(template.FuncMap{
"escape": escape,
"noescape": noescape,
"noescapeAttr": noescapeAttr,
"oddOrEven": oddOrEven,
"visibilityIcon": visibilityIcon,
"timestamp": timestamp,
"timestampVague": timestampVague,
"timestampPrecise": timestampPrecise,
"emojify": emojify,
"acctInstance": acctInstance,
})
}