hmn/src/website/timeline_helper.go

137 lines
4.5 KiB
Go
Raw Normal View History

2021-06-22 09:50:40 +00:00
package website
import (
"html/template"
"regexp"
"strings"
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/templates"
)
var TimelineTypeMap = map[models.ThreadType][]templates.TimelineType{
// { First post , Subsequent post }
models.ThreadTypeProjectBlogPost: {templates.TimelineTypeBlogPost, templates.TimelineTypeBlogComment},
models.ThreadTypeForumPost: {templates.TimelineTypeForumThread, templates.TimelineTypeForumReply},
2021-06-22 09:50:40 +00:00
}
var TimelineItemClassMap = map[templates.TimelineType]string{
templates.TimelineTypeUnknown: "",
templates.TimelineTypeForumThread: "forums",
templates.TimelineTypeForumReply: "forums",
templates.TimelineTypeBlogPost: "blogs",
templates.TimelineTypeBlogComment: "blogs",
templates.TimelineTypeSnippetImage: "snippets",
templates.TimelineTypeSnippetVideo: "snippets",
templates.TimelineTypeSnippetAudio: "snippets",
templates.TimelineTypeSnippetYoutube: "snippets",
}
var TimelineTypeTitleMap = map[templates.TimelineType]string{
templates.TimelineTypeUnknown: "",
templates.TimelineTypeForumThread: "New forum thread",
2021-06-22 09:50:40 +00:00
templates.TimelineTypeForumReply: "Forum reply",
templates.TimelineTypeBlogPost: "New blog post",
templates.TimelineTypeBlogComment: "Blog comment",
templates.TimelineTypeSnippetImage: "Snippet",
templates.TimelineTypeSnippetVideo: "Snippet",
templates.TimelineTypeSnippetAudio: "Snippet",
templates.TimelineTypeSnippetYoutube: "Snippet",
}
func PostToTimelineItem(lineageBuilder *models.SubforumLineageBuilder, post *models.Post, thread *models.Thread, project *models.Project, owner *models.User, currentTheme string) templates.TimelineItem {
2021-06-22 09:50:40 +00:00
itemType := templates.TimelineTypeUnknown
typeByCatKind, found := TimelineTypeMap[post.ThreadType]
2021-06-22 09:50:40 +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-06-22 09:50:40 +00:00
}
2021-07-30 19:59:48 +00:00
itemType = typeByCatKind[isNotFirst]
2021-06-22 09:50:40 +00:00
}
return templates.TimelineItem{
Type: itemType,
TypeTitle: TimelineTypeTitleMap[itemType],
Class: TimelineItemClassMap[itemType],
Date: post.PostDate,
Url: UrlForGenericPost(thread, post, lineageBuilder, project.Slug),
2021-06-22 09:50:40 +00:00
OwnerAvatarUrl: templates.UserAvatarUrl(owner, currentTheme),
2021-08-17 05:18:04 +00:00
OwnerName: owner.BestName(),
2021-06-22 09:50:40 +00:00
OwnerUrl: hmnurl.BuildUserProfile(owner.Username),
Description: "", // NOTE(asaf): No description for posts
Title: thread.Title,
2021-09-01 18:25:09 +00:00
Breadcrumbs: GenericThreadBreadcrumbs(lineageBuilder, project, thread),
2021-06-22 09:50:40 +00:00
}
}
var YoutubeRegex = regexp.MustCompile(`(?i)youtube\.com/watch\?.*v=(?P<videoid>[^/&]+)`)
var YoutubeShortRegex = regexp.MustCompile(`(?i)youtu\.be/(?P<videoid>[^/]+)`)
func SnippetToTimelineItem(snippet *models.Snippet, asset *models.Asset, discordMessage *models.DiscordMessage, owner *models.User, currentTheme string) templates.TimelineItem {
itemType := templates.TimelineTypeUnknown
youtubeId := ""
assetUrl := ""
2021-06-23 20:13:22 +00:00
mimeType := ""
2021-06-22 09:50:40 +00:00
width := 0
height := 0
discordMessageUrl := ""
if asset == nil {
match := YoutubeRegex.FindStringSubmatch(*snippet.Url)
index := YoutubeRegex.SubexpIndex("videoid")
if match == nil {
match = YoutubeShortRegex.FindStringSubmatch(*snippet.Url)
index = YoutubeShortRegex.SubexpIndex("videoid")
}
if match != nil {
youtubeId = match[index]
itemType = templates.TimelineTypeSnippetYoutube
}
} else {
if strings.HasPrefix(asset.MimeType, "image/") {
itemType = templates.TimelineTypeSnippetImage
} else if strings.HasPrefix(asset.MimeType, "video/") {
itemType = templates.TimelineTypeSnippetVideo
} else if strings.HasPrefix(asset.MimeType, "audio/") {
itemType = templates.TimelineTypeSnippetAudio
}
assetUrl = hmnurl.BuildS3Asset(asset.S3Key)
2021-06-23 20:13:22 +00:00
mimeType = asset.MimeType
2021-06-22 09:50:40 +00:00
width = asset.Width
height = asset.Height
}
if discordMessage != nil {
discordMessageUrl = discordMessage.Url
}
return templates.TimelineItem{
Type: itemType,
Class: TimelineItemClassMap[itemType],
Date: snippet.When,
Url: hmnurl.BuildSnippet(snippet.ID),
OwnerAvatarUrl: templates.UserAvatarUrl(owner, currentTheme),
2021-08-17 05:18:04 +00:00
OwnerName: owner.BestName(),
2021-06-22 09:50:40 +00:00
OwnerUrl: hmnurl.BuildUserProfile(owner.Username),
Description: template.HTML(snippet.DescriptionHtml),
DiscordMessageUrl: discordMessageUrl,
Width: width,
Height: height,
AssetUrl: assetUrl,
2021-06-23 20:13:22 +00:00
MimeType: mimeType,
2021-06-22 09:50:40 +00:00
YoutubeID: youtubeId,
}
}