Skip to content

Commit

Permalink
Fix spaces in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed May 12, 2023
1 parent 327dd53 commit 6d1b352
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/bugfix-spaces-in-search-results
Original file line number Diff line number Diff line change
@@ -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
16 changes: 13 additions & 3 deletions packages/web-app-files/src/search/sdk/list.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
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

export default class List implements SearchList {
public readonly component: Component
private readonly store: Store<any>
private readonly clientService: ClientService
private readonly projectSpaces: Ref<ProjectSpaceResource[]>

constructor(store: Store<any>, 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<SearchResult> {
Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions packages/web-app-files/src/search/sdk/preview.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +16,7 @@ export default class Preview implements SearchPreview {
private readonly router: Router
private readonly store: Store<any>
private readonly clientService: ClientService
private readonly projectSpaces: Ref<ProjectSpaceResource[]>

constructor(store: Store<any>, router: Router, clientService: ClientService) {
this.component = PreviewComponent
Expand All @@ -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<SearchResult> {
Expand All @@ -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
Expand Down
35 changes: 16 additions & 19 deletions packages/web-app-files/tests/unit/search/sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientService>()
Expand All @@ -13,16 +14,12 @@ jest.mock('web-client/src/helpers', () => ({
buildResource: (v) => v
}))

const store = mockDeep<Store<any>>({
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<Store<any>>({
getters: { capabilities: { dav: { reports: [] } } }
Expand All @@ -42,7 +39,7 @@ describe('SDKProvider', () => {
{ route: 'bar', available: true }
].forEach((v) => {
const search = new SDKSearch(
store,
getStore(),
mock<Router>({
currentRoute: ref(mock<RouteLocation>({ name: v.route }))
}),
Expand All @@ -54,11 +51,11 @@ describe('SDKProvider', () => {
})

it('can search', async () => {
const search = new SDKSearch(store, mock<Router>(), clientService)
const search = new SDKSearch(getStore(), mock<Router>(), 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('')
Expand All @@ -74,11 +71,11 @@ describe('SDKProvider', () => {
})
describe('SDKProvider listSearch', () => {
it('can search', async () => {
const search = new SDKSearch(store, mock<Router>(), clientService)
const search = new SDKSearch(getStore(), mock<Router>(), 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 })
Expand Down
1 change: 1 addition & 0 deletions packages/web-client/src/helpers/space/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export function buildSpace(data): SpaceResource {
mdate: data.lastModifiedDateTime,
size: data.quota?.used,
indicators: [],
tags: [],
permissions: '',
starred: false,
etag: '',
Expand Down

0 comments on commit 6d1b352

Please sign in to comment.