From a2eacd6d002ef0b561807473424446f4bbfa42bf Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 21 Jul 2021 21:26:28 -0500 Subject: [PATCH] Add handling for deleted users in templates --- src/templates/mapping.go | 17 +++---- src/templates/src/forum_thread.html | 79 ++++++++++++++--------------- src/templates/types.go | 2 +- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/templates/mapping.go b/src/templates/mapping.go index 289f62ff..78eaf9db 100644 --- a/src/templates/mapping.go +++ b/src/templates/mapping.go @@ -12,12 +12,6 @@ import ( ) func PostToTemplate(p *models.Post, author *models.User, currentTheme string) Post { - var authorUser *User - if author != nil { - authorTmpl := UserToTemplate(author, currentTheme) - authorUser = &authorTmpl - } - return Post{ ID: p.ID, @@ -26,7 +20,7 @@ func PostToTemplate(p *models.Post, author *models.User, currentTheme string) Po Preview: p.Preview, ReadOnly: p.ReadOnly, - Author: authorUser, + Author: UserToTemplate(author, currentTheme), // No content. A lot of the time we don't have this handy and don't need it. See AddContentVersion. PostDate: p.PostDate, } @@ -131,7 +125,7 @@ func UserAvatarUrl(u *models.User, currentTheme string) string { currentTheme = "light" } avatar := "" - if u.Avatar != nil && len(*u.Avatar) > 0 { + if u != nil && u.Avatar != nil && len(*u.Avatar) > 0 { avatar = hmnurl.BuildUserFile(*u.Avatar) } else { avatar = hmnurl.BuildTheme("empty-avatar.svg", currentTheme, true) @@ -148,7 +142,12 @@ func UserDisplayName(u *models.User) string { } func UserToTemplate(u *models.User, currentTheme string) User { - // TODO: Handle deleted users. Maybe not here, but if not, at call sites of this function. + if u == nil { + return User{ + Name: "Deleted user", + AvatarUrl: UserAvatarUrl(u, currentTheme), + } + } email := "" if u.ShowEmail { diff --git a/src/templates/src/forum_thread.html b/src/templates/src/forum_thread.html index 6364febc..d270e5ce 100644 --- a/src/templates/src/forum_thread.html +++ b/src/templates/src/forum_thread.html @@ -9,53 +9,48 @@ {{ range .Posts }}
- {{ if .Author }} -
- -
-
-
-
- {{ .Author.Username }} {{/* TODO: Text scale stuff? Seems unnecessary. */}} - -
- {{ if .Author.IsStaff }} -
- {{ end }} -
-
- {{ if and .Author.Name (ne .Author.Name .Author.Username) }} -
{{ .Author.Name }}
- {{ end }} - -
-
-
- {{/* TODO: Aggregate user data -
- {{ post.author.posts }} posts - {% if post.author.public_projects.values|length > 0 %} - / {{ post.author.public_projects.values|length }} project{%if post.author.public_projects.values|length > 1 %}s{% endif %} - {% endif %} -
*/}} - -
+
+ +
+
+
+
+ {{ .Author.Username }} {{/* TODO: Text scale stuff? Seems unnecessary. */}} + +
{{ if .Author.IsStaff }}
{{ end }}
-
- {{ if .Author.Blurb }} - {{ .Author.Blurb }} {{/* TODO: Linebreaks? */}} - {{ else if .Author.Bio }} - {{ .Author.Bio }} - {{ end }} -
- {{ else }} -
Deleted member
-
- {{ end }} + {{ if and .Author.Name (ne .Author.Name .Author.Username) }} +
{{ .Author.Name }}
+ {{ end }} + +
+
+
+ {{/* TODO: Aggregate user data +
+ {{ post.author.posts }} posts + {% if post.author.public_projects.values|length > 0 %} + / {{ post.author.public_projects.values|length }} project{%if post.author.public_projects.values|length > 1 %}s{% endif %} + {% endif %} +
*/}} + +
+ {{ if .Author.IsStaff }} +
+ {{ end }} +
+
+ {{ if .Author.Blurb }} + {{ .Author.Blurb }} {{/* TODO: Linebreaks? */}} + {{ else if .Author.Bio }} + {{ .Author.Bio }} + {{ end }} +
+
diff --git a/src/templates/types.go b/src/templates/types.go index 968c3ae7..626b6142 100644 --- a/src/templates/types.go +++ b/src/templates/types.go @@ -77,7 +77,7 @@ type Post struct { Preview string ReadOnly bool - Author *User + Author User Content template.HTML PostDate time.Time