From a772d4d98eb4dbb5bf36117de60594856d502483 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Sun, 12 Mar 2023 14:14:33 +0100 Subject: [PATCH] [chore] Fix opengraph properties (#1611) --- internal/web/opengraph.go | 87 +++++++++++++++++++++++++-------------- web/template/header.tmpl | 26 ++++++------ 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/internal/web/opengraph.go b/internal/web/opengraph.go index 6fcc524cd..f9a2110a2 100644 --- a/internal/web/opengraph.go +++ b/internal/web/opengraph.go @@ -31,26 +31,30 @@ const maxOGDescriptionLength = 300 // ogMeta represents supported OpenGraph Meta tags // -// see eg https://developer.yoast.com/features/opengraph/functional-specification/ +// see eg https://ogp.me/ type ogMeta struct { // vanilla og tags + Title string // og:title + Type string // og:type + Locale string // og:locale + URL string // og:url + SiteName string // og:site_name + Description string // og:description - Locale string // og:locale - ResourceType string // og:type - Title string // og:title - URL string // og:url - SiteName string // og:site_name - Description string // og:description - Image string // og:image - ImageWidth string // og:image:width - ImageHeight string // og:image:height + // image tags + Image string // og:image + ImageWidth string // og:image:width + ImageHeight string // og:image:height + ImageAlt string // og:image:alt // article tags - ArticlePublisher string // article:publisher ArticleAuthor string // article:author ArticleModifiedTime string // article:modified_time ArticlePublishedTime string // article:published_time + + // profile tags + ProfileUsername string // profile:username } // ogBase returns an *ogMeta suitable for serving at @@ -64,13 +68,15 @@ func ogBase(instance *apimodel.InstanceV1) *ogMeta { } og := &ogMeta{ - Locale: locale, - ResourceType: "website", - Title: text.SanitizePlaintext(instance.Title) + " - GoToSocial", - URL: instance.URI, - SiteName: instance.AccountDomain, - Description: parseDescription(instance.ShortDescription), - Image: instance.Thumbnail, + Title: text.SanitizePlaintext(instance.Title) + " - GoToSocial", + Type: "website", + Locale: locale, + URL: instance.URI, + SiteName: instance.AccountDomain, + Description: parseDescription(instance.ShortDescription), + + Image: instance.Thumbnail, + ImageAlt: instance.ThumbnailDescription, } return og @@ -80,11 +86,20 @@ func ogBase(instance *apimodel.InstanceV1) *ogMeta { // struct specific to that account. It's suitable for serving // at account profile pages. func (og *ogMeta) withAccount(account *apimodel.Account) *ogMeta { - og.ResourceType = "profile" og.Title = parseTitle(account, og.SiteName) + og.Type = "profile" og.URL = account.URL - og.Description = parseDescription(account.Note) + if account.Note != "" { + og.Description = parseDescription(account.Note) + } else { + og.Description = "This GoToSocial user hasn't written a bio yet!" + } + og.Image = account.Avatar + og.ImageAlt = "Avatar for " + account.Username + + og.ProfileUsername = account.Username + return og } @@ -92,31 +107,39 @@ func (og *ogMeta) withAccount(account *apimodel.Account) *ogMeta { // struct specific to that status. It's suitable for serving // at status pages. func (og *ogMeta) withStatus(status *apimodel.Status) *ogMeta { + og.Title = "Post by " + parseTitle(status.Account, og.SiteName) + og.Type = "article" + if status.Language != nil { + og.Locale = *status.Language + } + og.URL = status.URL + switch { + case status.SpoilerText != "": + og.Description = parseDescription("CW: " + status.SpoilerText) + case status.Text != "": + og.Description = parseDescription(status.Text) + default: + og.Description = og.Title + } + if !status.Sensitive && len(status.MediaAttachments) > 0 { a := status.MediaAttachments[0] og.Image = a.PreviewURL og.ImageWidth = strconv.Itoa(a.Meta.Small.Width) og.ImageHeight = strconv.Itoa(a.Meta.Small.Height) + if a.Description != nil { + og.ImageAlt = *a.Description + } } else { og.Image = status.Account.Avatar + og.ImageAlt = "Avatar for " + status.Account.Username } - if status.SpoilerText != "" { - og.Description = parseDescription("CW: " + status.SpoilerText) - } else { - og.Description = parseDescription(status.Text) - } - - if status.Language != nil { - og.Locale = *status.Language - } - og.ResourceType = "article" - og.Title = "Post by " + parseTitle(status.Account, og.SiteName) - og.URL = status.URL og.ArticlePublisher = status.Account.URL og.ArticleAuthor = status.Account.URL og.ArticlePublishedTime = status.CreatedAt og.ArticleModifiedTime = status.CreatedAt + return og } diff --git a/web/template/header.tmpl b/web/template/header.tmpl index 0557982fd..d5986e7bd 100644 --- a/web/template/header.tmpl +++ b/web/template/header.tmpl @@ -26,18 +26,20 @@ {{ if .ogMeta }}{{ if .ogMeta.Locale }} - {{ end }} - - - - - {{ if .ogMeta.ArticlePublisher }} - - - - {{ end }} - {{ if .ogMeta.ImageWidth }} - + {{ end }} + + + + + {{ if .ogMeta.ArticlePublisher }} + + + + {{ end }}{{ if .ogMeta.ProfileUsername }} + {{ end }} + {{ if .ogMeta.ImageAlt }} + {{ end }}{{ if .ogMeta.ImageWidth }} + {{ end }}{{ end }} {{ if .rssFeed }}{{ end }}