diff --git a/packages/frontpage/lib/api/comment.ts b/packages/frontpage/lib/api/comment.ts index 392e3b1..09530ae 100644 --- a/packages/frontpage/lib/api/comment.ts +++ b/packages/frontpage/lib/api/comment.ts @@ -42,7 +42,7 @@ export async function createComment({ invariant(cid, "Failed to create comment, rkey/cid missing"); - db.updateComment({ authorDid: user.did, rkey, cid }); + await db.updateComment({ authorDid: user.did, rkey, cid }); const didToNotify = parent ? parent.authorDid : post.authorDid; @@ -63,8 +63,9 @@ export async function deleteComment({ rkey }: db.DeleteCommentInput) { const user = await ensureUser(); try { - await atproto.deleteComment(user.did, rkey); + console.log("deleteComment", rkey); await db.deleteComment({ authorDid: user.did, rkey }); + await atproto.deleteComment(user.did, rkey); } catch (e) { throw new DataLayerError(`Failed to delete comment: ${e}`); } diff --git a/packages/frontpage/lib/api/post.ts b/packages/frontpage/lib/api/post.ts index 63b1482..35d6e1a 100644 --- a/packages/frontpage/lib/api/post.ts +++ b/packages/frontpage/lib/api/post.ts @@ -36,7 +36,7 @@ export async function createPost({ invariant(cid, "Failed to create comment, rkey/cid missing"); - db.updatePost({ authorDid: user.did, rkey, cid }); + await db.updatePost({ authorDid: user.did, rkey, cid }); const bskyProfile = await getBlueskyProfile(user.did); @@ -75,9 +75,8 @@ export async function deletePost({ rkey }: db.DeletePostInput) { const user = await ensureUser(); try { - await atproto.deletePost(user.did, rkey); - await db.deletePost({ authorDid: user.did, rkey }); + await atproto.deletePost(user.did, rkey); } catch (e) { throw new DataLayerError(`Failed to delete post: ${e}`); } diff --git a/packages/frontpage/lib/api/relayHandler.ts b/packages/frontpage/lib/api/relayHandler.ts index 832907d..2b0da11 100644 --- a/packages/frontpage/lib/api/relayHandler.ts +++ b/packages/frontpage/lib/api/relayHandler.ts @@ -139,33 +139,46 @@ export async function handleVote({ op, repo, rkey }: HandlerInput) { rkey, }); + invariant(hydratedRecord, "atproto vote record not found"); + switch (hydratedRecord.subject.uri.collection) { case atprotoPost.PostCollection: - const createdDbPostVote = await dbVote.createPostVote({ - repo, - rkey, - cid: hydratedRecord.cid, - subjectRkey: hydratedRecord.subject.uri.rkey, - subjectAuthorDid: hydratedRecord.subject.uri.authority as DID, - }); - - if (!createdDbPostVote) { - throw new Error("Failed to insert post vote from relay in database"); + const postVote = await dbVote.uncached_doesCommentVoteExist(repo, rkey); + if (!postVote) { + const createdDbPostVote = await dbVote.createPostVote({ + repo, + rkey, + cid: hydratedRecord.cid, + subjectRkey: hydratedRecord.subject.uri.rkey, + subjectAuthorDid: hydratedRecord.subject.uri.authority as DID, + }); + + if (!createdDbPostVote) { + throw new Error( + "Failed to insert post vote from relay in database", + ); + } } break; case atprotoComment.CommentCollection: - const createdDbCommentVote = await dbVote.createCommentVote({ + const commentVote = await dbVote.uncached_doesCommentVoteExist( repo, rkey, - cid: hydratedRecord.cid, - subjectRkey: hydratedRecord.subject.uri.rkey, - subjectAuthorDid: hydratedRecord.subject.uri.authority as DID, - }); - - if (!createdDbCommentVote) { - throw new Error( - "Failed to insert comment vote from relay in database", - ); + ); + if (!commentVote) { + const createdDbCommentVote = await dbVote.createCommentVote({ + repo, + rkey, + cid: hydratedRecord.cid, + subjectRkey: hydratedRecord.subject.uri.rkey, + subjectAuthorDid: hydratedRecord.subject.uri.authority as DID, + }); + + if (!createdDbCommentVote) { + throw new Error( + "Failed to insert comment vote from relay in database", + ); + } } break; default: @@ -174,6 +187,7 @@ export async function handleVote({ op, repo, rkey }: HandlerInput) { ); } } else if (op.action === "delete") { + console.log("deleting vote", rkey); await dbVote.deleteVote({ authorDid: repo, rkey }); } } diff --git a/packages/frontpage/lib/api/vote.ts b/packages/frontpage/lib/api/vote.ts index d53981e..daedb24 100644 --- a/packages/frontpage/lib/api/vote.ts +++ b/packages/frontpage/lib/api/vote.ts @@ -56,9 +56,9 @@ export async function createVote({ invariant(cid, "Failed to create vote, cid missing"); if (subjectCollection == PostCollection) { - db.updatePostVote({ authorDid: user.did, rkey, cid }); + await db.updatePostVote({ authorDid: user.did, rkey, cid }); } else if (subjectCollection == CommentCollection) { - db.updateCommentVote({ authorDid: user.did, rkey, cid }); + await db.updateCommentVote({ authorDid: user.did, rkey, cid }); } } catch (e) { db.deleteVote({ authorDid: user.did, rkey }); @@ -70,9 +70,9 @@ export async function deleteVote({ rkey }: db.DeleteVoteInput) { const user = await ensureUser(); try { - await atproto.deleteVote(user.did, rkey); + // await db.deleteVote({ authorDid: user.did, rkey }); - await db.deleteVote({ authorDid: user.did, rkey }); + await atproto.deleteVote(user.did, rkey); } catch (e) { throw new DataLayerError(`Failed to delete vote: ${e}`); }