Skip to content

Commit

Permalink
Refactor vote-related API calls to use a unified subject object; upda…
Browse files Browse the repository at this point in the history
…te createVote and related functions for improved readability and maintainability
  • Loading branch information
WillCorrigan committed Dec 2, 2024
1 parent 3948817 commit 4c72496
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 64 deletions.
10 changes: 6 additions & 4 deletions packages/frontpage/app/(app)/_components/post-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export async function PostCard({
"use server";
await ensureUser();
await createVote({
subjectRkey: rkey,
subjectCid: cid,
subjectAuthorDid: author,
subjectCollection: PostCollection,
subject: {
rkey,
cid,
authorDid: author,
collection: PostCollection,
},
});
}}
unvoteAction={async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ export async function commentVoteAction(input: {
}) {
await ensureUser();
await createVote({
subjectAuthorDid: input.authorDid,
subjectCid: input.cid,
subjectRkey: input.rkey,
subjectCollection: CommentCollection,
subject: {
rkey: input.rkey,
cid: input.cid,
authorDid: input.authorDid,
collection: CommentCollection,
},
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/frontpage/lib/api/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createNotification } from "../data/db/notification";
import { invariant } from "../utils";
import { TID } from "@atproto/common-web";

export type ApiCreateCommentInput = atproto.CommentInput & {
export type ApiCreateCommentInput = Omit<atproto.CommentInput, "rkey"> & {
repo: DID;
};

Expand Down Expand Up @@ -55,7 +55,7 @@ export async function createComment({
});
}
} catch (e) {
db.deleteComment({ authorDid: user.did, rkey });
await db.deleteComment({ authorDid: user.did, rkey });
throw new DataLayerError(`Failed to create comment: ${e}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/frontpage/lib/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function createPost({

return { rkey, cid };
} catch (e) {
db.deletePost({ authorDid: user.did, rkey });
await db.deletePost({ authorDid: user.did, rkey });
throw new DataLayerError(`Failed to create post: ${e}`);
}
}
Expand Down
12 changes: 8 additions & 4 deletions packages/frontpage/lib/api/relayHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ export async function handleVote({ op, repo, rkey }: HandlerInput) {
repo,
rkey,
cid: hydratedRecord.cid,
subjectRkey: hydratedRecord.subject.uri.rkey,
subjectAuthorDid: hydratedRecord.subject.uri.authority as DID,
subject: {
rkey: hydratedRecord.subject.uri.rkey,
authorDid: hydratedRecord.subject.uri.authority as DID,
},
});

if (!createdDbPostVote) {
Expand All @@ -170,8 +172,10 @@ export async function handleVote({ op, repo, rkey }: HandlerInput) {
repo,
rkey,
cid: hydratedRecord.cid,
subjectRkey: hydratedRecord.subject.uri.rkey,
subjectAuthorDid: hydratedRecord.subject.uri.authority as DID,
subject: {
rkey: hydratedRecord.subject.uri.rkey,
authorDid: hydratedRecord.subject.uri.authority as DID,
},
});

if (!createdDbCommentVote) {
Expand Down
44 changes: 21 additions & 23 deletions packages/frontpage/lib/api/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,57 @@ import { invariant } from "../utils";
import { TID } from "@atproto/common-web";

export type ApiCreateVoteInput = {
subjectRkey: string;
subjectCid: string;
subjectAuthorDid: DID;
subjectCollection: typeof PostCollection | typeof CommentCollection;
subject: {
rkey: string;
cid: string;
authorDid: DID;
collection: typeof PostCollection | typeof CommentCollection;
};
};

export async function createVote({
subjectRkey,
subjectAuthorDid,
subjectCollection,
subjectCid,
}: ApiCreateVoteInput) {
export async function createVote({ subject }: ApiCreateVoteInput) {
const user = await ensureUser();

const rkey = TID.next().toString();
try {
if (subjectCollection == PostCollection) {
if (subject.collection == PostCollection) {
const dbCreatedVote = await db.createPostVote({
repo: user.did,
rkey,
subjectRkey,
subjectAuthorDid,
subject: {
rkey: subject.rkey,
authorDid: subject.authorDid,
},
});

invariant(dbCreatedVote, "Failed to insert post vote in database");
} else if (subjectCollection == CommentCollection) {
} else if (subject.collection == CommentCollection) {
const dbCreatedVote = await db.createCommentVote({
repo: user.did,
rkey,
subjectRkey,
subjectAuthorDid,
subject: {
rkey: subject.rkey,
authorDid: subject.authorDid,
},
});

invariant(dbCreatedVote, "Failed to insert post vote in database");
}

const { cid } = await atproto.createVote({
rkey,
subjectRkey,
subjectCid,
subjectCollection,
subjectAuthorDid,
subject,
});

invariant(cid, "Failed to create vote, cid missing");

if (subjectCollection == PostCollection) {
if (subject.collection == PostCollection) {
await db.updatePostVote({ authorDid: user.did, rkey, cid });
} else if (subjectCollection == CommentCollection) {
} else if (subject.collection == CommentCollection) {
await db.updateCommentVote({ authorDid: user.did, rkey, cid });
}
} catch (e) {
db.deleteVote({ authorDid: user.did, rkey });
await db.deleteVote({ authorDid: user.did, rkey });
throw new DataLayerError(`Failed to create post vote: ${e}`);
}
}
Expand Down
22 changes: 9 additions & 13 deletions packages/frontpage/lib/data/atproto/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,22 @@ export const VoteRecord = z.object({
export type Vote = z.infer<typeof VoteRecord>;

export type VoteInput = {
subjectRkey: string;
subjectCid: string;
subjectCollection: string;
subjectAuthorDid: DID;
rkey: string;
subject: {
rkey: string;
cid: string;
authorDid: DID;
collection: typeof PostCollection | typeof CommentCollection;
};
};

export async function createVote({
rkey,
subjectRkey,
subjectCid,
subjectCollection,
subjectAuthorDid,
}: VoteInput) {
const uri = `at://${subjectAuthorDid}/${subjectCollection}/${subjectRkey}`;
export async function createVote({ rkey, subject }: VoteInput) {
const uri = `at://${subject.authorDid}/${subject.collection}/${subject.rkey}`;

const record = {
createdAt: new Date().toISOString(),
subject: {
cid: subjectCid,
cid: subject.cid,
uri,
},
};
Expand Down
1 change: 0 additions & 1 deletion packages/frontpage/lib/data/db/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { db } from "@/lib/db";
import { eq, sql, desc, and, isNull, or, InferSelectModel } from "drizzle-orm";
import * as schema from "@/lib/schema";
import { getUser, isAdmin } from "../user";
import * as atprotoPost from "../atproto/post";
import { DID } from "../atproto/did";
import { newPostAggregateTrigger } from "./triggers";

Expand Down
24 changes: 12 additions & 12 deletions packages/frontpage/lib/data/db/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ export type CreateVoteInput = {
repo: DID;
rkey: string;
cid?: string;
subjectRkey: string;
subjectAuthorDid: DID;
subject: {
rkey: string;
authorDid: DID;
};
};

export const createPostVote = async ({
repo,
rkey,
cid,
subjectRkey,
subjectAuthorDid,
subject,
}: CreateVoteInput) => {
return await db.transaction(async (tx) => {
const post = (
Expand All @@ -106,13 +107,13 @@ export const createPostVote = async ({
.from(schema.Post)
.where(
and(
eq(schema.Post.rkey, subjectRkey),
eq(schema.Post.authorDid, subjectAuthorDid),
eq(schema.Post.rkey, subject.rkey),
eq(schema.Post.authorDid, subject.authorDid),
),
)
)[0];

invariant(post, `Post not found with rkey: ${subjectRkey}`);
invariant(post, `Post not found with rkey: ${subject.rkey}`);

if (post.authorDid === repo) {
throw new Error(`[naughty] Cannot vote on own content ${repo}`);
Expand Down Expand Up @@ -142,8 +143,7 @@ export async function createCommentVote({
repo,
rkey,
cid,
subjectRkey,
subjectAuthorDid,
subject,
}: CreateVoteInput) {
return await db.transaction(async (tx) => {
const comment = (
Expand All @@ -152,13 +152,13 @@ export async function createCommentVote({
.from(schema.Comment)
.where(
and(
eq(schema.Comment.rkey, subjectRkey),
eq(schema.Comment.authorDid, subjectAuthorDid),
eq(schema.Comment.rkey, subject.rkey),
eq(schema.Comment.authorDid, subject.authorDid),
),
)
)[0];

invariant(comment, `Comment not found with rkey: ${subjectRkey}`);
invariant(comment, `Comment not found with rkey: ${subject.rkey}`);

if (comment.authorDid === repo) {
throw new Error(`[naughty] Cannot vote on own content ${repo}`);
Expand Down

0 comments on commit 4c72496

Please sign in to comment.