hmn/src/website/post_helper.go

119 lines
3.9 KiB
Go
Raw Normal View History

2021-05-11 22:53:23 +00:00
package website
import (
"git.handmade.network/hmn/hmn/src/hmndata"
2021-05-11 22:53:23 +00:00
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/templates"
)
2021-09-09 00:43:24 +00:00
// NOTE(asaf): Please don't use these if you already know the kind of the thread beforehand. Just call the appropriate build function.
func UrlForGenericThread(urlContext *hmnurl.UrlContext, thread *models.Thread, lineageBuilder *models.SubforumLineageBuilder) string {
2021-09-09 00:43:24 +00:00
switch thread.Type {
case models.ThreadTypeProjectBlogPost:
return urlContext.BuildBlogThread(thread.ID, thread.Title)
2021-09-09 00:43:24 +00:00
case models.ThreadTypeForumPost:
return urlContext.BuildForumThread(lineageBuilder.GetSubforumLineageSlugs(*thread.SubforumID), thread.ID, thread.Title, 1)
2021-09-09 00:43:24 +00:00
}
return urlContext.BuildHomepage()
2021-09-09 00:43:24 +00:00
}
func UrlForGenericPost(urlContext *hmnurl.UrlContext, thread *models.Thread, post *models.Post, lineageBuilder *models.SubforumLineageBuilder) string {
switch post.ThreadType {
case models.ThreadTypeProjectBlogPost:
return urlContext.BuildBlogThreadWithPostHash(post.ThreadID, thread.Title, post.ID)
case models.ThreadTypeForumPost:
return urlContext.BuildForumPost(lineageBuilder.GetSubforumLineageSlugs(*thread.SubforumID), post.ThreadID, post.ID)
2021-05-11 22:53:23 +00:00
}
return urlContext.BuildHomepage()
2021-05-11 22:53:23 +00:00
}
var PostTypeMap = map[models.ThreadType][]templates.PostType{
// { First post , Subsequent post }
models.ThreadTypeProjectBlogPost: {templates.PostTypeBlogPost, templates.PostTypeBlogComment},
models.ThreadTypeForumPost: {templates.PostTypeForumThread, templates.PostTypeForumReply},
}
var PostTypePrefix = map[templates.PostType]string{
templates.PostTypeBlogPost: "New blog post",
templates.PostTypeBlogComment: "Blog comment",
templates.PostTypeForumThread: "New forum thread",
templates.PostTypeForumReply: "Forum reply",
}
2021-09-01 18:25:09 +00:00
var ThreadTypeDisplayNames = map[models.ThreadType]string{
models.ThreadTypeProjectBlogPost: "Blog",
models.ThreadTypeForumPost: "Forums",
}
func GenericThreadBreadcrumbs(urlContext *hmnurl.UrlContext, lineageBuilder *models.SubforumLineageBuilder, thread *models.Thread) []templates.Breadcrumb {
2021-06-22 09:50:40 +00:00
var result []templates.Breadcrumb
2021-09-01 18:25:09 +00:00
if thread.Type == models.ThreadTypeForumPost {
result = SubforumBreadcrumbs(urlContext, lineageBuilder, *thread.SubforumID)
2021-09-01 18:25:09 +00:00
} else {
result = []templates.Breadcrumb{
{
Name: urlContext.ProjectName,
Url: urlContext.BuildHomepage(),
2021-09-01 18:25:09 +00:00
},
{
Name: ThreadTypeDisplayNames[thread.Type],
Url: BuildProjectRootResourceUrl(urlContext, thread.Type),
2021-09-01 18:25:09 +00:00
},
2021-06-22 09:50:40 +00:00
}
}
return result
}
func BuildProjectRootResourceUrl(urlContext *hmnurl.UrlContext, kind models.ThreadType) string {
2021-09-01 18:25:09 +00:00
switch kind {
case models.ThreadTypeProjectBlogPost:
return urlContext.BuildBlog(1)
2021-09-01 18:25:09 +00:00
case models.ThreadTypeForumPost:
return urlContext.BuildForum(nil, 1)
2021-09-01 18:25:09 +00:00
}
return urlContext.BuildHomepage()
2021-09-01 18:25:09 +00:00
}
func MakePostListItem(
lineageBuilder *models.SubforumLineageBuilder,
project *models.Project,
thread *models.Thread,
post *models.Post,
user *models.User,
unread bool,
includeBreadcrumbs bool,
currentTheme string,
) templates.PostListItem {
2021-05-11 22:53:23 +00:00
var result templates.PostListItem
urlContext := hmndata.UrlContextForProject(project)
2021-05-11 22:53:23 +00:00
result.Title = thread.Title
result.User = templates.UserToTemplate(user, currentTheme)
2021-05-11 22:53:23 +00:00
result.Date = post.PostDate
result.Unread = unread
result.Url = UrlForGenericPost(urlContext, thread, post, lineageBuilder)
result.Preview = post.Preview
postType := templates.PostTypeUnknown
postTypeOptions, found := PostTypeMap[post.ThreadType]
if found {
2021-07-30 19:59:48 +00:00
isNotFirst := 0
2021-07-30 20:01:40 +00:00
if thread.FirstID != post.ID {
2021-07-30 19:59:48 +00:00
isNotFirst = 1
}
2021-07-30 19:59:48 +00:00
postType = postTypeOptions[isNotFirst]
}
result.PostType = postType
result.PostTypePrefix = PostTypePrefix[result.PostType]
2021-05-11 22:53:23 +00:00
if includeBreadcrumbs {
result.Breadcrumbs = GenericThreadBreadcrumbs(urlContext, lineageBuilder, thread)
2021-05-11 22:53:23 +00:00
}
return result
}