Start every Rule private, with a rotating link and files that follow its readers.
New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
This commit is contained in:
1 parent
642551064c
commit
5cbda35e32
50 files changed
+2429
-219
No files matched your search
+22
-16
@@ -8,9 +8,11 @@ import {
|
||||
notFound,
|
||||
unauthorized,
|
||||
} from "../../../../lib/server/responses";
|
||||
import { getPublicPublishedRuleById } from "../../../../lib/server/publishedRules";
|
||||
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
|
||||
import { getSessionUser } from "../../../../lib/server/session";
|
||||
import { apiRoute } from "../../../../lib/server/apiRoute";
|
||||
import { deleteUploadsForRule } from "../../../../lib/server/uploads/deleteUploadedFiles";
|
||||
import { linkUploadsToRule } from "../../../../lib/server/uploads/linkUploads";
|
||||
import { publishRuleBodySchema } from "../../../../lib/server/validation/createFlowSchemas";
|
||||
import { readLimitedJson } from "../../../../lib/server/validation/requestBody";
|
||||
import { jsonFromZodError } from "../../../../lib/server/validation/zodHttp";
|
||||
@@ -19,29 +21,27 @@ type RouteContext = { params: Promise<{ id: string }> };
|
||||
|
||||
export const GET = apiRoute<RouteContext>(
|
||||
"rules.byId",
|
||||
async (_request, context) => {
|
||||
async (request, context) => {
|
||||
if (!isDatabaseConfigured()) {
|
||||
return dbUnavailable();
|
||||
}
|
||||
|
||||
const { id } = await context.params;
|
||||
|
||||
const rule = await getPublicPublishedRuleById(id);
|
||||
if (!rule) {
|
||||
const user = await getSessionUser();
|
||||
const resolved = await getReadablePublishedRule({
|
||||
idOrSlug: id,
|
||||
user,
|
||||
viaShareSlug: false,
|
||||
requestPath: request.nextUrl.pathname,
|
||||
});
|
||||
if (!resolved) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
const user = await getSessionUser();
|
||||
let viewerIsOwner = false;
|
||||
if (user) {
|
||||
const ownerRow = await prisma.publishedRule.findUnique({
|
||||
where: { id },
|
||||
select: { userId: true },
|
||||
});
|
||||
viewerIsOwner = ownerRow?.userId === user.id;
|
||||
}
|
||||
|
||||
return NextResponse.json({ rule, viewerIsOwner });
|
||||
return NextResponse.json({
|
||||
rule: resolved.rule,
|
||||
viewerIsOwner: resolved.viewerIsOwner,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -90,6 +90,11 @@ export const PATCH = apiRoute<RouteContext>(
|
||||
document: document as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
await linkUploadsToRule({
|
||||
ruleId: row.id,
|
||||
document,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
},
|
||||
@@ -120,6 +125,7 @@ export const DELETE = apiRoute<RouteContext>(
|
||||
return forbidden("You do not have permission to delete this rule");
|
||||
}
|
||||
|
||||
await deleteUploadsForRule(row.id);
|
||||
await prisma.publishedRule.delete({ where: { id: row.id } });
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { apiRoute } from "../../../../../lib/server/apiRoute";
|
||||
import { isDatabaseConfigured } from "../../../../../lib/server/env";
|
||||
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
||||
import {
|
||||
dbUnavailable,
|
||||
errorJson,
|
||||
forbidden,
|
||||
notFound,
|
||||
unauthorized,
|
||||
} from "../../../../../lib/server/responses";
|
||||
import {
|
||||
getOwnedRuleShareSettings,
|
||||
patchOwnedRuleShare,
|
||||
} from "../../../../../lib/server/ruleShare";
|
||||
import { getSessionUser } from "../../../../../lib/server/session";
|
||||
import { patchRuleShareBodySchema } from "../../../../../lib/server/validation/createFlowSchemas";
|
||||
import { readLimitedJson } from "../../../../../lib/server/validation/requestBody";
|
||||
import { jsonFromZodError } from "../../../../../lib/server/validation/zodHttp";
|
||||
|
||||
type RouteContext = { params: Promise<{ id: string }> };
|
||||
|
||||
export const GET = apiRoute<RouteContext>(
|
||||
"rules.share.get",
|
||||
async (request, context) => {
|
||||
if (!isDatabaseConfigured()) {
|
||||
return dbUnavailable();
|
||||
}
|
||||
const user = await getSessionUser();
|
||||
if (!user) {
|
||||
return unauthorized();
|
||||
}
|
||||
const { id } = await context.params;
|
||||
const settings = await getOwnedRuleShareSettings({
|
||||
ruleId: id,
|
||||
user,
|
||||
origin: getPublicOrigin(request),
|
||||
});
|
||||
if (settings === "not_found") return notFound();
|
||||
if (settings === "forbidden") {
|
||||
return forbidden("You do not have permission to share this rule");
|
||||
}
|
||||
return NextResponse.json({ share: settings });
|
||||
},
|
||||
);
|
||||
|
||||
export const PATCH = apiRoute<RouteContext>(
|
||||
"rules.share.patch",
|
||||
async (request: NextRequest, context) => {
|
||||
if (!isDatabaseConfigured()) {
|
||||
return dbUnavailable();
|
||||
}
|
||||
const user = await getSessionUser();
|
||||
if (!user) {
|
||||
return unauthorized();
|
||||
}
|
||||
const { id } = await context.params;
|
||||
const parsedBody = await readLimitedJson(request);
|
||||
if (parsedBody.ok === false) {
|
||||
return parsedBody.response;
|
||||
}
|
||||
const validated = patchRuleShareBodySchema.safeParse(parsedBody.value);
|
||||
if (!validated.success) {
|
||||
return jsonFromZodError(validated.error);
|
||||
}
|
||||
const result = await patchOwnedRuleShare({
|
||||
ruleId: id,
|
||||
user,
|
||||
origin: getPublicOrigin(request),
|
||||
visibility: validated.data.visibility,
|
||||
communityAgreed: validated.data.communityAgreed,
|
||||
});
|
||||
if (result.ok === false) {
|
||||
if (result.reason === "not_found") return notFound();
|
||||
if (result.reason === "community_agreed") {
|
||||
return errorJson(
|
||||
"validation_error",
|
||||
"Our community agreed to share this Rule is required.",
|
||||
400,
|
||||
);
|
||||
}
|
||||
return forbidden("You do not have permission to change this rule");
|
||||
}
|
||||
return NextResponse.json({ share: result.settings });
|
||||
},
|
||||
);
|
||||
@@ -25,6 +25,7 @@ import { stakeholderInviteVerifyUrl } from "../../../lib/server/ruleStakeholderI
|
||||
import { STAKEHOLDER_INVITE_TTL_MS } from "../../../lib/server/ruleStakeholders";
|
||||
import { getSessionUser } from "../../../lib/server/session";
|
||||
import { issueGuestRuleClaimCookie } from "../../../lib/server/guestRuleClaim";
|
||||
import { linkUploadsToRule } from "../../../lib/server/uploads/linkUploads";
|
||||
import { apiRoute } from "../../../lib/server/apiRoute";
|
||||
import { getPublicOrigin } from "../../../lib/server/publicOrigin";
|
||||
import {
|
||||
@@ -123,6 +124,12 @@ export const POST = apiRoute(
|
||||
});
|
||||
}
|
||||
|
||||
await linkUploadsToRule({
|
||||
ruleId: rule.id,
|
||||
document,
|
||||
userId: null,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
rule: {
|
||||
id: rule.id,
|
||||
@@ -155,6 +162,11 @@ export const POST = apiRoute(
|
||||
document: document as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
await linkUploadsToRule({
|
||||
ruleId: rule.id,
|
||||
document,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
rule: {
|
||||
@@ -241,6 +253,12 @@ export const POST = apiRoute(
|
||||
);
|
||||
}
|
||||
|
||||
await linkUploadsToRule({
|
||||
ruleId: rule.id,
|
||||
document,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
rule: {
|
||||
id: rule.id,
|
||||
|
||||
Reference in new issue
Block a user