Skip to content

Commit

Permalink
fix(search): isNote should not check all metadata, missing while fetc…
Browse files Browse the repository at this point in the history
…hing

we don't fetch all metadata param of file, because this field may make requests much bigger

that's why we need to create a specific method to fix this problem
  • Loading branch information
trollepierre committed Aug 22, 2022
1 parent 4dcbc83 commit db97874
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/drive/web/modules/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ export const prepareSuggestionQuery = () => ({
'path',
'type',
'mime',
'metadata'
'metadata.title',
'metadata.version'
],
indexedFields: ['_id'],
limit: 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('SuggestionProvider', () => {
'path',
'type',
'mime',
'metadata'
'metadata.title',
'metadata.version'
],
indexedFields: ['_id'],
limit: 1000,
Expand Down
25 changes: 22 additions & 3 deletions src/drive/web/modules/services/components/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { models } from 'cozy-client'

export const TYPE_DIRECTORY = 'directory'
const FILE_TYPE = 'file'

/**
* Is file param a correct note
*
* Inspired by https://github.com/cozy/cozy-client/blob/b7ffdd97d017bbd52226e0b61099da3090d8253f/packages/cozy-client/src/models/file.js#L52
* but don't check metadata (not provided while fetching Suggestions)
*
* @param {IOCozyFile} file io.cozy.files
* @returns {boolean}
*/
export const isNote = file => {
if (
file &&
file.name &&
file.name.endsWith('.cozy-note') &&
file.type === FILE_TYPE
)
return true
return false
}

/**
* Normalize file for Front usage in <AutoSuggestion> component inside <SearchBar>
Expand All @@ -27,7 +46,7 @@ export const makeNormalizedFile = (client, folders, file, getIconUrl) => {
} else {
const parentDir = folders.find(folder => folder._id === file.dir_id)
path = parentDir && parentDir.path ? parentDir.path : ''
if (models.file.isNote(file)) {
if (isNote(file)) {
onSelect = `id_note:${file.id}`
} else {
url = `${urlToFolder}/file/${file._id}`
Expand Down
20 changes: 20 additions & 0 deletions src/drive/web/modules/services/components/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ describe('makeNormalizedFile', () => {
onSelect: 'id_note:noteId'
})
})

it('should return correct values for a note without metadata', () => {
const folders = [{ _id: 'folderId', path: 'folderPath' }]
const file = {
_id: 'fileId',
id: 'noteId',
dir_id: 'folderId',
type: 'file',
name: 'note.cozy-note'
}

const normalizedFile = makeNormalizedFile(client, folders, file, getIconUrl)

expect(normalizedFile).toMatchObject({
id: 'fileId',
name: 'note.cozy-note',
path: 'folderPath',
onSelect: 'id_note:noteId'
})
})
})
5 changes: 2 additions & 3 deletions src/drive/web/modules/services/components/iconContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TYPE_DIRECTORY } from './helpers'
import { models } from 'cozy-client'
import { isNote, TYPE_DIRECTORY } from './helpers'
import { isEncryptedFolder } from 'drive/lib/encryption'
import { getFileMimetype } from 'drive/lib/getFileMimetype'

Expand All @@ -23,7 +22,7 @@ export function getIconUrl(file) {
keyIcon = 'folder'
}
} else {
keyIcon = models.file.isNote(file)
keyIcon = isNote(file)
? 'note'
: getFileMimetype(icons)(file.mime, file.name) || 'files'
}
Expand Down

0 comments on commit db97874

Please sign in to comment.