From d2ec8b8e1f58a8d39b31b3f43e20335c35540220 Mon Sep 17 00:00:00 2001 From: Adrian-Stefan Mares Date: Mon, 26 Feb 2024 16:35:57 +0100 Subject: [PATCH] all: Add relative duration and time email template functions --- pkg/email/funcs.go | 17 +++++++++++++---- pkg/email/templates/invitation.html.tmpl | 2 +- pkg/email/templates/invitation.txt.tmpl | 2 +- pkg/email/templates/login_token.html.tmpl | 2 +- pkg/email/templates/login_token.txt.tmpl | 2 +- .../templates/temporary_password.html.tmpl | 2 +- pkg/email/templates/temporary_password.txt.tmpl | 2 +- pkg/email/templates/validate.html.tmpl | 2 +- pkg/email/templates/validate.txt.tmpl | 2 +- 9 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/email/funcs.go b/pkg/email/funcs.go index 21365bd31f..c36bc8d17f 100644 --- a/pkg/email/funcs.go +++ b/pkg/email/funcs.go @@ -33,8 +33,9 @@ var defaultFuncs = template.FuncMap{ p := path.Join(elems...) return documentationBaseURL + "/" + strings.TrimPrefix(p, "/") }, - "relTime": relTime, - "enumDesc": enumDesc, + "relDuration": relDuration, + "relTime": relTime, + "enumDesc": enumDesc, } const ( @@ -44,9 +45,17 @@ const ( year = 12 * month ) -func relTime(d time.Duration) string { +func relDuration(d time.Duration) string { now := time.Now() - return humanize.CustomRelTime(now.Add(d), now, "ago", "from now", []humanize.RelTimeMagnitude{ + return relativeTime(now.Add(d), now) +} + +func relTime(t time.Time) string { + return relativeTime(t, time.Now()) +} + +func relativeTime(a, b time.Time) string { + return humanize.CustomRelTime(a, b, "ago", "from now", []humanize.RelTimeMagnitude{ {D: time.Second, Format: "now", DivBy: time.Second}, {D: 2 * time.Second, Format: "a second %s", DivBy: 1}, diff --git a/pkg/email/templates/invitation.html.tmpl b/pkg/email/templates/invitation.html.tmpl index 744f744fc3..92528354ed 100644 --- a/pkg/email/templates/invitation.html.tmpl +++ b/pkg/email/templates/invitation.html.tmpl @@ -19,7 +19,7 @@ Alternatively, you can use the invitation token {{ .InvitationToken }} {{- with .TTL }}

-Your invitation expires {{ relTime . }}, so register before then. +Your invitation expires {{ relDuration . }}, so register before then.

{{- end }}

diff --git a/pkg/email/templates/invitation.txt.tmpl b/pkg/email/templates/invitation.txt.tmpl index f1f9f0d174..3db9468fec 100644 --- a/pkg/email/templates/invitation.txt.tmpl +++ b/pkg/email/templates/invitation.txt.tmpl @@ -6,7 +6,7 @@ You can now go to {{ .Network.IdentityServerURL }}/register?invitation_token={{ {{- with .TTL }} -Your invitation expires {{ relTime . }}, so register before then. +Your invitation expires {{ relDuration . }}, so register before then. {{- end }} After successful registration, you can go to {{ .Network.ConsoleURL }} to start adding devices and gateways. diff --git a/pkg/email/templates/login_token.html.tmpl b/pkg/email/templates/login_token.html.tmpl index fb6494b652..05c993b899 100644 --- a/pkg/email/templates/login_token.html.tmpl +++ b/pkg/email/templates/login_token.html.tmpl @@ -19,7 +19,7 @@ Alternatively, you can use the login token {{ .LoginToken }} direct

{{- with .TTL }}

-Your login token expires {{ relTime . }}, so if you don't log in before then, you'll have to request a new token if you still want to log in. +Your login token expires {{ relDuration . }}, so if you don't log in before then, you'll have to request a new token if you still want to log in.

{{- end }} {{- end -}} diff --git a/pkg/email/templates/login_token.txt.tmpl b/pkg/email/templates/login_token.txt.tmpl index f6c09167f5..5adc5d8606 100644 --- a/pkg/email/templates/login_token.txt.tmpl +++ b/pkg/email/templates/login_token.txt.tmpl @@ -7,5 +7,5 @@ Alternatively, you can use the login token "{{ .LoginToken }}" directly. {{- with .TTL }} -Your login token expires {{ relTime . }}, so if you don't log in before then, you'll have to request a new token if you still want to log in. +Your login token expires {{ relDuration . }}, so if you don't log in before then, you'll have to request a new token if you still want to log in. {{- end }} diff --git a/pkg/email/templates/temporary_password.html.tmpl b/pkg/email/templates/temporary_password.html.tmpl index b8e7100580..ab268e8384 100644 --- a/pkg/email/templates/temporary_password.html.tmpl +++ b/pkg/email/templates/temporary_password.html.tmpl @@ -20,7 +20,7 @@ Please note that this temporary password can only be used to change your passwor

{{- with .TTL }}

-Your temporary password expires {{ relTime . }}, so if you don't change your password before then, you'll have to request a new temporary password if you still want to change your password. +Your temporary password expires {{ relDuration . }}, so if you don't change your password before then, you'll have to request a new temporary password if you still want to change your password.

{{- end }} {{- end -}} diff --git a/pkg/email/templates/temporary_password.txt.tmpl b/pkg/email/templates/temporary_password.txt.tmpl index 61b235e7fe..916d576362 100644 --- a/pkg/email/templates/temporary_password.txt.tmpl +++ b/pkg/email/templates/temporary_password.txt.tmpl @@ -8,5 +8,5 @@ Please note that this temporary password can only be used to change your passwor {{- with .TTL }} -Your temporary password expires {{ relTime . }}, so if you don't change your password before then, you'll have to request a new temporary password if you still want to change your password. +Your temporary password expires {{ relDuration . }}, so if you don't change your password before then, you'll have to request a new temporary password if you still want to change your password. {{- end }} diff --git a/pkg/email/templates/validate.html.tmpl b/pkg/email/templates/validate.html.tmpl index 4c2832de3e..4b535446d8 100644 --- a/pkg/email/templates/validate.html.tmpl +++ b/pkg/email/templates/validate.html.tmpl @@ -22,7 +22,7 @@ Alternatively, you can use the reference {{ .ID }} and confirmation

{{- with .TTL }}

-The confirmation token expires {{ relTime . }}, so if you don't confirm your email address before then, you'll have to request a new confirmation if you still want to confirm your email address. +The confirmation token expires {{ relDuration . }}, so if you don't confirm your email address before then, you'll have to request a new confirmation if you still want to confirm your email address.

{{- end }} diff --git a/pkg/email/templates/validate.txt.tmpl b/pkg/email/templates/validate.txt.tmpl index 2f725d966c..83c270d1b2 100644 --- a/pkg/email/templates/validate.txt.tmpl +++ b/pkg/email/templates/validate.txt.tmpl @@ -9,5 +9,5 @@ Alternatively, you can use the reference "{{ .ID }}" and confirmation token "{{ {{- with .TTL }} -The confirmation token expires {{ relTime . }}, so if you don't confirm your email address before then, you'll have to request a new confirmation if you still want to confirm your email address. +The confirmation token expires {{ relDuration . }}, so if you don't confirm your email address before then, you'll have to request a new confirmation if you still want to confirm your email address. {{- end }}