2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2022-08-11 03:59:59 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-08-10 05:10:47 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
2022-08-11 03:59:59 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/markdown"
|
2022-08-13 06:11:36 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/photo"
|
2022-08-10 05:10:47 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/session"
|
2022-08-11 03:59:59 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/utility"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TemplateFuncs available to all pages.
|
|
|
|
func TemplateFuncs(r *http.Request) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2022-08-11 03:59:59 +00:00
|
|
|
"InputCSRF": InputCSRF(r),
|
|
|
|
"SincePrettyCoarse": SincePrettyCoarse(),
|
|
|
|
"ComputeAge": utility.Age,
|
|
|
|
"Split": strings.Split,
|
|
|
|
"ToMarkdown": ToMarkdown,
|
2022-08-13 06:11:36 +00:00
|
|
|
"PhotoURL": photo.URLPath,
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputCSRF returns the HTML snippet for a CSRF token hidden input field.
|
|
|
|
func InputCSRF(r *http.Request) func() template.HTML {
|
|
|
|
return func() template.HTML {
|
|
|
|
ctx := r.Context()
|
|
|
|
if token, ok := ctx.Value(session.CSRFKey).(string); ok {
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<input type="hidden" name="%s" value="%s">`,
|
|
|
|
config.CSRFInputName,
|
|
|
|
token,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
return template.HTML(`[CSRF middleware error]`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 03:59:59 +00:00
|
|
|
|
|
|
|
// SincePrettyCoarse formats a time.Duration in plain English. Intended for "joined 2 months ago" type
|
|
|
|
// strings - returns the coarsest level of granularity.
|
|
|
|
func SincePrettyCoarse() func(time.Time) template.HTML {
|
|
|
|
var result = func(text string, v int64) template.HTML {
|
|
|
|
if v == 1 {
|
|
|
|
text = strings.TrimSuffix(text, "s")
|
|
|
|
}
|
|
|
|
return template.HTML(fmt.Sprintf(text, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(since time.Time) template.HTML {
|
|
|
|
var (
|
|
|
|
duration = time.Since(since)
|
|
|
|
)
|
|
|
|
|
|
|
|
if duration.Seconds() < 60.0 {
|
|
|
|
return result("%d seconds", int64(duration.Seconds()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration.Minutes() < 60.0 {
|
|
|
|
return result("%d minutes", int64(duration.Minutes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration.Hours() < 24.0 {
|
|
|
|
return result("%d hours", int64(duration.Hours()))
|
|
|
|
}
|
|
|
|
|
|
|
|
days := int64(duration.Hours() / 24)
|
|
|
|
if days < 30 {
|
|
|
|
return result("%d days", days)
|
|
|
|
}
|
|
|
|
|
|
|
|
months := int64(days / 30)
|
|
|
|
if months < 12 {
|
|
|
|
return result("%d months", months)
|
|
|
|
}
|
|
|
|
|
|
|
|
years := int64(days / 365)
|
|
|
|
return result("%d years", years)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMarkdown renders input text as Markdown.
|
|
|
|
func ToMarkdown(input string) template.HTML {
|
|
|
|
return template.HTML(markdown.Render(input))
|
|
|
|
}
|