From 164f3629ab24d8e763caab25bd9b157ed99ec938 Mon Sep 17 00:00:00 2001 From: astravexton Date: Mon, 16 Jun 2025 17:05:44 +0100 Subject: [PATCH] add alias support for mentions --- bsky/bluesky.go | 38 ++++++++++++++++++++++++++++++++++++++ bsky/parse.go | 10 +++++++++- main.go | 12 ++++++------ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index 6ed10e4..ae8d5cf 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -133,6 +133,13 @@ type TelegramRecord struct { Message string `json:"message"` } +type Alias struct { + Target string `json:"target"` + Subject string `json:"subject"` + Error string `json:"error"` + Message string `json:"message"` +} + func (bluesky *Bluesky) CommitTelegramResponse(data *TelegramRecord, rkey string) *CommitResponse { bluesky.CheckSessionValid() @@ -225,3 +232,34 @@ func (bluesky *Bluesky) DeleteRecord(args []string) *CommitResponse { Post("/xrpc/com.atproto.repo.deleteRecord").BodyJSON(params).Receive(resp, resp) return resp } + +func (bluesky *Bluesky) FetchAliases() []Records { + resp := new(AliasRecord) + params := struct { + Repo string `url:"repo"` + Collection string `url:"collection"` + }{ + Repo: bluesky.Cfg.DID, + Collection: "blue.zio.bsky2tg.alias", + } + + bluesky.sling.New().Get("/xrpc/com.atproto.repo.listRecords").QueryStruct(¶ms).Receive(resp, resp) + return resp.Records +} + +type AliasRecord struct { + Records []Records `json:"records"` + Cursor string `json:"cursor"` +} +type Value struct { + Type string `json:"$type"` + Error string `json:"error"` + Target string `json:"target"` + Message string `json:"message"` + Subject string `json:"subject"` +} +type Records struct { + URI string `json:"uri"` + Cid string `json:"cid"` + Value Value `json:"value"` +} diff --git a/bsky/parse.go b/bsky/parse.go index 4fcbf4e..1560be2 100644 --- a/bsky/parse.go +++ b/bsky/parse.go @@ -133,7 +133,7 @@ func (b *BSky) ParsePost(post []byte) (*Post, error) { return p, nil } -func (post *Post) ProcessFacets() string { +func (post *Post) ProcessFacets(aliases []Records) string { if post == nil { return "" } @@ -160,6 +160,14 @@ func (post *Post) ProcessFacets() string { switch feature.Type { case "app.bsky.richtext.facet#mention": link := fmt.Sprintf(`%s`, feature.Did, post.Text[start:end]) + if aliases != nil { + for _, alias := range aliases { + if alias.Value.Subject == feature.Did { + link = fmt.Sprintf(`%s`, + strings.SplitN(alias.Value.Target, "#", 2)[0], strings.SplitN(alias.Value.Target, "#", 2)[1]) + } + } + } result.WriteString(link) case "app.bsky.richtext.facet#link": link := fmt.Sprintf(`%s`, feature.URI, post.Text[start:end]) diff --git a/main.go b/main.go index cf3a4a4..b45363f 100644 --- a/main.go +++ b/main.go @@ -110,7 +110,7 @@ func main() { // return // } // for _, post := range posts.Records { - // log.Printf("post: %s\n", post.Value.ProcessFacets()) + // log.Printf("post: %s\n", post.Value.ProcessFacets(h.bsky.Bluesky.FetchAliases())) // s, _ := json.Marshal(post.Value) // h.ProcessPost(&models.Event{Did: bskyClient.Bluesky.Cfg.DID, Commit: &models.Commit{ // Record: s, @@ -180,7 +180,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - ps.ProcessFacets(), + ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), strings.Split(ps.Embed.Record.Record.URI, "/")[2], strings.Split(ps.Embed.Record.Record.URI, "/")[4], handle, @@ -191,7 +191,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - ps.ProcessFacets(), + ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4], handle, @@ -202,8 +202,8 @@ func (h *handler) ProcessPost(event *models.Event) error { } if captionText == "" { - if ps.ProcessFacets() != "" { - captionText = fmt.Sprintf(postFormat, ps.ProcessFacets(), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle) + if ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()) != "" { + captionText = fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle) } else { captionText = fmt.Sprintf("🦋 @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle) } @@ -276,7 +276,7 @@ func (h *handler) ProcessPost(event *models.Event) error { } else { m := tgbotapi.MessageConfig{} if captionText == "" { - m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, ps.ProcessFacets(), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) + m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) } else { m = tgbotapi.NewMessage(cid, captionText) } -- 2.47.2