Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit bcf9720

Browse files
committed
Add beaker.crawler.listSuggestions
1 parent a15b3f5 commit bcf9720

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

crawler/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ exports.resetSite = async function (url) {
126126
}
127127

128128
exports.WEBAPI = {
129+
listSuggestions: require('./search').listSuggestions,
129130
createEventsStream,
130131
getCrawlStates,
131132
crawlSite: async (url) => {

crawler/search.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

web-apis/fg/beaker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ exports.setup = function (rpc) {
167167

168168
// beaker.crawler
169169
beaker.crawler = {}
170+
beaker.crawler.listSuggestions = crawlerRPC.listSuggestions
170171
beaker.crawler.getCrawlStates = crawlerRPC.getCrawlStates
171172
beaker.crawler.crawlSite = crawlerRPC.crawlSite
172173
beaker.crawler.resetSite = crawlerRPC.resetSite

web-apis/manifests/internal/crawler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
listSuggestions: 'promise',
23
getCrawlStates: 'promise',
34
crawlSite: 'promise',
45
resetSite: 'promise',

0 commit comments

Comments
 (0)