Files
community-rule/lib/ruleVisibility.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

89 lines
2.6 KiB
TypeScript

export const RULE_VISIBILITY = {
private: "private",
shared_by_link: "shared_by_link",
public_and_featured: "public_and_featured",
} as const;
export type RuleVisibility =
(typeof RULE_VISIBILITY)[keyof typeof RULE_VISIBILITY];
export function isRuleVisibility(value: unknown): value is RuleVisibility {
return (
value === RULE_VISIBILITY.private ||
value === RULE_VISIBILITY.shared_by_link ||
value === RULE_VISIBILITY.public_and_featured
);
}
/** Listed / indexed / sitemap: public and featured only. */
export function isListedPublishedRule(row: {
visibility: RuleVisibility;
}): boolean {
return row.visibility === RULE_VISIBILITY.public_and_featured;
}
export type PublishedRuleReadAccess = {
visibility: RuleVisibility;
/** True when the request used `/r/{shareSlug}` (or treated a file as shared-by-link). */
viaShareSlug: boolean;
userId: string | null;
ownerUserId: string | null;
viewerEmail: string | null;
invitedEmails: string[];
invitedUserIds: string[];
isAdmin: boolean;
holdsGuestClaim: boolean;
};
function isOwner(opts: PublishedRuleReadAccess): boolean {
return opts.userId != null && opts.userId === opts.ownerUserId;
}
function isInvited(opts: PublishedRuleReadAccess): boolean {
if (opts.userId != null && opts.invitedUserIds.includes(opts.userId)) {
return true;
}
if (opts.viewerEmail != null && opts.invitedEmails.includes(opts.viewerEmail)) {
return true;
}
return false;
}
/**
* Who can read a published Rule.
*
* Private: owner, invited people, guest-claim browser, named admins.
* Shared by link: those readers plus anyone who arrived via the rotating slug.
* Public and featured: anyone.
*/
export function canReadPublishedRule(opts: PublishedRuleReadAccess): boolean {
if (isOwner(opts) || opts.holdsGuestClaim) return true;
if (isInvited(opts)) return true;
if (opts.isAdmin) return true;
if (opts.visibility === RULE_VISIBILITY.public_and_featured) return true;
if (
opts.visibility === RULE_VISIBILITY.shared_by_link &&
opts.viaShareSlug
) {
return true;
}
return false;
}
/**
* True when the viewer is a named admin who would not otherwise be able to
* read the Rule (so the read must be logged).
*/
export function isPrivilegedAdminRead(opts: PublishedRuleReadAccess): boolean {
if (!opts.isAdmin) return false;
if (isOwner(opts) || opts.holdsGuestClaim || isInvited(opts)) return false;
if (opts.visibility === RULE_VISIBILITY.public_and_featured) return false;
if (
opts.visibility === RULE_VISIBILITY.shared_by_link &&
opts.viaShareSlug
) {
return false;
}
return true;
}