Skip to content

Commit

Permalink
Refactor database update calls in comment, post, and vote APIs to ens…
Browse files Browse the repository at this point in the history
…ure proper async handling; add logging for delete operations
  • Loading branch information
WillCorrigan committed Dec 2, 2024
1 parent 8a58a30 commit ddf3423
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
5 changes: 3 additions & 2 deletions packages/frontpage/lib/api/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}`);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/frontpage/lib/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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}`);
}
Expand Down
54 changes: 34 additions & 20 deletions packages/frontpage/lib/api/relayHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 });
}
}
8 changes: 4 additions & 4 deletions packages/frontpage/lib/api/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Check failure on line 64 in packages/frontpage/lib/api/vote.ts

View workflow job for this annotation

GitHub Actions / lint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
Expand All @@ -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}`);
}
Expand Down

0 comments on commit ddf3423

Please sign in to comment.