diff --git a/package-lock.json b/package-lock.json index c30f0382..0d172c15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@mdi/svg": "^7.3.67", "@nextcloud/auth": "^2.2.1", "@nextcloud/axios": "^2.4.0", + "@nextcloud/event-bus": "^3.1.0", "@nextcloud/files": "^3.0.0", "@nextcloud/l10n": "^2.2.0", "@nextcloud/router": "^2.2.0", diff --git a/package.json b/package.json index b184bb42..f1b33ecf 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@mdi/svg": "^7.3.67", "@nextcloud/auth": "^2.2.1", "@nextcloud/axios": "^2.4.0", + "@nextcloud/event-bus": "^3.1.0", "@nextcloud/files": "^3.0.0", "@nextcloud/l10n": "^2.2.0", "@nextcloud/router": "^2.2.0", diff --git a/src/main.ts b/src/main.ts index df4df72f..c346dfb2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,12 +3,11 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -import { encodePath } from '@nextcloud/paths' import { generateOcsUrl } from '@nextcloud/router' -import { getCurrentUser } from '@nextcloud/auth' 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' @@ -17,13 +16,6 @@ enum LockType { App = 1, } -registerDavProperty('nc:lock', { nc: 'http://nextcloud.org/ns' }) -registerDavProperty('nc:lock-owner', { nc: 'http://nextcloud.org/ns' }) -registerDavProperty('nc:lock-owner-displayname', { nc: 'http://nextcloud.org/ns' }) -registerDavProperty('nc:lock-owner-type', { nc: 'http://nextcloud.org/ns' }) -registerDavProperty('nc:lock-owner-editor', { nc: 'http://nextcloud.org/ns' }) -registerDavProperty('nc:lock-time', { nc: 'http://nextcloud.org/ns' }) - type LockState = { isLocked: boolean, lockOwner: string, @@ -33,9 +25,16 @@ type LockState = { lockTime: number, } +registerDavProperty('nc:lock', { nc: 'http://nextcloud.org/ns' }) +registerDavProperty('nc:lock-owner', { nc: 'http://nextcloud.org/ns' }) +registerDavProperty('nc:lock-owner-displayname', { nc: 'http://nextcloud.org/ns' }) +registerDavProperty('nc:lock-owner-type', { nc: 'http://nextcloud.org/ns' }) +registerDavProperty('nc:lock-owner-editor', { nc: 'http://nextcloud.org/ns' }) +registerDavProperty('nc:lock-time', { nc: 'http://nextcloud.org/ns' }) + const getLockStateFromAttributes = (node: Node): LockState => { return { - isLocked: !!node.attributes['lock'], + isLocked: !!node.attributes.lock, lockOwner: node.attributes['lock-owner'], lockOwnerDisplayName: node.attributes['lock-owner-displayname'], lockOwnerType: parseInt(node.attributes['lock-owner-type']), @@ -49,6 +48,11 @@ const switchLock = async (node: 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' } else { const result = await axios.delete(generateOcsUrl(`/apps/files_lock/lock/${node.fileid}`)) console.debug('unlock result', result) @@ -58,6 +62,7 @@ const switchLock = async (node: Node) => { } node.attributes.lock = newLockState.isLocked ? '1' : '0' } + emit('files:node:updated', node) } const action = new FileAction({ @@ -95,4 +100,4 @@ const action = new FileAction({ order: 25, }) -registerFileAction(action) \ No newline at end of file +registerFileAction(action)