-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathfavoriteAction.ts
More file actions
135 lines (117 loc) · 3.83 KB
/
favoriteAction.ts
File metadata and controls
135 lines (117 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { INode, IView } from '@nextcloud/files'
import StarOutlineSvg from '@mdi/svg/svg/star-outline.svg?raw'
import StarSvg from '@mdi/svg/svg/star.svg?raw'
import axios from '@nextcloud/axios'
import { emit } from '@nextcloud/event-bus'
import { FileAction, Permission } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import { encodePath } from '@nextcloud/paths'
import { generateUrl } from '@nextcloud/router'
import { isPublicShare } from '@nextcloud/sharing/public'
import PQueue from 'p-queue'
import Vue from 'vue'
import logger from '../logger.ts'
export const ACTION_FAVORITE = 'favorite'
const queue = new PQueue({ concurrency: 5 })
/**
* If any of the nodes is not favorited, we display the favorite action.
*
* @param nodes - The nodes to check
*/
function shouldFavorite(nodes: INode[]): boolean {
return nodes.some((node) => node.attributes.favorite !== 1)
}
/**
* Favorite or unfavorite a node
*
* @param node - The node to favorite/unfavorite
* @param view - The current view
* @param willFavorite - Whether to favorite or unfavorite the node
*/
export async function favoriteNode(node: INode, view: IView, willFavorite: boolean): Promise<boolean> {
try {
// TODO: migrate to webdav tags plugin
const url = generateUrl('/apps/files/api/v1/files') + encodePath(node.path)
await axios.post(url, {
tags: willFavorite
? [window.OC.TAG_FAVORITE]
: [],
})
// Let's delete if we are in the favourites view
// AND if it is removed from the user favorites
// AND it's in the root of the favorites view
if (view.id === 'favorites' && !willFavorite && node.dirname === '/') {
emit('files:node:deleted', node)
}
// Update the node webdav attribute
Vue.set(node.attributes, 'favorite', willFavorite ? 1 : 0)
emit('files:node:updated', node)
// Dispatch event to whoever is interested
if (willFavorite) {
emit('files:favorites:added', node)
} else {
emit('files:favorites:removed', node)
}
return true
} catch (error) {
const action = willFavorite ? 'adding a file to favourites' : 'removing a file from favourites'
logger.error('Error while ' + action, { error, source: node.source, node })
return false
}
}
export const action = new FileAction({
id: ACTION_FAVORITE,
displayName({ nodes }) {
return shouldFavorite(nodes)
? t('files', 'Add to favorites')
: t('files', 'Remove from favorites')
},
iconSvgInline: ({ nodes }) => {
return shouldFavorite(nodes)
? StarOutlineSvg
: StarSvg
},
enabled({ nodes }) {
// Not enabled for public shares
if (isPublicShare()) {
return false
}
// We can only favorite nodes if they are located in files
return nodes.every((node) => node.root?.startsWith?.('/files'))
// and we have permissions
&& nodes.every((node) => node.permissions !== Permission.NONE)
},
async exec({ nodes, view }): Promise<boolean> {
const willFavorite = shouldFavorite([nodes[0]])
return await favoriteNode(nodes[0], view, willFavorite)
},
async execBatch({ nodes, view }): Promise<boolean[]> {
const willFavorite = shouldFavorite(nodes)
// Map each node to a promise that resolves with the result of exec(node)
const promises = nodes.map((node) => {
// Create a promise that resolves with the result of exec(node)
const promise = new Promise<boolean>((resolve) => {
queue.add(async () => {
try {
await favoriteNode(node, view, willFavorite)
resolve(true)
} catch (error) {
logger.error('Error while adding file to favorite', { error, source: node.source, node })
resolve(false)
}
})
})
return promise
})
return Promise.all(promises)
},
order: -50,
hotkey: {
description: t('files', 'Add or remove favorite'),
key: 'S',
},
})