summaryrefslogtreecommitdiffstats
path: root/apps/comments/src/services/GetComments.ts
blob: 2ca725c2ae6303a658c7ed0a729d09be751a18a1 (plain)
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
/**
 * @copyright Copyright (c) 2020 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 { parseXML, type DAVResult, type FileStat, type ResponseDataDetailed } from 'webdav'

// https://github.com/perry-mitchell/webdav-client/issues/339
import { processResponsePayload } from '../../../../node_modules/webdav/dist/node/response.js'
import { prepareFileFromProps } from '../../../../node_modules/webdav/dist/node/tools/dav.js'
import client from './DavClient.js'

export const DEFAULT_LIMIT = 20

/**
 * Retrieve the comments list
 *
 * @param {object} data destructuring object
 * @param {string} data.commentsType the ressource type
 * @param {number} data.ressourceId the ressource ID
 * @param {object} [options] optional options for axios
 * @param {number} [options.offset] the pagination offset
 * @param {number} [options.limit] the pagination limit, defaults to 20
 * @param {Date} [options.datetime] optional date to query
 * @return {{data: object[]}} the comments list
 */
export const getComments = async function({ commentsType, ressourceId }, options: { offset: number, limit?: number, datetime?: Date }) {
	const ressourcePath = ['', commentsType, ressourceId].join('/')
	const datetime = options.datetime ? `<oc:datetime>${options.datetime.toISOString()}</oc:datetime>` : ''
	const response = await client.customRequest(ressourcePath, Object.assign({
		method: 'REPORT',
		data: `<?xml version="1.0"?>
			<oc:filter-comments
				xmlns:d="DAV:"
				xmlns:oc="http://owncloud.org/ns"
				xmlns:nc="http://nextcloud.org/ns"
				xmlns:ocs="http://open-collaboration-services.org/ns">
				<oc:limit>${options.limit ?? DEFAULT_LIMIT}</oc:limit>
				<oc:offset>${options.offset || 0}</oc:offset>
				${datetime}
			</oc:filter-comments>`,
	}, options))

	const responseData = await response.text()
	const result = await parseXML(responseData)
	const stat = getDirectoryFiles(result, true)
	return processResponsePayload(response, stat, true) as ResponseDataDetailed<FileStat[]>
}

// https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/operations/directoryContents.ts
const getDirectoryFiles = function(
	result: DAVResult,
	isDetailed = false,
): Array<FileStat> {
	// Extract the response items (directory contents)
	const {
		multistatus: { response: responseItems },
	} = result

	// Map all items to a consistent output structure (results)
	return responseItems.map(item => {
		// Each item should contain a stat object
		const {
			propstat: { prop: props },
		} = item

		return prepareFileFromProps(props, props.id.toString(), isDetailed)
	})
}