diff --git a/src/@types/cozy-client.d.ts b/src/@types/cozy-client.d.ts index ed01af8..a684e0a 100644 --- a/src/@types/cozy-client.d.ts +++ b/src/@types/cozy-client.d.ts @@ -145,10 +145,6 @@ declare module 'cozy-client' { }[] } - interface MissingFileDocumentAttributes { - md5sum: string - } - interface Collection { findReferencedBy: ( params: object diff --git a/src/search/SearchEngine.ts b/src/search/SearchEngine.ts index a173403..be9efaf 100644 --- a/src/search/SearchEngine.ts +++ b/src/search/SearchEngine.ts @@ -12,8 +12,6 @@ import { DOCTYPE_ORDER, LIMIT_DOCTYPE_SEARCH, REPLICATION_DEBOUNCE, - ROOT_DIR_ID, - SHARED_DRIVES_DIR_ID, SearchedDoctype } from '@/search/consts' import { getPouchLink } from '@/search/helpers/client' @@ -37,6 +35,8 @@ import { isSearchedDoctype } from '@/search/types' +import { shouldKeepFile } from './helpers/normalizeFile' + const log = Minilog('🗂️ [Indexing]') interface FlexSearchResultWithDoctype @@ -158,13 +158,7 @@ class SearchEngine { shouldIndexDoc(doc: CozyDoc): boolean { if (isIOCozyFile(doc)) { - const notInTrash = !doc.trashed && !/^\/\.cozy_trash/.test(doc.path ?? '') - const notRootDir = doc._id !== ROOT_DIR_ID - // Shared drives folder to be hidden in search. - // The files inside it though must appear. Thus only the file with the folder ID is filtered out. - const notSharedDrivesDir = doc._id !== SHARED_DRIVES_DIR_ID - - return notInTrash && notRootDir && notSharedDrivesDir + return shouldKeepFile(doc) } return true } diff --git a/src/search/helpers/normalizeFile.ts b/src/search/helpers/normalizeFile.ts index 5d0ac96..e58abbb 100644 --- a/src/search/helpers/normalizeFile.ts +++ b/src/search/helpers/normalizeFile.ts @@ -1,8 +1,18 @@ +import CozyClient, { Q } from 'cozy-client' import { IOCozyFile } from 'cozy-client/types/types' -import { TYPE_DIRECTORY } from '@/search/consts' +import { + FILES_DOCTYPE, + TYPE_DIRECTORY, + ROOT_DIR_ID, + SHARED_DRIVES_DIR_ID +} from '@/search/consts' import { CozyDoc } from '@/search/types' +interface FileQueryResult { + data: IOCozyFile +} + /** * Normalize file for Front usage in component inside * @@ -14,7 +24,7 @@ import { CozyDoc } from '@/search/types' * @param {IOCozyFile} file - file to normalize * @returns file with normalized field to be used in AutoSuggestion */ -export const normalizeFile = ( +export const normalizeFileWithFolders = ( folders: IOCozyFile[], file: IOCozyFile ): CozyDoc => { @@ -28,3 +38,34 @@ export const normalizeFile = ( } return { ...file, _type: 'io.cozy.files', path } } + +export const normalizeFileWithStore = async ( + client: CozyClient, + file: IOCozyFile +): Promise => { + const isDir = file.type === TYPE_DIRECTORY + let path = '' + if (isDir) { + path = file.path ?? '' + } else { + const query = Q(FILES_DOCTYPE).getById(file.dir_id).limitBy(1) + // XXX - Take advantage of cozy-client store to avoid querying database + const { data: parentDir } = (await client.query(query, { + executeFromStore: true, + singleDocData: true + })) as FileQueryResult + const parentPath = parentDir?.path ?? '' + path = `${parentPath}/${file.name}` + } + return { ...file, _type: 'io.cozy.files', path } +} + +export const shouldKeepFile = (file: IOCozyFile): boolean => { + const notInTrash = !file.trashed && !/^\/\.cozy_trash/.test(file.path ?? '') + const notRootDir = file._id !== ROOT_DIR_ID + // Shared drives folder to be hidden in search. + // The files inside it though must appear. Thus only the file with the folder ID is filtered out. + const notSharedDrivesDir = file._id !== SHARED_DRIVES_DIR_ID + + return notInTrash && notRootDir && notSharedDrivesDir +} diff --git a/src/search/helpers/normalizeSearchResult.ts b/src/search/helpers/normalizeSearchResult.ts index 5b76846..094378e 100644 --- a/src/search/helpers/normalizeSearchResult.ts +++ b/src/search/helpers/normalizeSearchResult.ts @@ -1,7 +1,7 @@ -import CozyClient, { Q, generateWebLink, models } from 'cozy-client' -import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types' +import CozyClient, { generateWebLink, models } from 'cozy-client' +import { IOCozyContact } from 'cozy-client/types/types' -import { APPS_DOCTYPE, FILES_DOCTYPE, TYPE_DIRECTORY } from '@/search/consts' +import { APPS_DOCTYPE, TYPE_DIRECTORY } from '@/search/consts' import { CozyDoc, RawSearchResult, @@ -11,9 +11,7 @@ import { SearchResult } from '@/search/types' -interface FileQueryResult { - data: IOCozyFile -} +import { normalizeFileWithStore } from './normalizeFile' export const normalizeSearchResult = async ( client: CozyClient, @@ -38,21 +36,8 @@ const normalizeDoc = async ( client: CozyClient, doc: CozyDoc ): Promise => { - if (!isIOCozyFile(doc)) { - return doc - } - if (!doc.path) { - const query = Q(FILES_DOCTYPE).getById(doc.dir_id).limitBy(1) - // XXX - Take advantage of cozy-client store to avoid querying database - const { data: parentDir } = (await client.query(query, { - executeFromStore: true, - singleDocData: true - })) as FileQueryResult - if (!parentDir) { - return doc - } - const path = `${parentDir.path}/${doc.name}` - return { ...doc, path } + if (isIOCozyFile(doc)) { + return normalizeFileWithStore(client, doc) } return doc } diff --git a/src/search/queries/index.ts b/src/search/queries/index.ts index fb14e39..d87ccd5 100644 --- a/src/search/queries/index.ts +++ b/src/search/queries/index.ts @@ -1,14 +1,11 @@ import CozyClient, { Q } from 'cozy-client' import { IOCozyFile } from 'cozy-client/types/types' +import { CONTACTS_DOCTYPE, APPS_DOCTYPE, TYPE_DIRECTORY } from '@/search/consts' import { - CONTACTS_DOCTYPE, - APPS_DOCTYPE, - TYPE_DIRECTORY, - ROOT_DIR_ID, - SHARED_DRIVES_DIR_ID -} from '@/search/consts' -import { normalizeFile } from '@/search/helpers/normalizeFile' + normalizeFileWithFolders, + shouldKeepFile +} from '@/search/helpers/normalizeFile' import { CozyDoc } from '@/search/types' interface DBRow { @@ -32,30 +29,9 @@ export const queryFilesForSearch = async ( const files = resp.rows.map(row => ({ id: row.id, ...row.doc }) as IOCozyFile) const folders = files.filter(file => file.type === TYPE_DIRECTORY) - const notInTrash = (file: IOCozyFile): boolean => - // @ts-expect-error TODO: file.trashed is not TS typed in CozyClient - !file.trashed && !/^\/\.cozy_trash/.test(file.path ?? '') - - const notOrphans = (file: IOCozyFile): boolean => - folders.find(folder => folder._id === file.dir_id) !== undefined - - const notRoot = (file: IOCozyFile): boolean => file._id !== ROOT_DIR_ID - - // Shared drives folder to be hidden in search. - // The files inside it though must appear. Thus only the file with the folder ID is filtered out. - const notSharedDrivesDir = (file: IOCozyFile): boolean => - file._id !== SHARED_DRIVES_DIR_ID - - const normalizedFilesPrevious = files.filter( - file => - notInTrash(file) && - notOrphans(file) && - notRoot(file) && - notSharedDrivesDir(file) - ) - - const normalizedFiles = normalizedFilesPrevious.map(file => - normalizeFile(folders, file) + const filteredFiles = files.filter(file => shouldKeepFile(file)) + const normalizedFiles = filteredFiles.map(file => + normalizeFileWithFolders(folders, file) ) return normalizedFiles