+29
-38
hydration/post.go
+29
-38
hydration/post.go
···
9
10
"github.com/bluesky-social/indigo/api/bsky"
11
"github.com/bluesky-social/indigo/lex/util"
12
"go.opentelemetry.io/otel"
13
)
14
···
39
defer span.End()
40
41
// Query post from database
42
-
var dbPost struct {
43
-
ID uint
44
-
Cid string
45
-
Raw []byte
46
-
NotFound bool
47
-
ReplyTo uint
48
-
ReplyToUsr uint
49
-
InThread uint
50
-
AuthorID uint
51
-
}
52
-
53
-
err := h.db.Raw(`
54
-
SELECT p.id, p.cid, p.raw, p.not_found, p.reply_to, p.reply_to_usr, p.in_thread, p.author as author_id
55
-
FROM posts p
56
-
WHERE p.id = (
57
-
SELECT id FROM posts
58
WHERE author = (SELECT id FROM repos WHERE did = ?)
59
AND rkey = ?
60
-
)
61
`, extractDIDFromURI(uri), extractRkeyFromURI(uri)).Scan(&dbPost).Error
62
63
if err != nil {
···
77
var wg sync.WaitGroup
78
79
// Get author DID
80
-
var authorDID string
81
-
var likes, reposts, replies int
82
83
-
wg.Go(func() {
84
-
h.db.Raw("SELECT did FROM repos WHERE id = ?", dbPost.AuthorID).Scan(&authorDID)
85
-
})
86
87
// Get engagement counts
88
wg.Go(func() {
89
h.db.Raw("SELECT COUNT(*) FROM likes WHERE subject = ?", dbPost.ID).Scan(&likes)
90
})
···
95
h.db.Raw("SELECT COUNT(*) FROM posts WHERE reply_to = ?", dbPost.ID).Scan(&replies)
96
})
97
98
wg.Wait()
99
100
info := &PostInfo{
···
108
LikeCount: likes,
109
RepostCount: reposts,
110
ReplyCount: replies,
111
}
112
113
if info.Cid == "" {
···
115
info.Cid = fakeCid
116
}
117
118
-
// Check if viewer liked this post
119
-
if viewerDID != "" {
120
-
var likeRkey string
121
-
h.db.Raw(`
122
-
SELECT l.rkey FROM likes l
123
-
WHERE l.subject = ?
124
-
AND l.author = (SELECT id FROM repos WHERE did = ?)
125
-
`, dbPost.ID, viewerDID).Scan(&likeRkey)
126
-
if likeRkey != "" {
127
-
info.ViewerLike = fmt.Sprintf("at://%s/app.bsky.feed.like/%s", viewerDID, likeRkey)
128
-
}
129
-
}
130
-
131
// Hydrate embed
132
-
if feedPost.Embed != nil {
133
-
info.EmbedInfo = h.formatEmbed(ctx, feedPost.Embed, authorDID, viewerDID)
134
-
}
135
136
return info, nil
137
}
···
9
10
"github.com/bluesky-social/indigo/api/bsky"
11
"github.com/bluesky-social/indigo/lex/util"
12
+
"github.com/whyrusleeping/market/models"
13
"go.opentelemetry.io/otel"
14
)
15
···
40
defer span.End()
41
42
// Query post from database
43
+
var dbPost models.Post
44
+
err := h.db.Raw(`SELECT * FROM posts
45
WHERE author = (SELECT id FROM repos WHERE did = ?)
46
AND rkey = ?
47
`, extractDIDFromURI(uri), extractRkeyFromURI(uri)).Scan(&dbPost).Error
48
49
if err != nil {
···
63
var wg sync.WaitGroup
64
65
// Get author DID
66
67
+
authorDID := extractDIDFromURI(uri)
68
69
// Get engagement counts
70
+
var likes, reposts, replies int
71
wg.Go(func() {
72
h.db.Raw("SELECT COUNT(*) FROM likes WHERE subject = ?", dbPost.ID).Scan(&likes)
73
})
···
78
h.db.Raw("SELECT COUNT(*) FROM posts WHERE reply_to = ?", dbPost.ID).Scan(&replies)
79
})
80
81
+
// Check if viewer liked this post
82
+
var likeRkey string
83
+
if viewerDID != "" {
84
+
wg.Go(func() {
85
+
h.db.Raw(`
86
+
SELECT l.rkey FROM likes l
87
+
WHERE l.subject = ?
88
+
AND l.author = (SELECT id FROM repos WHERE did = ?)
89
+
`, dbPost.ID, viewerDID).Scan(&likeRkey)
90
+
})
91
+
}
92
+
93
+
var ei *bsky.FeedDefs_PostView_Embed
94
+
if feedPost.Embed != nil {
95
+
wg.Go(func() {
96
+
ei = h.formatEmbed(ctx, feedPost.Embed, authorDID, viewerDID)
97
+
})
98
+
}
99
+
100
wg.Wait()
101
102
info := &PostInfo{
···
110
LikeCount: likes,
111
RepostCount: reposts,
112
ReplyCount: replies,
113
+
EmbedInfo: ei,
114
+
}
115
+
116
+
if likeRkey != "" {
117
+
info.ViewerLike = fmt.Sprintf("at://%s/app.bsky.feed.like/%s", viewerDID, likeRkey)
118
}
119
120
if info.Cid == "" {
···
122
info.Cid = fakeCid
123
}
124
125
// Hydrate embed
126
127
return info, nil
128
}