Skip to content

Commit

Permalink
refactor: Mutualize file normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
paultranvan committed Oct 24, 2024
1 parent 4f31903 commit b05ce08
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 67 deletions.
4 changes: 0 additions & 4 deletions src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ declare module 'cozy-client' {
}[]
}

interface MissingFileDocumentAttributes {
md5sum: string
}

interface Collection {
findReferencedBy: (
params: object
Expand Down
12 changes: 3 additions & 9 deletions src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -37,6 +35,8 @@ import {
isSearchedDoctype
} from '@/search/types'

import { shouldKeepFile } from './helpers/normalizeFile'

const log = Minilog('🗂️ [Indexing]')

interface FlexSearchResultWithDoctype
Expand Down Expand Up @@ -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
}
Expand Down
45 changes: 43 additions & 2 deletions src/search/helpers/normalizeFile.ts
Original file line number Diff line number Diff line change
@@ -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 <AutoSuggestion> component inside <BarSearchAutosuggest>
*
Expand All @@ -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 => {
Expand All @@ -28,3 +38,34 @@ export const normalizeFile = (
}
return { ...file, _type: 'io.cozy.files', path }
}

export const normalizeFileWithStore = async (
client: CozyClient,
file: IOCozyFile
): Promise<IOCozyFile> => {
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
}
27 changes: 6 additions & 21 deletions src/search/helpers/normalizeSearchResult.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,9 +11,7 @@ import {
SearchResult
} from '@/search/types'

interface FileQueryResult {
data: IOCozyFile
}
import { normalizeFileWithStore } from './normalizeFile'

export const normalizeSearchResult = async (
client: CozyClient,
Expand All @@ -38,21 +36,8 @@ const normalizeDoc = async (
client: CozyClient,
doc: CozyDoc
): Promise<CozyDoc> => {
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
}
Expand Down
38 changes: 7 additions & 31 deletions src/search/queries/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit b05ce08

Please sign in to comment.