···
4
+
"Coves/internal/atproto/utils"
"Coves/internal/core/users"
"Coves/internal/core/votes"
···
return fmt.Errorf("failed to insert vote: %w", err)
182
-
// 2. Update post vote counts atomically
183
-
// Increment upvote_count or downvote_count based on direction
184
-
// Also update score (upvote_count - downvote_count)
183
+
// 2. Update vote counts on the subject (post or comment)
184
+
// Parse collection from subject URI to determine target table
185
+
collection := utils.ExtractCollectionFromURI(vote.SubjectURI)
186
-
if vote.Direction == "up" {
189
-
SET upvote_count = upvote_count + 1,
190
-
score = upvote_count + 1 - downvote_count
191
-
WHERE uri = $1 AND deleted_at IS NULL
196
-
SET downvote_count = downvote_count + 1,
197
-
score = upvote_count - (downvote_count + 1)
198
-
WHERE uri = $1 AND deleted_at IS NULL
188
+
switch collection {
189
+
case "social.coves.community.post":
190
+
// Vote on post - update posts table
191
+
if vote.Direction == "up" {
194
+
SET upvote_count = upvote_count + 1,
195
+
score = upvote_count + 1 - downvote_count
196
+
WHERE uri = $1 AND deleted_at IS NULL
201
+
SET downvote_count = downvote_count + 1,
202
+
score = upvote_count - (downvote_count + 1)
203
+
WHERE uri = $1 AND deleted_at IS NULL
207
+
case "social.coves.feed.comment":
208
+
// Vote on comment - update comments table
209
+
if vote.Direction == "up" {
212
+
SET upvote_count = upvote_count + 1,
213
+
score = upvote_count + 1 - downvote_count
214
+
WHERE uri = $1 AND deleted_at IS NULL
219
+
SET downvote_count = downvote_count + 1,
220
+
score = upvote_count - (downvote_count + 1)
221
+
WHERE uri = $1 AND deleted_at IS NULL
226
+
// Unknown or unsupported collection
227
+
// Vote is still indexed in votes table, we just don't update denormalized counts
228
+
log.Printf("Vote subject has unsupported collection: %s (vote indexed, counts not updated)", collection)
229
+
if commitErr := tx.Commit(); commitErr != nil {
230
+
return fmt.Errorf("failed to commit transaction: %w", commitErr)
result, err := tx.ExecContext(ctx, updateQuery, vote.SubjectURI)
204
-
return fmt.Errorf("failed to update post counts: %w", err)
237
+
return fmt.Errorf("failed to update vote counts: %w", err)
rowsAffected, err := result.RowsAffected()
···
return fmt.Errorf("failed to check update result: %w", err)
212
-
// If post doesn't exist or is deleted, that's OK (vote still indexed)
213
-
// Future: We could validate post exists before indexing vote
245
+
// If subject doesn't exist or is deleted, that's OK (vote still indexed)
215
-
log.Printf("Warning: Post not found or deleted: %s (vote indexed anyway)", vote.SubjectURI)
247
+
log.Printf("Warning: Vote subject not found or deleted: %s (vote indexed anyway)", vote.SubjectURI)
···
264
-
// 2. Decrement post vote counts atomically
265
-
// Decrement upvote_count or downvote_count based on direction
266
-
// Also update score (use GREATEST to prevent negative counts)
296
+
// 2. Decrement vote counts on the subject (post or comment)
297
+
// Parse collection from subject URI to determine target table
298
+
collection := utils.ExtractCollectionFromURI(vote.SubjectURI)
268
-
if vote.Direction == "up" {
271
-
SET upvote_count = GREATEST(0, upvote_count - 1),
272
-
score = GREATEST(0, upvote_count - 1) - downvote_count
273
-
WHERE uri = $1 AND deleted_at IS NULL
278
-
SET downvote_count = GREATEST(0, downvote_count - 1),
279
-
score = upvote_count - GREATEST(0, downvote_count - 1)
280
-
WHERE uri = $1 AND deleted_at IS NULL
301
+
switch collection {
302
+
case "social.coves.community.post":
303
+
// Vote on post - update posts table
304
+
if vote.Direction == "up" {
307
+
SET upvote_count = GREATEST(0, upvote_count - 1),
308
+
score = GREATEST(0, upvote_count - 1) - downvote_count
309
+
WHERE uri = $1 AND deleted_at IS NULL
314
+
SET downvote_count = GREATEST(0, downvote_count - 1),
315
+
score = upvote_count - GREATEST(0, downvote_count - 1)
316
+
WHERE uri = $1 AND deleted_at IS NULL
320
+
case "social.coves.feed.comment":
321
+
// Vote on comment - update comments table
322
+
if vote.Direction == "up" {
325
+
SET upvote_count = GREATEST(0, upvote_count - 1),
326
+
score = GREATEST(0, upvote_count - 1) - downvote_count
327
+
WHERE uri = $1 AND deleted_at IS NULL
332
+
SET downvote_count = GREATEST(0, downvote_count - 1),
333
+
score = upvote_count - GREATEST(0, downvote_count - 1)
334
+
WHERE uri = $1 AND deleted_at IS NULL
339
+
// Unknown or unsupported collection
340
+
// Vote is still deleted, we just don't update denormalized counts
341
+
log.Printf("Vote subject has unsupported collection: %s (vote deleted, counts not updated)", collection)
342
+
if commitErr := tx.Commit(); commitErr != nil {
343
+
return fmt.Errorf("failed to commit transaction: %w", commitErr)
result, err = tx.ExecContext(ctx, updateQuery, vote.SubjectURI)
286
-
return fmt.Errorf("failed to update post counts: %w", err)
350
+
return fmt.Errorf("failed to update vote counts: %w", err)
rowsAffected, err = result.RowsAffected()
···
return fmt.Errorf("failed to check update result: %w", err)
294
-
// If post doesn't exist or is deleted, that's OK (vote still deleted)
358
+
// If subject doesn't exist or is deleted, that's OK (vote still deleted)
296
-
log.Printf("Warning: Post not found or deleted: %s (vote deleted anyway)", vote.SubjectURI)
360
+
log.Printf("Warning: Vote subject not found or deleted: %s (vote deleted anyway)", vote.SubjectURI)