Skip to content

Commit

Permalink
Change postNum incrementing into atomic operation; Edit db
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrezerino committed Sep 13, 2024
1 parent 49f1193 commit 709c902
Show file tree
Hide file tree
Showing 5 changed files with 25,145 additions and 13,629 deletions.
31 changes: 25 additions & 6 deletions app/api/posts/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getMongoDb as db } from '@/app/lib/mongodb';
import { newPostSchema } from '@/app/lib/newPostSchema';
import { NewPostType } from '@/app/lib/definitions';
import { NewPostType, CounterDocument } from '@/app/lib/definitions';
import {
ACCEPTED_IMAGE_TYPES,
MAX_FILE_SIZE, boards,
Expand Down Expand Up @@ -116,24 +116,43 @@ export const POST = async (req: NextRequest) => {
newPost.imageUrl = file?.size > 0 ? `${AWS_URL}/img/posts/${filename}` : '';
delete newPost.image;

// Save post to database
const result = await (await db()).collection('posts').insertOne(newPost);
// Increment counter value atomically
const getNextPostNum = async (): Promise<number> => {
const result = await (await db()).collection<CounterDocument>('counters').findOneAndUpdate(
{ _id: 'postNum' },
{ $inc: { seq_value: 1 } },
{ returnDocument: 'after', upsert: true }
);

if (result) {
return result.seq_value;
} else {
throw { message: 'Failed incrementing postNum counter', status: 500 };
}
};

// Get the postNum of the post we just inserted
const newPostNum = await fetchPostWithPostNum(result.insertedId);
const newPostNum = await getNextPostNum();

// Save post to database
const result = await (await db()).collection('posts').insertOne({
...newPost,
postNum: newPostNum
});

// Add the postNum to all recipients' reply arrays
const recipients = JSON.parse(formData.get('replyTo') as string);

if (recipients?.length > 0) {
await (await db()).collection('posts').updateMany(
await (await db()).collection<NewPostType>('posts').updateMany(
{ 'postNum': { $in: recipients } },
{
$push: { replies: newPostNum }
}
);
}



if (result.acknowledged && result.insertedId) {
return NextResponse.json('Created', { status: 201 });
}
Expand Down
9 changes: 7 additions & 2 deletions app/dashboard/[board]/[postNum]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ type PostParams = {
const Page = async ({ params }: PostParams) => {
const data: PostType = await getPost(params.board, params.postNum.toString());
if (Object.keys(data).length === 0) notFound();
if (!data.OP) redirect(`/dashboard/${data.board}/${data.replyTo[0]}`);

/*
If the post is not OP (thread starter), don't allow single post view,
redirect to parent thread instead
*/
if (!data.OP) redirect(`/dashboard/${data.board}/${data.thread}`);

return (
<Post post={data}/>
<Post post={data} />
);
};

Expand Down
7 changes: 6 additions & 1 deletion app/lib/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ export interface CatalogOPType extends Omit<PostType, 'replies' | 'replyTo' | 'O
export type ErrorWithStatusCode = {
message: string;
status: number
};
};

export interface CounterDocument {
_id: string;
seq_value: number;
}
Loading

0 comments on commit 709c902

Please sign in to comment.