Skip to content

Commit

Permalink
fixup! feat: Migrate to new files API
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr committed Nov 9, 2023
1 parent e9d0182 commit af97cee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 16 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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,
Expand All @@ -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']),
Expand All @@ -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)
Expand All @@ -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({
Expand Down Expand Up @@ -95,4 +100,4 @@ const action = new FileAction({
order: 25,
})

registerFileAction(action)
registerFileAction(action)

0 comments on commit af97cee

Please sign in to comment.