-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathUserSearchMixin.js
More file actions
227 lines (209 loc) Β· 5.73 KB
/
Copy pathUserSearchMixin.js
File metadata and controls
227 lines (209 loc) Β· 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { getCurrentUser } from '@nextcloud/auth'
import axios from '@nextcloud/axios'
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { generateOcsUrl } from '@nextcloud/router'
import debounce from 'debounce'
import { INPUT_DEBOUNCE_MS } from '../models/Constants.ts'
import logger from '../utils/Logger.ts'
import OcsResponse2Data from '../utils/OcsResponse2Data.ts'
import ShareTypes from './ShareTypes.js'
export default {
mixins: [ShareTypes],
data() {
return {
loading: false,
query: '',
// TODO: have a global mixin for this, shared with server?
maxAutocompleteResults:
parseInt(OC.config['sharing.maxAutocompleteResults'], 10) || 200,
minSearchStringLength:
parseInt(OC.config['sharing.minSearchStringLength'], 10) || 0,
// Search Results
recommendations: [],
suggestions: [],
}
},
computed: {
/**
* Is the search query valid ?
*
* @return {boolean}
*/
isValidQuery() {
return (
this.query
&& this.query.trim() !== ''
&& this.query.length > this.minSearchStringLength
)
},
/**
* Text when there is no Results to be shown
*
* @return {string}
*/
noResultText() {
if (!this.query) {
return t('forms', 'No recommendations. Start typing.')
}
return t('forms', 'No elements found.')
},
},
methods: {
/**
* Search for suggestions
*
* @param {string} query The search query to search for
* @param {number[]|undefined} shareType The type of recipient to search.
*/
async asyncSearch(query, shareType) {
// save query to check if valid
this.query = query.trim()
if (this.isValidQuery) {
// already set loading to have proper ux feedback during debounce
this.loading = true
this.debounceGetSuggestions(query, shareType)
}
},
/**
* Debounce getSuggestions
*
* @param {...*} args arguments to pass
*/
debounceGetSuggestions: debounce(function (...args) {
this.getSuggestions(...args)
}, INPUT_DEBOUNCE_MS),
/**
* Get suggestions
*
* @param {string} query the search query
* @param {number[]|undefined} shareType The type of recipient to search.
*/
async getSuggestions(query, shareType) {
this.loading = true
// Search for all used share-types, except public link.
shareType ??= this.SHARE_TYPES_USED.filter(
(type) => type !== this.SHARE_TYPES.SHARE_TYPE_LINK,
)
try {
const request = await axios.get(
generateOcsUrl('apps/files_sharing/api/v1/sharees'),
{
params: {
format: 'json',
itemType: 'file',
perPage: this.maxAutocompleteResults,
search: query,
shareType,
},
},
)
const data = OcsResponse2Data(request)
const exact = data.exact
delete data.exact // removing exact from general results
const exactSuggestions = this.formatSearchResults(exact)
const suggestions = this.formatSearchResults(data)
this.suggestions = exactSuggestions.concat(suggestions)
} catch (error) {
logger.error('Loading Suggestions failed.', { error })
} finally {
this.loading = false
}
},
/**
* Get the sharing recommendations
*/
async getRecommendations() {
this.loading = true
try {
const request = await axios.get(
generateOcsUrl('apps/files_sharing/api/v1/sharees_recommended'),
{
params: {
format: 'json',
itemType: 'file',
},
},
)
this.recommendations = this.formatSearchResults(
OcsResponse2Data(request).exact,
)
} catch (error) {
logger.error('Fetching recommendations failed.', { error })
} finally {
this.loading = false
}
},
/**
* A OCS Sharee response
*
* @typedef {{label: string, shareWithDisplayNameUnique: string, value: { shareType: number, shareWith: string }, status?: unknown }} Sharee
*/
/**
* Format search results
*
* @param {Record<string, Sharee>} results Results as returned by search
* @return {Sharee[]} results as we use them on storage
*/
formatSearchResults(results) {
// flatten array of arrays
const flatResults = Object.values(results).flat()
return (
this.filterUnwantedShares(flatResults)
.map((share) => this.formatForMultiselect(share))
// sort by type so we can get user&groups first...
.sort((a, b) => a.shareType - b.shareType)
)
},
/**
* Remove static unwanted shares from search results
* Existing shares must be done dynamically to account for new shares.
*
* @param {Sharee[]} shares the array of share objects
* @return {Sharee[]}
*/
filterUnwantedShares(shares) {
return shares.filter((share) => {
// only use proper objects
if (typeof share !== 'object') {
return false
}
try {
// filter out current user
if (
share.value.shareType === this.SHARE_TYPES.SHARE_TYPE_USER
&& share.value.shareWith === getCurrentUser().uid
) {
return false
}
// All good, let's add the suggestion
return true
} catch {
return false
}
})
},
/**
* Format shares for the multiselect options
*
* @param {Sharee} share Share in search formatting
* @return {object} Share in multiselect formatting
*/
formatForMultiselect(share) {
return {
shareWith: share.value.shareWith,
shareType: share.value.shareType,
user: share.value.shareWith,
isNoUser: share.value.shareType !== this.SHARE_TYPES.SHARE_TYPE_USER,
id: share.value.shareWith,
displayName: share.label,
subname: share.shareWithDisplayNameUnique,
iconSvg: this.shareTypeToIcon(share.value.shareType),
// Vue unique binding to render within Multiselect's AvatarSelectOption
key: share.value.shareWith + '-' + share.value.shareType,
}
},
},
}