Skip to content

Commit e5e0afc

Browse files
fix: retrieve all display names with one request
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent 28a2965 commit e5e0afc

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

apps/files_versions/src/components/Version.vue

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,10 @@ import { Permission, formatFileSize } from '@nextcloud/files'
122122
import { loadState } from '@nextcloud/initial-state'
123123
import { t } from '@nextcloud/l10n'
124124
import { joinPaths } from '@nextcloud/paths'
125-
import { getRootUrl, generateUrl } from '@nextcloud/router'
125+
import { getRootUrl } from '@nextcloud/router'
126126
import { defineComponent } from 'vue'
127127
128-
import axios from '@nextcloud/axios'
129128
import moment from '@nextcloud/moment'
130-
import logger from '../utils/logger'
131129
132130
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
133131
import Delete from 'vue-material-design-icons/Delete.vue'
@@ -204,7 +202,6 @@ export default defineComponent({
204202
previewLoaded: false,
205203
previewErrored: false,
206204
capabilities: loadState('core', 'capabilities', { files: { version_labeling: false, version_deletion: false } }),
207-
versionAuthor: '' as string | null,
208205
}
209206
},
210207
@@ -231,6 +228,18 @@ export default defineComponent({
231228
return label
232229
},
233230
231+
versionAuthor() {
232+
if (!this.version.author || !this.version.authorName) {
233+
return ''
234+
}
235+
236+
if (this.version.author === getCurrentUser()?.uid) {
237+
return t('files_versions', 'You')
238+
}
239+
240+
return this.version.authorName ?? this.version.author
241+
},
242+
234243
versionHumanExplicitDate(): string {
235244
return moment(this.version.mtime).format('LLLL')
236245
},
@@ -299,24 +308,6 @@ export default defineComponent({
299308
this.$emit('delete', this.version)
300309
},
301310
302-
async fetchDisplayName() {
303-
this.versionAuthor = null
304-
if (!this.version.author) {
305-
return
306-
}
307-
308-
if (this.version.author === getCurrentUser()?.uid) {
309-
this.versionAuthor = t('files_versions', 'You')
310-
} else {
311-
try {
312-
const { data } = await axios.post(generateUrl('/displaynames'), { users: [this.version.author] })
313-
this.versionAuthor = data.users[this.version.author]
314-
} catch (error) {
315-
logger.warn('Could not load user display name', { error })
316-
}
317-
}
318-
},
319-
320311
click() {
321312
if (!this.canView) {
322313
window.location.href = this.downloadURL

apps/files_versions/src/utils/versions.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
1111
import { getCurrentUser } from '@nextcloud/auth'
1212
import { joinPaths, encodePath } from '@nextcloud/paths'
1313
import moment from '@nextcloud/moment'
14+
import axios from '@nextcloud/axios'
1415

1516
import client from '../utils/davClient.js'
1617
import davRequest from '../utils/davRequest.js'
@@ -20,6 +21,7 @@ export interface Version {
2021
fileId: string, // The id of the file associated to the version.
2122
label: string, // 'Current version' or ''
2223
author: string|null, // UID for the author of the version
24+
authorName: string|null, // Display name of the author
2325
filename: string, // File name relative to the version DAV endpoint
2426
basename: string, // A base name generated from the mtime
2527
mime: string, // Empty for the current version, else the actual mime type of the version
@@ -31,7 +33,7 @@ export interface Version {
3133
hasPreview: boolean, // Whether the version has a preview
3234
previewUrl: string, // Preview URL of the version
3335
url: string, // Download URL of the version
34-
source: string, // The WebDAV endpoint of the ressource
36+
source: string, // The WebDAV endpoint of the resource
3537
fileVersion: string|null, // The version id, null for the current version
3638
}
3739

@@ -44,10 +46,21 @@ export async function fetchVersions(fileInfo: any): Promise<Version[]> {
4446
details: true,
4547
}) as ResponseDataDetailed<FileStat[]>
4648

47-
return response.data
49+
const versions = response.data
4850
// Filter out root
4951
.filter(({ mime }) => mime !== '')
5052
.map(version => formatVersion(version, fileInfo))
53+
54+
const authors = await axios.post(generateUrl('/displaynames'), { users: [...versions.map(version => version.author)] })
55+
56+
versions.forEach(version => {
57+
const author = authors.data.users[version.author]
58+
if (author) {
59+
version.authorName = author
60+
}
61+
})
62+
63+
return versions
5164
} catch (exception) {
5265
logger.error('Could not fetch version', { exception })
5366
throw exception
@@ -94,6 +107,7 @@ function formatVersion(version: any, fileInfo: any): Version {
94107
// If version-label is defined make sure it is a string (prevent issue if the label is a number an PHP returns a number then)
95108
label: version.props['version-label'] && String(version.props['version-label']),
96109
author: version.props['version-author'] ?? null,
110+
authorName: null,
97111
filename: version.filename,
98112
basename: moment(mtime).format('LLL'),
99113
mime: version.mime,

dist/files_versions-files_versions.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_versions-files_versions.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)