Handle recordWithMedia, refactor processPost function
All checks were successful
/ build (push) Successful in 2m20s

This commit is contained in:
Astra 2026-03-26 07:49:58 +00:00
parent e2faeaac75
commit e8b6e04239
3 changed files with 249 additions and 117 deletions

View file

@ -294,6 +294,6 @@ func (bluesky *Bluesky) FetchPost(did string, rkey string) FetchedPost {
}{
URIs: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey),
}
bluesky.sling.New().Get("/xrpc/app.bsky.feed.getPosts").QueryStruct(&params).Receive(resp, resp)
bluesky.publicSling.New().Get("/xrpc/app.bsky.feed.getPosts").QueryStruct(&params).Receive(resp, resp)
return resp.Posts[0]
}

View file

@ -16,7 +16,7 @@ type Post struct {
Langs []string `json:"langs,omitempty"`
Labels *Labels `json:"labels,omitempty"`
Reply *Reply `json:"reply,omitempty"`
Facets *[]Facets `json:"facets,omitempty"`
Facets []Facets `json:"facets,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
@ -67,7 +67,7 @@ type Media struct {
Type string `json:"$type,omitempty"`
External *External `json:"external,omitempty"`
Video *Video `json:"video,omitempty"`
Images *[]Images `json:"images,omitempty"`
Images []Images `json:"images,omitempty"`
AspectRatio *AspectRatio `json:"aspectRatio,omitempty"`
}
@ -86,7 +86,7 @@ type PostRecord struct {
type Embed struct {
Type string `json:"$type,omitempty"`
Media *Media `json:"media,omitempty"`
Images *[]Images `json:"images,omitempty"`
Images []Images `json:"images,omitempty"`
Video *Video `json:"video,omitempty"`
Record *PostRecord `json:"record,omitempty"`
External *External `json:"external,omitempty"`
@ -97,8 +97,8 @@ type Values struct {
}
type Labels struct {
Type string `json:"$type,omitempty"`
Values *[]Values `json:"values,omitempty"`
Type string `json:"$type,omitempty"`
Values []Values `json:"values,omitempty"`
}
type Root struct {
@ -145,9 +145,9 @@ type Features struct {
}
type Facets struct {
Type string `json:"$type"`
Index *Index `json:"index,omitempty"`
Features *[]Features `json:"features,omitempty"`
Type string `json:"$type"`
Index *Index `json:"index,omitempty"`
Features []Features `json:"features,omitempty"`
}
type ParsedEmbeds struct {
@ -333,33 +333,36 @@ func (post *Post) ProcessFacets(aliases []Records) string {
return html.EscapeString(post.Text)
}
sort.Slice((*post.Facets), func(i, j int) bool {
return (*post.Facets)[i].Index.ByteStart < (*post.Facets)[j].Index.ByteStart
sort.Slice((post.Facets), func(i, j int) bool {
return (post.Facets)[i].Index.ByteStart < (post.Facets)[j].Index.ByteStart
})
var result strings.Builder
lastIndex := 0
for _, facet := range *post.Facets {
for _, facet := range post.Facets {
start := facet.Index.ByteStart
end := facet.Index.ByteEnd
// Escape HTML in plain text portions
result.WriteString(html.EscapeString(post.Text[lastIndex:start]))
for _, feature := range *facet.Features {
for _, feature := range facet.Features {
switch feature.Type {
case "app.bsky.richtext.facet#mention":
link := fmt.Sprintf(`<a href="https://bsky.app/profile/%s">%s</a>`, feature.Did, html.EscapeString(post.Text[start:end]))
for _, alias := range aliases {
if alias.Value.Subject == feature.Did {
link = fmt.Sprintf(`<a href="%s">%s</a>`,
strings.SplitN(alias.Value.Target, "#", 2)[0], strings.SplitN(alias.Value.Target, "#", 2)[1])
parts := strings.SplitN(alias.Value.Target, "#", 2)
if len(parts) == 2 {
link = fmt.Sprintf(`<a href="%s">%s</a>`, parts[0], parts[1])
}
}
}
result.WriteString(link)
case "app.bsky.richtext.facet#link":
link := fmt.Sprintf(`<a href="%s">%s</a>`, feature.URI, html.EscapeString(post.Text[start:end]))
uri := strings.Trim(feature.URI, "\"")
link := fmt.Sprintf(`<a href="%s">%s</a>`, uri, html.EscapeString(post.Text[start:end]))
result.WriteString(link)
case "app.bsky.richtext.facet#tag":
link := fmt.Sprintf(`<a href="https://bsky.app/hashtag/%s">%s</a>`, feature.Tag, html.EscapeString(post.Text[start:end]))
@ -375,60 +378,88 @@ func (post *Post) ProcessFacets(aliases []Records) string {
return result.String()
}
func (p *Post) GetEmbeds() *[]ParsedEmbeds {
var parsedEmbeds = &[]ParsedEmbeds{}
if p.Embed != nil {
if p.Embed.Video != nil {
parsedEmbed := ParsedEmbeds{
func (p *Post) GetEmbeds() []ParsedEmbeds {
var parsedEmbeds []ParsedEmbeds
if p.Embed == nil {
return parsedEmbeds
}
switch p.Embed.Type {
case "app.bsky.embed.images":
for _, image := range p.Embed.Images {
if image.Image != nil && image.Image.Ref != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: image.Image.Ref.Link,
Type: "image",
})
}
}
case "app.bsky.embed.video":
if p.Embed.Video != nil && p.Embed.Video.Ref != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.Video.Ref.Link,
Type: "video",
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
})
}
case "app.bsky.embed.external":
if p.Embed.External != nil {
if strings.Contains(p.Embed.External.URI, "media.tenor.com") {
parsedEmbed := ParsedEmbeds{
URI: p.Embed.External.URI,
Type: "external",
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
t := "external"
if strings.Contains(p.Embed.External.URI, "tenor.com") {
t = "gif"
}
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.External.URI,
Type: t,
})
}
case "app.bsky.embed.record":
if p.Embed.Record != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.Record.Record.URI,
Cid: p.Embed.Record.Record.Cid,
Type: "record",
})
}
case "app.bsky.embed.recordWithMedia":
// Quote post - also extract the media it contains
if p.Embed.Record != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.Record.Record.URI,
Cid: p.Embed.Record.Record.Cid,
Type: "record",
})
}
if p.Embed.Media != nil {
if p.Embed.Media.Images != nil {
for _, image := range *p.Embed.Media.Images {
parsedEmbed := ParsedEmbeds{
URI: image.Image.Ref.Link,
Type: "image",
for _, image := range p.Embed.Media.Images {
if image.Image != nil && image.Image.Ref != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: image.Image.Ref.Link,
Type: "image",
})
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
}
}
if p.Embed.Media.Video != nil {
parsedEmbed := ParsedEmbeds{
if p.Embed.Media.Video != nil && p.Embed.Media.Video.Ref != nil {
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.Media.Video.Ref.Link,
Type: "video",
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
})
}
if p.Embed.Media.External != nil {
parsedEmbed := ParsedEmbeds{
parsedEmbeds = append(parsedEmbeds, ParsedEmbeds{
URI: p.Embed.Media.External.URI,
Type: "external",
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
}
}
if p.Embed.Images != nil {
for _, image := range *p.Embed.Images {
parsedEmbed := ParsedEmbeds{
URI: image.Image.Ref.Link,
Type: "image",
}
*parsedEmbeds = append(*parsedEmbeds, parsedEmbed)
})
}
}
}
return parsedEmbeds
}
@ -441,7 +472,7 @@ func (p *Post) GetMedia() *Media {
return nil
}
func (p *Post) GetMediaImages() *[]Images {
func (p *Post) GetMediaImages() []Images {
if p.GetMedia() != nil {
return p.GetMedia().Images
}