hmn/src/templates/mapping.go

85 lines
1.8 KiB
Go
Raw Normal View History

package templates
2021-04-22 23:02:50 +00:00
import (
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models"
)
2021-04-23 04:07:44 +00:00
func PostToTemplate(p *models.Post, author models.User) Post {
return Post{
2021-04-23 04:07:44 +00:00
Author: UserToTemplate(&author),
Preview: p.Preview,
ReadOnly: p.ReadOnly,
2021-04-23 04:07:44 +00:00
// No content. Do it yourself if you care.
IP: p.IP.String(),
}
}
2021-04-23 04:07:44 +00:00
func PostToTemplateWithContent(p *models.Post, author models.User, content string) Post {
post := PostToTemplate(p, author)
post.Content = content
return post
}
func ProjectToTemplate(p *models.Project) Project {
return Project{
Name: maybeString(p.Name),
2021-04-22 23:02:50 +00:00
Subdomain: p.Subdomain(),
Color1: p.Color1,
Color2: p.Color2,
IsHMN: p.IsHMN(),
HasBlog: true, // TODO: Check flag sets or whatever
HasForum: true,
HasWiki: true,
HasLibrary: true,
}
}
func UserToTemplate(u *models.User) User {
2021-04-25 19:33:22 +00:00
// TODO: Handle deleted users. Maybe not here, but if not, at call sites of this function.
2021-04-22 23:02:50 +00:00
avatar := ""
if u.Avatar != nil {
avatar = hmnurl.StaticUrl(*u.Avatar, nil)
}
name := u.Name
if u.Name == "" {
name = u.Username
}
return User{
Username: u.Username,
Email: u.Email,
IsSuperuser: u.IsSuperuser,
IsStaff: u.IsStaff,
2021-04-17 00:01:13 +00:00
2021-04-22 23:02:50 +00:00
Name: name,
Blurb: u.Blurb,
Signature: u.Signature,
AvatarUrl: avatar, // TODO
ProfileUrl: hmnurl.Url("m/"+u.Username, nil),
2021-04-17 00:01:13 +00:00
DarkTheme: u.DarkTheme,
Timezone: u.Timezone,
ProfileColor1: u.ProfileColor1,
ProfileColor2: u.ProfileColor2,
CanEditLibrary: u.CanEditLibrary,
DiscordSaveShowcase: u.DiscordSaveShowcase,
DiscordDeleteSnippetOnMessageDelete: u.DiscordDeleteSnippetOnMessageDelete,
}
}
func maybeString(s *string) string {
if s == nil {
return ""
}
return *s
}