Skip to content

Commit

Permalink
feat: added filtering to filter out files that are shared
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Morales <emoral435@gmail.com>
  • Loading branch information
emoral435 committed Feb 1, 2024
1 parent 06c3689 commit a994ff3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 32 deletions.
12 changes: 2 additions & 10 deletions apps/files/src/services/Favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@
import type { ContentsWithRoot } from '@nextcloud/files'
import type { FileStat, ResponseDataDetailed } from 'webdav'

import { Folder, getDavNameSpaces, getDavProperties, davGetDefaultPropfind } from '@nextcloud/files'
import { Folder, davGetDefaultPropfind, davGetFavoritesReport } from '@nextcloud/files'

import { getClient } from './WebdavClient'
import { resultToNode } from './Files'

const client = getClient()

const reportPayload = `<?xml version="1.0"?>
<oc:filter-files ${getDavNameSpaces()}>
<d:prop>
${getDavProperties()}
</d:prop>
<oc:filter-rules>
<oc:favorite>1</oc:favorite>
</oc:filter-rules>
</oc:filter-files>`
const reportPayload = davGetFavoritesReport()

export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
const propfindPayload = davGetDefaultPropfind()
Expand Down
61 changes: 53 additions & 8 deletions apps/files/src/services/PersonalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,68 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @types
*/
import type { ContentsWithRoot } from '@nextcloud/files'

import { CancelablePromise } from 'cancelable-promise'
import { davGetClient } from "@nextcloud/files";
import type { FileStat, ResponseDataDetailed } from 'webdav';
import { davGetDefaultPropfind} from "@nextcloud/files";
import { Folder, File, type ContentsWithRoot } from '@nextcloud/files'
import { getCurrentUser } from '@nextcloud/auth';

import logger from '../logger'
import { resultToNode } from './Files';
import { getClient } from './WebdavClient';

const client = davGetClient()
const client = getClient()

/**
* @brief filters each file/folder on its shared statuses
* MOVE TO @nextcloud/files
*
* eventually, this should be the WebDAV search, similar to
*
* @param {FileStat} node that contains
* @return {Boolean}
*/
export const davNotShared = function(node: FileStat): Boolean {
// could use further filtering based on this issues description
// https://github.com/nextcloud/server/issues/42919
return node.props?.['owner-id'] === getCurrentUser()?.uid.toString()
&& node.props?.['share-types'] === ""
}

// TODO filter out the root file path for personal / non shared / non group folder files and nodes
export const getContents = (path: string = "/"): Promise<ContentsWithRoot> => {
const controller = new AbortController()
// FIXME we would filter each file during the WebDAV query, instead of after getting all the files
// and then filtering from there
const propfindPayload = davGetDefaultPropfind() // change the davGet here

return new CancelablePromise(async (resolve, reject, onCancel) => {
onCancel(() => controller.abort())
try {
const contentsResponse = await client.getDirectoryContents(path, {
details: true,
data: propfindPayload,
includeSelf: true,
signal: controller.signal,
}) as ResponseDataDetailed<FileStat[]>

const root = contentsResponse.data[0]
const contents = contentsResponse.data.slice(1)

if (root.filename !== path) {
throw new Error('Root node does not match requested path')
}

resolve({
folder: resultToNode(root) as Folder,
contents: contents.filter(davNotShared).map(result => {
try {
return resultToNode(result)
} catch (error) {
logger.error(`Invalid node detected '${result.basename}'`, { error })
return null
}
}).filter(Boolean) as File[],
})
} catch (error) {
reject(error)
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/views/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default () => {
emptyCaption: t('files', 'Files and folders you mark as favorite will show up here'),

icon: StarSvg,
order: 5,
order: 15,

columns: [],

Expand Down
16 changes: 7 additions & 9 deletions apps/files/src/views/personal-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@
*
*/
import { translate as t } from '@nextcloud/l10n'
import type { Folder, Node } from '@nextcloud/files'

import FolderHome from '@mdi/svg/svg/folder-home.svg?raw'
import { View, getNavigation } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'

import { getContents } from '../services/PersonalFiles'
import FolderHome from '@mdi/svg/svg/folder-home.svg?raw'
import logger from '../logger'
import { subscribe } from '@nextcloud/event-bus'

/**
* NOTE since we are only filtering at the root level, we only need to use the
* getContents methods only on this default folder view / route / path.
* Every other subroot from the main root with be rendered normally, as it
* would be in the all-files paths.
*/
export default () => {
logger.debug("Loading root level personal files view...")

const Navigation = getNavigation()
Navigation.register(new View({
id: 'personal-files',
Expand All @@ -47,18 +45,18 @@ export default () => {
emptyCaption: t('files', 'Files that are not shared will show up here.'),

icon: FolderHome,
order: 1,
order: 5,

getContents,
}))

/**
* Update root personal files navigation when a folder is no longer shared
* Update personal files view when a folder is no longer shared
*/
// subscribe()

/**
* Remove root personal files navigation when a folder is shared
* Update personal files view when a folder is shared from the user
*/
// subscribe()

Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/views/recent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default () => {
emptyCaption: t('files', 'Files and folders you recently modified will show up here.'),

icon: HistorySvg,
order: 2,
order: 10,

defaultSortKey: 'mtime',

Expand Down
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions dist/files-init.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
* @copyright Copyright (c) 2024 Eduardo Morales <emoral435@gmail.com>
*
* @author Eduardo Morales <emoral435@gmail.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/>.
*
*/
2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

0 comments on commit a994ff3

Please sign in to comment.