Skip to content

Commit 1de190a

Browse files
committed
Limit polls page results and expire stale polls
1 parent e245da9 commit 1de190a

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

Diff for: app/actions.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { kv } from "@vercel/kv";
44
import { revalidatePath } from "next/cache";
5-
import {Poll} from "./types";
5+
import {Poll, POLL_EXPIRY} from "./types";
66
import {redirect} from "next/navigation";
77

88
export async function savePoll(poll: Poll, formData: FormData) {
@@ -16,6 +16,7 @@ export async function savePoll(poll: Poll, formData: FormData) {
1616
option4: formData.get("option4") as string,
1717
}
1818
await kv.hset(`poll:${poll.id}`, poll);
19+
await kv.expire(`poll:${poll.id}`, POLL_EXPIRY);
1920
await kv.zadd("polls_by_date", {
2021
score: Number(poll.created_at),
2122
member: newPoll.id,

Diff for: app/polls/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const SEVEN_DAYS_IN_MS = 1000 * 60 * 60 * 24 * 7;
66

77
async function getPolls() {
88
try {
9-
let pollIds = await kv.zrange("polls_by_date", Date.now(), Date.now() - SEVEN_DAYS_IN_MS, {byScore: true, rev: true});
9+
let pollIds = await kv.zrange("polls_by_date", Date.now(), Date.now() - SEVEN_DAYS_IN_MS, {byScore: true, rev: true, count: 100, offset: 0});
1010

1111
if (!pollIds.length) {
1212
return [];

Diff for: app/types.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export type Poll = {
1212
created_at: number;
1313
};
1414

15-
15+
export const POLL_EXPIRY = 60 * 60 * 24 * 180; // Expire polls after 3 months

Diff for: pages/api/vote.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NextApiRequest, NextApiResponse } from 'next';
2-
import {Poll} from "@/app/types";
2+
import {Poll, POLL_EXPIRY} from "@/app/types";
33
import {kv} from "@vercel/kv";
44
import {getSSLHubRpcClient, Message} from "@farcaster/hub-nodejs";
55

@@ -58,6 +58,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
5858
let multi = kv.multi();
5959
multi.hincrby(`poll:${pollId}`, `votes${buttonId}`, 1);
6060
multi.sadd(`poll:${pollId}:voted`, fid);
61+
multi.expire(`poll:${pollId}`, POLL_EXPIRY);
62+
multi.expire(`poll:${pollId}:voted`, POLL_EXPIRY);
6163
await multi.exec();
6264
}
6365

0 commit comments

Comments
 (0)