Skip to content

Commit a7338b0

Browse files
Merge pull request #51727 from nextcloud/fix/noid-retrieve-all-authors-at-the-same-time
fix(files_versions): retrieve all display names with one request
2 parents 40117dc + 289e87e commit a7338b0

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

apps/files_versions/src/components/Version.vue

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ import { Permission, formatFileSize } from '@nextcloud/files'
125125
import { loadState } from '@nextcloud/initial-state'
126126
import { t } from '@nextcloud/l10n'
127127
import { joinPaths } from '@nextcloud/paths'
128-
import { getRootUrl, generateUrl } from '@nextcloud/router'
128+
import { getRootUrl } from '@nextcloud/router'
129129
import { defineComponent } from 'vue'
130130
131-
import axios from '@nextcloud/axios'
132131
import moment from '@nextcloud/moment'
133-
import logger from '../utils/logger'
134132
135133
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
136134
import Delete from 'vue-material-design-icons/Delete.vue'
@@ -207,7 +205,6 @@ export default defineComponent({
207205
previewLoaded: false,
208206
previewErrored: false,
209207
capabilities: loadState('core', 'capabilities', { files: { version_labeling: false, version_deletion: false } }),
210-
versionAuthor: '' as string | null,
211208
}
212209
},
213210
@@ -234,6 +231,18 @@ export default defineComponent({
234231
return label
235232
},
236233
234+
versionAuthor() {
235+
if (!this.version.author || !this.version.authorName) {
236+
return ''
237+
}
238+
239+
if (this.version.author === getCurrentUser()?.uid) {
240+
return t('files_versions', 'You')
241+
}
242+
243+
return this.version.authorName ?? this.version.author
244+
},
245+
237246
versionHumanExplicitDate(): string {
238247
return moment(this.version.mtime).format('LLLL')
239248
},
@@ -302,24 +311,6 @@ export default defineComponent({
302311
this.$emit('delete', this.version)
303312
},
304313
305-
async fetchDisplayName() {
306-
this.versionAuthor = null
307-
if (!this.version.author) {
308-
return
309-
}
310-
311-
if (this.version.author === getCurrentUser()?.uid) {
312-
this.versionAuthor = t('files_versions', 'You')
313-
} else {
314-
try {
315-
const { data } = await axios.post(generateUrl('/displaynames'), { users: [this.version.author] })
316-
this.versionAuthor = data.users[this.version.author]
317-
} catch (error) {
318-
logger.warn('Could not load user display name', { error })
319-
}
320-
}
321-
},
322-
323314
click() {
324315
if (!this.canView) {
325316
window.location.href = this.downloadURL

apps/files_versions/src/utils/versions.ts

Lines changed: 17 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
@@ -30,7 +32,7 @@ export interface Version {
3032
permissions: string, // Only readable: 'R'
3133
previewUrl: string, // Preview URL of the version
3234
url: string, // Download URL of the version
33-
source: string, // The WebDAV endpoint of the ressource
35+
source: string, // The WebDAV endpoint of the resource
3436
fileVersion: string|null, // The version id, null for the current version
3537
}
3638

@@ -43,10 +45,22 @@ export async function fetchVersions(fileInfo: any): Promise<Version[]> {
4345
details: true,
4446
}) as ResponseDataDetailed<FileStat[]>
4547

46-
return response.data
48+
const versions = response.data
4749
// Filter out root
4850
.filter(({ mime }) => mime !== '')
4951
.map(version => formatVersion(version, fileInfo))
52+
53+
const authorIds = new Set(versions.map(version => version.author))
54+
const authors = await axios.post(generateUrl('/displaynames'), { users: [...authorIds] })
55+
56+
for (const version of versions) {
57+
const author = authors.data.users[version.author]
58+
if (author) {
59+
version.authorName = author
60+
}
61+
}
62+
63+
return versions
5064
} catch (exception) {
5165
logger.error('Could not fetch version', { exception })
5266
throw exception
@@ -93,6 +107,7 @@ function formatVersion(version: any, fileInfo: any): Version {
93107
// 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)
94108
label: version.props['version-label'] && String(version.props['version-label']),
95109
author: version.props['version-author'] ?? null,
110+
authorName: null,
96111
filename: version.filename,
97112
basename: moment(mtime).format('LLL'),
98113
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)