diff --git a/packages/frontpage/app/(app)/_components/post-card.tsx b/packages/frontpage/app/(app)/_components/post-card.tsx index 0a5401b..7a81d2f 100644 --- a/packages/frontpage/app/(app)/_components/post-card.tsx +++ b/packages/frontpage/app/(app)/_components/post-card.tsx @@ -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 () => { diff --git a/packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx b/packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx index 19ae948..259be68 100644 --- a/packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx +++ b/packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/actions.tsx @@ -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, + }, }); } diff --git a/packages/frontpage/lib/api/comment.ts b/packages/frontpage/lib/api/comment.ts index adb259b..a88cf3a 100644 --- a/packages/frontpage/lib/api/comment.ts +++ b/packages/frontpage/lib/api/comment.ts @@ -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 & { repo: DID; }; @@ -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}`); } } diff --git a/packages/frontpage/lib/api/post.ts b/packages/frontpage/lib/api/post.ts index 8335359..e58f05f 100644 --- a/packages/frontpage/lib/api/post.ts +++ b/packages/frontpage/lib/api/post.ts @@ -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}`); } } diff --git a/packages/frontpage/lib/api/relayHandler.ts b/packages/frontpage/lib/api/relayHandler.ts index bbcd4a3..c3f2733 100644 --- a/packages/frontpage/lib/api/relayHandler.ts +++ b/packages/frontpage/lib/api/relayHandler.ts @@ -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) { @@ -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) { diff --git a/packages/frontpage/lib/api/vote.ts b/packages/frontpage/lib/api/vote.ts index d3d402a..f5854ae 100644 --- a/packages/frontpage/lib/api/vote.ts +++ b/packages/frontpage/lib/api/vote.ts @@ -10,37 +10,38 @@ 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"); @@ -48,21 +49,18 @@ export async function createVote({ 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}`); } } diff --git a/packages/frontpage/lib/data/atproto/vote.ts b/packages/frontpage/lib/data/atproto/vote.ts index 68027a6..685e8e7 100644 --- a/packages/frontpage/lib/data/atproto/vote.ts +++ b/packages/frontpage/lib/data/atproto/vote.ts @@ -29,26 +29,22 @@ export const VoteRecord = z.object({ export type Vote = z.infer; 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, }, }; diff --git a/packages/frontpage/lib/data/db/post.ts b/packages/frontpage/lib/data/db/post.ts index 395aa36..4822933 100644 --- a/packages/frontpage/lib/data/db/post.ts +++ b/packages/frontpage/lib/data/db/post.ts @@ -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"; diff --git a/packages/frontpage/lib/data/db/vote.ts b/packages/frontpage/lib/data/db/vote.ts index 29da743..b3811f2 100644 --- a/packages/frontpage/lib/data/db/vote.ts +++ b/packages/frontpage/lib/data/db/vote.ts @@ -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 = ( @@ -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}`); @@ -142,8 +143,7 @@ export async function createCommentVote({ repo, rkey, cid, - subjectRkey, - subjectAuthorDid, + subject, }: CreateVoteInput) { return await db.transaction(async (tx) => { const comment = ( @@ -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}`);