Skip to content

Commit

Permalink
fixup! 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 af97cee commit 82d285b
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,44 +44,63 @@ const getLockStateFromAttributes = (node: Node): LockState => {
}
}

const generateAvatarSvg = (userId: string) => {
const avatarUrl = generateUrl('/avatar/{userId}/32', { userId })
return `<svg width="32" height="32" viewBox="0 0 32 32"
xmlns="http://www.w3.org/2000/svg" class="sharing-status__avatar">
<image href="${avatarUrl}" height="32" width="32" />
</svg>`
}

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)
}

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[]) {
Expand Down

0 comments on commit 82d285b

Please sign in to comment.