-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[F2V] favorites view #37727
Closed
Closed
[F2V] favorites view #37727
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b88d0d2
feat(files): favorites
skjnldsv 84fd21f
feat(files): add download action
skjnldsv fa2c539
fix(files): download folder action
skjnldsv 91b0b50
fixup! feat(files): favorites
skjnldsv 21c777a
fix(files): make open folder an action
skjnldsv 749481b
feat(files): add edit locally action
skjnldsv 3951c7b
feat(files): add rename action
skjnldsv 11b19d7
fix(files): fix favorites legacy to vue handling and sorting
skjnldsv 844f9be
feat(files): sort favorites first
skjnldsv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @author John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
import { emit } from '@nextcloud/event-bus' | ||
import { Permission, Node, FileType } from '@nextcloud/files' | ||
import { translate as t } from '@nextcloud/l10n' | ||
import ArrowDown from '@mdi/svg/svg/arrow-down.svg?raw' | ||
|
||
import { registerFileAction, FileAction } from '../services/FileAction' | ||
import { generateUrl } from '@nextcloud/router' | ||
import type { Navigation } from '../services/Navigation.ts' | ||
|
||
const triggerDownload = function(url: string) { | ||
const hiddenElement = document.createElement('a') | ||
hiddenElement.download = '' | ||
hiddenElement.href = url | ||
hiddenElement.click() | ||
} | ||
|
||
const downloadNodes = function(dir = '/', nodes: Node[]) { | ||
const secret = Math.random().toString(36).substring(2) | ||
const url = generateUrl('/apps/files/ajax/download.php?dir={dir}&files={files}&downloadStartSecret={secret}', { | ||
dir, | ||
secret, | ||
files: JSON.stringify(nodes.map(node => node.basename)), | ||
}) | ||
triggerDownload(url) | ||
} | ||
|
||
registerFileAction(new FileAction({ | ||
id: 'download', | ||
displayName: () => t('files', 'Download'), | ||
iconSvgInline: () => ArrowDown, | ||
|
||
enabled(nodes: Node[]) { | ||
return nodes.length > 0 && nodes | ||
.map(node => node.permissions) | ||
.every(permission => (permission & Permission.READ) !== 0) | ||
}, | ||
|
||
async exec(node: Node, view: Navigation, dir: string) { | ||
if (node.type === FileType.Folder) { | ||
downloadNodes(dir, [node]) | ||
return null | ||
} | ||
|
||
triggerDownload(node.source) | ||
return null | ||
}, | ||
|
||
async execBatch(nodes: Node[], view: Navigation, dir: string) { | ||
if (nodes.length === 1) { | ||
this.exec(nodes[0], view, dir) | ||
return [null] | ||
} | ||
|
||
downloadNodes(dir, nodes) | ||
return new Array(nodes.length).fill(null) | ||
}, | ||
|
||
order: 30, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @author John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
import { encodePath } from '@nextcloud/paths' | ||
import { Permission, type Node } from '@nextcloud/files' | ||
import { translate as t } from '@nextcloud/l10n' | ||
import axios from '@nextcloud/axios' | ||
import DevicesSvg from '@mdi/svg/svg/devices.svg?raw' | ||
|
||
import { generateOcsUrl } from '@nextcloud/router' | ||
import { getCurrentUser } from '@nextcloud/auth' | ||
import { registerFileAction, FileAction } from '../services/FileAction' | ||
import { showError } from '@nextcloud/dialogs' | ||
|
||
const openLocalClient = function(path: string) { | ||
const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json' | ||
|
||
axios.post(link, { path }) | ||
.then(function(result) { | ||
const scheme = 'nc://' | ||
const command = 'open' | ||
const uid = getCurrentUser()?.uid | ||
let url = scheme + command + '/' + uid + '@' + window.location.host + encodePath(path) | ||
url += '?token=' + result.data.ocs.data.token | ||
|
||
window.location.href = url | ||
}) | ||
.catch(function() { | ||
showError(t('files', 'Failed to redirect to client')) | ||
}) | ||
} | ||
|
||
if (!/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) { | ||
registerFileAction(new FileAction({ | ||
id: 'edit-locally', | ||
displayName: () => t('files', 'Edit locally'), | ||
iconSvgInline: () => DevicesSvg, | ||
|
||
// Only works on single files | ||
enabled(nodes: Node[]) { | ||
// Only works on single node | ||
if (nodes.length !== 1) { | ||
return false | ||
} | ||
|
||
return (nodes[0].permissions & Permission.UPDATE) !== 0 | ||
}, | ||
|
||
async exec(node: Node) { | ||
if (!node.path) { | ||
return false | ||
} | ||
openLocalClient(node.path) | ||
return null | ||
}, | ||
|
||
default: true, | ||
order: 25, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @author John Molakvoæ <skjnldsv@protonmail.com> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
import { emit } from '@nextcloud/event-bus' | ||
import { translate as t } from '@nextcloud/l10n' | ||
import axios from '@nextcloud/axios' | ||
import Star from '@mdi/svg/svg/star.svg?raw' | ||
import type { Node } from '@nextcloud/files' | ||
|
||
import { generateUrl } from '@nextcloud/router' | ||
import { registerFileAction, FileAction } from '../services/FileAction' | ||
import logger from '../logger.js' | ||
import type { Navigation } from '../services/Navigation' | ||
|
||
/** | ||
* If any of the nodes is not favorited | ||
* we display the favorite action. | ||
*/ | ||
const shouldFavorite = (nodes: Node[]): boolean => { | ||
return nodes.some(node => node.attributes.favorite === 0) | ||
} | ||
|
||
registerFileAction(new FileAction({ | ||
id: 'favorite', | ||
displayName(nodes: Node[]) { | ||
return shouldFavorite(nodes) | ||
? t('files', 'Add to favorites') | ||
: t('files', 'Remove from favorites') | ||
}, | ||
iconSvgInline: () => Star, | ||
|
||
enabled(nodes: Node[]) { | ||
// We can only favorite nodes within files | ||
return !nodes.some(node => !node.root?.startsWith?.('/files')) | ||
}, | ||
|
||
async exec(node: Node, view: Navigation) { | ||
const willFavorite = shouldFavorite([node]) | ||
try { | ||
// TODO: migrate to webdav tags plugin | ||
const url = generateUrl('/apps/files/api/v1/files/') + node.path | ||
await axios.post(url, { | ||
tags: willFavorite | ||
? [OC.TAG_FAVORITE] | ||
: [], | ||
}) | ||
|
||
// Let's delete even 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 | ||
node.attributes.favorite = willFavorite ? 1 : 0 | ||
|
||
// 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 | ||
} | ||
}, | ||
async execBatch(nodes: Node[], view: Navigation) { | ||
return Promise.all(nodes.map(node => this.exec(node, view))) | ||
}, | ||
|
||
order: -50, | ||
})) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Psalm
PossiblyUndefinedVariable