Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Include expanded urls in social cards (#2427)

* include expanded urls in social cards (#2427)

* break expandPostLinks into its own function, add bounds checking

authored by

Zach Lipton and committed by
GitHub
c58e6500 cdbdb37a

+50 -4
+46
bskyweb/cmd/bskyweb/server.go
··· 10 10 "net/http" 11 11 "os" 12 12 "os/signal" 13 + "slices" 13 14 "strings" 14 15 "syscall" 15 16 "time" ··· 351 352 data["imgThumbUrls"] = thumbUrls 352 353 } 353 354 } 355 + 356 + data["postText"] = expandPostLinks(postView) 357 + 354 358 return c.Render(http.StatusOK, "post.html", data) 359 + } 360 + 361 + // function to expand shortened links in rich text back to full urls, replacing shortened urls in 362 + // social card meta tags and the noscript output. this essentially reverses the effect 363 + // of shortenLinks() in src/lib/strings/rich-text-manip.ts 364 + func expandPostLinks(postView *appbsky.FeedDefs_PostView) string { 365 + if postView.Record != nil { 366 + rec := postView.Record.Val.(*appbsky.FeedPost) 367 + postText := rec.Text 368 + var charsAdded int64 = 0 369 + // iterate over facets, check if they're link facets, and if found, grab the uri 370 + for _, facet := range rec.Facets { 371 + linkUri := "" 372 + if slices.ContainsFunc(facet.Features, func(feat *appbsky.RichtextFacet_Features_Elem) bool { 373 + if feat.RichtextFacet_Link != nil && feat.RichtextFacet_Link.LexiconTypeID == "app.bsky.richtext.facet#link" { 374 + linkUri = feat.RichtextFacet_Link.Uri 375 + // only expand uris that have been shortened (as opposed to those with non-uri anchor text) 376 + if int64(len(postText)) >= facet.Index.ByteEnd+charsAdded && 377 + strings.HasSuffix(postText[facet.Index.ByteStart+charsAdded:facet.Index.ByteEnd+charsAdded], "...") && 378 + strings.Contains(linkUri, postText[facet.Index.ByteStart+charsAdded:(facet.Index.ByteEnd+charsAdded)-3]) { 379 + return true 380 + } 381 + } 382 + return false 383 + }) { 384 + // replace the shortened uri with the full length one from the facet using utf8 byte offsets 385 + if int64(len(postText)) >= facet.Index.ByteEnd+charsAdded { 386 + postText = postText[0:facet.Index.ByteStart+charsAdded] + linkUri + postText[facet.Index.ByteEnd+charsAdded:] 387 + charsAdded += int64(len(linkUri)) - (facet.Index.ByteEnd - facet.Index.ByteStart) 388 + } 389 + } 390 + } 391 + // if the post has an embeded link and its url doesn't already appear in postText, append it to 392 + // the end to avoid social cards with missing links 393 + if postView.Embed != nil && 394 + postView.Embed.EmbedExternal_View != nil && 395 + !strings.Contains(postText, postView.Embed.EmbedExternal_View.External.Uri) { 396 + postText = fmt.Sprintf("%s\n%s", postText, postView.Embed.EmbedExternal_View.External.Uri) 397 + } 398 + return postText 399 + } 400 + return "" 355 401 } 356 402 357 403 func (srv *Server) WebProfile(c echo.Context) error {
+4 -4
bskyweb/templates/post.html
··· 21 21 {% else %} 22 22 <meta property="og:title" content="@{{ postView.Author.Handle }}"> 23 23 {% endif -%} 24 - {%- if postView.Record.Val.Text %} 25 - <meta name="description" content="{{ postView.Record.Val.Text }}"> 26 - <meta property="og:description" content="{{ postView.Record.Val.Text }}"> 24 + {%- if postText %} 25 + <meta name="description" content="{{ postText }}"> 26 + <meta property="og:description" content="{{ postText }}"> 27 27 {% endif -%} 28 28 {%- if imgThumbUrls %} 29 29 {% for imgThumbUrl in imgThumbUrls %} ··· 47 47 <p id="bsky_display_name">{{ postView.Author.DisplayName }}</p> 48 48 <p id="bsky_handle">{{ postView.Author.Handle }}</p> 49 49 <p id="bsky_did">{{ postView.Author.Did }}</p> 50 - <p id="bsky_post_text">{{ postView.Record.Val.Text }}</p> 50 + <p id="bsky_post_text">{{ postText }}</p> 51 51 <p id="bsky_post_indexedat">{{ postView.IndexedAt }}</p> 52 52 </div> 53 53 {% endif -%}