Skip to content

Commit

Permalink
Merge pull request #6963 from TheThingsNetwork/backport/email-changes
Browse files Browse the repository at this point in the history
Add relative duration and time email template functions
  • Loading branch information
adriansmares authored Mar 1, 2024
2 parents 8449d7c + d2ec8b8 commit 03ab175
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 12 deletions.
17 changes: 13 additions & 4 deletions pkg/email/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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},

Expand Down
2 changes: 1 addition & 1 deletion pkg/email/templates/invitation.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Alternatively, you can use the invitation token <code>{{ .InvitationToken }}</co
</p>
{{- with .TTL }}
<p>
Your invitation expires {{ relTime . }}, so register before then.
Your invitation expires {{ relDuration . }}, so register before then.
</p>
{{- end }}
<p>
Expand Down
2 changes: 1 addition & 1 deletion pkg/email/templates/invitation.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/email/templates/login_token.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Alternatively, you can use the login token <code>{{ .LoginToken }}</code> direct
</p>
{{- with .TTL }}
<p>
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.
</p>
{{- end }}
{{- end -}}
2 changes: 1 addition & 1 deletion pkg/email/templates/login_token.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion pkg/email/templates/temporary_password.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please note that this temporary password can only be used to change your passwor
</p>
{{- with .TTL }}
<p>
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.
</p>
{{- end }}
{{- end -}}
2 changes: 1 addition & 1 deletion pkg/email/templates/temporary_password.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion pkg/email/templates/validate.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Alternatively, you can use the reference <code>{{ .ID }}</code> and confirmation
</p>
{{- with .TTL }}
<p>
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.
</p>
{{- end }}

Expand Down
2 changes: 1 addition & 1 deletion pkg/email/templates/validate.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit 03ab175

Please sign in to comment.