Skip to content

Commit 7e220cc

Browse files
committed
fix: condensed filtering logic
Signed-off-by: Eduardo Morales <emoral435@gmail.com>
1 parent a911086 commit 7e220cc

3 files changed

Lines changed: 21 additions & 80 deletions

File tree

apps/files/src/services/PersonalFiles.ts

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,70 +19,36 @@
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*
2121
*/
22-
import { CancelablePromise } from 'cancelable-promise'
23-
import type { FileStat, ResponseDataDetailed } from 'webdav';
24-
import { davGetDefaultPropfind} from "@nextcloud/files";
25-
import { Folder, File, type ContentsWithRoot } from '@nextcloud/files'
22+
import { File, type ContentsWithRoot } from '@nextcloud/files'
2623
import { getCurrentUser } from '@nextcloud/auth';
2724

28-
import logger from '../logger'
29-
import { resultToNode } from './Files';
30-
import { getClient } from './WebdavClient';
25+
import { getContents as getFiles } from './Files';
3126

32-
const client = getClient()
27+
const currUserID = getCurrentUser()?.uid
3328

3429
/**
3530
* NOTE MOVE TO @nextcloud/files
36-
* @brief filters each file/folder on its shared statuses
31+
* @brief filters each file/folder on its shared status
32+
* A personal file is considered a file that has all of the following properties:
33+
* a.) the current user owns
34+
* b.) the file is not shared with anyone
35+
* c.) the file is not a group folder
3736
* @param {FileStat} node that contains
3837
* @return {Boolean}
3938
*/
40-
export const davNotShared = function(node: File | Folder | null, currUserID: string | undefined): Boolean {
41-
// (essentially .filter(Boolean))
42-
if (!node) return false
43-
44-
const isNotShared = currUserID ? node.attributes['owner-id'] === currUserID : true
39+
export const personalFile = function(node: File): Boolean {
40+
const isNotShared = currUserID ? node.owner === currUserID : true
4541
&& node.attributes['mount-type'] !== 'group'
4642
&& node.attributes['mount-type'] !== 'shared'
47-
48-
return isNotShared
43+
return isNotShared
4944
}
5045

5146
export const getContents = (path: string = "/"): Promise<ContentsWithRoot> => {
52-
const controller = new AbortController()
53-
const propfindPayload = davGetDefaultPropfind()
54-
const currUserID = getCurrentUser()?.uid.toString()
55-
56-
return new CancelablePromise(async (resolve, reject, onCancel) => {
57-
onCancel(() => controller.abort())
58-
try {
59-
const contentsResponse = await client.getDirectoryContents(path, {
60-
details: true,
61-
data: propfindPayload,
62-
includeSelf: true,
63-
signal: controller.signal,
64-
}) as ResponseDataDetailed<FileStat[]>
65-
66-
const root = contentsResponse.data[0]
67-
const contents = contentsResponse.data.slice(1)
68-
69-
if (root.filename !== path) {
70-
throw new Error('Root node does not match requested path')
71-
}
72-
73-
resolve({
74-
folder: resultToNode(root) as Folder,
75-
contents: contents.map(result => {
76-
try {
77-
return resultToNode(result)
78-
} catch (error) {
79-
logger.error(`Invalid node detected '${result.basename}'`, { error })
80-
return null
81-
}
82-
}).filter(node => davNotShared(node, currUserID)) as File[],
47+
// get all the files from the current path as a cancellable promise
48+
// then filter the files that the user does not own, or has shared / is a group folder
49+
return getFiles(path)
50+
.then(c => {
51+
c.contents = c.contents.filter(personalFile) as File[]
52+
return c
8353
})
84-
} catch (error) {
85-
reject(error)
86-
}
87-
})
88-
}
54+
}

apps/files/src/views/personal-files.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ import { View, getNavigation } from '@nextcloud/files'
2424

2525
import { getContents } from '../services/PersonalFiles'
2626
import AccountIcon from '@mdi/svg/svg/account.svg?raw'
27-
import logger from '../logger'
2827

2928
export default () => {
30-
logger.debug("Loading root level personal files view...")
31-
3229
const Navigation = getNavigation()
3330
Navigation.register(new View({
34-
id: 'personal-files',
31+
id: 'personal',
3532
name: t('files', 'Personal Files'),
3633
caption: t('files', 'List of your files and folders that are not shared.'),
3734

dist/files-init.js.LICENSE.txt

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,6 @@
4343
*
4444
*/
4545

46-
/**
47-
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
48-
*
49-
* @author John Molakvoæ <skjnldsv@protonmail.com>
50-
*
51-
* @license AGPL-3.0-or-later
52-
*
53-
* This program is free software: you can redistribute it and/or modify
54-
* it under the terms of the GNU Affero General Public License as
55-
* published by the Free Software Foundation, either version 3 of the
56-
* License, or (at your option) any later version.
57-
*
58-
* This program is distributed in the hope that it will be useful,
59-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
60-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61-
* GNU Affero General Public License for more details.
62-
*
63-
* You should have received a copy of the GNU Affero General Public License
64-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
65-
*
66-
*/
67-
6846
/**
6947
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
7048
*
@@ -155,9 +133,9 @@
155133
*/
156134

157135
/**
158-
* @copyright Copyright (c) 2024 Eduardo Morales <emoral435@gmail.com>
136+
* @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
159137
*
160-
* @author Eduardo Morales <emoral435@gmail.com>
138+
* @author Ferdinand Thiessen <opensource@fthiessen.de>
161139
*
162140
* @license AGPL-3.0-or-later
163141
*

0 commit comments

Comments
 (0)