From 89dedb8ac199b23c196a1e51aa09a228eccb99b2 Mon Sep 17 00:00:00 2001 From: Cozy Pierre Date: Fri, 5 Aug 2022 10:42:52 +0200 Subject: [PATCH] refactor(queries): extract queries in correct file --- src/drive/web/modules/queries.js | 42 ++++++++++++++++++- .../components/SuggestionProvider.jsx | 41 ++---------------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/drive/web/modules/queries.js b/src/drive/web/modules/queries.js index f1592dad6c..46d4216eaf 100644 --- a/src/drive/web/modules/queries.js +++ b/src/drive/web/modules/queries.js @@ -1,6 +1,6 @@ import CozyClient, { Q } from 'cozy-client' import { TRASH_DIR_ID } from 'drive/constants/config' -import { DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes' +import { DOCTYPE_FILES, DOCTYPE_FILES_ENCRYPTION } from 'drive/lib/doctypes' // Needs to be less than 10 minutes, since "thumbnails" links // are only valid for 10 minutes. @@ -325,6 +325,46 @@ export const buildEncryptionByIdQuery = id => ({ } }) +/** + * Query to get all the files not trashed + * @returns {{definition: QueryDefinition}} + */ +export const buildSuggestionsQuery = () => ({ + definition: Q(DOCTYPE_FILES) + .partialIndex({ + _id: { + $ne: TRASH_DIR_ID + }, + trashed: { + $or: [ + { + $exists: false + }, + { + $eq: false + } + ] + }, + path: { + $or: [ + { + $exists: false + }, + { + $regex: '^(?!/.cozy_trash)' + } + ] + } + }) + .select(['_id', 'trashed', 'dir_id', 'name', 'path']) + .where({ + _id: { + $gt: null + } + }) + .limitBy(1000) +}) + export { buildDriveQuery, buildRecentQuery, diff --git a/src/drive/web/modules/services/components/SuggestionProvider.jsx b/src/drive/web/modules/services/components/SuggestionProvider.jsx index 4eeea76113..2ba184bcfa 100644 --- a/src/drive/web/modules/services/components/SuggestionProvider.jsx +++ b/src/drive/web/modules/services/components/SuggestionProvider.jsx @@ -1,11 +1,10 @@ import React from 'react' import FuzzyPathSearch from '../FuzzyPathSearch' -import { withClient, Q } from 'cozy-client' +import { withClient } from 'cozy-client' -import { DOCTYPE_FILES } from 'drive/lib/doctypes' import { TYPE_DIRECTORY, makeNormalizedFile } from './helpers' import { getIconUrl } from './iconContext' -import { TRASH_DIR_ID } from 'drive/constants/config' +import { buildSuggestionsQuery } from '../../queries' class SuggestionProvider extends React.Component { componentDidMount() { @@ -51,41 +50,7 @@ class SuggestionProvider extends React.Component { async indexFiles() { const { client } = this.props - const files = await client.queryAll( - Q(DOCTYPE_FILES) - .partialIndex({ - _id: { - $ne: TRASH_DIR_ID - }, - trashed: { - $or: [ - { - $exists: false - }, - { - $eq: false - } - ] - }, - path: { - $or: [ - { - $exists: false - }, - { - $regex: '^(?!/.cozy_trash)' - } - ] - } - }) - .select(['_id', 'trashed', 'dir_id', 'name', 'path']) - .where({ - _id: { - $gt: null - } - }) - .limitBy(1000) - ) + const files = await client.queryAll(buildSuggestionsQuery().definition) const folders = files.filter(file => file.type === TYPE_DIRECTORY)