fix(admin): let moderators preview a post before approving it - #1340
Conversation
The "in review" queue showed a title, author and excerpt but gave no way
to read the post, so there was nothing to base an Approve/Decline on.
Each queued item now has a Preview link that opens where the post
actually renders: /d/{slug} for discussions and questions, the
destination itself for shared links, and /{username}/{slug} for
everything the site renders. For that last case the reader resolvers now
grant admins the bypass authors already had for their own in_review and
rejected posts, so the preview is the article exactly as readers would
eventually see it, "Awaiting review" banner and all.
That visibility rule was about to be a third copy of the same
published-or-owner check, so it moves to server/lib/postVisibility.ts
with unit tests covering the anonymous, author and admin branches — the
author predicate is the only thing stopping one member reading another's
drafts, so it is worth pinning down.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Uh oh! @vercel[bot], the image you shared is missing helpful alt text. Check #1340 (comment). Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image. Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs. |
WalkthroughThis change centralizes post visibility rules, passes administrator context to post pages, and adds preview links for posts in the moderation queue. ChangesModeration preview visibility
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant AdminModerationQueue
participant PreviewLink
participant PostPage
participant postVisibilityFilter
AdminModerationQueue->>PreviewLink: provide post type and destination fields
PreviewLink->>PostPage: navigate to selected preview route
PostPage->>postVisibilityFilter: provide viewer ID and administrator role
postVisibilityFilter-->>PostPage: return visibility predicate
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/`(admin)/admin/moderation/_client.tsx:
- Around line 55-70: Update postPreviewHref and the RSS import path to validate
external links with safeExternalHref before navigation or persistence. For link
posts, pass post.externalUrl through safeExternalHref and return null for
rejected schemes; ensure RSS imports also reject invalid item.link schemes
rather than storing them in posts.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 6e6e3da8-4c83-4d41-bf3b-421f01f4b4b2
📒 Files selected for processing (7)
app/(admin)/admin/moderation/_client.tsxapp/(app)/[username]/[slug]/page.tsxapp/(app)/d/[slug]/page.tsxcomponents/ContentDetail/PostReader.tsxserver/api/router/admin.tsserver/lib/postVisibility.test.tsserver/lib/postVisibility.ts
| function postPreviewHref(post: PreviewablePost): string | null { | ||
| if (post.type === "link") return post.externalUrl; | ||
| if (!post.slug) return null; | ||
| if (post.type === "discussion" || post.type === "question") { | ||
| return `/d/${post.slug}`; | ||
| } | ||
| if (!post.authorUsername) return null; | ||
| return `/${post.authorUsername}/${post.slug}`; | ||
| } | ||
|
|
||
| const PreviewLink = ({ post }: { post: PreviewablePost }) => { | ||
| const href = postPreviewHref(post); | ||
| if (!href) return null; | ||
|
|
||
| return ( | ||
| <Link href={href} target="_blank" className="secondary-button"> |
There was a problem hiding this comment.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Locate the schema and all TypeScript write/read paths for external URLs.
fd -t f -e ts -e tsx | while IFS= read -r file; do
rg -n -C 3 'externalUrl|external_url' "$file" || true
doneRepository: codu-code/codu
Length of output: 35426
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- httpUrl and safeExternalHref definitions ---'
rg -n -C 8 'function httpUrl|const httpUrl|export .*httpUrl|safeExternalHref' .
printf '%s\n' '--- post write handlers and schemas ---'
rg -l 'externalUrl' --glob '*.ts' --glob '*.tsx' | while IFS= read -r file; do
if rg -q 'insert\(posts\)|update\(posts\)|INSERT INTO posts|externalUrl: input\.externalUrl|externalUrl: item\.link|externalUrl = ' "$file"; then
printf '\n### %s\n' "$file"
rg -n -C 12 'insert\(posts\)|update\(posts\)|INSERT INTO posts|externalUrl: input\.externalUrl|externalUrl: item\.link|externalUrl = ' "$file"
fi
done
printf '%s\n' '--- URL normalization and direct navigation helpers ---'
rg -n -C 8 'normalizeUrl|ensureHttps|new URL\(|startsWith\("http"\)|startsWith\('\''http' "$(
rg -l 'externalUrl' --glob '*.ts' --glob '*.tsx' .
)" || trueRepository: codu-code/codu
Length of output: 16142
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- URL schema and render guard ---'
cat -n schema/shared.ts | sed -n '1,35p'
cat -n utils/url.ts | sed -n '1,70p'
printf '%s\n' '--- direct post write sites ---'
rg -l --glob '*.ts' --glob '*.tsx' \
'externalUrl: input\.externalUrl|externalUrl: item\.link|INSERT INTO posts|createLinkPost' . |
while IFS= read -r file; do
printf '\n### %s\n' "$file"
rg -n -C 18 \
'externalUrl: input\.externalUrl|externalUrl: item\.link|INSERT INTO posts|createLinkPost' \
"$file"
done
printf '%s\n' '--- all externalUrl validation declarations ---'
rg -n -C 6 \
'externalUrl: (httpUrl\(\)|z\.string\(\)\.url|z\.string\(\)\.trim|item\.link|input\.externalUrl)' \
--glob '*.ts' --glob '*.tsx' .Repository: codu-code/codu
Length of output: 17334
Guard externalUrl before navigation.
RSS importers persist item.link directly to posts, so httpUrl() does not cover every write path. Use safeExternalHref before passing externalUrl to Link, and reject invalid schemes during RSS imports.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@app/`(admin)/admin/moderation/_client.tsx around lines 55 - 70, Update
postPreviewHref and the RSS import path to validate external links with
safeExternalHref before navigation or persistence. For link posts, pass
post.externalUrl through safeExternalHref and return null for rejected schemes;
ensure RSS imports also reject invalid item.link schemes rather than storing
them in posts.
|
Uh oh! @vercel[bot], the image you shared is missing helpful alt text. Check #1340 (comment). Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image. Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs. |
What
The in review queue at
/admin/moderationlisted a title, author and (now) an excerpt, but there was no way to actually read the post — so Approve/Decline was a judgement call with nothing to judge.Each queued item now has a Preview link that opens where the post really renders:
discussion,question/d/{slug}link/{username}/{slug}For the last case the reader resolvers now grant admins the bypass authors already had for their own
in_review/rejectedposts, so a moderator sees the article exactly as readers eventually will — "Awaiting review" banner included — rather than a bespoke admin rendering that could drift from the real thing.Why the shared filter
That published-or-owner rule was about to exist in a third place, in a third shape. It moves to
server/lib/postVisibility.tsand both reader resolvers call it.The author predicate in there is the only thing stopping one member reading another's drafts, so it gets unit tests: anonymous sees live posts only, a signed-in member only bypasses for their own posts, an admin bypasses for any author, and no viewer ever sees
draft.Verified locally
in_reviewpost; signed-in non-author → 404; author → 200; admin → 200.linkpost moved into review (which would otherwise 404, since the link resolver is published-only).npm run lint,npm run prettier,npm run test:unit(123 passing),npm run build.