Files
community-rule/lib/server/ruleShare.ts
T
adilallo 5cbda35e32 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.
2026-11-09 19:54:04 -07:00

168 lines
4.5 KiB
TypeScript

import type { User } from "@prisma/client";
import { RULE_VISIBILITY, type RuleVisibility } from "../ruleVisibility";
import {
emailInitials,
type RuleShareSettings,
type SharePerson,
} from "../ruleShareTypes";
import { formatShareUrlDisplay, shareCopyUrl } from "../sharePaths";
import { prisma } from "./db";
import { isRuleAdminEmail } from "./ruleAdmins";
import { allocateShareSlug } from "./shareSlug";
function toShareSettings(opts: {
visibility: RuleVisibility;
shareSlug: string | null;
title: string;
origin: string;
ruleId: string;
ownerEmail: string;
readers: SharePerson[];
}): RuleShareSettings {
const shareUrl = shareCopyUrl({
origin: opts.origin,
ruleId: opts.ruleId,
shareSlug: opts.shareSlug,
visibility: opts.visibility,
});
const shareUrlDisplay =
opts.shareSlug &&
(opts.visibility === RULE_VISIBILITY.shared_by_link ||
opts.visibility === RULE_VISIBILITY.public_and_featured)
? formatShareUrlDisplay(opts.origin, opts.shareSlug)
: null;
return {
visibility: opts.visibility,
shareSlug: opts.shareSlug,
shareUrl,
shareUrlDisplay,
title: opts.title,
ownerEmail: opts.ownerEmail,
ownerInitials: emailInitials(opts.ownerEmail),
readers: opts.readers,
copyEnabled: true,
};
}
export async function getOwnedRuleShareSettings(opts: {
ruleId: string;
user: User;
origin: string;
}): Promise<RuleShareSettings | "not_found" | "forbidden"> {
const row = await prisma.publishedRule.findUnique({
where: { id: opts.ruleId },
select: {
id: true,
title: true,
userId: true,
visibility: true,
shareSlug: true,
user: { select: { email: true } },
stakeholders: {
select: { email: true },
orderBy: [{ invitedAt: "asc" }, { id: "asc" }],
},
},
});
if (!row) return "not_found";
const isOwner = row.userId === opts.user.id;
const isAdmin = isRuleAdminEmail(opts.user.email);
if (!isOwner && !isAdmin) return "forbidden";
const ownerEmail = (
row.user?.email ?? opts.user.email
)
.trim()
.toLowerCase();
const readers: SharePerson[] = row.stakeholders
.map((s) => s.email.trim().toLowerCase())
.filter((email) => email.length > 0 && email !== ownerEmail)
.map((email) => ({
email,
initials: emailInitials(email),
role: "reader" as const,
}));
return toShareSettings({
visibility: row.visibility,
shareSlug: row.shareSlug,
title: row.title,
origin: opts.origin,
ruleId: row.id,
ownerEmail,
readers,
});
}
export async function patchOwnedRuleShare(opts: {
ruleId: string;
user: User;
origin: string;
visibility: RuleVisibility;
communityAgreed?: boolean;
}): Promise<
| { ok: true; settings: RuleShareSettings }
| { ok: false; reason: "not_found" | "forbidden" | "community_agreed" }
> {
const row = await prisma.publishedRule.findUnique({
where: { id: opts.ruleId },
select: {
id: true,
userId: true,
visibility: true,
shareSlug: true,
},
});
if (!row) return { ok: false, reason: "not_found" };
const isOwner = row.userId === opts.user.id;
const isAdmin = isRuleAdminEmail(opts.user.email);
if (!isOwner && !isAdmin) return { ok: false, reason: "forbidden" };
if (
opts.visibility === RULE_VISIBILITY.public_and_featured &&
!isAdmin
) {
return { ok: false, reason: "forbidden" };
}
if (
opts.visibility === RULE_VISIBILITY.shared_by_link &&
row.visibility !== RULE_VISIBILITY.shared_by_link &&
row.visibility !== RULE_VISIBILITY.public_and_featured &&
opts.communityAgreed !== true
) {
return { ok: false, reason: "community_agreed" };
}
if (!isOwner && opts.visibility !== RULE_VISIBILITY.public_and_featured) {
return { ok: false, reason: "forbidden" };
}
let shareSlug = row.shareSlug;
if (opts.visibility === RULE_VISIBILITY.private) {
shareSlug = null;
} else if (
opts.visibility === RULE_VISIBILITY.shared_by_link &&
(row.visibility === RULE_VISIBILITY.private || shareSlug == null)
) {
shareSlug = await allocateShareSlug();
}
await prisma.publishedRule.update({
where: { id: row.id },
data: {
visibility: opts.visibility,
shareSlug,
},
});
const settings = await getOwnedRuleShareSettings({
ruleId: row.id,
user: opts.user,
origin: opts.origin,
});
if (settings === "not_found" || settings === "forbidden") {
return { ok: false, reason: settings };
}
return { ok: true, settings };
}