Files
Epicure/apps/web/lib/visibility.ts
T
Arnaud 9da57dd1d0 feat: collections visibility enum (matches recipes) + QR on collection PDF (v0.54.0)
Replaces collections.isPublic (boolean) with collections.visibility
(private/unlisted/public/followers — same enum recipes use). Two-step
migration (0050 adds+backfills, 0051 drops isPublic) since drizzle-kit's
add+drop-in-one-diff rename heuristic needs an interactive prompt we
can't satisfy here.

New collectionVisibleToViewer(viewerId) in lib/visibility.ts mirrors the
existing recipe helper (author always sees own; public/unlisted visible
to anyone; followers-only via the same user_follows EXISTS pattern) —
used by the collection detail page, its print view, fork, and favorite,
replacing their old `or(isPublic, own)` checks.

Create/edit collection dialogs get the same 4-option visibility select
as the recipe form instead of a public/private checkbox.

Collection PDF export now generates a QR code (qrcode, same as the
recipe PDF) linking to /collections/{id}, shown only when visibility is
public/unlisted — same "would an anonymous scanner actually resolve
this" rule as the recipe QR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-19 19:26:34 +02:00

40 lines
1.7 KiB
TypeScript

import { recipes, collections, eq, or, and, inArray, sql } from "@epicure/db";
/**
* Drizzle condition: true when `viewerId` may view a given recipe row — the
* author, always; `public`/`unlisted` recipes, to anyone; `followers`-only
* recipes, only to viewers who follow the author. Meant to be `and()`ed with
* an `eq(recipes.id, id)` (or similar) in a single query, so the DB does the
* filtering rather than fetching content the viewer can't see.
*
* Uses a literal `user_follows` identifier rather than a Drizzle column
* proxy for that table — inside `db.query.recipes.findFirst`/`findMany`'s
* `where`, the relational query builder rewrites embedded column refs to
* the outer query's own table alias regardless of which table they actually
* belong to (same issue documented in recipes/page.tsx's search filter),
* which would otherwise silently produce broken SQL referencing a column
* that doesn't exist on `recipes`.
*/
export function visibleToViewer(viewerId: string) {
return or(
eq(recipes.authorId, viewerId),
inArray(recipes.visibility, ["public", "unlisted"]),
and(
eq(recipes.visibility, "followers"),
sql`exists (select 1 from user_follows where user_follows.follower_id = ${viewerId} and user_follows.following_id = ${recipes.authorId})`
)
);
}
/** Same rationale/pattern as visibleToViewer(), for collections. */
export function collectionVisibleToViewer(viewerId: string) {
return or(
eq(collections.userId, viewerId),
inArray(collections.visibility, ["public", "unlisted"]),
and(
eq(collections.visibility, "followers"),
sql`exists (select 1 from user_follows where user_follows.follower_id = ${viewerId} and user_follows.following_id = ${collections.userId})`
)
);
}