Spaces:
Running
Running
fix: sanitize imported idea copy and clean deployed data
Browse files- Dockerfile +1 -1
- app/api/ideas/route.ts +10 -7
- lib/db-mappers.ts +8 -0
- lib/idea-copy.ts +65 -0
- lib/title.ts +3 -1
- scripts/cleanup_idea_copy.js +85 -0
Dockerfile
CHANGED
|
@@ -26,4 +26,4 @@ RUN npm run prisma:generate && npm run build
|
|
| 26 |
|
| 27 |
EXPOSE 7860
|
| 28 |
|
| 29 |
-
CMD ["sh", "-lc", "if [ -z \"$DATABASE_URL\" ]; then echo 'DATABASE_URL is required (set it in HF secrets, e.g. Supabase Postgres URL)'; exit 1; fi; npx prisma db push --skip-generate && npm start -- -H 0.0.0.0 -p ${PORT}"]
|
|
|
|
| 26 |
|
| 27 |
EXPOSE 7860
|
| 28 |
|
| 29 |
+
CMD ["sh", "-lc", "if [ -z \"$DATABASE_URL\" ]; then echo 'DATABASE_URL is required (set it in HF secrets, e.g. Supabase Postgres URL)'; exit 1; fi; npx prisma db push --skip-generate && node scripts/cleanup_idea_copy.js && npm start -- -H 0.0.0.0 -p ${PORT}"]
|
app/api/ideas/route.ts
CHANGED
|
@@ -8,6 +8,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 |
import { buildMeaningfulTitle } from "@/lib/title";
|
| 12 |
import { computeIdeaSearchScore, createTrigrams, normalizeSearchText, tokenizeSearchTerms } from "@/lib/fuzzy-search";
|
| 13 |
|
|
@@ -234,8 +235,9 @@ export async function POST(request: NextRequest) {
|
|
| 234 |
}
|
| 235 |
|
| 236 |
const sourceTag = normalizeSourceTag(parsed.data.source_tag);
|
|
|
|
| 237 |
|
| 238 |
-
const moderation = findBlockedContent(
|
| 239 |
if (moderation.blocked) {
|
| 240 |
return NextResponse.json(
|
| 241 |
{
|
|
@@ -246,7 +248,7 @@ export async function POST(request: NextRequest) {
|
|
| 246 |
);
|
| 247 |
}
|
| 248 |
|
| 249 |
-
const dedupInput = [
|
| 250 |
.filter((value): value is string => Boolean(value))
|
| 251 |
.join("\n");
|
| 252 |
|
|
@@ -289,7 +291,7 @@ export async function POST(request: NextRequest) {
|
|
| 289 |
targetIdeaId: existing.id,
|
| 290 |
mergedByAnonId: anonId || null,
|
| 291 |
mergedByUserId: userId,
|
| 292 |
-
rawInputText:
|
| 293 |
targetUsers: parsed.data.target_users,
|
| 294 |
platform: parsed.data.platform,
|
| 295 |
constraints: parsed.data.constraints,
|
|
@@ -320,10 +322,11 @@ export async function POST(request: NextRequest) {
|
|
| 320 |
|
| 321 |
try {
|
| 322 |
const spec = await generateSpec(parsed.data);
|
|
|
|
| 323 |
const displayTitle = buildMeaningfulTitle({
|
| 324 |
-
rawInputText:
|
| 325 |
title: spec.title,
|
| 326 |
-
problemStatement:
|
| 327 |
});
|
| 328 |
|
| 329 |
let submitterVisibleName: string | null = null;
|
|
@@ -343,12 +346,12 @@ export async function POST(request: NextRequest) {
|
|
| 343 |
createdByUserId: userId,
|
| 344 |
isAnonymous,
|
| 345 |
submitterVisibleName,
|
| 346 |
-
rawInputText:
|
| 347 |
targetUsers: parsed.data.target_users,
|
| 348 |
platform: parsed.data.platform,
|
| 349 |
constraints: parsed.data.constraints,
|
| 350 |
title: displayTitle,
|
| 351 |
-
problemStatement:
|
| 352 |
tags: JSON.stringify(ideaTags),
|
| 353 |
features: JSON.stringify(spec.features),
|
| 354 |
openQuestions: JSON.stringify(spec.open_questions),
|
|
|
|
| 8 |
import { auth } from "@/auth";
|
| 9 |
import { findBlockedContent } from "@/lib/content-moderation";
|
| 10 |
import { detectMergeTarget } from "@/lib/idea-dedup";
|
| 11 |
+
import { cleanProblemStatement, stripSocialRequirementPrefix } from "@/lib/idea-copy";
|
| 12 |
import { buildMeaningfulTitle } from "@/lib/title";
|
| 13 |
import { computeIdeaSearchScore, createTrigrams, normalizeSearchText, tokenizeSearchTerms } from "@/lib/fuzzy-search";
|
| 14 |
|
|
|
|
| 235 |
}
|
| 236 |
|
| 237 |
const sourceTag = normalizeSourceTag(parsed.data.source_tag);
|
| 238 |
+
const cleanedRawInputText = stripSocialRequirementPrefix(parsed.data.raw_input_text);
|
| 239 |
|
| 240 |
+
const moderation = findBlockedContent(cleanedRawInputText, parsed.data.target_users, parsed.data.constraints);
|
| 241 |
if (moderation.blocked) {
|
| 242 |
return NextResponse.json(
|
| 243 |
{
|
|
|
|
| 248 |
);
|
| 249 |
}
|
| 250 |
|
| 251 |
+
const dedupInput = [cleanedRawInputText, parsed.data.target_users, parsed.data.platform, parsed.data.constraints]
|
| 252 |
.filter((value): value is string => Boolean(value))
|
| 253 |
.join("\n");
|
| 254 |
|
|
|
|
| 291 |
targetIdeaId: existing.id,
|
| 292 |
mergedByAnonId: anonId || null,
|
| 293 |
mergedByUserId: userId,
|
| 294 |
+
rawInputText: cleanedRawInputText,
|
| 295 |
targetUsers: parsed.data.target_users,
|
| 296 |
platform: parsed.data.platform,
|
| 297 |
constraints: parsed.data.constraints,
|
|
|
|
| 322 |
|
| 323 |
try {
|
| 324 |
const spec = await generateSpec(parsed.data);
|
| 325 |
+
const cleanedProblemStatement = cleanProblemStatement(spec.problem_statement, cleanedRawInputText);
|
| 326 |
const displayTitle = buildMeaningfulTitle({
|
| 327 |
+
rawInputText: cleanedRawInputText,
|
| 328 |
title: spec.title,
|
| 329 |
+
problemStatement: cleanedProblemStatement,
|
| 330 |
});
|
| 331 |
|
| 332 |
let submitterVisibleName: string | null = null;
|
|
|
|
| 346 |
createdByUserId: userId,
|
| 347 |
isAnonymous,
|
| 348 |
submitterVisibleName,
|
| 349 |
+
rawInputText: cleanedRawInputText,
|
| 350 |
targetUsers: parsed.data.target_users,
|
| 351 |
platform: parsed.data.platform,
|
| 352 |
constraints: parsed.data.constraints,
|
| 353 |
title: displayTitle,
|
| 354 |
+
problemStatement: cleanedProblemStatement,
|
| 355 |
tags: JSON.stringify(ideaTags),
|
| 356 |
features: JSON.stringify(spec.features),
|
| 357 |
openQuestions: JSON.stringify(spec.open_questions),
|
lib/db-mappers.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import type { Idea, Task, TaskLink } from "@prisma/client";
|
|
|
|
| 2 |
|
| 3 |
export function parseJsonArray<T>(value: string, fallback: T[] = []): T[] {
|
| 4 |
try {
|
|
@@ -9,8 +10,15 @@ export function parseJsonArray<T>(value: string, fallback: T[] = []): T[] {
|
|
| 9 |
}
|
| 10 |
|
| 11 |
export function mapIdea(idea: Idea) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
return {
|
| 13 |
...idea,
|
|
|
|
|
|
|
| 14 |
tags: parseJsonArray<string>(idea.tags),
|
| 15 |
features: parseJsonArray<string>(idea.features),
|
| 16 |
open_questions: parseJsonArray<string>(idea.openQuestions),
|
|
|
|
| 1 |
import type { Idea, Task, TaskLink } from "@prisma/client";
|
| 2 |
+
import { normalizeIdeaCopy } from "@/lib/idea-copy";
|
| 3 |
|
| 4 |
export function parseJsonArray<T>(value: string, fallback: T[] = []): T[] {
|
| 5 |
try {
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
export function mapIdea(idea: Idea) {
|
| 13 |
+
const normalizedCopy = normalizeIdeaCopy({
|
| 14 |
+
rawInputText: idea.rawInputText,
|
| 15 |
+
problemStatement: idea.problemStatement,
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
return {
|
| 19 |
...idea,
|
| 20 |
+
rawInputText: normalizedCopy.rawInputText,
|
| 21 |
+
problemStatement: normalizedCopy.problemStatement,
|
| 22 |
tags: parseJsonArray<string>(idea.tags),
|
| 23 |
features: parseJsonArray<string>(idea.features),
|
| 24 |
open_questions: parseJsonArray<string>(idea.openQuestions),
|
lib/idea-copy.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const SOCIAL_REQUIREMENT_PREFIX = "User requirement from social community:";
|
| 2 |
+
const GENERIC_PROBLEM_STATEMENT =
|
| 3 |
+
/^Build a\s+.+?\s+product for\s+.+?\s+based on the submitted idea\.$/i;
|
| 4 |
+
|
| 5 |
+
function collapseWhitespace(value: string): string {
|
| 6 |
+
return value.replace(/\s+/g, " ").trim();
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export function stripSocialRequirementPrefix(value: string | null | undefined): string {
|
| 10 |
+
if (!value) return "";
|
| 11 |
+
|
| 12 |
+
const trimmed = value.trim();
|
| 13 |
+
if (!trimmed) return "";
|
| 14 |
+
|
| 15 |
+
if (trimmed.startsWith(SOCIAL_REQUIREMENT_PREFIX)) {
|
| 16 |
+
return trimmed.slice(SOCIAL_REQUIREMENT_PREFIX.length).trim();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
return trimmed;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
export function cleanProblemStatement(
|
| 23 |
+
problemStatement: string | null | undefined,
|
| 24 |
+
rawInputText: string | null | undefined,
|
| 25 |
+
): string {
|
| 26 |
+
const cleanedRawInput = stripSocialRequirementPrefix(rawInputText);
|
| 27 |
+
const cleanedProblem = stripSocialRequirementPrefix(problemStatement);
|
| 28 |
+
|
| 29 |
+
if (!cleanedProblem) {
|
| 30 |
+
return cleanedRawInput;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
if (GENERIC_PROBLEM_STATEMENT.test(cleanedProblem)) {
|
| 34 |
+
return cleanedRawInput || cleanedProblem;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
return cleanedProblem;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export function normalizeIdeaCopy(input: {
|
| 41 |
+
rawInputText: string | null | undefined;
|
| 42 |
+
problemStatement: string | null | undefined;
|
| 43 |
+
}) {
|
| 44 |
+
const rawInputText = stripSocialRequirementPrefix(input.rawInputText);
|
| 45 |
+
const problemStatement = cleanProblemStatement(input.problemStatement, rawInputText);
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
rawInputText,
|
| 49 |
+
problemStatement,
|
| 50 |
+
};
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export function looksLikeDirtyIdeaCopy(input: {
|
| 54 |
+
rawInputText: string | null | undefined;
|
| 55 |
+
problemStatement: string | null | undefined;
|
| 56 |
+
}) {
|
| 57 |
+
const currentRaw = collapseWhitespace(input.rawInputText || "");
|
| 58 |
+
const currentProblem = collapseWhitespace(input.problemStatement || "");
|
| 59 |
+
const normalized = normalizeIdeaCopy(input);
|
| 60 |
+
|
| 61 |
+
return (
|
| 62 |
+
currentRaw !== collapseWhitespace(normalized.rawInputText) ||
|
| 63 |
+
currentProblem !== collapseWhitespace(normalized.problemStatement)
|
| 64 |
+
);
|
| 65 |
+
}
|
lib/title.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
const BAD_ENDINGS = new Set(["and", "or", "but", "to", "for", "with", "of", "a", "an", "the"]);
|
| 2 |
|
| 3 |
function collapseWhitespace(value: string): string {
|
|
@@ -34,7 +36,7 @@ function titleCase(phrase: string): string {
|
|
| 34 |
}
|
| 35 |
|
| 36 |
function deriveTitleFromInput(rawInputText: string): string {
|
| 37 |
-
let text = collapseWhitespace(rawInputText);
|
| 38 |
text = text
|
| 39 |
.replace(/^i\s+(want|need|would\s+like|wish|hope)\s+(to\s+)?/i, "")
|
| 40 |
.replace(/^please\s+/i, "")
|
|
|
|
| 1 |
+
import { stripSocialRequirementPrefix } from "@/lib/idea-copy";
|
| 2 |
+
|
| 3 |
const BAD_ENDINGS = new Set(["and", "or", "but", "to", "for", "with", "of", "a", "an", "the"]);
|
| 4 |
|
| 5 |
function collapseWhitespace(value: string): string {
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
function deriveTitleFromInput(rawInputText: string): string {
|
| 39 |
+
let text = collapseWhitespace(stripSocialRequirementPrefix(rawInputText));
|
| 40 |
text = text
|
| 41 |
.replace(/^i\s+(want|need|would\s+like|wish|hope)\s+(to\s+)?/i, "")
|
| 42 |
.replace(/^please\s+/i, "")
|
scripts/cleanup_idea_copy.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { PrismaClient } = require("@prisma/client");
|
| 2 |
+
|
| 3 |
+
const SOCIAL_REQUIREMENT_PREFIX = "User requirement from social community:";
|
| 4 |
+
const GENERIC_PROBLEM_STATEMENT =
|
| 5 |
+
/^Build a\s+.+?\s+product for\s+.+?\s+based on the submitted idea\.$/i;
|
| 6 |
+
|
| 7 |
+
function stripSocialRequirementPrefix(value) {
|
| 8 |
+
if (!value) return "";
|
| 9 |
+
|
| 10 |
+
const trimmed = String(value).trim();
|
| 11 |
+
if (!trimmed) return "";
|
| 12 |
+
|
| 13 |
+
if (trimmed.startsWith(SOCIAL_REQUIREMENT_PREFIX)) {
|
| 14 |
+
return trimmed.slice(SOCIAL_REQUIREMENT_PREFIX.length).trim();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
return trimmed;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
function cleanProblemStatement(problemStatement, rawInputText) {
|
| 21 |
+
const cleanedRawInput = stripSocialRequirementPrefix(rawInputText);
|
| 22 |
+
const cleanedProblem = stripSocialRequirementPrefix(problemStatement);
|
| 23 |
+
|
| 24 |
+
if (!cleanedProblem) {
|
| 25 |
+
return cleanedRawInput;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
if (GENERIC_PROBLEM_STATEMENT.test(cleanedProblem)) {
|
| 29 |
+
return cleanedRawInput || cleanedProblem;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return cleanedProblem;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
async function main() {
|
| 36 |
+
const databaseUrl = process.env.DATABASE_URL || "";
|
| 37 |
+
|
| 38 |
+
if (!databaseUrl.startsWith("postgresql://") && !databaseUrl.startsWith("postgres://")) {
|
| 39 |
+
console.log("Idea copy cleanup skipped. DATABASE_URL is not a PostgreSQL connection string.");
|
| 40 |
+
return;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const prisma = new PrismaClient();
|
| 44 |
+
|
| 45 |
+
try {
|
| 46 |
+
const ideas = await prisma.idea.findMany({
|
| 47 |
+
select: {
|
| 48 |
+
id: true,
|
| 49 |
+
rawInputText: true,
|
| 50 |
+
problemStatement: true,
|
| 51 |
+
},
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
let updates = 0;
|
| 55 |
+
|
| 56 |
+
for (const idea of ideas) {
|
| 57 |
+
const rawInputText = stripSocialRequirementPrefix(idea.rawInputText);
|
| 58 |
+
const problemStatement = cleanProblemStatement(idea.problemStatement, rawInputText);
|
| 59 |
+
|
| 60 |
+
if (rawInputText === idea.rawInputText && problemStatement === idea.problemStatement) {
|
| 61 |
+
continue;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
await prisma.idea.update({
|
| 65 |
+
where: { id: idea.id },
|
| 66 |
+
data: {
|
| 67 |
+
rawInputText,
|
| 68 |
+
problemStatement,
|
| 69 |
+
},
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
updates += 1;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
console.log(`Idea copy cleanup complete. Updated ${updates} row(s).`);
|
| 76 |
+
} finally {
|
| 77 |
+
await prisma.$disconnect();
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
main().catch((error) => {
|
| 82 |
+
console.error("Idea copy cleanup failed.");
|
| 83 |
+
console.error(error);
|
| 84 |
+
process.exit(1);
|
| 85 |
+
});
|