hmn/src/website/base_data.go

133 lines
4.3 KiB
Go
Raw Normal View History

2021-10-28 01:35:53 +00:00
package website
import (
"git.handmade.network/hmn/hmn/src/config"
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/templates"
)
func getBaseDataAutocrumb(c *RequestContext, title string) templates.BaseData {
return getBaseData(c, title, []templates.Breadcrumb{{Name: title, Url: ""}})
}
// NOTE(asaf): If you set breadcrumbs, the breadcrumb for the current project will automatically be prepended when necessary.
// If you pass nil, no breadcrumbs will be created.
func getBaseData(c *RequestContext, title string, breadcrumbs []templates.Breadcrumb) templates.BaseData {
var project models.Project
2021-11-11 19:00:46 +00:00
if c.CurrentProject != nil {
project = *c.CurrentProject
}
2021-10-28 01:35:53 +00:00
var templateUser *templates.User
var templateSession *templates.Session
if c.CurrentUser != nil {
u := templates.UserToTemplate(c.CurrentUser, c.Theme)
s := templates.SessionToTemplate(c.CurrentSession)
templateUser = &u
templateSession = &s
}
notices := getNoticesFromCookie(c)
if len(breadcrumbs) > 0 {
projectUrl := c.UrlContext.BuildHomepage()
2021-10-28 01:35:53 +00:00
if breadcrumbs[0].Url != projectUrl {
rootBreadcrumb := templates.Breadcrumb{
2021-11-11 19:00:46 +00:00
Name: project.Name,
2021-10-28 01:35:53 +00:00
Url: projectUrl,
}
breadcrumbs = append([]templates.Breadcrumb{rootBreadcrumb}, breadcrumbs...)
}
}
baseData := templates.BaseData{
Theme: c.Theme,
Title: title,
Breadcrumbs: breadcrumbs,
CurrentUrl: c.FullUrl(),
CurrentProjectUrl: c.UrlContext.BuildHomepage(),
2021-10-28 01:35:53 +00:00
LoginPageUrl: hmnurl.BuildLoginPage(c.FullUrl()),
2021-11-11 19:00:46 +00:00
ProjectCSSUrl: hmnurl.BuildProjectCSS(project.Color1),
2021-10-28 01:35:53 +00:00
Project: templates.ProjectToTemplate(&project, c.UrlContext.BuildHomepage()),
2021-10-28 01:35:53 +00:00
User: templateUser,
Session: templateSession,
Notices: notices,
2022-06-02 01:49:19 +00:00
ReportIssueEmail: "team@handmade.network",
2021-10-28 01:35:53 +00:00
2022-02-13 20:07:09 +00:00
OpenGraphItems: buildDefaultOpenGraphItems(&project, c.CurrentProjectLogoUrl, title),
2021-10-28 01:35:53 +00:00
2021-11-11 19:00:46 +00:00
IsProjectPage: !project.IsHMN(),
2021-10-28 01:35:53 +00:00
Header: templates.Header{
AdminUrl: hmnurl.BuildAdminApprovalQueue(), // TODO(asaf): Replace with general-purpose admin page
UserSettingsUrl: hmnurl.BuildUserSettings(""),
LoginActionUrl: hmnurl.BuildLoginAction(c.FullUrl()),
LogoutActionUrl: hmnurl.BuildLogoutAction(c.FullUrl()),
ForgotPasswordUrl: hmnurl.BuildRequestPasswordReset(),
RegisterUrl: hmnurl.BuildRegister(),
HMNHomepageUrl: hmnurl.BuildHomepage(),
ProjectIndexUrl: hmnurl.BuildProjectIndex(1),
PodcastUrl: hmnurl.BuildPodcast(),
ForumsUrl: hmnurl.HMNProjectContext.BuildForum(nil, 1),
2021-10-28 01:35:53 +00:00
LibraryUrl: hmnurl.BuildLibrary(),
},
Footer: templates.Footer{
HomepageUrl: hmnurl.BuildHomepage(),
AboutUrl: hmnurl.BuildAbout(),
ManifestoUrl: hmnurl.BuildManifesto(),
CommunicationGuidelinesUrl: hmnurl.BuildCommunicationGuidelines(),
ProjectIndexUrl: hmnurl.BuildProjectIndex(1),
ForumsUrl: hmnurl.HMNProjectContext.BuildForum(nil, 1),
2021-10-28 01:35:53 +00:00
ContactUrl: hmnurl.BuildContactPage(),
2022-02-10 20:27:28 +00:00
SearchActionUrl: "https://duckduckgo.com",
2021-10-28 01:35:53 +00:00
},
}
if c.CurrentUser != nil {
baseData.Header.UserProfileUrl = hmnurl.BuildUserProfile(c.CurrentUser.Username)
}
2021-11-11 19:00:46 +00:00
if !project.IsHMN() {
2021-10-28 01:35:53 +00:00
episodeGuideUrl := ""
2021-11-11 19:00:46 +00:00
defaultTopic, hasAnnotations := config.Config.EpisodeGuide.Projects[project.Slug]
2021-10-28 01:35:53 +00:00
if hasAnnotations {
episodeGuideUrl = c.UrlContext.BuildEpisodeList(defaultTopic)
2021-10-28 01:35:53 +00:00
}
baseData.Header.Project = &templates.ProjectHeader{
2021-11-11 19:00:46 +00:00
HasForums: project.HasForums(),
HasBlog: project.HasBlog(),
2021-10-28 01:35:53 +00:00
HasEpisodeGuide: hasAnnotations,
2021-12-04 14:55:45 +00:00
CanEdit: c.CurrentUserCanEditCurrentProject,
ForumsUrl: c.UrlContext.BuildForum(nil, 1),
BlogUrl: c.UrlContext.BuildBlog(1),
2021-10-28 01:35:53 +00:00
EpisodeGuideUrl: episodeGuideUrl,
2021-12-04 14:55:45 +00:00
EditUrl: c.UrlContext.BuildProjectEdit(""),
2021-10-28 01:35:53 +00:00
}
}
return baseData
}
2022-02-13 20:07:09 +00:00
func buildDefaultOpenGraphItems(project *models.Project, projectLogoUrl string, title string) []templates.OpenGraphItem {
2021-10-28 01:35:53 +00:00
if title == "" {
title = "Handmade Network"
}
image := hmnurl.BuildPublic("logo.png", false)
if !project.IsHMN() {
2022-02-13 20:07:09 +00:00
image = projectLogoUrl
2021-10-28 01:35:53 +00:00
}
return []templates.OpenGraphItem{
{Property: "og:title", Value: title},
{Property: "og:site_name", Value: "Handmade Network"},
{Property: "og:type", Value: "website"},
{Property: "og:image", Value: image},
}
}