Skip to content

Commit

Permalink
Add rkey parameter to createComment, createPost, and createVote funct…
Browse files Browse the repository at this point in the history
…ions; update related types and API calls
  • Loading branch information
WillCorrigan committed Dec 2, 2024
1 parent 4b9ac99 commit 3948817
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/frontpage/lib/api/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function createComment({
parent,
post,
content,
rkey,
});

invariant(cid, "Failed to create comment, rkey/cid missing");
Expand Down
1 change: 1 addition & 0 deletions packages/frontpage/lib/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function createPost({
const { cid } = await atproto.createPost({
title: title,
url: url,
rkey,
});

invariant(cid, "Failed to create comment, rkey/cid missing");
Expand Down
2 changes: 1 addition & 1 deletion packages/frontpage/lib/api/relayHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function handleVote({ op, repo, rkey }: HandlerInput) {

switch (hydratedRecord.subject.uri.collection) {
case atprotoPost.PostCollection:
const postVote = await dbVote.uncached_doesCommentVoteExist(repo, rkey);
const postVote = await dbVote.uncached_doesPostVoteExist(repo, rkey);
if (!postVote) {
const createdDbPostVote = await dbVote.createPostVote({
repo,
Expand Down
3 changes: 2 additions & 1 deletion packages/frontpage/lib/api/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function createVote({
}

const { cid } = await atproto.createVote({
rkey,
subjectRkey,
subjectCid,
subjectCollection,
Expand All @@ -70,7 +71,7 @@ export async function deleteVote({ rkey }: db.DeleteVoteInput) {
const user = await ensureUser();

try {
// await db.deleteVote({ authorDid: user.did, rkey });
await db.deleteVote({ authorDid: user.did, rkey });

await atproto.deleteVote(user.did, rkey);
} catch (e) {
Expand Down
9 changes: 8 additions & 1 deletion packages/frontpage/lib/data/atproto/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ export type CommentInput = {
parent?: { cid: string; rkey: string; authorDid: DID };
post: { cid: string; rkey: string; authorDid: DID };
content: string;
rkey: string;
};

export async function createComment({ parent, post, content }: CommentInput) {
export async function createComment({
parent,
post,
content,
rkey,
}: CommentInput) {
// Collapse newlines into a single \n\n and trim whitespace
const sanitizedContent = content.replace(/\n\n+/g, "\n\n").trim();
const record = {
Expand Down Expand Up @@ -64,6 +70,7 @@ export async function createComment({ parent, post, content }: CommentInput) {
const result = await atprotoCreateRecord({
record,
collection: CommentCollection,
rkey,
});

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/frontpage/lib/data/atproto/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export type Post = z.infer<typeof PostRecord>;
export type PostInput = {
title: string;
url: string;
rkey: string;
};

export async function createPost({ title, url }: PostInput) {
export async function createPost({ title, url, rkey }: PostInput) {
const record = { title, url, createdAt: new Date().toISOString() };
const parseResult = PostRecord.safeParse(record);
if (!parseResult.success) {
Expand All @@ -36,6 +37,7 @@ export async function createPost({ title, url }: PostInput) {
const result = await atprotoCreateRecord({
record,
collection: PostCollection,
rkey,
});

return {
Expand Down
3 changes: 3 additions & 0 deletions packages/frontpage/lib/data/atproto/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const CreateRecordResponse = z.object({
type CreateRecordInput = {
record: unknown;
collection: string;
rkey: string;
};

export async function atprotoCreateRecord({
record,
collection,
rkey,
}: CreateRecordInput) {
const user = await ensureUser();
const pdsUrl = new URL(user.pdsUrl);
Expand All @@ -32,6 +34,7 @@ export async function atprotoCreateRecord({
body: JSON.stringify({
repo: user.did,
collection,
rkey,
validate: false,
record: record,
}),
Expand Down
3 changes: 3 additions & 0 deletions packages/frontpage/lib/data/atproto/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export type VoteInput = {
subjectCid: string;
subjectCollection: string;
subjectAuthorDid: DID;
rkey: string;
};

export async function createVote({
rkey,
subjectRkey,
subjectCid,
subjectCollection,
Expand All @@ -61,6 +63,7 @@ export async function createVote({
const response = await atprotoCreateRecord({
collection: VoteCollection,
record: record,
rkey,
});

return {
Expand Down

0 comments on commit 3948817

Please sign in to comment.