From 59545c874d8635c39ccdbecf84f953686dd5c03c Mon Sep 17 00:00:00 2001 From: Guillermo Serrahima Date: Wed, 18 Mar 2026 22:02:13 +0100 Subject: more naming refactors --- backend/notes/app.bsky.actor.go | 40 ++++++++++++++ backend/notes/app.bsky.feed.go | 102 ++++++++++++++++++++++++++++++++++++ backend/notes/app.bsky.feed.post.go | 102 ------------------------------------ backend/notes/author.go | 9 ++++ 4 files changed, 151 insertions(+), 102 deletions(-) create mode 100644 backend/notes/app.bsky.actor.go create mode 100644 backend/notes/app.bsky.feed.go delete mode 100644 backend/notes/app.bsky.feed.post.go create mode 100644 backend/notes/author.go (limited to 'backend/notes') diff --git a/backend/notes/app.bsky.actor.go b/backend/notes/app.bsky.actor.go new file mode 100644 index 0000000..dc5d536 --- /dev/null +++ b/backend/notes/app.bsky.actor.go @@ -0,0 +1,40 @@ +package notes + +import ( + "fmt" +) + +type AppBskyActorProfileRecord struct { + DisplayName string `json:"displayName"` + Avatar *struct { + Ref struct { + Link string `json:"$link"` + } `json:"ref"` + } `json:"avatar,omitempty"` + Banner *struct { + Ref struct { + Link string `json:"$link"` + } `json:"ref"` + } `json:"banner,omitempty"` + Pronouns string `json:"pronouns"` +} + +func AuthorFromAppBskyActorProfileRecord(record AppBskyActorProfileRecord, did, handle string) (*Author, error) { + var avatarURL, bannerURL string + if record.Avatar != nil { + avatarURL = fmt.Sprintf("https://cdn.bsky.app/img/avatar/plain/%s/%s@jpeg", did, record.Avatar.Ref.Link) + } + if record.Banner != nil { + bannerURL = fmt.Sprintf("https://cdn.bsky.app/img/banner/plain/%s/%s@jpeg", did, record.Banner.Ref.Link) + } + + author := &Author{ + Handle: handle, + DisplayName: record.DisplayName, + AvatarURL: avatarURL, + BannerURL: bannerURL, + Pronouns: record.Pronouns, + } + + return author, nil +} diff --git a/backend/notes/app.bsky.feed.go b/backend/notes/app.bsky.feed.go new file mode 100644 index 0000000..b5cfd03 --- /dev/null +++ b/backend/notes/app.bsky.feed.go @@ -0,0 +1,102 @@ +package notes + +import ( + "fmt" + "llostre/backend/general" + "time" +) + +type AppBskyFeedPostRecord struct { + URI string `json:"uri"` + Value struct { + Text string `json:"text"` + CreatedAt string `json:"createdAt"` + Reply *struct { + Parent struct { + URI string `json:"uri"` + } `json:"parent"` + } `json:"reply,omitempty"` + Facets []struct { + Index struct { + ByteStart int `json:"byteStart"` + ByteEnd int `json:"byteEnd"` + } `json:"index"` + Features []struct { + Type string `json:"$type"` + URI string `json:"uri,omitempty"` + DID string `json:"did,omitempty"` + Tag string `json:"tag,omitempty"` + } `json:"features"` + } `json:"facets,omitempty"` + } `json:"value"` +} + +func NoteFromAppBskyFeedPostRecord(record AppBskyFeedPostRecord, author *Author) (*Note, error) { + if record.URI == "" { + return nil, fmt.Errorf("missing required field: uri") + } + + createdAt, err := time.Parse(time.RFC3339, record.Value.CreatedAt) + if err != nil { + return nil, fmt.Errorf("failed to parse timestamp: %w", err) + } + + var replyTo *string + if record.Value.Reply != nil { + replyTo = &record.Value.Reply.Parent.URI + } + + var facets []Facet + for _, f := range record.Value.Facets { + for _, feature := range f.Features { + var value string + var facetType string + + switch feature.Type { + case "app.bsky.richtext.facet#link": + facetType = "link" + value = feature.URI + case "app.bsky.richtext.facet#mention": + facetType = "mention" + value = feature.DID + case "app.bsky.richtext.facet#tag": + facetType = "tag" + value = feature.Tag + default: + continue + } + + facets = append(facets, Facet{ + ByteStart: f.Index.ByteStart, + ByteEnd: f.Index.ByteEnd, + Type: facetType, + Value: value, + }) + } + } + + var handle, displayName, avatarURL string + if author != nil { + handle = author.Handle + displayName = author.DisplayName + avatarURL = author.AvatarURL + } else { + handle = "handle.invalid" + displayName = "" + avatarURL = "" + } + + did, _, _, err := general.ParseURI(record.URI) + + return &Note{ + URI: record.URI, + AuthorDID: did, + AuthorHandle: handle, + AuthorName: displayName, + AvatarURL: avatarURL, + CreatedAt: createdAt, + Text: record.Value.Text, + ReplyTo: replyTo, + Facets: facets, + }, nil +} diff --git a/backend/notes/app.bsky.feed.post.go b/backend/notes/app.bsky.feed.post.go deleted file mode 100644 index 451633e..0000000 --- a/backend/notes/app.bsky.feed.post.go +++ /dev/null @@ -1,102 +0,0 @@ -package notes - -import ( - "fmt" - "llostre/backend/general" - "time" -) - -type AppBskyFeedPostRecord struct { - URI string `json:"uri"` - Value struct { - Text string `json:"text"` - CreatedAt string `json:"createdAt"` - Reply *struct { - Parent struct { - URI string `json:"uri"` - } `json:"parent"` - } `json:"reply,omitempty"` - Facets []struct { - Index struct { - ByteStart int `json:"byteStart"` - ByteEnd int `json:"byteEnd"` - } `json:"index"` - Features []struct { - Type string `json:"$type"` - URI string `json:"uri,omitempty"` - DID string `json:"did,omitempty"` - Tag string `json:"tag,omitempty"` - } `json:"features"` - } `json:"facets,omitempty"` - } `json:"value"` -} - -func NoteFromAppBskyFeedPostRecord(record AppBskyFeedPostRecord, profile *general.Profile) (*Note, error) { - if record.URI == "" { - return nil, fmt.Errorf("missing required field: uri") - } - - createdAt, err := time.Parse(time.RFC3339, record.Value.CreatedAt) - if err != nil { - return nil, fmt.Errorf("failed to parse timestamp: %w", err) - } - - var replyTo *string - if record.Value.Reply != nil { - replyTo = &record.Value.Reply.Parent.URI - } - - var facets []Facet - for _, f := range record.Value.Facets { - for _, feature := range f.Features { - var value string - var facetType string - - switch feature.Type { - case "app.bsky.richtext.facet#link": - facetType = "link" - value = feature.URI - case "app.bsky.richtext.facet#mention": - facetType = "mention" - value = feature.DID - case "app.bsky.richtext.facet#tag": - facetType = "tag" - value = feature.Tag - default: - continue - } - - facets = append(facets, Facet{ - ByteStart: f.Index.ByteStart, - ByteEnd: f.Index.ByteEnd, - Type: facetType, - Value: value, - }) - } - } - - var handle, displayName, avatarURL string - if profile != nil { - handle = profile.Handle - displayName = profile.DisplayName - avatarURL = profile.AvatarURL - } else { - handle = "handle.invalid" - displayName = "" - avatarURL = "" - } - - did, _, _, err := general.ParseURI(record.URI) - - return &Note{ - URI: record.URI, - AuthorDID: did, - AuthorHandle: handle, - AuthorName: displayName, - AvatarURL: avatarURL, - CreatedAt: createdAt, - Text: record.Value.Text, - ReplyTo: replyTo, - Facets: facets, - }, nil -} diff --git a/backend/notes/author.go b/backend/notes/author.go new file mode 100644 index 0000000..02afabf --- /dev/null +++ b/backend/notes/author.go @@ -0,0 +1,9 @@ +package notes + +type Author struct { + Handle string + DisplayName string + AvatarURL string + BannerURL string + Pronouns string +} -- cgit v1.2.3