2021-06-22 09:50:40 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
2021-10-23 22:28:06 +00:00
|
|
|
"fmt"
|
2021-06-22 09:50:40 +00:00
|
|
|
"html/template"
|
|
|
|
"regexp"
|
2021-11-11 19:00:46 +00:00
|
|
|
"sort"
|
2021-06-22 09:50:40 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/hmnurl"
|
2021-10-23 22:28:06 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/logging"
|
2021-06-22 09:50:40 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
|
|
|
"git.handmade.network/hmn/hmn/src/templates"
|
|
|
|
)
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
type TimelineTypeTitles struct {
|
|
|
|
TypeTitleFirst string
|
|
|
|
TypeTitleNotFirst string
|
|
|
|
FilterTitle string
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
var TimelineTypeTitleMap = map[models.ThreadType]TimelineTypeTitles{
|
|
|
|
models.ThreadTypeProjectBlogPost: {"New blog post", "Blog comment", "Blogs"},
|
|
|
|
models.ThreadTypeForumPost: {"New forum thread", "Forum reply", "Forums"},
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
func PostToTimelineItem(
|
2021-11-10 04:11:39 +00:00
|
|
|
urlContext *hmnurl.UrlContext,
|
2021-10-23 22:28:06 +00:00
|
|
|
lineageBuilder *models.SubforumLineageBuilder,
|
|
|
|
post *models.Post,
|
|
|
|
thread *models.Thread,
|
|
|
|
owner *models.User,
|
|
|
|
currentTheme string,
|
|
|
|
) templates.TimelineItem {
|
|
|
|
item := templates.TimelineItem{
|
|
|
|
Date: post.PostDate,
|
|
|
|
Title: thread.Title,
|
2021-11-10 04:11:39 +00:00
|
|
|
Breadcrumbs: GenericThreadBreadcrumbs(urlContext, lineageBuilder, thread),
|
|
|
|
Url: UrlForGenericPost(urlContext, thread, post, lineageBuilder),
|
2021-06-22 09:50:40 +00:00
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
OwnerAvatarUrl: templates.UserAvatarUrl(owner, currentTheme),
|
|
|
|
OwnerName: owner.BestName(),
|
|
|
|
OwnerUrl: hmnurl.BuildUserProfile(owner.Username),
|
|
|
|
}
|
2021-06-22 09:50:40 +00:00
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
if typeTitles, ok := TimelineTypeTitleMap[post.ThreadType]; ok {
|
|
|
|
if thread.FirstID == post.ID {
|
|
|
|
item.TypeTitle = typeTitles.TypeTitleFirst
|
|
|
|
} else {
|
|
|
|
item.TypeTitle = typeTitles.TypeTitleNotFirst
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
2021-10-23 22:28:06 +00:00
|
|
|
item.FilterTitle = typeTitles.FilterTitle
|
|
|
|
} else {
|
|
|
|
logging.Warn().
|
|
|
|
Int("postID", post.ID).
|
|
|
|
Int("threadType", int(post.ThreadType)).
|
|
|
|
Msg("unknown thread type for post")
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
func SnippetToTimelineItem(
|
|
|
|
snippet *models.Snippet,
|
|
|
|
asset *models.Asset,
|
|
|
|
discordMessage *models.DiscordMessage,
|
2021-11-11 19:00:46 +00:00
|
|
|
tags []*models.Tag,
|
2021-10-23 22:28:06 +00:00
|
|
|
owner *models.User,
|
|
|
|
currentTheme string,
|
|
|
|
) templates.TimelineItem {
|
|
|
|
item := templates.TimelineItem{
|
|
|
|
Date: snippet.When,
|
|
|
|
FilterTitle: "Snippets",
|
|
|
|
Url: hmnurl.BuildSnippet(snippet.ID),
|
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),
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
Description: template.HTML(snippet.DescriptionHtml),
|
|
|
|
|
|
|
|
CanShowcase: true,
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
if asset != nil {
|
2021-06-22 09:50:40 +00:00
|
|
|
if strings.HasPrefix(asset.MimeType, "image/") {
|
2021-10-23 22:28:06 +00:00
|
|
|
item.EmbedMedia = append(item.EmbedMedia, imageMediaItem(asset))
|
2021-06-22 09:50:40 +00:00
|
|
|
} else if strings.HasPrefix(asset.MimeType, "video/") {
|
2021-10-23 22:28:06 +00:00
|
|
|
item.EmbedMedia = append(item.EmbedMedia, videoMediaItem(asset))
|
2021-06-22 09:50:40 +00:00
|
|
|
} else if strings.HasPrefix(asset.MimeType, "audio/") {
|
2021-10-23 22:28:06 +00:00
|
|
|
item.EmbedMedia = append(item.EmbedMedia, audioMediaItem(asset))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if snippet.Url != nil {
|
|
|
|
url := *snippet.Url
|
|
|
|
if videoId := getYoutubeVideoID(url); videoId != "" {
|
|
|
|
item.EmbedMedia = append(item.EmbedMedia, youtubeMediaItem(videoId))
|
|
|
|
item.CanShowcase = false
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 20:48:28 +00:00
|
|
|
if len(item.EmbedMedia) > 0 && (item.EmbedMedia[0].Width == 0 || item.EmbedMedia[0].Height == 0) {
|
|
|
|
item.CanShowcase = false
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:50:40 +00:00
|
|
|
if discordMessage != nil {
|
2021-10-23 22:28:06 +00:00
|
|
|
item.DiscordMessageUrl = discordMessage.Url
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 19:00:46 +00:00
|
|
|
sort.Slice(tags, func(i, j int) bool {
|
|
|
|
return tags[i].Text < tags[j].Text
|
|
|
|
})
|
|
|
|
for _, tag := range tags {
|
|
|
|
item.Tags = append(item.Tags, templates.TagToTemplate(tag))
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
return item
|
|
|
|
}
|
2021-06-22 09:50:40 +00:00
|
|
|
|
2021-10-23 22:28:06 +00:00
|
|
|
var youtubeRegexes = [...]*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`(?i)youtube\.com/watch\?.*v=(?P<videoid>[^/&]+)`),
|
|
|
|
regexp.MustCompile(`(?i)youtu\.be/(?P<videoid>[^/]+)`),
|
|
|
|
}
|
|
|
|
|
|
|
|
func getYoutubeVideoID(url string) string {
|
|
|
|
for _, regex := range youtubeRegexes {
|
|
|
|
match := regex.FindStringSubmatch(url)
|
|
|
|
if match != nil {
|
|
|
|
return match[regex.SubexpIndex("videoid")]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageMediaItem(asset *models.Asset) templates.TimelineItemMedia {
|
|
|
|
assetUrl := hmnurl.BuildS3Asset(asset.S3Key)
|
|
|
|
|
|
|
|
return templates.TimelineItemMedia{
|
|
|
|
Type: templates.TimelineItemMediaTypeImage,
|
|
|
|
AssetUrl: assetUrl,
|
|
|
|
ThumbnailUrl: assetUrl, // TODO: Use smaller thumbnails?
|
|
|
|
MimeType: asset.MimeType,
|
|
|
|
Width: asset.Width,
|
|
|
|
Height: asset.Height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func videoMediaItem(asset *models.Asset) templates.TimelineItemMedia {
|
|
|
|
assetUrl := hmnurl.BuildS3Asset(asset.S3Key)
|
|
|
|
|
|
|
|
return templates.TimelineItemMedia{
|
|
|
|
Type: templates.TimelineItemMediaTypeVideo,
|
|
|
|
AssetUrl: assetUrl,
|
|
|
|
// TODO: Use image thumbnails
|
|
|
|
MimeType: asset.MimeType,
|
|
|
|
Width: asset.Width,
|
|
|
|
Height: asset.Height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func audioMediaItem(asset *models.Asset) templates.TimelineItemMedia {
|
|
|
|
assetUrl := hmnurl.BuildS3Asset(asset.S3Key)
|
|
|
|
|
|
|
|
return templates.TimelineItemMedia{
|
|
|
|
Type: templates.TimelineItemMediaTypeAudio,
|
|
|
|
AssetUrl: assetUrl,
|
|
|
|
MimeType: asset.MimeType,
|
|
|
|
Width: asset.Width,
|
|
|
|
Height: asset.Height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func youtubeMediaItem(videoId string) templates.TimelineItemMedia {
|
|
|
|
return templates.TimelineItemMedia{
|
|
|
|
Type: templates.TimelineItemMediaTypeEmbed,
|
|
|
|
EmbedHTML: template.HTML(fmt.Sprintf(
|
|
|
|
`<iframe src="https://www.youtube-nocookie.com/embed/%s" allow="accelerometer; encrypted-media; gyroscope;" allowfullscreen frameborder="0"></iframe>`,
|
|
|
|
template.HTMLEscapeString(videoId),
|
|
|
|
)),
|
|
|
|
ExtraOpenGraphItems: []templates.OpenGraphItem{
|
|
|
|
{Property: "og:video", Value: fmt.Sprintf("https://youtube.com/watch?v=%s", videoId)},
|
|
|
|
{Name: "twitter:card", Value: "player"},
|
|
|
|
},
|
2021-06-22 09:50:40 +00:00
|
|
|
}
|
|
|
|
}
|