|
| 1 | +const bookmarksDb = require('../dbs/bookmarks') |
| 2 | +const historyDb = require('../dbs/history') |
| 3 | +const datLibrary = require('../dat/library') |
| 4 | + |
| 5 | +const BUILTIN_PAGES = [ |
| 6 | + {title: 'Feed', url: 'beaker://feed'}, |
| 7 | + {title: 'Library', url: 'beaker://library'}, |
| 8 | + {title: 'Search', url: 'beaker://search'}, |
| 9 | + {title: 'Bookmarks', url: 'beaker://bookmarks'}, |
| 10 | + {title: 'History', url: 'beaker://history'}, |
| 11 | + {title: 'Watchlist', url: 'beaker://watchlist'}, |
| 12 | + {title: 'Downloads', url: 'beaker://downloads'}, |
| 13 | + {title: 'Settings', url: 'beaker://settings'}, |
| 14 | +] |
| 15 | + |
| 16 | +// exported api |
| 17 | +// = |
| 18 | + |
| 19 | +exports.listSuggestions = async function (query = '', opts = {}) { |
| 20 | + var suggestions = {} |
| 21 | + const filterFn = a => ((a.url || a.href).includes(query) || a.title.toLowerCase().includes(query)) |
| 22 | + |
| 23 | + // builtin pages |
| 24 | + suggestions.apps = BUILTIN_PAGES.filter(filterFn) |
| 25 | + |
| 26 | + // bookmarks |
| 27 | + var bookmarkResults = await bookmarksDb.listBookmarks(0) |
| 28 | + if (opts.filterPins) { |
| 29 | + bookmarkResults = bookmarkResults.filter(b => !b.pinned && filterFn(b)) |
| 30 | + } else { |
| 31 | + bookmarkResults = bookmarkResults.filter(filterFn) |
| 32 | + } |
| 33 | + bookmarkResults = bookmarkResults.slice(0, 12) |
| 34 | + suggestions.bookmarks = bookmarkResults.map(b => ({title: b.title, url: b.href})) |
| 35 | + |
| 36 | + // library |
| 37 | + var libraryResults = await datLibrary.queryArchives({isSaved: true}) |
| 38 | + libraryResults = libraryResults.filter(filterFn) |
| 39 | + suggestions.library = libraryResults.slice(0, 12) |
| 40 | + |
| 41 | + // fetch history |
| 42 | + if (query) { |
| 43 | + var historyResults = await historyDb.search(query) |
| 44 | + suggestions.history = historyResults.slice(0, 12) |
| 45 | + suggestions.history.sort((a, b) => a.url.length - b.url.length) // shorter urls at top |
| 46 | + } |
| 47 | + |
| 48 | + return suggestions |
| 49 | +} |
0 commit comments