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'
2623import { 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
5146export 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+ }
0 commit comments