Add handling for deleted users in templates

This commit is contained in:
Ben Visness 2021-07-21 21:26:28 -05:00
parent e9ba9b3dde
commit a2eacd6d00
3 changed files with 46 additions and 52 deletions

View File

@ -12,12 +12,6 @@ import (
) )
func PostToTemplate(p *models.Post, author *models.User, currentTheme string) Post { 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{ return Post{
ID: p.ID, ID: p.ID,
@ -26,7 +20,7 @@ func PostToTemplate(p *models.Post, author *models.User, currentTheme string) Po
Preview: p.Preview, Preview: p.Preview,
ReadOnly: p.ReadOnly, 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. // No content. A lot of the time we don't have this handy and don't need it. See AddContentVersion.
PostDate: p.PostDate, PostDate: p.PostDate,
} }
@ -131,7 +125,7 @@ func UserAvatarUrl(u *models.User, currentTheme string) string {
currentTheme = "light" currentTheme = "light"
} }
avatar := "" avatar := ""
if u.Avatar != nil && len(*u.Avatar) > 0 { if u != nil && u.Avatar != nil && len(*u.Avatar) > 0 {
avatar = hmnurl.BuildUserFile(*u.Avatar) avatar = hmnurl.BuildUserFile(*u.Avatar)
} else { } else {
avatar = hmnurl.BuildTheme("empty-avatar.svg", currentTheme, true) 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 { 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 := "" email := ""
if u.ShowEmail { if u.ShowEmail {

View File

@ -9,7 +9,6 @@
{{ range .Posts }} {{ range .Posts }}
<div class="post background-even pa3"> <div class="post background-even pa3">
<div class="fl w-100 w-25-l pt3 pa3-l flex tc-l"> <div class="fl w-100 w-25-l pt3 pa3-l flex tc-l">
{{ if .Author }}
<div class="fl w-20 mw3 dn-l w3"> <div class="fl w-20 mw3 dn-l w3">
<!-- Mobile avatar --> <!-- Mobile avatar -->
<div class="aspect-ratio--1x1 contain bg-center" style="background-image:url('{{ .Author.AvatarUrl }}');"></div> <div class="aspect-ratio--1x1 contain bg-center" style="background-image:url('{{ .Author.AvatarUrl }}');"></div>
@ -52,10 +51,6 @@
{{ end }} {{ end }}
</div> </div>
</div> </div>
{{ else }}
<div class="username">Deleted member</div>
<div class="avatar" style="background-image:url('{{ .Author.AvatarUrl }}');"></div>
{{ end }}
</div> </div>
<div class="fl w-100 w-75-l pv3 pa3-l"> <div class="fl w-100 w-75-l pv3 pa3-l">
<div class="w-100 flex-l flex-row-reverse-l"> <div class="w-100 flex-l flex-row-reverse-l">

View File

@ -77,7 +77,7 @@ type Post struct {
Preview string Preview string
ReadOnly bool ReadOnly bool
Author *User Author User
Content template.HTML Content template.HTML
PostDate time.Time PostDate time.Time