Spaces:
Running
Running
Add idea merge moderation, owner/admin delete, and submit filters
Browse files- app/api/ideas/[id]/route.ts +50 -0
- app/api/ideas/route.ts +93 -0
- app/ideas/[id]/page.tsx +10 -0
- components/IdeaDetailClient.tsx +43 -0
- components/SubmitForm.tsx +6 -1
- lib/content-moderation.ts +24 -0
- lib/idea-dedup.ts +76 -0
- lib/permissions.ts +26 -0
- prisma/schema.prisma +22 -0
app/api/ideas/[id]/route.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { prisma } from "@/lib/prisma";
|
|
| 3 |
import { mapIdea, mapTask } from "@/lib/db-mappers";
|
| 4 |
import { getAnonId } from "@/lib/identity";
|
| 5 |
import { auth } from "@/auth";
|
|
|
|
| 6 |
|
| 7 |
export async function GET(_: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
| 8 |
const session = await auth();
|
|
@@ -31,6 +32,9 @@ export async function GET(_: NextRequest, { params }: { params: Promise<{ id: st
|
|
| 31 |
},
|
| 32 |
orderBy: { createdAt: "desc" },
|
| 33 |
},
|
|
|
|
|
|
|
|
|
|
| 34 |
},
|
| 35 |
});
|
| 36 |
|
|
@@ -50,6 +54,14 @@ export async function GET(_: NextRequest, { params }: { params: Promise<{ id: st
|
|
| 50 |
? idea.userVotes.some((vote) => vote.userId === userId)
|
| 51 |
: idea.votes.some((vote) => vote.anonId === anonId);
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return NextResponse.json({
|
| 54 |
idea: {
|
| 55 |
...mapIdea(idea),
|
|
@@ -59,6 +71,9 @@ export async function GET(_: NextRequest, { params }: { params: Promise<{ id: st
|
|
| 59 |
viewerAnonId: anonId,
|
| 60 |
viewerUserId: userId,
|
| 61 |
canApproveSolutions: Boolean(userId ? idea.createdByUserId === userId : !idea.createdByUserId && idea.createdByAnonId === anonId),
|
|
|
|
|
|
|
|
|
|
| 62 |
isVoted,
|
| 63 |
idea_working_count: idea.workVotes.length,
|
| 64 |
idea_working: userId ? idea.workVotes.some((vote) => vote.userId === userId) : false,
|
|
@@ -86,3 +101,38 @@ export async function GET(_: NextRequest, { params }: { params: Promise<{ id: st
|
|
| 86 |
},
|
| 87 |
});
|
| 88 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import { mapIdea, mapTask } from "@/lib/db-mappers";
|
| 4 |
import { getAnonId } from "@/lib/identity";
|
| 5 |
import { auth } from "@/auth";
|
| 6 |
+
import { canDeleteIdea, isAdminEmail } from "@/lib/permissions";
|
| 7 |
|
| 8 |
export async function GET(_: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
| 9 |
const session = await auth();
|
|
|
|
| 32 |
},
|
| 33 |
orderBy: { createdAt: "desc" },
|
| 34 |
},
|
| 35 |
+
merges: {
|
| 36 |
+
select: { id: true },
|
| 37 |
+
},
|
| 38 |
},
|
| 39 |
});
|
| 40 |
|
|
|
|
| 54 |
? idea.userVotes.some((vote) => vote.userId === userId)
|
| 55 |
: idea.votes.some((vote) => vote.anonId === anonId);
|
| 56 |
|
| 57 |
+
const canDelete = canDeleteIdea({
|
| 58 |
+
ideaOwnerUserId: idea.createdByUserId,
|
| 59 |
+
ideaOwnerAnonId: idea.createdByAnonId,
|
| 60 |
+
actorUserId: userId,
|
| 61 |
+
actorAnonId: anonId || null,
|
| 62 |
+
actorEmail: session?.user?.email,
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
return NextResponse.json({
|
| 66 |
idea: {
|
| 67 |
...mapIdea(idea),
|
|
|
|
| 71 |
viewerAnonId: anonId,
|
| 72 |
viewerUserId: userId,
|
| 73 |
canApproveSolutions: Boolean(userId ? idea.createdByUserId === userId : !idea.createdByUserId && idea.createdByAnonId === anonId),
|
| 74 |
+
canDelete,
|
| 75 |
+
isAdmin: isAdminEmail(session?.user?.email),
|
| 76 |
+
merged_submission_count: idea.merges.length,
|
| 77 |
isVoted,
|
| 78 |
idea_working_count: idea.workVotes.length,
|
| 79 |
idea_working: userId ? idea.workVotes.some((vote) => vote.userId === userId) : false,
|
|
|
|
| 101 |
},
|
| 102 |
});
|
| 103 |
}
|
| 104 |
+
|
| 105 |
+
export async function DELETE(_: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
| 106 |
+
const session = await auth();
|
| 107 |
+
const userId = session?.user?.id ?? null;
|
| 108 |
+
const anonId = await getAnonId();
|
| 109 |
+
const { id } = await params;
|
| 110 |
+
|
| 111 |
+
const idea = await prisma.idea.findUnique({
|
| 112 |
+
where: { id },
|
| 113 |
+
select: {
|
| 114 |
+
id: true,
|
| 115 |
+
createdByUserId: true,
|
| 116 |
+
createdByAnonId: true,
|
| 117 |
+
},
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
if (!idea) {
|
| 121 |
+
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
const allowed = canDeleteIdea({
|
| 125 |
+
ideaOwnerUserId: idea.createdByUserId,
|
| 126 |
+
ideaOwnerAnonId: idea.createdByAnonId,
|
| 127 |
+
actorUserId: userId,
|
| 128 |
+
actorAnonId: anonId || null,
|
| 129 |
+
actorEmail: session?.user?.email,
|
| 130 |
+
});
|
| 131 |
+
|
| 132 |
+
if (!allowed) {
|
| 133 |
+
return NextResponse.json({ error: "Only submitter or admin can delete this idea" }, { status: 403 });
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
await prisma.idea.delete({ where: { id } });
|
| 137 |
+
return NextResponse.json({ ok: true });
|
| 138 |
+
}
|
app/api/ideas/route.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { getAnonId, getClientIp } from "@/lib/identity";
|
|
| 6 |
import { mapIdea, mapTask } from "@/lib/db-mappers";
|
| 7 |
import { scoreHot } from "@/lib/hot-score";
|
| 8 |
import { auth } from "@/auth";
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const createIdeaSchema = z.object({
|
| 11 |
raw_input_text: z.string().min(20).max(3000),
|
|
@@ -46,6 +48,25 @@ async function checkRateLimit(rateKey: string, ipAddress: string): Promise<boole
|
|
| 46 |
return true;
|
| 47 |
}
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
export async function GET(request: NextRequest) {
|
| 50 |
const sortParam = request.nextUrl.searchParams.get("sort");
|
| 51 |
const sort = sortParam === "new" ? "new" : "hot";
|
|
@@ -139,6 +160,78 @@ export async function POST(request: NextRequest) {
|
|
| 139 |
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
| 140 |
}
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
try {
|
| 143 |
const spec = await generateSpec(parsed.data);
|
| 144 |
|
|
|
|
| 6 |
import { mapIdea, mapTask } from "@/lib/db-mappers";
|
| 7 |
import { scoreHot } from "@/lib/hot-score";
|
| 8 |
import { auth } from "@/auth";
|
| 9 |
+
import { findBlockedContent } from "@/lib/content-moderation";
|
| 10 |
+
import { detectMergeTarget } from "@/lib/idea-dedup";
|
| 11 |
|
| 12 |
const createIdeaSchema = z.object({
|
| 13 |
raw_input_text: z.string().min(20).max(3000),
|
|
|
|
| 48 |
return true;
|
| 49 |
}
|
| 50 |
|
| 51 |
+
async function applyMergeSupportVote(ideaId: string, userId: string | null, anonId: string | null) {
|
| 52 |
+
if (userId) {
|
| 53 |
+
const existing = await prisma.ideaUserVote.findUnique({ where: { ideaId_userId: { ideaId, userId } } });
|
| 54 |
+
if (existing) return;
|
| 55 |
+
|
| 56 |
+
await prisma.ideaUserVote.create({ data: { ideaId, userId } });
|
| 57 |
+
await prisma.idea.update({ where: { id: ideaId }, data: { upvotesCount: { increment: 1 } } });
|
| 58 |
+
return;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
if (anonId) {
|
| 62 |
+
const existing = await prisma.vote.findUnique({ where: { ideaId_anonId: { ideaId, anonId } } });
|
| 63 |
+
if (existing) return;
|
| 64 |
+
|
| 65 |
+
await prisma.vote.create({ data: { ideaId, anonId } });
|
| 66 |
+
await prisma.idea.update({ where: { id: ideaId }, data: { upvotesCount: { increment: 1 } } });
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
export async function GET(request: NextRequest) {
|
| 71 |
const sortParam = request.nextUrl.searchParams.get("sort");
|
| 72 |
const sort = sortParam === "new" ? "new" : "hot";
|
|
|
|
| 160 |
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
| 161 |
}
|
| 162 |
|
| 163 |
+
const moderation = findBlockedContent(parsed.data.raw_input_text, parsed.data.target_users, parsed.data.constraints);
|
| 164 |
+
if (moderation.blocked) {
|
| 165 |
+
return NextResponse.json(
|
| 166 |
+
{
|
| 167 |
+
error: "Submission blocked: obvious offensive or inappropriate language detected. Please revise and submit again.",
|
| 168 |
+
labels: moderation.labels,
|
| 169 |
+
},
|
| 170 |
+
{ status: 400 }
|
| 171 |
+
);
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
const dedupInput = [parsed.data.raw_input_text, parsed.data.target_users, parsed.data.platform, parsed.data.constraints]
|
| 175 |
+
.filter((value): value is string => Boolean(value))
|
| 176 |
+
.join("\n");
|
| 177 |
+
|
| 178 |
+
const candidates = await prisma.idea.findMany({
|
| 179 |
+
orderBy: { createdAt: "desc" },
|
| 180 |
+
take: 200,
|
| 181 |
+
select: {
|
| 182 |
+
id: true,
|
| 183 |
+
rawInputText: true,
|
| 184 |
+
title: true,
|
| 185 |
+
problemStatement: true,
|
| 186 |
+
},
|
| 187 |
+
});
|
| 188 |
+
|
| 189 |
+
const mergeTarget = detectMergeTarget(
|
| 190 |
+
dedupInput,
|
| 191 |
+
candidates.map((candidate) => ({
|
| 192 |
+
id: candidate.id,
|
| 193 |
+
text: `${candidate.rawInputText}\n${candidate.title}\n${candidate.problemStatement}`,
|
| 194 |
+
}))
|
| 195 |
+
);
|
| 196 |
+
|
| 197 |
+
if (mergeTarget) {
|
| 198 |
+
const existing = await prisma.idea.findUnique({ where: { id: mergeTarget.targetIdeaId } });
|
| 199 |
+
|
| 200 |
+
if (existing) {
|
| 201 |
+
await prisma.ideaMerge.create({
|
| 202 |
+
data: {
|
| 203 |
+
targetIdeaId: existing.id,
|
| 204 |
+
mergedByAnonId: anonId || null,
|
| 205 |
+
mergedByUserId: userId,
|
| 206 |
+
rawInputText: parsed.data.raw_input_text,
|
| 207 |
+
targetUsers: parsed.data.target_users,
|
| 208 |
+
platform: parsed.data.platform,
|
| 209 |
+
constraints: parsed.data.constraints,
|
| 210 |
+
reason: mergeTarget.reason,
|
| 211 |
+
similarityScore: mergeTarget.similarityScore,
|
| 212 |
+
},
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
await applyMergeSupportVote(existing.id, userId, anonId || null);
|
| 216 |
+
|
| 217 |
+
return NextResponse.json(
|
| 218 |
+
{
|
| 219 |
+
merged: true,
|
| 220 |
+
reason: mergeTarget.reason,
|
| 221 |
+
message:
|
| 222 |
+
mergeTarget.reason === "SUBSET"
|
| 223 |
+
? "Your submission is a subset of an existing idea and has been merged into that thread."
|
| 224 |
+
: "Your submission matches an existing idea and has been merged into that thread.",
|
| 225 |
+
idea: {
|
| 226 |
+
id: existing.id,
|
| 227 |
+
title: existing.title,
|
| 228 |
+
},
|
| 229 |
+
},
|
| 230 |
+
{ status: 200 }
|
| 231 |
+
);
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
try {
|
| 236 |
const spec = await generateSpec(parsed.data);
|
| 237 |
|
app/ideas/[id]/page.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import { mapIdea, mapTask } from "@/lib/db-mappers";
|
|
| 4 |
import { getAnonId, getNickname } from "@/lib/identity";
|
| 5 |
import { IdeaDetailClient } from "@/components/IdeaDetailClient";
|
| 6 |
import { auth } from "@/auth";
|
|
|
|
| 7 |
|
| 8 |
const ALLOWED_STATUS = new Set(["OPEN", "IN_PROGRESS", "DONE"] as const);
|
| 9 |
|
|
@@ -45,6 +46,7 @@ export default async function IdeaPage({ params }: { params: Promise<{ id: strin
|
|
| 45 |
},
|
| 46 |
orderBy: { createdAt: "desc" },
|
| 47 |
},
|
|
|
|
| 48 |
},
|
| 49 |
});
|
| 50 |
|
|
@@ -75,6 +77,14 @@ export default async function IdeaPage({ params }: { params: Promise<{ id: strin
|
|
| 75 |
viewerUserId: userId,
|
| 76 |
isAuthenticated: Boolean(userId),
|
| 77 |
canApproveSolutions: Boolean(userId ? idea.createdByUserId === userId : !idea.createdByUserId && idea.createdByAnonId === anonId),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
idea_working_count: idea.workVotes.length,
|
| 79 |
idea_working: userId ? idea.workVotes.some((vote) => vote.userId === userId) : false,
|
| 80 |
tasks: idea.tasks.map((task) => {
|
|
|
|
| 4 |
import { getAnonId, getNickname } from "@/lib/identity";
|
| 5 |
import { IdeaDetailClient } from "@/components/IdeaDetailClient";
|
| 6 |
import { auth } from "@/auth";
|
| 7 |
+
import { canDeleteIdea } from "@/lib/permissions";
|
| 8 |
|
| 9 |
const ALLOWED_STATUS = new Set(["OPEN", "IN_PROGRESS", "DONE"] as const);
|
| 10 |
|
|
|
|
| 46 |
},
|
| 47 |
orderBy: { createdAt: "desc" },
|
| 48 |
},
|
| 49 |
+
merges: { select: { id: true } },
|
| 50 |
},
|
| 51 |
});
|
| 52 |
|
|
|
|
| 77 |
viewerUserId: userId,
|
| 78 |
isAuthenticated: Boolean(userId),
|
| 79 |
canApproveSolutions: Boolean(userId ? idea.createdByUserId === userId : !idea.createdByUserId && idea.createdByAnonId === anonId),
|
| 80 |
+
canDelete: canDeleteIdea({
|
| 81 |
+
ideaOwnerUserId: idea.createdByUserId,
|
| 82 |
+
ideaOwnerAnonId: idea.createdByAnonId,
|
| 83 |
+
actorUserId: userId,
|
| 84 |
+
actorAnonId: anonId || null,
|
| 85 |
+
actorEmail: session?.user?.email,
|
| 86 |
+
}),
|
| 87 |
+
merged_submission_count: idea.merges.length,
|
| 88 |
idea_working_count: idea.workVotes.length,
|
| 89 |
idea_working: userId ? idea.workVotes.some((vote) => vote.userId === userId) : false,
|
| 90 |
tasks: idea.tasks.map((task) => {
|
components/IdeaDetailClient.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
import Link from "next/link";
|
|
|
|
| 4 |
import { useMemo, useState } from "react";
|
| 5 |
import { fetchWithAnon } from "@/lib/client-anon";
|
| 6 |
|
|
@@ -66,6 +67,8 @@ type IdeaPayload = {
|
|
| 66 |
viewerUserId: string | null;
|
| 67 |
isAuthenticated: boolean;
|
| 68 |
canApproveSolutions: boolean;
|
|
|
|
|
|
|
| 69 |
idea_working_count: number;
|
| 70 |
idea_working: boolean;
|
| 71 |
tasks: TaskItem[];
|
|
@@ -105,6 +108,8 @@ export function IdeaDetailClient({
|
|
| 105 |
isVoted: boolean;
|
| 106 |
initialNickname: string;
|
| 107 |
}) {
|
|
|
|
|
|
|
| 108 |
const [idea, setIdea] = useState(initialIdea);
|
| 109 |
const [voted, setVoted] = useState(isVoted);
|
| 110 |
const [nickname, setNickname] = useState(initialNickname);
|
|
@@ -120,6 +125,14 @@ export function IdeaDetailClient({
|
|
| 120 |
|
| 121 |
const [solutionComments, setSolutionComments] = useState<Record<string, SolutionCommentItem[]>>({});
|
| 122 |
const [solutionCommentInput, setSolutionCommentInput] = useState<Record<string, string>>({});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
function applyTaskUpdate(taskId: string, taskUpdate: TaskUpdate) {
|
| 125 |
setIdea((prev) => ({
|
|
@@ -363,6 +376,24 @@ export function IdeaDetailClient({
|
|
| 363 |
setSolutions((prev) => prev.map((solution) => (solution.id === solutionId ? { ...solution, approvedAt } : solution)));
|
| 364 |
}
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
async function loadSolutionComments(solutionId: string) {
|
| 367 |
const response = await fetchWithAnon(`/api/solutions/${solutionId}/comments`);
|
| 368 |
if (!response.ok) return;
|
|
@@ -405,6 +436,10 @@ export function IdeaDetailClient({
|
|
| 405 |
<h1 className="section-title mt-2 text-3xl md:text-4xl">{idea.title}</h1>
|
| 406 |
<p className="subtle mt-4 max-w-4xl text-sm md:text-base">{idea.problemStatement}</p>
|
| 407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
<div className="mt-4 flex flex-wrap gap-1.5">
|
| 409 |
{idea.tags.map((tag) => (
|
| 410 |
<span key={tag} className="badge">
|
|
@@ -412,6 +447,7 @@ export function IdeaDetailClient({
|
|
| 412 |
</span>
|
| 413 |
))}
|
| 414 |
<span className="pill">Submitter: {idea.submitter_label}</span>
|
|
|
|
| 415 |
</div>
|
| 416 |
|
| 417 |
<div className="mt-5 flex flex-wrap items-center gap-2 md:gap-3">
|
|
@@ -423,8 +459,15 @@ export function IdeaDetailClient({
|
|
| 423 |
</button>
|
| 424 |
<span className="pill">{idea.commentsCount} comments</span>
|
| 425 |
<span className="pill">{idea.tasks.length} tasks</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
</div>
|
| 427 |
|
|
|
|
|
|
|
| 428 |
{!idea.isAuthenticated ? (
|
| 429 |
<p className="subtle mt-2 text-xs">
|
| 430 |
<Link href="/login" className="underline">Sign in</Link> to submit solutions and add developer work votes.
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
import Link from "next/link";
|
| 4 |
+
import { useRouter, useSearchParams } from "next/navigation";
|
| 5 |
import { useMemo, useState } from "react";
|
| 6 |
import { fetchWithAnon } from "@/lib/client-anon";
|
| 7 |
|
|
|
|
| 67 |
viewerUserId: string | null;
|
| 68 |
isAuthenticated: boolean;
|
| 69 |
canApproveSolutions: boolean;
|
| 70 |
+
canDelete: boolean;
|
| 71 |
+
merged_submission_count: number;
|
| 72 |
idea_working_count: number;
|
| 73 |
idea_working: boolean;
|
| 74 |
tasks: TaskItem[];
|
|
|
|
| 108 |
isVoted: boolean;
|
| 109 |
initialNickname: string;
|
| 110 |
}) {
|
| 111 |
+
const router = useRouter();
|
| 112 |
+
const searchParams = useSearchParams();
|
| 113 |
const [idea, setIdea] = useState(initialIdea);
|
| 114 |
const [voted, setVoted] = useState(isVoted);
|
| 115 |
const [nickname, setNickname] = useState(initialNickname);
|
|
|
|
| 125 |
|
| 126 |
const [solutionComments, setSolutionComments] = useState<Record<string, SolutionCommentItem[]>>({});
|
| 127 |
const [solutionCommentInput, setSolutionCommentInput] = useState<Record<string, string>>({});
|
| 128 |
+
const [actionError, setActionError] = useState("");
|
| 129 |
+
|
| 130 |
+
const mergeNotice = useMemo(() => {
|
| 131 |
+
if (searchParams.get("merged") !== "1") return "";
|
| 132 |
+
return searchParams.get("reason") === "SUBSET"
|
| 133 |
+
? "Your submission was merged: it is a subset of this existing idea."
|
| 134 |
+
: "Your submission was merged: this idea already exists, so discussion stays in one thread.";
|
| 135 |
+
}, [searchParams]);
|
| 136 |
|
| 137 |
function applyTaskUpdate(taskId: string, taskUpdate: TaskUpdate) {
|
| 138 |
setIdea((prev) => ({
|
|
|
|
| 376 |
setSolutions((prev) => prev.map((solution) => (solution.id === solutionId ? { ...solution, approvedAt } : solution)));
|
| 377 |
}
|
| 378 |
|
| 379 |
+
async function deleteIdea() {
|
| 380 |
+
if (!idea.canDelete) return;
|
| 381 |
+
|
| 382 |
+
const confirmed = window.confirm("Delete this post? This action cannot be undone.");
|
| 383 |
+
if (!confirmed) return;
|
| 384 |
+
|
| 385 |
+
setActionError("");
|
| 386 |
+
const response = await fetchWithAnon(`/api/ideas/${idea.id}`, { method: "DELETE" });
|
| 387 |
+
if (!response.ok) {
|
| 388 |
+
const data = await response.json().catch(() => null);
|
| 389 |
+
setActionError((data && typeof data.error === "string" ? data.error : "Failed to delete post.") || "Failed to delete post.");
|
| 390 |
+
return;
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
router.push("/");
|
| 394 |
+
router.refresh();
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
async function loadSolutionComments(solutionId: string) {
|
| 398 |
const response = await fetchWithAnon(`/api/solutions/${solutionId}/comments`);
|
| 399 |
if (!response.ok) return;
|
|
|
|
| 436 |
<h1 className="section-title mt-2 text-3xl md:text-4xl">{idea.title}</h1>
|
| 437 |
<p className="subtle mt-4 max-w-4xl text-sm md:text-base">{idea.problemStatement}</p>
|
| 438 |
|
| 439 |
+
{mergeNotice ? (
|
| 440 |
+
<p className="mt-4 rounded-lg border border-amber-300 bg-amber-50 px-3 py-2 text-sm text-amber-900">{mergeNotice}</p>
|
| 441 |
+
) : null}
|
| 442 |
+
|
| 443 |
<div className="mt-4 flex flex-wrap gap-1.5">
|
| 444 |
{idea.tags.map((tag) => (
|
| 445 |
<span key={tag} className="badge">
|
|
|
|
| 447 |
</span>
|
| 448 |
))}
|
| 449 |
<span className="pill">Submitter: {idea.submitter_label}</span>
|
| 450 |
+
<span className="pill">Merged submissions: {idea.merged_submission_count}</span>
|
| 451 |
</div>
|
| 452 |
|
| 453 |
<div className="mt-5 flex flex-wrap items-center gap-2 md:gap-3">
|
|
|
|
| 459 |
</button>
|
| 460 |
<span className="pill">{idea.commentsCount} comments</span>
|
| 461 |
<span className="pill">{idea.tasks.length} tasks</span>
|
| 462 |
+
{idea.canDelete ? (
|
| 463 |
+
<button className="btn border-red-200 text-red-700 hover:bg-red-50" onClick={deleteIdea}>
|
| 464 |
+
Delete Post
|
| 465 |
+
</button>
|
| 466 |
+
) : null}
|
| 467 |
</div>
|
| 468 |
|
| 469 |
+
{actionError ? <p className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-700">{actionError}</p> : null}
|
| 470 |
+
|
| 471 |
{!idea.isAuthenticated ? (
|
| 472 |
<p className="subtle mt-2 text-xs">
|
| 473 |
<Link href="/login" className="underline">Sign in</Link> to submit solutions and add developer work votes.
|
components/SubmitForm.tsx
CHANGED
|
@@ -51,7 +51,12 @@ export function SubmitForm({ canShowIdentity }: { canShowIdentity: boolean }) {
|
|
| 51 |
throw new Error(parseErrorMessage(data));
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
router.refresh();
|
| 56 |
} catch (err) {
|
| 57 |
setError(err instanceof Error ? err.message : "Unknown error.");
|
|
|
|
| 51 |
throw new Error(parseErrorMessage(data));
|
| 52 |
}
|
| 53 |
|
| 54 |
+
if (data.merged && data.idea?.id) {
|
| 55 |
+
const reason = encodeURIComponent(data.reason || "DUPLICATE");
|
| 56 |
+
router.push(`/ideas/${data.idea.id}?merged=1&reason=${reason}`);
|
| 57 |
+
} else {
|
| 58 |
+
router.push(`/ideas/${data.idea.id}`);
|
| 59 |
+
}
|
| 60 |
router.refresh();
|
| 61 |
} catch (err) {
|
| 62 |
setError(err instanceof Error ? err.message : "Unknown error.");
|
lib/content-moderation.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const BLOCK_PATTERNS: Array<{ label: string; pattern: RegExp }> = [
|
| 2 |
+
{ label: "hate slur", pattern: /\b(?:nigger|faggot|kike|chink)\b/i },
|
| 3 |
+
{ label: "sexual slur", pattern: /\b(?:cunt|whore)\b/i },
|
| 4 |
+
{ label: "explicit profanity", pattern: /\b(?:fuck|motherfucker|shit|asshole|bitch)\b/i },
|
| 5 |
+
{ label: "violent abuse", pattern: /\b(?:rape|kill\s+all|exterminate\s+all)\b/i },
|
| 6 |
+
];
|
| 7 |
+
|
| 8 |
+
export function findBlockedContent(...inputs: Array<string | undefined | null>) {
|
| 9 |
+
const text = inputs
|
| 10 |
+
.filter((value): value is string => typeof value === "string" && value.trim().length > 0)
|
| 11 |
+
.join("\n")
|
| 12 |
+
.slice(0, 5000);
|
| 13 |
+
|
| 14 |
+
if (!text) {
|
| 15 |
+
return { blocked: false, labels: [] as string[] };
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const labels = BLOCK_PATTERNS.filter((item) => item.pattern.test(text)).map((item) => item.label);
|
| 19 |
+
|
| 20 |
+
return {
|
| 21 |
+
blocked: labels.length > 0,
|
| 22 |
+
labels,
|
| 23 |
+
};
|
| 24 |
+
}
|
lib/idea-dedup.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const STOP_WORDS = new Set([
|
| 2 |
+
"the", "and", "for", "with", "that", "this", "from", "into", "your", "about", "would", "should", "could", "their", "there",
|
| 3 |
+
"have", "has", "had", "are", "was", "were", "will", "can", "our", "you", "they", "them", "but", "not", "use", "using",
|
| 4 |
+
"app", "platform", "idea", "build", "make", "need", "want", "users", "user", "solution", "project", "feature",
|
| 5 |
+
]);
|
| 6 |
+
|
| 7 |
+
function tokenize(value: string): Set<string> {
|
| 8 |
+
const cleaned = value
|
| 9 |
+
.toLowerCase()
|
| 10 |
+
.replace(/[^a-z0-9\s]/g, " ")
|
| 11 |
+
.split(/\s+/)
|
| 12 |
+
.map((token) => token.trim())
|
| 13 |
+
.filter((token) => token.length >= 3 && !STOP_WORDS.has(token));
|
| 14 |
+
|
| 15 |
+
return new Set(cleaned);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
function intersectionSize(a: Set<string>, b: Set<string>): number {
|
| 19 |
+
let size = 0;
|
| 20 |
+
for (const token of a) {
|
| 21 |
+
if (b.has(token)) size += 1;
|
| 22 |
+
}
|
| 23 |
+
return size;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
export type MergeReason = "DUPLICATE" | "SUBSET";
|
| 27 |
+
|
| 28 |
+
export type DedupCandidate = {
|
| 29 |
+
id: string;
|
| 30 |
+
text: string;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
export function detectMergeTarget(
|
| 34 |
+
inputText: string,
|
| 35 |
+
candidates: DedupCandidate[]
|
| 36 |
+
): { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null {
|
| 37 |
+
const inputTokens = tokenize(inputText);
|
| 38 |
+
if (inputTokens.size < 6) return null;
|
| 39 |
+
|
| 40 |
+
let best: { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null = null;
|
| 41 |
+
|
| 42 |
+
for (const candidate of candidates) {
|
| 43 |
+
const candidateTokens = tokenize(candidate.text);
|
| 44 |
+
if (candidateTokens.size < 6) continue;
|
| 45 |
+
|
| 46 |
+
const overlap = intersectionSize(inputTokens, candidateTokens);
|
| 47 |
+
if (overlap === 0) continue;
|
| 48 |
+
|
| 49 |
+
const union = inputTokens.size + candidateTokens.size - overlap;
|
| 50 |
+
const jaccard = union > 0 ? overlap / union : 0;
|
| 51 |
+
const newCoveredByExisting = overlap / inputTokens.size;
|
| 52 |
+
|
| 53 |
+
let reason: MergeReason | null = null;
|
| 54 |
+
let score = jaccard;
|
| 55 |
+
|
| 56 |
+
if (jaccard >= 0.62) {
|
| 57 |
+
reason = "DUPLICATE";
|
| 58 |
+
score = jaccard;
|
| 59 |
+
} else if (newCoveredByExisting >= 0.86) {
|
| 60 |
+
reason = "SUBSET";
|
| 61 |
+
score = newCoveredByExisting;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
if (!reason) continue;
|
| 65 |
+
|
| 66 |
+
if (!best || score > best.similarityScore) {
|
| 67 |
+
best = {
|
| 68 |
+
targetIdeaId: candidate.id,
|
| 69 |
+
reason,
|
| 70 |
+
similarityScore: Number(score.toFixed(4)),
|
| 71 |
+
};
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
return best;
|
| 76 |
+
}
|
lib/permissions.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const ADMIN_EMAILS = new Set(["jacksuyu@gmail.com"]);
|
| 2 |
+
|
| 3 |
+
export function isAdminEmail(email?: string | null): boolean {
|
| 4 |
+
if (!email) return false;
|
| 5 |
+
return ADMIN_EMAILS.has(email.trim().toLowerCase());
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
export function canDeleteIdea(params: {
|
| 9 |
+
ideaOwnerUserId: string | null;
|
| 10 |
+
ideaOwnerAnonId: string | null;
|
| 11 |
+
actorUserId: string | null;
|
| 12 |
+
actorAnonId: string | null;
|
| 13 |
+
actorEmail?: string | null;
|
| 14 |
+
}): boolean {
|
| 15 |
+
if (isAdminEmail(params.actorEmail)) return true;
|
| 16 |
+
|
| 17 |
+
if (params.actorUserId) {
|
| 18 |
+
return params.ideaOwnerUserId === params.actorUserId;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
if (!params.ideaOwnerUserId && params.actorAnonId) {
|
| 22 |
+
return params.ideaOwnerAnonId === params.actorAnonId;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
return false;
|
| 26 |
+
}
|
prisma/schema.prisma
CHANGED
|
@@ -32,6 +32,7 @@ model User {
|
|
| 32 |
approvedSolutions Solution[] @relation("SolutionApprovedByUser")
|
| 33 |
solutionVotes SolutionUserVote[]
|
| 34 |
solutionComments SolutionComment[] @relation("SolutionCommentUser")
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
model Account {
|
|
@@ -113,6 +114,7 @@ model Idea {
|
|
| 113 |
userVotes IdeaUserVote[]
|
| 114 |
workVotes IdeaWorkVote[]
|
| 115 |
solutions Solution[]
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
model Task {
|
|
@@ -285,3 +287,23 @@ model SubmissionRateLimit {
|
|
| 285 |
|
| 286 |
@@unique([anonId, ipAddress, windowStart])
|
| 287 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
approvedSolutions Solution[] @relation("SolutionApprovedByUser")
|
| 33 |
solutionVotes SolutionUserVote[]
|
| 34 |
solutionComments SolutionComment[] @relation("SolutionCommentUser")
|
| 35 |
+
ideaMerges IdeaMerge[]
|
| 36 |
}
|
| 37 |
|
| 38 |
model Account {
|
|
|
|
| 114 |
userVotes IdeaUserVote[]
|
| 115 |
workVotes IdeaWorkVote[]
|
| 116 |
solutions Solution[]
|
| 117 |
+
merges IdeaMerge[]
|
| 118 |
}
|
| 119 |
|
| 120 |
model Task {
|
|
|
|
| 287 |
|
| 288 |
@@unique([anonId, ipAddress, windowStart])
|
| 289 |
}
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
model IdeaMerge {
|
| 293 |
+
id String @id @default(cuid())
|
| 294 |
+
targetIdeaId String
|
| 295 |
+
mergedByAnonId String?
|
| 296 |
+
mergedByUserId String?
|
| 297 |
+
rawInputText String
|
| 298 |
+
targetUsers String?
|
| 299 |
+
platform String?
|
| 300 |
+
constraints String?
|
| 301 |
+
reason String
|
| 302 |
+
similarityScore Float
|
| 303 |
+
createdAt DateTime @default(now())
|
| 304 |
+
|
| 305 |
+
targetIdea Idea @relation(fields: [targetIdeaId], references: [id], onDelete: Cascade)
|
| 306 |
+
mergedByUser User? @relation(fields: [mergedByUserId], references: [id], onDelete: SetNull)
|
| 307 |
+
|
| 308 |
+
@@index([targetIdeaId, createdAt])
|
| 309 |
+
}
|