package views
import (
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/user"
"log"
"strings"
"text/template" // sic!
)
type l10nEntry struct {
_en string
_ru string
}
func en(v string) l10nEntry {
return l10nEntry{_en: v}
}
func (e l10nEntry) ru(v string) l10nEntry {
e._ru = v
return e
}
func (e l10nEntry) get(lang string) string {
if lang == "ru" && e._ru != "" {
return e._ru
}
return e._en
}
const aboutTemplateString = `
{{ printf (get .L.Title) .Cfg.WikiName }}
- {{ get .L.Version }}
- 1.11.0
{{ if .Cfg.UseAuth }}
- {{ get .L.HomeHypha }}
- {{ .Cfg.HomeHypha }}
- {{get .L.Auth}}
- {{ get .L.AuthOn }}
{{if .Cfg.TelegramEnabled}}- {{get .L.TelegramOn}}
{{end}}
- {{ get .L.UserCount }}
- {{ .UserCount }}
{{if .Cfg.RegistrationLimit}}
- {{get .L.RegistrationLimit}}
- {{.RegistrationLimit}}
{{end}}
- {{ get .L.Admins }}
{{$cfg := .Cfg}}{{ range $i, $username := .Admins }}
- {{ $username }}
{{ end }}
{{ else }}
- {{get .L.Auth}}
- {{ get .L.AuthOff }}
{{ end }}
`
var aboutData = struct {
L map[string]l10nEntry
Cfg map[string]interface{}
Admins []string
UserCount uint64
RegistrationLimit uint64
}{
L: map[string]l10nEntry{
"Title": en("About %s").ru("О %s"),
"Version": en("Mycorrhiza Wiki version").ru("Версия Микоризы"),
"UserCount": en("User count").ru("Число пользователей"),
"HomeHypha": en("Home hypha").ru("Домашняя гифа"),
"RegistrationLimit": en("RegistrationLimit").ru("Максимум пользователей"),
"Admins": en("Administrators").ru("Администраторы"),
"Auth": en("Authentication").ru("Аутентификация"),
"AuthOn": en("Authentication is on").ru("Аутентификация включена"),
"AuthOff": en("Authentication is off").ru("Аутентификация не включена"),
"TelegramOn": en("Telegram authentication is on").ru("Вход через Телеграм включён"),
},
}
func AboutHTML(lc *l18n.Localizer) string {
get := func(e l10nEntry) string {
return e.get(lc.Locale)
}
temp, err := template.New("about wiki").Funcs(template.FuncMap{"get": get}).Parse(aboutTemplateString)
if err != nil {
log.Fatalln(err)
}
data := aboutData
data.Admins = user.ListUsersWithGroup("admin")
data.UserCount = user.Count()
data.RegistrationLimit = cfg.RegistrationLimit
data.Cfg = map[string]interface{}{
"UseAuth": cfg.UseAuth,
"WikiName": cfg.WikiName,
"HomeHypha": cfg.HomeHypha,
"TelegramEnabled": cfg.TelegramEnabled,
"RegistrationLimit": cfg.RegistrationLimit,
}
var out strings.Builder
err = temp.Execute(&out, data)
if err != nil {
log.Println(err)
}
return out.String()
}