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
+138
-32
@@ -1,14 +1,22 @@
|
||||
import type { User } from "@prisma/client";
|
||||
import {
|
||||
LISTED_PUBLISHED_RULE_WHERE,
|
||||
canReadPublishedRule,
|
||||
isListedPublishedRule,
|
||||
isPrivilegedAdminRead,
|
||||
type RuleVisibility,
|
||||
} from "../publishedRuleAccess";
|
||||
import { prisma } from "./db";
|
||||
import { isDatabaseConfigured } from "./env";
|
||||
import { browserHoldsClaimForHash } from "./guestRuleClaim";
|
||||
import { isRuleAdminEmail } from "./ruleAdmins";
|
||||
import { logAdminRuleRead } from "./adminRuleReadLog";
|
||||
|
||||
/**
|
||||
* Public fields safe to expose via the unauthenticated rule detail surfaces
|
||||
* (`GET /api/rules/[id]` and `/rules/[id]`). `userId` and `claimTokenHash`
|
||||
* stay server-side; callers get {@link PublicPublishedRule.isListed} instead.
|
||||
* Public fields safe to expose via readable rule detail surfaces
|
||||
* (`GET /api/rules/[id]` and `/rules/[id]` / `/r/[shareSlug]`).
|
||||
* `userId` and `claimTokenHash` stay server-side; callers get
|
||||
* {@link PublicPublishedRule.isListed} instead.
|
||||
*/
|
||||
const PUBLISHED_RULE_PUBLIC_SELECT = {
|
||||
id: true,
|
||||
@@ -26,38 +34,136 @@ export type PublicPublishedRule = {
|
||||
document: unknown;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
/** False for guest-claim rows; true for owned and orphaned-after-delete rows. */
|
||||
/** True only for public-and-featured Rules. */
|
||||
isListed: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch a single published rule by id for public read surfaces.
|
||||
*
|
||||
* Returns `null` when the database is not configured, the id does not match
|
||||
* any row, or the query throws — callers render a 404 in all missing cases
|
||||
* and are expected to surface the "DB not configured" state separately if
|
||||
* they care about distinguishing it (the API route does; the page does not).
|
||||
*/
|
||||
export async function getPublicPublishedRuleById(
|
||||
id: string,
|
||||
): Promise<PublicPublishedRule | null> {
|
||||
if (!isDatabaseConfigured()) return null;
|
||||
if (typeof id !== "string" || id.trim() === "") return null;
|
||||
try {
|
||||
const rule = await prisma.publishedRule.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
...PUBLISHED_RULE_PUBLIC_SELECT,
|
||||
userId: true,
|
||||
claimTokenHash: true,
|
||||
},
|
||||
export type ReadablePublishedRule = {
|
||||
rule: PublicPublishedRule;
|
||||
viewerIsOwner: boolean;
|
||||
visibility: RuleVisibility;
|
||||
shareSlug: string | null;
|
||||
};
|
||||
|
||||
const RULE_ACCESS_SELECT = {
|
||||
...PUBLISHED_RULE_PUBLIC_SELECT,
|
||||
userId: true,
|
||||
claimTokenHash: true,
|
||||
visibility: true,
|
||||
shareSlug: true,
|
||||
stakeholders: {
|
||||
select: { email: true, userId: true },
|
||||
},
|
||||
} as const;
|
||||
|
||||
function toPublicRule(row: {
|
||||
id: string;
|
||||
title: string;
|
||||
summary: string | null;
|
||||
document: unknown;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
visibility: RuleVisibility;
|
||||
}): PublicPublishedRule {
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
summary: row.summary,
|
||||
document: row.document,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
isListed: isListedPublishedRule({ visibility: row.visibility }),
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveAccessForRow(opts: {
|
||||
row: {
|
||||
id: string;
|
||||
title: string;
|
||||
summary: string | null;
|
||||
document: unknown;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
userId: string | null;
|
||||
claimTokenHash: string | null;
|
||||
visibility: RuleVisibility;
|
||||
shareSlug: string | null;
|
||||
stakeholders: Array<{ email: string; userId: string | null }>;
|
||||
};
|
||||
viaShareSlug: boolean;
|
||||
user: User | null;
|
||||
requestPath: string;
|
||||
}): Promise<ReadablePublishedRule | null> {
|
||||
const { row, viaShareSlug, user, requestPath } = opts;
|
||||
const viewerEmail = user?.email.trim().toLowerCase() ?? null;
|
||||
const invitedEmails = row.stakeholders.map((s) => s.email.toLowerCase());
|
||||
const invitedUserIds = row.stakeholders
|
||||
.map((s) => s.userId)
|
||||
.filter((id): id is string => typeof id === "string" && id.length > 0);
|
||||
const isAdmin = isRuleAdminEmail(user?.email ?? null);
|
||||
const holdsGuestClaim = await browserHoldsClaimForHash(row.claimTokenHash);
|
||||
const access = {
|
||||
visibility: row.visibility,
|
||||
viaShareSlug,
|
||||
userId: user?.id ?? null,
|
||||
ownerUserId: row.userId,
|
||||
viewerEmail,
|
||||
invitedEmails,
|
||||
invitedUserIds,
|
||||
isAdmin,
|
||||
holdsGuestClaim,
|
||||
};
|
||||
if (!canReadPublishedRule(access)) return null;
|
||||
if (isPrivilegedAdminRead(access) && viewerEmail) {
|
||||
await logAdminRuleRead({
|
||||
ruleId: row.id,
|
||||
adminEmail: viewerEmail,
|
||||
adminUserId: user?.id ?? null,
|
||||
path: requestPath,
|
||||
});
|
||||
}
|
||||
return {
|
||||
rule: toPublicRule(row),
|
||||
viewerIsOwner: user != null && row.userId === user.id,
|
||||
visibility: row.visibility,
|
||||
shareSlug: row.shareSlug,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a published rule the viewer is allowed to read.
|
||||
*
|
||||
* Lookup is by internal id unless `viaShareSlug` is set. Shared-by-link
|
||||
* strangers must use the rotating slug so turning the link off invalidates it.
|
||||
* Returns `null` for missing rows, denied readers, and DB failures — callers
|
||||
* render the same 404 as a missing Rule.
|
||||
*/
|
||||
export async function getReadablePublishedRule(opts: {
|
||||
idOrSlug: string;
|
||||
user: User | null;
|
||||
viaShareSlug: boolean;
|
||||
requestPath: string;
|
||||
}): Promise<ReadablePublishedRule | null> {
|
||||
if (!isDatabaseConfigured()) return null;
|
||||
const idOrSlug = opts.idOrSlug.trim();
|
||||
if (idOrSlug.length === 0) return null;
|
||||
try {
|
||||
const row = opts.viaShareSlug
|
||||
? await prisma.publishedRule.findUnique({
|
||||
where: { shareSlug: idOrSlug },
|
||||
select: RULE_ACCESS_SELECT,
|
||||
})
|
||||
: await prisma.publishedRule.findUnique({
|
||||
where: { id: idOrSlug },
|
||||
select: RULE_ACCESS_SELECT,
|
||||
});
|
||||
if (!row) return null;
|
||||
return resolveAccessForRow({
|
||||
row,
|
||||
viaShareSlug: opts.viaShareSlug,
|
||||
user: opts.user,
|
||||
requestPath: opts.requestPath,
|
||||
});
|
||||
if (!rule) return null;
|
||||
const { userId, claimTokenHash, ...publicFields } = rule;
|
||||
return {
|
||||
...publicFields,
|
||||
isListed: isListedPublishedRule({ userId, claimTokenHash }),
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
@@ -89,7 +195,7 @@ const LISTED_PUBLISHED_RULE_SITEMAP_CAP = 10_000;
|
||||
|
||||
/**
|
||||
* Public catalog of listed published rules (**most recently updated first**,
|
||||
* then stable `id` tie-break). Guest-claim rows are omitted.
|
||||
* then stable `id` tie-break). Only public-and-featured rows.
|
||||
* Returns `null` when the database is not configured or the query throws.
|
||||
*/
|
||||
export async function listListedPublishedRules(
|
||||
|
||||
Reference in new issue
Block a user