-
Notifications
You must be signed in to change notification settings - Fork 2
feat: persist saved searches for authenticated users #44
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| CREATE TABLE IF NOT EXISTS "saved_search" ( | ||
| "id" text PRIMARY KEY NOT NULL, | ||
| "user_id" text NOT NULL, | ||
| "name" text NOT NULL, | ||
| "tech" text NOT NULL, | ||
| "label" text NOT NULL, | ||
| "sort" text NOT NULL, | ||
| "linked_pr" text NOT NULL, | ||
| "hacktoberfest" text NOT NULL, | ||
| "created_at" integer NOT NULL, | ||
| FOREIGN KEY ("user_id") REFERENCES "user"("id") ON UPDATE no action ON DELETE cascade | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS "saved_search_userId_idx" ON "saved_search" ("user_id"); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,25 @@ | ||
| import { and, eq } from "drizzle-orm"; | ||
| import { auth } from "@/lib/auth"; | ||
| import { savedSearch } from "@/lib/auth-schema"; | ||
| import { getDatabase } from "@/lib/db"; | ||
|
|
||
| export async function DELETE( | ||
| request: Request, | ||
| { params }: { params: Promise<{ id: string }> }, | ||
| ) { | ||
| const session = await auth.api.getSession({ headers: request.headers }); | ||
|
|
||
| if (!session) { | ||
| return Response.json({ error: "Unauthorized." }, { status: 401 }); | ||
| } | ||
|
|
||
| const { id } = await params; | ||
|
|
||
| await getDatabase() | ||
| .delete(savedSearch) | ||
| .where( | ||
| and(eq(savedSearch.id, id), eq(savedSearch.userId, session.user.id)), | ||
| ); | ||
|
|
||
| return new Response(null, { status: 204 }); | ||
| } |
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,87 @@ | ||
| import { asc, eq } from "drizzle-orm"; | ||
| import { auth } from "@/lib/auth"; | ||
| import { savedSearch } from "@/lib/auth-schema"; | ||
| import { getDatabase } from "@/lib/db"; | ||
| import { | ||
| isValidSavedSearch, | ||
| type SavedSearch, | ||
| } from "@/features/issues/lib/saved-searches"; | ||
|
|
||
| const MAX_SAVED_SEARCHES_PER_SYNC = 100; | ||
| const MAX_TEXT_LENGTH = 200; | ||
|
|
||
| function isSafeSavedSearch(value: unknown): value is SavedSearch { | ||
| if (!isValidSavedSearch(value)) return false; | ||
|
|
||
| return ( | ||
| value.id.length > 0 && | ||
| value.id.length <= MAX_TEXT_LENGTH && | ||
| value.name.length > 0 && | ||
| value.name.length <= MAX_TEXT_LENGTH && | ||
| value.tech.length > 0 && | ||
| value.tech.length <= MAX_TEXT_LENGTH && | ||
| !Number.isNaN(Date.parse(value.createdAt)) | ||
| ); | ||
| } | ||
|
|
||
| async function listSavedSearches(userId: string): Promise<SavedSearch[]> { | ||
| const rows = await getDatabase() | ||
| .select() | ||
| .from(savedSearch) | ||
| .where(eq(savedSearch.userId, userId)) | ||
| .orderBy(asc(savedSearch.createdAt)); | ||
|
|
||
| return rows.map((row) => ({ | ||
| id: row.id, | ||
| name: row.name, | ||
| tech: row.tech, | ||
| label: row.label, | ||
| sort: row.sort, | ||
| linkedPr: row.linkedPr, | ||
| hacktoberfest: row.hacktoberfest, | ||
| createdAt: row.createdAt.toISOString(), | ||
| })); | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| const session = await auth.api.getSession({ headers: request.headers }); | ||
|
|
||
| if (!session) { | ||
| return Response.json({ error: "Unauthorized." }, { status: 401 }); | ||
| } | ||
|
|
||
| let body: unknown; | ||
|
|
||
| try { | ||
| body = await request.json(); | ||
| } catch { | ||
| return Response.json({ error: "Invalid request body." }, { status: 400 }); | ||
| } | ||
|
|
||
| const searches = (body as { searches?: unknown } | null)?.searches; | ||
|
|
||
| if ( | ||
| !Array.isArray(searches) || | ||
| searches.length > MAX_SAVED_SEARCHES_PER_SYNC || | ||
| !searches.every(isSafeSavedSearch) | ||
| ) { | ||
| return Response.json({ error: "Invalid saved searches." }, { status: 400 }); | ||
| } | ||
|
|
||
| const database = getDatabase(); | ||
|
|
||
| for (const search of searches) { | ||
| await database | ||
| .insert(savedSearch) | ||
| .values({ | ||
| ...search, | ||
| userId: session.user.id, | ||
| createdAt: new Date(search.createdAt), | ||
| }) | ||
| .onConflictDoNothing(); | ||
| } | ||
|
|
||
| return Response.json({ | ||
| searches: await listSavedSearches(session.user.id), | ||
| }); | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,46 @@ | ||
| import type { SavedSearch } from "@/features/issues/lib/saved-searches"; | ||
|
|
||
| const MAX_SEARCHES_PER_REQUEST = 100; | ||
|
|
||
| export async function syncSavedSearches( | ||
| searches: SavedSearch[], | ||
| ): Promise<SavedSearch[]> { | ||
| const batches = searches.length | ||
| ? Array.from( | ||
| { length: Math.ceil(searches.length / MAX_SEARCHES_PER_REQUEST) }, | ||
| (_, index) => | ||
| searches.slice( | ||
| index * MAX_SEARCHES_PER_REQUEST, | ||
| (index + 1) * MAX_SEARCHES_PER_REQUEST, | ||
| ), | ||
| ) | ||
| : [[]]; | ||
| let syncedSearches: SavedSearch[] = []; | ||
|
|
||
| for (const batch of batches) { | ||
| const response = await fetch("/api/saved-searches", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ searches: batch }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error("Unable to sync saved searches."); | ||
| } | ||
|
|
||
| const result = (await response.json()) as { searches: SavedSearch[] }; | ||
| syncedSearches = result.searches; | ||
| } | ||
|
|
||
| return syncedSearches; | ||
| } | ||
|
|
||
| export async function deleteCloudSavedSearch(id: string): Promise<void> { | ||
| const response = await fetch(`/api/saved-searches/${encodeURIComponent(id)}`, { | ||
| method: "DELETE", | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error("Unable to remove the saved search from your account."); | ||
| } | ||
| } |
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
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.