Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Story voting #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 298 additions & 0 deletions app/(stories)/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,305 @@
"use server";

import { signOut } from "@/app/auth";
import z from "zod";
import { db, usersTable, storiesTable, votesTable, genVoteId } from "@/app/db";
import { auth } from "@/app/auth";
import { sql } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { unvoteRateLimit, voteRateLimit } from "@/lib/rate-limit";
import { redirect } from "next/navigation";

export async function signOutAction() {
await signOut();
return {};
}

const VoteActionSchema = z.object({
storyId: z.string(),
});

export type VoteActionData = {
error?:
| {
code: "INTERNAL_ERROR";
message: string;
}
| {
code: "VALIDATION_ERROR";
fieldErrors: {
[field: string]: string[];
};
}
| {
code: "RATE_LIMIT_ERROR";
message: string;
}
| {
code: "ALREADY_VOTED_ERROR";
message: string;
};
};

export async function voteAction(
formData: FormData
): Promise<VoteActionData | void> {
const session = await auth();

if (!session?.user?.id) {
redirect("/login");
}

const data = VoteActionSchema.safeParse({
storyId: formData.get("storyId"),
});

if (!data.success) {
return {
error: {
code: "VALIDATION_ERROR",
fieldErrors: data.error.flatten().fieldErrors,
},
};
}

const user = (
await db
.select()
.from(usersTable)
.where(sql`${usersTable.id} = ${session.user.id}`)
.limit(1)
)[0];

if (!user) {
return {
error: {
code: "INTERNAL_ERROR",
message: "User not found",
},
};
}

const rl = await voteRateLimit.limit(user.id);

if (!rl.success) {
return {
error: {
code: "RATE_LIMIT_ERROR",
message: "Too many votes. Try again later",
},
};
}

// TODO: transaction
// await db.transaction(async (tx) => {
const tx = db;
try {
const story = (
await tx
.select({
id: storiesTable.id,
username: storiesTable.username,
submitted_by: storiesTable.submitted_by,
vote_id: votesTable.id,
})
.from(storiesTable)
.where(sql`${storiesTable.id} = ${data.data.storyId}`)
.leftJoin(
votesTable,
sql`${storiesTable.id} = ${votesTable.story_id} AND ${votesTable.user_id} = ${user.id}`
)
.limit(1)
)[0];

if (!story) {
throw new Error("Story not found");
}

if (story.vote_id) {
return {
error: {
code: "ALREADY_VOTED_ERROR",
message: "You already voted for this story",
},
};
}

await Promise.all([
tx.insert(votesTable).values({
id: genVoteId(),
user_id: user.id,
story_id: story.id,
}),
tx
.update(storiesTable)
.set({
points: sql`${storiesTable.points} + 1`,
})
.where(sql`${storiesTable.id} = ${story.id}`),
story.submitted_by
? tx
.update(usersTable)
.set({
karma: sql`${usersTable.karma} + 1`,
})
.where(sql`${usersTable.id} = ${story.submitted_by}`)
: Promise.resolve(),
]);

// revalidate all data, including points for all stories
revalidatePath("/", "layout");

return {};
} catch (err) {
console.error(err);
return {
error: {
code: "INTERNAL_ERROR",
message: "Something went wrong",
},
};
}
}

const UnvoteActionSchema = z.object({
storyId: z.string(),
});

export type UnvoteActionData = {
error?:
| {
code: "INTERNAL_ERROR";
message: string;
}
| {
code: "VALIDATION_ERROR";
fieldErrors: {
[field: string]: string[];
};
}
| {
code: "RATE_LIMIT_ERROR";
message: string;
}
| {
code: "SELF_UNVOTE_ERROR";
message: string;
};
};

export async function unvoteAction(
formData: FormData
): Promise<UnvoteActionData | void> {
const session = await auth();

if (!session?.user?.id) {
redirect("/login");
}

const data = UnvoteActionSchema.safeParse({
storyId: formData.get("storyId"),
});

if (!data.success) {
return {
error: {
code: "VALIDATION_ERROR",
fieldErrors: data.error.flatten().fieldErrors,
},
};
}

const user = (
await db
.select()
.from(usersTable)
.where(sql`${usersTable.id} = ${session.user.id}`)
.limit(1)
)[0];

if (!user) {
return {
error: {
code: "INTERNAL_ERROR",
message: "User not found",
},
};
}

const rl = await unvoteRateLimit.limit(user.id);

if (!rl.success) {
return {
error: {
code: "RATE_LIMIT_ERROR",
message: "Too many unvotes. Try again later",
},
};
}

// TODO: transaction
// await db.transaction(async (tx) => {
const tx = db;
try {
const story = (
await tx
.select({
id: storiesTable.id,
username: storiesTable.username,
submitted_by: storiesTable.submitted_by,
vote_id: votesTable.id,
})
.from(storiesTable)
.where(sql`${storiesTable.id} = ${data.data.storyId}`)
.leftJoin(
votesTable,
sql`${storiesTable.id} = ${votesTable.story_id} AND ${votesTable.user_id} = ${user.id}`
)
.limit(1)
)[0];

if (!story) {
throw new Error("Story not found");
}

if (story.submitted_by === user.id) {
return {
error: {
code: "SELF_UNVOTE_ERROR",
message: "You can't unvote your own story",
},
};
}

await Promise.all([
tx.delete(votesTable).where(sql`${votesTable.id} = ${story.vote_id}`),
tx
.update(storiesTable)
.set({
points: sql`${storiesTable.points} - 1`,
})
.where(sql`${storiesTable.id} = ${story.id}`),
story.submitted_by
? tx
.update(usersTable)
.set({
karma: sql`${usersTable.karma} - 1`,
})
.where(sql`${usersTable.id} = ${story.submitted_by}`)
: Promise.resolve(),
]);

// revalidate all data, including points for all stories
revalidatePath("/", "layout");

return {};
} catch (err) {
console.error(err);
return {
error: {
code: "INTERNAL_ERROR",
message: "Something went wrong",
},
};
}
}
Loading