2021-05-11 22:53:23 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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(thread *models.Thread, lineageBuilder *models.SubforumLineageBuilder, projectSlug string) string {
|
|
|
|
switch thread.Type {
|
|
|
|
case models.ThreadTypeProjectBlogPost:
|
|
|
|
return hmnurl.BuildBlogThread(projectSlug, thread.ID, thread.Title)
|
|
|
|
case models.ThreadTypeForumPost:
|
|
|
|
return hmnurl.BuildForumThread(projectSlug, lineageBuilder.GetSubforumLineageSlugs(*thread.SubforumID), thread.ID, thread.Title, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hmnurl.BuildProjectHomepage(projectSlug)
|
|
|
|
}
|
|
|
|
|
2021-07-30 03:40:47 +00:00
|
|
|
func UrlForGenericPost(thread *models.Thread, post *models.Post, lineageBuilder *models.SubforumLineageBuilder, projectSlug string) string {
|
|
|
|
switch post.ThreadType {
|
2021-07-30 23:08:42 +00:00
|
|
|
case models.ThreadTypeProjectBlogPost:
|
2021-07-30 22:32:19 +00:00
|
|
|
return hmnurl.BuildBlogThreadWithPostHash(projectSlug, post.ThreadID, thread.Title, post.ID)
|
2021-07-30 03:40:47 +00:00
|
|
|
case models.ThreadTypeForumPost:
|
|
|
|
return hmnurl.BuildForumPost(projectSlug, lineageBuilder.GetSubforumLineageSlugs(*thread.SubforumID), post.ThreadID, post.ID)
|
2021-05-11 22:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hmnurl.BuildProjectHomepage(projectSlug)
|
|
|
|
}
|
|
|
|
|
2021-07-30 03:40:47 +00:00
|
|
|
var PostTypeMap = map[models.ThreadType][]templates.PostType{
|
2021-07-30 23:08:42 +00:00
|
|
|
// { First post , Subsequent post }
|
|
|
|
models.ThreadTypeProjectBlogPost: {templates.PostTypeBlogPost, templates.PostTypeBlogComment},
|
|
|
|
models.ThreadTypeForumPost: {templates.PostTypeForumThread, templates.PostTypeForumReply},
|
2021-05-30 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var PostTypePrefix = map[templates.PostType]string{
|
2021-07-30 03:40:47 +00:00
|
|
|
templates.PostTypeBlogPost: "New blog post",
|
|
|
|
templates.PostTypeBlogComment: "Blog comment",
|
|
|
|
templates.PostTypeForumThread: "New forum thread",
|
|
|
|
templates.PostTypeForumReply: "Forum reply",
|
2021-05-30 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 18:25:09 +00:00
|
|
|
var ThreadTypeDisplayNames = map[models.ThreadType]string{
|
|
|
|
models.ThreadTypeProjectBlogPost: "Blog",
|
|
|
|
models.ThreadTypeForumPost: "Forums",
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenericThreadBreadcrumbs(lineageBuilder *models.SubforumLineageBuilder, project *models.Project, 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(lineageBuilder, project, *thread.SubforumID)
|
|
|
|
} else {
|
|
|
|
result = []templates.Breadcrumb{
|
|
|
|
{
|
|
|
|
Name: project.Name,
|
|
|
|
Url: hmnurl.BuildProjectHomepage(project.Slug),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: ThreadTypeDisplayNames[thread.Type],
|
|
|
|
Url: BuildProjectRootResourceUrl(project.Slug, thread.Type),
|
|
|
|
},
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-09-01 18:25:09 +00:00
|
|
|
func BuildProjectRootResourceUrl(projectSlug string, kind models.ThreadType) string {
|
|
|
|
switch kind {
|
|
|
|
case models.ThreadTypeProjectBlogPost:
|
|
|
|
return hmnurl.BuildBlog(projectSlug, 1)
|
|
|
|
case models.ThreadTypeForumPost:
|
|
|
|
return hmnurl.BuildForum(projectSlug, nil, 1)
|
|
|
|
}
|
|
|
|
return hmnurl.BuildProjectHomepage(projectSlug)
|
|
|
|
}
|
|
|
|
|
2021-07-30 03:40:47 +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
|
|
|
|
|
|
|
|
result.Title = thread.Title
|
2021-05-25 13:12:20 +00:00
|
|
|
result.User = templates.UserToTemplate(user, currentTheme)
|
2021-05-11 22:53:23 +00:00
|
|
|
result.Date = post.PostDate
|
|
|
|
result.Unread = unread
|
2021-07-30 03:40:47 +00:00
|
|
|
result.Url = UrlForGenericPost(thread, post, lineageBuilder, project.Slug)
|
2021-05-30 18:35:01 +00:00
|
|
|
result.Preview = post.Preview
|
|
|
|
|
|
|
|
postType := templates.PostTypeUnknown
|
2021-07-30 03:40:47 +00:00
|
|
|
postTypeOptions, found := PostTypeMap[post.ThreadType]
|
2021-05-30 18:35:01 +00:00
|
|
|
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-05-30 18:35:01 +00:00
|
|
|
}
|
2021-07-30 19:59:48 +00:00
|
|
|
postType = postTypeOptions[isNotFirst]
|
2021-05-30 18:35:01 +00:00
|
|
|
}
|
|
|
|
result.PostType = postType
|
|
|
|
result.PostTypePrefix = PostTypePrefix[result.PostType]
|
2021-05-11 22:53:23 +00:00
|
|
|
|
|
|
|
if includeBreadcrumbs {
|
2021-09-01 18:25:09 +00:00
|
|
|
result.Breadcrumbs = GenericThreadBreadcrumbs(lineageBuilder, project, thread)
|
2021-05-11 22:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|