···
+
"Coves/internal/atproto/utils"
"Coves/internal/core/users"
"Coves/internal/core/votes"
···
return fmt.Errorf("failed to insert vote: %w", err)
+
// 2. Update vote counts on the subject (post or comment)
+
// Parse collection from subject URI to determine target table
+
collection := utils.ExtractCollectionFromURI(vote.SubjectURI)
+
case "social.coves.community.post":
+
// Vote on post - update posts table
+
if vote.Direction == "up" {
+
SET upvote_count = upvote_count + 1,
+
score = upvote_count + 1 - downvote_count
+
WHERE uri = $1 AND deleted_at IS NULL
+
SET downvote_count = downvote_count + 1,
+
score = upvote_count - (downvote_count + 1)
+
WHERE uri = $1 AND deleted_at IS NULL
+
case "social.coves.feed.comment":
+
// Vote on comment - update comments table
+
if vote.Direction == "up" {
+
SET upvote_count = upvote_count + 1,
+
score = upvote_count + 1 - downvote_count
+
WHERE uri = $1 AND deleted_at IS NULL
+
SET downvote_count = downvote_count + 1,
+
score = upvote_count - (downvote_count + 1)
+
WHERE uri = $1 AND deleted_at IS NULL
+
// Unknown or unsupported collection
+
// Vote is still indexed in votes table, we just don't update denormalized counts
+
log.Printf("Vote subject has unsupported collection: %s (vote indexed, counts not updated)", collection)
+
if commitErr := tx.Commit(); commitErr != nil {
+
return fmt.Errorf("failed to commit transaction: %w", commitErr)
result, err := tx.ExecContext(ctx, updateQuery, vote.SubjectURI)
+
return fmt.Errorf("failed to update vote counts: %w", err)
rowsAffected, err := result.RowsAffected()
···
return fmt.Errorf("failed to check update result: %w", err)
+
// If subject doesn't exist or is deleted, that's OK (vote still indexed)
+
log.Printf("Warning: Vote subject not found or deleted: %s (vote indexed anyway)", vote.SubjectURI)
···
+
// 2. Decrement vote counts on the subject (post or comment)
+
// Parse collection from subject URI to determine target table
+
collection := utils.ExtractCollectionFromURI(vote.SubjectURI)
+
case "social.coves.community.post":
+
// Vote on post - update posts table
+
if vote.Direction == "up" {
+
SET upvote_count = GREATEST(0, upvote_count - 1),
+
score = GREATEST(0, upvote_count - 1) - downvote_count
+
WHERE uri = $1 AND deleted_at IS NULL
+
SET downvote_count = GREATEST(0, downvote_count - 1),
+
score = upvote_count - GREATEST(0, downvote_count - 1)
+
WHERE uri = $1 AND deleted_at IS NULL
+
case "social.coves.feed.comment":
+
// Vote on comment - update comments table
+
if vote.Direction == "up" {
+
SET upvote_count = GREATEST(0, upvote_count - 1),
+
score = GREATEST(0, upvote_count - 1) - downvote_count
+
WHERE uri = $1 AND deleted_at IS NULL
+
SET downvote_count = GREATEST(0, downvote_count - 1),
+
score = upvote_count - GREATEST(0, downvote_count - 1)
+
WHERE uri = $1 AND deleted_at IS NULL
+
// Unknown or unsupported collection
+
// Vote is still deleted, we just don't update denormalized counts
+
log.Printf("Vote subject has unsupported collection: %s (vote deleted, counts not updated)", collection)
+
if commitErr := tx.Commit(); commitErr != nil {
+
return fmt.Errorf("failed to commit transaction: %w", commitErr)
result, err = tx.ExecContext(ctx, updateQuery, vote.SubjectURI)
+
return fmt.Errorf("failed to update vote counts: %w", err)
rowsAffected, err = result.RowsAffected()
···
return fmt.Errorf("failed to check update result: %w", err)
+
// If subject doesn't exist or is deleted, that's OK (vote still deleted)
+
log.Printf("Warning: Vote subject not found or deleted: %s (vote deleted anyway)", vote.SubjectURI)