Skip to content

Commit

Permalink
Accept a filter in the GET bookmarks endpoint (#105)
Browse files Browse the repository at this point in the history
* Add filter parameter to get bookmarks endpoint
* Update OAS.
Fix auth endpoint's body.
* Remove debug log
  • Loading branch information
pe1uca committed May 13, 2024
1 parent 7f92c7e commit d1aaaae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
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

0 comments on commit d1aaaae

Please sign in to comment.