my fork of the bluesky client
at main 79 lines 2.5 kB view raw
1package main 2 3import ( 4 "bytes" 5 "fmt" 6 "html/template" 7 8 appbsky "github.com/bluesky-social/indigo/api/bsky" 9 "github.com/bluesky-social/indigo/atproto/syntax" 10) 11 12func (srv *Server) postEmbedHTML(postView *appbsky.FeedDefs_PostView) (string, error) { 13 // ensure that there isn't an injection from the URI 14 aturi, err := syntax.ParseATURI(postView.Uri) 15 if err != nil { 16 log.Error("bad AT-URI in reponse", "aturi", aturi, "err", err) 17 return "", err 18 } 19 20 post, ok := postView.Record.Val.(*appbsky.FeedPost) 21 if !ok { 22 log.Error("bad post record value", "err", err) 23 return "", err 24 } 25 26 const tpl = `<blockquote class="bluesky-embed" data-bluesky-uri="{{ .PostURI }}" data-bluesky-cid="{{ .PostCID }}"><p{{ if .PostLang }} lang="{{ .PostLang }}"{{ end }}>{{ .PostText }}</p>&mdash; <a href="{{ .ProfileURL }}">{{ .PostAuthor }}</a> <a href="{{ .PostURL }}">{{ .PostIndexedAt }}</a></blockquote><script async src="{{ .WidgetURL }}" charset="utf-8"></script>` 27 28 t, err := template.New("snippet").Parse(tpl) 29 if err != nil { 30 log.Error("template parse error", "err", err) 31 return "", err 32 } 33 34 sortAt := postView.IndexedAt 35 createdAt, err := syntax.ParseDatetime(post.CreatedAt) 36 if nil == err && createdAt.String() < sortAt { 37 sortAt = createdAt.String() 38 } 39 40 var lang string 41 if len(post.Langs) > 0 { 42 lang = post.Langs[0] 43 } 44 var authorName string 45 if postView.Author.DisplayName != nil { 46 authorName = fmt.Sprintf("%s (@%s)", *postView.Author.DisplayName, postView.Author.Handle) 47 } else { 48 authorName = fmt.Sprintf("@%s", postView.Author.Handle) 49 } 50 data := struct { 51 PostURI template.URL 52 PostCID string 53 PostLang string 54 PostText string 55 PostAuthor string 56 PostIndexedAt string 57 ProfileURL template.URL 58 PostURL template.URL 59 WidgetURL template.URL 60 }{ 61 PostURI: template.URL(postView.Uri), 62 PostCID: postView.Cid, 63 PostLang: lang, 64 PostText: post.Text, 65 PostAuthor: authorName, 66 PostIndexedAt: sortAt, 67 ProfileURL: template.URL(fmt.Sprintf("https://bsky.app/profile/%s?ref_src=embed", aturi.Authority())), 68 PostURL: template.URL(fmt.Sprintf("https://bsky.app/profile/%s/post/%s?ref_src=embed", aturi.Authority(), aturi.RecordKey())), 69 WidgetURL: template.URL("https://embed.bsky.app/static/embed.js"), 70 } 71 72 var buf bytes.Buffer 73 err = t.Execute(&buf, data) 74 if err != nil { 75 log.Error("template parse error", "err", err) 76 return "", err 77 } 78 return buf.String(), nil 79}