Skip to content

Commit

Permalink
Merge pull request #1048 from WorldBrain/feature/improved-remote-inde…
Browse files Browse the repository at this point in the history
…xing

Allow remote apps to add page to lists while indexing a page
  • Loading branch information
ShishKabab committed Jun 4, 2020
2 parents 9201c69 + 23ec8dc commit 1223dff
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 24 deletions.
69 changes: 52 additions & 17 deletions src/background-script/setup.ts
Expand Up @@ -191,6 +191,15 @@ export function createBackgroundModules(options: {
}).processor
: undefined

const customLists = new CustomListBackground({
storageManager,
queryTabs: bindMethod(options.browserAPIs.tabs, 'query'),
windows: options.browserAPIs.windows,
searchIndex: search.searchIndex,
pageStorage: pages.storage,
localBrowserStorage: options.browserAPIs.storage.local,
})

return {
auth,
social,
Expand All @@ -208,14 +217,7 @@ export function createBackgroundModules(options: {
}),
search,
eventLog: new EventLogBackground({ storageManager }),
customLists: new CustomListBackground({
storageManager,
queryTabs: bindMethod(options.browserAPIs.tabs, 'query'),
windows: options.browserAPIs.windows,
searchIndex: search.searchIndex,
pageStorage: pages.storage,
localBrowserStorage: options.browserAPIs.storage.local,
}),
customLists,
tags,
bookmarks,
backupModule: new backup.BackupBackgroundModule({
Expand Down Expand Up @@ -243,18 +245,51 @@ export function createBackgroundModules(options: {
localBrowserStorage: options.browserAPIs.storage.local,
fetchPageData: options.fetchPageDataProcessor,
storePageContent,
addVisit: (visit) => pages.addVisit(visit.url, visit.time),
addVisit: (visit) =>
pages.addVisit(visit.normalizedUrl, visit.time),
addBookmark: async (bookmark) => {
if (!(await bookmarks.storage.pageHasBookmark(bookmark.url))) {
await bookmarks.addBookmark(bookmark)
if (
!(await bookmarks.storage.pageHasBookmark(
bookmark.normalizedUrl,
))
) {
await bookmarks.addBookmark({
url: bookmark.normalizedUrl,
time: bookmark.time,
})
}
},
addTags: (params) =>
Promise.all(
params.tags.map((tag) =>
tags.addTagToPage({ url: params.url, tag }),
),
),
addTags: async (params) => {
const existingTags = await tags.storage.fetchPageTags({
url: params.normalizedUrl,
})
await Promise.all(
params.tags.map(async (tag) => {
if (!existingTags.includes(tag)) {
await tags.addTagToPage({
url: params.normalizedUrl,
tag,
})
}
}),
)
},
addToLists: async (params) => {
const existingEntries = await customLists.storage.fetchListIdsByUrl(
params.normalizedUrl,
)
await Promise.all(
params.lists.map(async (listId) => {
if (!existingEntries.includes(listId)) {
await customLists.storage.insertPageToList({
listId,
pageUrl: params.normalizedUrl,
fullUrl: params.fullUrl,
})
}
}),
)
},
}),
features: new FeatureOptIns(),
pages,
Expand Down
5 changes: 5 additions & 0 deletions src/custom-lists/background/storage.ts
Expand Up @@ -211,6 +211,11 @@ export default class CustomListStorage extends StorageModule {
return this.operation('findListEntriesByListId', { listId })
}

async fetchListIdsByUrl(url: string): Promise<number[]> {
const entries = await this.operation('findListEntriesByUrl', { url })
return entries.map((entry) => entry.listId)
}

async fetchListPagesByUrl({ url }: { url: string }) {
const pages = await this.operation('findListEntriesByUrl', { url })

Expand Down
54 changes: 47 additions & 7 deletions src/storex-hub/background/index.ts
Expand Up @@ -7,6 +7,7 @@ import {
StorexHubCallbacks_v0,
HandleRemoteCallResult_v0,
} from '@worldbrain/storex-hub/lib/public-api'
import { normalizeUrl } from '@worldbrain/memex-url-utils'
import {
FetchPageProcessor,
PageContent,
Expand All @@ -32,9 +33,26 @@ export class StorexHubBackground {
fetchPageData: FetchPageProcessor
storePageContent: (content: PageContent) => Promise<void>
localBrowserStorage: Storage.LocalStorageArea
addVisit: (visit: { url: string; time: number }) => void
addBookmark: (bookmark: { url: string; time: number }) => void
addTags: (params: { url: string; tags: string[] }) => void
addVisit: (visit: {
normalizedUrl: string
fullUrl: string
time: number
}) => Promise<void>
addBookmark: (bookmark: {
normalizedUrl: string
fullUrl: string
time: number
}) => Promise<void>
addTags: (params: {
normalizedUrl: string
fullUrl: string
tags: string[]
}) => Promise<void>
addToLists: (params: {
normalizedUrl: string
fullUrl: string
lists: number[]
}) => Promise<void>
},
) {
this.logger = console['log'].bind(console)
Expand Down Expand Up @@ -101,10 +119,13 @@ export class StorexHubBackground {
return { status: 'invalid-args' }
}

const fullUrl = args.url
const normalizedUrl = normalizeUrl(fullUrl)

let processedData: PipelineRes
try {
processedData = await this.dependencies.fetchPageData.process(
args.url,
fullUrl,
)
} catch (e) {
return {
Expand All @@ -126,7 +147,8 @@ export class StorexHubBackground {

try {
await this.dependencies.addVisit({
url: args.url,
fullUrl,
normalizedUrl,
time: args.visitTime ?? Date.now(),
})
} catch (e) {
Expand All @@ -144,7 +166,8 @@ export class StorexHubBackground {
? null
: args.bookmark.creationTime
await this.dependencies.addBookmark({
url: args.url,
fullUrl,
normalizedUrl,
time: time || Date.now(),
})
}
Expand All @@ -159,7 +182,8 @@ export class StorexHubBackground {
try {
if (args.tags) {
await this.dependencies.addTags({
url: args.url,
fullUrl,
normalizedUrl,
tags: args.tags,
})
}
Expand All @@ -171,6 +195,22 @@ export class StorexHubBackground {
}
}

try {
if (args.lists) {
await this.dependencies.addToLists({
fullUrl,
normalizedUrl,
lists: args.lists,
})
}
} catch (e) {
return {
status: 'internal-error',
errorStatus: 'could-not-list',
errorText: `Error while adding page to lists: ${e}`,
}
}

return { status: 'success', result: {} }
}

Expand Down
1 change: 1 addition & 0 deletions src/storex-hub/background/types.ts
Expand Up @@ -3,4 +3,5 @@ export interface IndexPageArgs {
visitTime?: number
bookmark?: true | { creationTime: number }
tags?: string[]
lists?: number[]
}

0 comments on commit 1223dff

Please sign in to comment.