-
Notifications
You must be signed in to change notification settings - Fork 0
Notify viewers when documents update #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ae9932
Notify viewers when documents update
ehfeng 6058cf1
Test document update polling
ehfeng d129d59
Harden document version polling
ehfeng 62676dd
Remove deprecated test renderer
ehfeng b2e1311
Cache document version checks
ehfeng ba361d9
Compact update prompt on mobile
ehfeng a403259
Harden document update checks
ehfeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| findVersionBySlug: vi.fn(), | ||
| canViewSession: vi.fn(), | ||
| getSessionReadOnly: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/docs/version-cache", () => ({ findVersionBySlug: mocks.findVersionBySlug })); | ||
| vi.mock("@/lib/docs/access", async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import("@/lib/docs/access")>()), | ||
| canViewSession: mocks.canViewSession, | ||
| })); | ||
| vi.mock("@/lib/auth/session", () => ({ getSessionReadOnly: mocks.getSessionReadOnly })); | ||
|
|
||
| import { GET } from "@/app/d/[slug]/version/route"; | ||
|
|
||
| const doc = { | ||
| id: 1, | ||
| owner_id: 2, | ||
| is_public: false, | ||
| view_token: "secret-token", | ||
| version: 7, | ||
| }; | ||
|
|
||
| const session = { id: 3, email: "viewer@example.com", user_id: 4 }; | ||
|
|
||
| function request(viewtoken?: string) { | ||
| const query = viewtoken ? `?viewtoken=${viewtoken}` : ""; | ||
| return new Request(`https://justhtml.sh/d/quiet-moon-12345/version${query}`); | ||
| } | ||
|
|
||
| const ctx = { params: Promise.resolve({ slug: "quiet-moon-12345" }) }; | ||
|
|
||
| describe("document version", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.findVersionBySlug.mockResolvedValue(doc); | ||
| mocks.getSessionReadOnly.mockResolvedValue(session); | ||
| mocks.canViewSession.mockResolvedValue(true); | ||
| }); | ||
|
|
||
| it("returns the current version without browser caching", async () => { | ||
| const res = await GET(request(), ctx); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(await res.text()).toBe("7"); | ||
| expect(res.headers.get("Cache-Control")).toBe("private, no-store"); | ||
| expect(mocks.getSessionReadOnly).toHaveBeenCalledOnce(); | ||
| expect(mocks.canViewSession).toHaveBeenCalledWith(doc, session, null); | ||
| }); | ||
|
|
||
| it("skips session resolution for a valid view token", async () => { | ||
| const res = await GET(request("secret-token"), ctx); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(mocks.getSessionReadOnly).not.toHaveBeenCalled(); | ||
| expect(mocks.canViewSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("skips session resolution for a public document", async () => { | ||
| mocks.findVersionBySlug.mockResolvedValueOnce({ ...doc, is_public: true }); | ||
|
|
||
| const res = await GET(request(), ctx); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(mocks.getSessionReadOnly).not.toHaveBeenCalled(); | ||
| expect(mocks.canViewSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not distinguish missing and unauthorized documents", async () => { | ||
| mocks.findVersionBySlug.mockResolvedValueOnce(null); | ||
| const missing = await GET(request(), ctx); | ||
|
|
||
| mocks.canViewSession.mockResolvedValueOnce(false); | ||
| const unauthorized = await GET(request(), ctx); | ||
|
|
||
| expect(missing.status).toBe(404); | ||
| expect(unauthorized.status).toBe(404); | ||
| expect(await missing.text()).toBe(""); | ||
| expect(await unauthorized.text()).toBe(""); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { getSessionReadOnly } from "@/lib/auth/session"; | ||
| import { canView, canViewSession } from "@/lib/docs/access"; | ||
| import { findVersionBySlug } from "@/lib/docs/version-cache"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| type Ctx = { params: Promise<{ slug: string }> }; | ||
|
|
||
| function notFound(): Response { | ||
| return new Response(null, { status: 404 }); | ||
| } | ||
|
|
||
| export async function GET(req: Request, ctx: Ctx): Promise<Response> { | ||
| const { slug } = await ctx.params; | ||
| const doc = await findVersionBySlug(slug); | ||
| if (!doc) return notFound(); | ||
|
ehfeng marked this conversation as resolved.
|
||
|
|
||
| const viewtoken = new URL(req.url).searchParams.get("viewtoken"); | ||
| if (!canView(doc, viewtoken)) { | ||
| const session = await getSessionReadOnly(req); | ||
| if (!(await canViewSession(doc, session, null))) return notFound(); | ||
| } | ||
|
|
||
| return new Response(String(doc.version), { | ||
| headers: { | ||
| "Content-Type": "text/plain; charset=utf-8", | ||
| "Cache-Control": "private, no-store", | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ query: vi.fn() })); | ||
|
|
||
| vi.mock("@/lib/db", () => ({ query: mocks.query })); | ||
|
|
||
| import { getSession, getSessionReadOnly } from "@/lib/auth/session"; | ||
|
|
||
| function request() { | ||
| return new Request("https://justhtml.sh/d/test/version", { | ||
| headers: { cookie: "jh_sess=sess_test-token" }, | ||
| }); | ||
| } | ||
|
|
||
| describe("session lookup", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.query.mockResolvedValue({ | ||
| rows: [ | ||
| { | ||
| id: 1, | ||
| email: "viewer@example.com", | ||
| user_id: 2, | ||
| last_seen_at: "2000-01-01T00:00:00.000Z", | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("validates expiry and revocation without renewing a read-only session", async () => { | ||
| await expect(getSessionReadOnly(request())).resolves.toEqual({ | ||
| id: 1, | ||
| email: "viewer@example.com", | ||
| user_id: 2, | ||
| }); | ||
|
|
||
| expect(mocks.query).toHaveBeenCalledOnce(); | ||
| expect(mocks.query.mock.calls[0][0]).toContain("revoked_at IS NULL AND expires_at > now()"); | ||
| expect(mocks.query.mock.calls[0][0]).not.toContain("UPDATE sessions"); | ||
| }); | ||
|
|
||
| it("preserves sliding expiry for normal session lookup", async () => { | ||
| await getSession(request()); | ||
|
|
||
| expect(mocks.query).toHaveBeenCalledTimes(2); | ||
| expect(mocks.query.mock.calls[1][0]).toContain("UPDATE sessions"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.