Skip to content
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

Accept a filter in the GET bookmarks endpoint #105

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/routes/api/api-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"schema": {
"type": "object",
"properties": {
"username": {
"login": {
"type": "string",
"description": "User's username or email"
},
Expand Down Expand Up @@ -107,12 +107,37 @@
"required": false,
"schema": {
"type": "string",
"examples": ["id1,id2,id3"]
"examples": [
"id1,id2,id3"
]
},
"description": "Comma separated bookmark IDs to retrieve"
},
{
"in": "query",
"name": "url",
"required": false,
"schema": {
"type": "string",
"examples": [
"https://www.google.com"
]
},
"description": "URL of the bookmark to retrieve"
},
{
"in": "query",
"name": "filter",
"required": false,
"schema": {
"type": "string",
"examples": [
"Grimoire github"
]
},
"description": "A search term to filter the bookmarks to retrieve"
}
],

"responses": {
"200": {
"description": "A list of bookmarks",
Expand Down
25 changes: 25 additions & 0 deletions src/routes/api/bookmarks/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
UpdateBookmarkRequestBody
} from '$lib/types/api/Bookmarks.type';
import type Client from 'pocketbase';
import { initializeSearch, searchIndexKeys } from '$lib/utils/search';
const prepareRequestedTags = (
requestBody: AddBookmarkRequestBody | UpdateBookmarkRequestBody,
userTags: { id: string; name: string }[]
Expand Down Expand Up @@ -104,6 +105,27 @@ const getBookmarkByUrl = async (url: string, owner: string, pb: Client) => {
return removePocketbaseFields(records);
};

const getBookmarksByFilter = async (filter: string, owner: string, pb: Client) => {
let records = await pb
.collection('bookmarks')
.getFullList({
fields: 'id,' + searchIndexKeys.join(','),
expand: 'tags',
filter: `owner="${owner}"`,
batchSize: 100000
})
.then((res) =>
res.map(({ expand, ...b }) => ({
...expand,
...b
}))
);
records = removePocketbaseFields(records);
const searchEngine = initializeSearch(records);
const res = searchEngine.search(filter).map((b) => b.item);
return res;
}

export async function GET({ locals, url, request }) {
let owner = locals.pb.authStore.model?.id;
let bookmarks: Bookmark[] = [];
Expand All @@ -120,12 +142,15 @@ export async function GET({ locals, url, request }) {

const idsParam = url.searchParams.get('ids')?.split(',') || [];
const urlParam = url.searchParams.get('url');
const filterParam = url.searchParams.get('filter');

try {
if (urlParam) {
const bookmark = await getBookmarkByUrl(urlParam, owner, locals.pb);

bookmarks = bookmark ? [bookmark] : [];
} else if (filterParam) {
bookmarks = await getBookmarksByFilter(filterParam, owner, locals.pb);
} else {
bookmarks = await getBookmarksByIds(idsParam, owner, locals.pb);
}
Expand Down