A community based topic aggregation platform built on atproto
1package posts
2
3import (
4 "fmt"
5)
6
7// TransformBlobRefsToURLs transforms all blob references in a PostView to PDS URLs
8// This modifies the Embed field in-place, converting blob refs to direct URLs
9// The transformation only affects external embeds with thumbnail blobs
10func TransformBlobRefsToURLs(postView *PostView) {
11 if postView == nil || postView.Embed == nil {
12 return
13 }
14
15 // Get community PDS URL from post view
16 if postView.Community == nil || postView.Community.PDSURL == "" {
17 return // Cannot transform without PDS URL
18 }
19
20 communityDID := postView.Community.DID
21 pdsURL := postView.Community.PDSURL
22
23 // Check if embed is a map (should be for external embeds)
24 embedMap, ok := postView.Embed.(map[string]interface{})
25 if !ok {
26 return
27 }
28
29 // Check embed type
30 embedType, ok := embedMap["$type"].(string)
31 if !ok {
32 return
33 }
34
35 // Only transform external embeds
36 if embedType == "social.coves.embed.external" {
37 if external, ok := embedMap["external"].(map[string]interface{}); ok {
38 transformThumbToURL(external, communityDID, pdsURL)
39 }
40 }
41}
42
43// transformThumbToURL converts a thumb blob ref to a PDS URL
44// This modifies the external map in-place
45func transformThumbToURL(external map[string]interface{}, communityDID, pdsURL string) {
46 // Check if thumb exists
47 thumb, ok := external["thumb"]
48 if !ok {
49 return
50 }
51
52 // If thumb is already a string (URL), don't transform
53 if _, isString := thumb.(string); isString {
54 return
55 }
56
57 // Try to parse as blob ref
58 thumbMap, ok := thumb.(map[string]interface{})
59 if !ok {
60 return
61 }
62
63 // Extract CID from blob ref
64 ref, ok := thumbMap["ref"].(map[string]interface{})
65 if !ok {
66 return
67 }
68
69 cid, ok := ref["$link"].(string)
70 if !ok || cid == "" {
71 return
72 }
73
74 // Transform to PDS blob endpoint URL
75 // Format: {pds_url}/xrpc/com.atproto.sync.getBlob?did={community_did}&cid={cid}
76 blobURL := fmt.Sprintf("%s/xrpc/com.atproto.sync.getBlob?did=%s&cid=%s",
77 pdsURL, communityDID, cid)
78
79 // Replace blob ref with URL string
80 external["thumb"] = blobURL
81}