Skip to content

Commit

Permalink
Fix annot shared state in dashboard results
Browse files Browse the repository at this point in the history
- Privacy level now returned with search results and proper state is derived in the UI logic
- Better, but more time consuming, solution would have been to add proper annotationsCache support to the dashboard here.
  • Loading branch information
poltak committed May 13, 2024
1 parent 413b166 commit 49200c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
16 changes: 10 additions & 6 deletions src/dashboard-refactor/logic.test.data.ts
@@ -1,5 +1,8 @@
import { PageData, NoteData } from './search-results/types'
import { StandardSearchResponse, AnnotPage } from 'src/search/background/types'
import {
StandardSearchResponse,
SearchResultPage,
} from 'src/search/background/types'
import { Annotation } from 'src/annotations/types'

interface TestMetadata {
Expand All @@ -12,15 +15,16 @@ const pageDataToSearchRes = (
metadata?: TestMetadata & {
notes?: Array<{ note: NoteData; metadata?: TestMetadata }>
},
): AnnotPage => ({
): SearchResultPage => ({
url: page.normalizedUrl,
fullUrl: page.fullUrl,
fullTitle: page.fullTitle,
// hasBookmark: false,
annotations:
metadata?.notes?.map(({ note, metadata }) =>
noteDataToSearchRes(note, page, metadata),
) ?? [],
// TODO : This type changed, though the tests aren't in a working state, so I'm just commenting it out for now. Needs to be fixed
annotations: [],
// metadata?.notes?.map(({ note, metadata }) =>
// noteDataToSearchRes(note, page, metadata),
// ) ?? [],
displayTime: page.displayTime,
lists: metadata?.lists ?? [],
text: page?.text ?? '',
Expand Down
13 changes: 8 additions & 5 deletions src/dashboard-refactor/search-results/util.ts
@@ -1,7 +1,8 @@
import type {
StandardSearchResponse,
AnnotPage,
SearchResultPage,
UnifiedSearchPaginationParams,
SearchResultAnnotation,
} from 'src/search/background/types'
import type {
PageData,
Expand All @@ -26,6 +27,7 @@ import type {
PageAnnotationsCacheInterface,
RGBAColor,
} from 'src/annotations/cache/types'
import { getAnnotationPrivacyState } from 'src/content-sharing/utils'

export const notesTypeToString = (type: NotesType): string => {
if (type === 'user') {
Expand Down Expand Up @@ -138,7 +140,7 @@ export const getInitialNoteResultState = (
})

const pageResultToPageData = (
pageResult: AnnotPage,
pageResult: SearchResultPage,
cache: PageAnnotationsCacheInterface,
): PageData => {
const isPdf = isMemexPageAPdf(pageResult)
Expand All @@ -162,7 +164,7 @@ const pageResultToPageData = (
}

const annotationToNoteData = (
annotation: Annotation,
annotation: SearchResultAnnotation,
cache: PageAnnotationsCacheInterface,
): NoteData & NoteResult => {
const lists =
Expand All @@ -171,6 +173,7 @@ const annotationToNoteData = (
(localListId) => cache.getListByLocalId(localListId)?.unifiedId,
)
.filter((id) => id != null) ?? []
const privacyState = getAnnotationPrivacyState(annotation.privacyLevel)
return {
url: annotation.url,
pageUrl: annotation.pageUrl,
Expand All @@ -185,8 +188,8 @@ const annotationToNoteData = (
annotation.lastEdited ?? annotation.createdWhen,
).getTime(),
isEdited: annotation.lastEdited != null,
isShared: annotation.isShared,
isBulkShareProtected: !!annotation.isBulkShareProtected,
isShared: privacyState.public,
isBulkShareProtected: privacyState.protected,
...getInitialNoteResultState(),
editNoteForm: {
inputValue: annotation.comment ?? '',
Expand Down
26 changes: 19 additions & 7 deletions src/search/background/index.ts
Expand Up @@ -6,13 +6,14 @@ import SearchStorage from './storage'
import { makeRemotelyCallable } from 'src/util/webextensionRPC'
import type {
RemoteSearchInterface,
AnnotPage,
SearchResultPage,
UnifiedSearchParams,
UnifiedBlankSearchParams,
UnifiedTermsSearchParams,
ResultDataByPage,
IntermediarySearchResult,
UnifiedSearchPaginationParams,
UnifiedSearchLookupData,
} from './types'
import { SearchError, BadTermError } from './errors'
import type { PageIndexingBackground } from 'src/page-indexing/background'
Expand Down Expand Up @@ -61,11 +62,6 @@ import { EVENT_PROVIDER_URLS } from '@worldbrain/memex-common/lib/constants'
import { TWITTER_URLS } from '@worldbrain/memex-common/lib/twitter-integration/constants'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'

type UnifiedSearchLookupData = {
pages: Map<string, Omit<AnnotPage, 'annotations' | 'hasBookmark'>>
annotations: Map<string, Annotation & { lists: number[] }>
}

export default class SearchBackground {
storage: SearchStorage
public remoteFunctions: RemoteSearchInterface
Expand All @@ -89,7 +85,11 @@ export default class SearchBackground {
}
}

static shapePageResult(results: AnnotPage[], limit: number, extra = {}) {
static shapePageResult(
results: SearchResultPage[],
limit: number,
extra = {},
) {
return {
resultsExhausted: results.length < limit,
totalCount: null, // TODO: try to get this implemented
Expand Down Expand Up @@ -812,13 +812,19 @@ export default class SearchBackground {
const [
pages,
annotations,
annotPrivacyLevels,
pageListEntries,
annotListEntries,
] = await Promise.all([
// TODO: these Dexie queries are here because the storex query didn't result in an indexed query happening
// Need to fix the bug in storex-backend-dexie when it comes time to port this and revert them to storex queries
dexie.table<Page>('pages').bulkGet(pageIds),
dexie.table<Annotation>('annotations').bulkGet(annotIds),
dexie
.table<AnnotationPrivacyLevel>('annotationPrivacyLevels')
.where('annotation')
.anyOf(annotIds)
.toArray(),
dexie
.table<PageListEntry, [number, string]>('pageListEntries')
.where('pageUrl')
Expand Down Expand Up @@ -890,6 +896,9 @@ export default class SearchBackground {
annotListEntries,
([, pageUrl]) => pageUrl,
)
const privacyLevelByAnnot = fromPairs(
annotPrivacyLevels.map((l) => [l.annotation, l.privacyLevel]),
)

const lookups: UnifiedSearchLookupData = {
annotations: new Map(),
Expand All @@ -910,6 +919,9 @@ export default class SearchBackground {
for (const annotation of annotations) {
lookups.annotations.set(annotation.url, {
...annotation,
privacyLevel:
privacyLevelByAnnot[annotation.url] ??
AnnotationPrivacyLevels.PROTECTED,
lists: (listIdsByAnnot[annotation.url] ?? []).map(
([listId]) => listId,
),
Expand Down
19 changes: 15 additions & 4 deletions src/search/background/types.ts
Expand Up @@ -3,16 +3,22 @@ import type SearchStorage from './storage'
import type { Annotation } from 'src/annotations/types'
import type { PageIndexingBackground } from 'src/page-indexing/background'
import type { Annotation as _Annotation } from '@worldbrain/memex-common/lib/types/core-data-types/client'
import type { AnnotationPrivacyLevels } from '@worldbrain/memex-common/lib/annotations/types'

export interface AnnotPage {
export type SearchResultAnnotation = Annotation & {
lists: number[]
privacyLevel: AnnotationPrivacyLevels
}

export interface SearchResultPage {
url: string
fullUrl: string | null
fullPdfUrl?: string
fullTitle?: string
/** Object URL to the in-memory location of the assoc. fav-icon. */
favIcon?: string
displayTime: number
annotations: Annotation[]
annotations: SearchResultAnnotation[]
totalAnnotationsCount: number
pageId?: string
lists: number[]
Expand Down Expand Up @@ -77,7 +83,7 @@ export interface SocialSearchParams extends AnnotSearchParams {
export interface StandardSearchResponse {
resultsExhausted: boolean
totalCount?: number
docs: AnnotPage[]
docs: SearchResultPage[]
isBadTerm?: boolean
}

Expand Down Expand Up @@ -153,7 +159,7 @@ export type UnifiedBlankSearchParams = UnifiedSearchParams &
}

export type UnifiedSearchResult = {
docs: AnnotPage[]
docs: SearchResultPage[]
resultsExhausted: boolean
oldestResultTimestamp: number
}
Expand All @@ -180,3 +186,8 @@ export type UnifiedSearchPageResultData = {
*/
oldestTimestamp: number
}

export type UnifiedSearchLookupData = {
pages: Map<string, Omit<SearchResultPage, 'annotations' | 'hasBookmark'>>
annotations: Map<string, SearchResultAnnotation>
}

0 comments on commit 49200c6

Please sign in to comment.