From deba75cad14f47bb42ea55e7d126d3adea7855a6 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Sun, 2 Oct 2022 15:54:42 +0200 Subject: [chore] Use shorter timestamps in frontend for replies (#875) * rename timestampShort -> timestampVague * add ParseISO8601 * start fiddling with timestamp * pad/margin a bit more consistently * remove visibilty icon, change timestamp use * update timestamp logic * check + log errors * properly cut-off long display- and usernames Co-authored-by: f0x --- internal/router/template.go | 69 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 13 deletions(-) (limited to 'internal/router') diff --git a/internal/router/template.go b/internal/router/template.go index 2d6026a2e..fcdccf783 100644 --- a/internal/router/template.go +++ b/internal/router/template.go @@ -30,7 +30,18 @@ import ( "github.com/gin-gonic/gin" "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/regexes" + "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 @@ -76,13 +87,44 @@ func noescapeAttr(str string) template.HTMLAttr { } func timestamp(stamp string) string { - t, _ := time.Parse(time.RFC3339, stamp) - return t.Format("January 2, 2006, 15:04:05") + t, err := util.ParseISO8601(stamp) + if err != nil { + log.Errorf("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 timestampShort(stamp string) string { - t, _ := time.Parse(time.RFC3339, stamp) - return t.Format("January, 2006") +func timestampPrecise(stamp string) string { + t, err := util.ParseISO8601(stamp) + if err != nil { + log.Errorf("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("error parsing timestamp %s: %s", stamp, err) + return badTimestamp + } + return t.Format(monthYear) } type iconWithLabel struct { @@ -154,13 +196,14 @@ func emojify(emojis []model.Emoji, text template.HTML) template.HTML { func LoadTemplateFunctions(engine *gin.Engine) { engine.SetFuncMap(template.FuncMap{ - "escape": escape, - "noescape": noescape, - "noescapeAttr": noescapeAttr, - "oddOrEven": oddOrEven, - "visibilityIcon": visibilityIcon, - "timestamp": timestamp, - "timestampShort": timestampShort, - "emojify": emojify, + "escape": escape, + "noescape": noescape, + "noescapeAttr": noescapeAttr, + "oddOrEven": oddOrEven, + "visibilityIcon": visibilityIcon, + "timestamp": timestamp, + "timestampVague": timestampVague, + "timestampPrecise": timestampPrecise, + "emojify": emojify, }) } -- cgit v1.2.3