From 6d1b352989919c08d26114465e656f4f70fcdbfb Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Fri, 12 May 2023 08:12:19 +0200 Subject: [PATCH] Fix spaces in search results --- .../bugfix-spaces-in-search-results | 6 ++++ packages/web-app-files/src/search/sdk/list.ts | 16 +++++++-- .../web-app-files/src/search/sdk/preview.ts | 17 ++++++--- .../tests/unit/search/sdk.spec.ts | 35 +++++++++---------- .../web-client/src/helpers/space/functions.ts | 1 + 5 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 changelog/unreleased/bugfix-spaces-in-search-results diff --git a/changelog/unreleased/bugfix-spaces-in-search-results b/changelog/unreleased/bugfix-spaces-in-search-results new file mode 100644 index 00000000000..aeccd64b55d --- /dev/null +++ b/changelog/unreleased/bugfix-spaces-in-search-results @@ -0,0 +1,6 @@ +Bugfix: Spaces in search results + +Spaces in search results are no longer being displayed as folder resources, fixing wrong icons and sidebar panels. + +https://github.com/owncloud/web/issues/9022 +https://github.com/owncloud/web/pull/9026 diff --git a/packages/web-app-files/src/search/sdk/list.ts b/packages/web-app-files/src/search/sdk/list.ts index 47004c9c37f..e900ea072a4 100644 --- a/packages/web-app-files/src/search/sdk/list.ts +++ b/packages/web-app-files/src/search/sdk/list.ts @@ -1,10 +1,11 @@ import { SearchList, SearchResult } from 'web-app-search/src/types' import ListComponent from '../../components/Search/List.vue' import { ClientService } from 'web-pkg/src/services' -import { buildResource } from 'web-client/src/helpers' +import { ProjectSpaceResource, buildResource, isProjectSpaceResource } from 'web-client/src/helpers' import { Component } from 'vue' -import { DavProperties } from 'web-client/src/webdav/constants' +import { DavProperties, DavProperty } from 'web-client/src/webdav/constants' import { Store } from 'vuex' +import { computed, Ref, unref } from 'vue' export const searchLimit = 200 @@ -12,11 +13,19 @@ export default class List implements SearchList { public readonly component: Component private readonly store: Store private readonly clientService: ClientService + private readonly projectSpaces: Ref constructor(store: Store, clientService: ClientService) { this.component = ListComponent this.store = store this.clientService = clientService + this.projectSpaces = computed(() => + this.store.getters['runtime/spaces/spaces'].filter((s) => isProjectSpaceResource(s)) + ) + } + + getMatchingSpace(resourceId): ProjectSpaceResource { + return unref(this.projectSpaces).find(({ id }) => resourceId.startsWith(id)) } async search(term: string): Promise { @@ -36,7 +45,8 @@ export default class List implements SearchList { return { totalResults: range ? parseInt(range?.split('/')[1]) : null, values: results.map((result) => { - const resource = buildResource(result) + const matchingSpace = this.getMatchingSpace(result.fileInfo[DavProperty.FileId]) + const resource = !!matchingSpace ? matchingSpace : buildResource(result) // info: in oc10 we have no storageId in resources. All resources are mounted into the personal space. if (!resource.storageId) { resource.storageId = this.store.getters.user.id diff --git a/packages/web-app-files/src/search/sdk/preview.ts b/packages/web-app-files/src/search/sdk/preview.ts index 5a8cdf06d7c..a7a14cc7ff6 100644 --- a/packages/web-app-files/src/search/sdk/preview.ts +++ b/packages/web-app-files/src/search/sdk/preview.ts @@ -1,11 +1,11 @@ import { SearchPreview, SearchResult } from 'web-app-search/src/types' import PreviewComponent from '../../components/Search/Preview.vue' import { ClientService } from 'web-pkg/src/services' -import { buildResource } from 'web-client/src/helpers' +import { ProjectSpaceResource, buildResource, isProjectSpaceResource } from 'web-client/src/helpers' import { Cache } from 'web-pkg/src/helpers/cache' -import { Component, unref } from 'vue' +import { Component, computed, Ref, unref } from 'vue' import { Router } from 'vue-router' -import { DavProperties } from 'web-client/src/webdav/constants' +import { DavProperties, DavProperty } from 'web-client/src/webdav/constants' import { Store } from 'vuex' export const previewSearchLimit = 8 @@ -16,6 +16,7 @@ export default class Preview implements SearchPreview { private readonly router: Router private readonly store: Store private readonly clientService: ClientService + private readonly projectSpaces: Ref constructor(store: Store, router: Router, clientService: ClientService) { this.component = PreviewComponent @@ -24,6 +25,13 @@ export default class Preview implements SearchPreview { // define how long the cache should be valid, maybe conf option? this.cache = new Cache({ ttl: 10000, capacity: 100 }) this.clientService = clientService + this.projectSpaces = computed(() => + this.store.getters['runtime/spaces/spaces'].filter((s) => isProjectSpaceResource(s)) + ) + } + + getMatchingSpace(resourceId): ProjectSpaceResource { + return unref(this.projectSpaces).find(({ id }) => resourceId.startsWith(id)) } public async search(term: string): Promise { @@ -45,7 +53,8 @@ export default class Preview implements SearchPreview { DavProperties.Default ) const resources = results.reduce((acc, result) => { - const resource = buildResource(result) + const matchingSpace = this.getMatchingSpace(result.fileInfo[DavProperty.FileId]) + const resource = !!matchingSpace ? matchingSpace : buildResource(result) // info: in oc10 we have no storageId in resources. All resources are mounted into the personal space. if (!resource.storageId) { resource.storageId = this.store.getters.user.id diff --git a/packages/web-app-files/tests/unit/search/sdk.spec.ts b/packages/web-app-files/tests/unit/search/sdk.spec.ts index fd80c53f519..188d5362655 100644 --- a/packages/web-app-files/tests/unit/search/sdk.spec.ts +++ b/packages/web-app-files/tests/unit/search/sdk.spec.ts @@ -4,6 +4,7 @@ import { Store } from 'vuex' import { RouteLocation, Router } from 'vue-router' import { mock, mockDeep } from 'jest-mock-extended' import { ref } from 'vue' +import { createStore, defaultStoreMockOptions } from 'web-test-helpers/src' const searchMock = jest.fn() const clientService = mockDeep() @@ -13,16 +14,12 @@ jest.mock('web-client/src/helpers', () => ({ buildResource: (v) => v })) -const store = mockDeep>({ - getters: { - user: () => ({ id: 1 }), - capabilities: { - dav: { - reports: ['search-files'] - } - } - } -}) +const getStore = (spaces = []) => { + const storeOptions = defaultStoreMockOptions + storeOptions.getters.capabilities.mockReturnValue({ dav: { reports: ['search-files'] } }) + storeOptions.modules.runtime.modules.spaces.getters.spaces.mockReturnValue(spaces) + return createStore(storeOptions) +} const storeWithoutFileSearch = mockDeep>({ getters: { capabilities: { dav: { reports: [] } } } @@ -42,7 +39,7 @@ describe('SDKProvider', () => { { route: 'bar', available: true } ].forEach((v) => { const search = new SDKSearch( - store, + getStore(), mock({ currentRoute: ref(mock({ name: v.route })) }), @@ -54,11 +51,11 @@ describe('SDKProvider', () => { }) it('can search', async () => { - const search = new SDKSearch(store, mock(), clientService) + const search = new SDKSearch(getStore(), mock(), clientService) const files = [ - { id: 'foo', name: 'foo' }, - { id: 'bar', name: 'bar' }, - { id: 'baz', name: 'baz' } + { id: 'foo', name: 'foo', fileInfo: {} }, + { id: 'bar', name: 'bar', fileInfo: {} }, + { id: 'baz', name: 'baz', fileInfo: {} } ] const noTerm = await search.previewSearch.search('') @@ -74,11 +71,11 @@ describe('SDKProvider', () => { }) describe('SDKProvider listSearch', () => { it('can search', async () => { - const search = new SDKSearch(store, mock(), clientService) + const search = new SDKSearch(getStore(), mock(), clientService) const files = [ - { id: 'foo', name: 'foo' }, - { id: 'bar', name: 'bar' }, - { id: 'baz', name: 'baz' } + { id: 'foo', name: 'foo', fileInfo: {} }, + { id: 'bar', name: 'bar', fileInfo: {} }, + { id: 'baz', name: 'baz', fileInfo: {} } ] searchMock.mockReturnValueOnce({ results: files }) diff --git a/packages/web-client/src/helpers/space/functions.ts b/packages/web-client/src/helpers/space/functions.ts index 74bcc50fee9..eec47cc5f81 100644 --- a/packages/web-client/src/helpers/space/functions.ts +++ b/packages/web-client/src/helpers/space/functions.ts @@ -145,6 +145,7 @@ export function buildSpace(data): SpaceResource { mdate: data.lastModifiedDateTime, size: data.quota?.used, indicators: [], + tags: [], permissions: '', starred: false, etag: '',