diff --git a/src/main.ts b/src/main.ts index c346dfb2..cc613ecd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,13 +3,14 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -import { generateOcsUrl } from '@nextcloud/router' +import { generateOcsUrl, generateUrl } from '@nextcloud/router' import { FileAction, Permission, type Node, registerFileAction, registerDavProperty } from '@nextcloud/files' import { translate as t } from '@nextcloud/l10n' import axios from '@nextcloud/axios' import { emit } from '@nextcloud/event-bus' import LockSvg from '@mdi/svg/svg/lock.svg?raw' +import Vue from 'vue' enum LockType { User = 0, @@ -43,24 +44,33 @@ const getLockStateFromAttributes = (node: Node): LockState => { } } +const generateAvatarSvg = (userId: string) => { + const avatarUrl = generateUrl('/avatar/{userId}/32', { userId }) + return ` + + ` +} + const switchLock = async (node: Node) => { const state = getLockStateFromAttributes(node) if (!state.isLocked) { const result = await axios.put(generateOcsUrl(`/apps/files_lock/lock/${node.fileid}`)) console.debug('lock result', result) const data = result.data.ocs.data - const newLockState = { - isLocked: data.locked, - } - node.attributes.lock = newLockState.isLocked ? '1' : '0' + Vue.set(node.attributes, 'lock', '1') + Vue.set(node.attributes, 'lock-owner', data.userId) + Vue.set(node.attributes, 'lock-owner-displayname', data.displayName) + Vue.set(node.attributes, 'lock-owner-type', data.type) + Vue.set(node.attributes, 'lock-time', data.creation) } else { const result = await axios.delete(generateOcsUrl(`/apps/files_lock/lock/${node.fileid}`)) console.debug('unlock result', result) - const data = result.data.ocs.data - const newLockState = { - isLocked: data.locked, - } - node.attributes.lock = newLockState.isLocked ? '1' : '0' + Vue.set(node.attributes, 'lock', '') + Vue.set(node.attributes, 'lock-owner', '') + Vue.set(node.attributes, 'lock-owner-displayname', '') + Vue.set(node.attributes, 'lock-owner-type', '') + Vue.set(node.attributes, 'lock-time', '') } emit('files:node:updated', node) } @@ -68,19 +78,29 @@ const switchLock = async (node: Node) => { const action = new FileAction({ id: 'lock', title: () => t('files', 'Lock file'), - iconSvgInline: () => LockSvg, - inline: (file, view) => { + inline: (file) => { const state = getLockStateFromAttributes(file) return state.isLocked }, - displayName(files, view) { + iconSvgInline(nodes: Node[]) { + const node = nodes[0] + + const state = getLockStateFromAttributes(node) + if (state.isLocked && state.lockOwnerType === LockType.User) { + return generateAvatarSvg(state.lockOwner) + } + + return LockSvg + }, + + displayName(files) { if (files.length !== 1) { return '' } const node = files[0] - return getLockStateFromAttributes(node).isLocked ? 'Locked by' : 'Lock file' + return getLockStateFromAttributes(node).isLocked ? t('files_lock', 'Locked') : t('files_lock', 'Lock file') }, enabled(nodes: Node[]) {