From a66cae02efcc27d962d867ba9a9e5da0441333e5 Mon Sep 17 00:00:00 2001 From: John Molakvoæ Date: Fri, 31 Mar 2023 14:56:11 +0200 Subject: fix(deps): update webdav 5 usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- apps/comments/src/services/DavClient.js | 21 +++---- apps/comments/src/services/GetComments.js | 92 ---------------------------- apps/comments/src/services/GetComments.ts | 83 +++++++++++++++++++++++++ apps/comments/src/utils/cancelableRequest.js | 22 ++----- apps/comments/src/views/Comments.vue | 10 +-- 5 files changed, 104 insertions(+), 124 deletions(-) delete mode 100644 apps/comments/src/services/GetComments.js create mode 100644 apps/comments/src/services/GetComments.ts (limited to 'apps/comments') diff --git a/apps/comments/src/services/DavClient.js b/apps/comments/src/services/DavClient.js index 6837ce90c56..5c2fc96e4db 100644 --- a/apps/comments/src/services/DavClient.js +++ b/apps/comments/src/services/DavClient.js @@ -20,19 +20,18 @@ * */ -import { createClient, getPatcher } from 'webdav' -import axios from '@nextcloud/axios' - +import { createClient } from 'webdav' import { getRootPath } from '../utils/davUtils.js' - -// Add this so the server knows it is an request from the browser -axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest' - -// force our axios -const patcher = getPatcher() -patcher.patch('request', axios) +import { getRequestToken } from '@nextcloud/auth' // init webdav client -const client = createClient(getRootPath()) +const client = createClient(getRootPath(), { + headers: { + // Add this so the server knows it is an request from the browser + 'X-Requested-With': 'XMLHttpRequest', + // Inject user auth + requesttoken: getRequestToken() ?? '', + }, +}) export default client diff --git a/apps/comments/src/services/GetComments.js b/apps/comments/src/services/GetComments.js deleted file mode 100644 index 4bdab9046a2..00000000000 --- a/apps/comments/src/services/GetComments.js +++ /dev/null @@ -1,92 +0,0 @@ -/** - * @copyright Copyright (c) 2020 John Molakvoæ - * - * @author John Molakvoæ - * - * @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 . - * - */ - -import { parseXML, prepareFileFromProps } from 'webdav/dist/node/tools/dav.js' -import { processResponsePayload } from 'webdav/dist/node/response.js' -import { decodeHtmlEntities } from '../utils/decodeHtmlEntities.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 - * @return {object[]} the comments list - */ -export default async function({ commentsType, ressourceId }, options = {}) { - let response = null - const ressourcePath = ['', commentsType, ressourceId].join('/') - - return await client.customRequest(ressourcePath, Object.assign({ - method: 'REPORT', - data: ` - - ${DEFAULT_LIMIT} - ${options.offset || 0} - `, - }, options)) - // See example on how it's done normally - // https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/stat.js#L19 - // Waiting for proper REPORT integration https://github.com/perry-mitchell/webdav-client/issues/207 - .then(res => { - response = res - return res.data - }) - .then(parseXML) - .then(xml => processMultistatus(xml, true)) - .then(comments => processResponsePayload(response, comments, true)) - .then(response => response.data) -} - -// https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/directoryContents.js#L32 -/** - * @param {any} result - - * @param {any} isDetailed - - */ -function processMultistatus(result, isDetailed = false) { - // Extract the response items (directory contents) - const { - multistatus: { response: responseItems }, - } = result - return responseItems.map(item => { - // Each item should contain a stat object - const { - propstat: { prop: props }, - } = item - // Decode HTML entities - const decodedProps = { - ...props, - // Decode twice to handle potentially double-encoded entities - // FIXME Remove this once https://github.com/nextcloud/server/issues/29306 is resolved - actorDisplayName: decodeHtmlEntities(props.actorDisplayName, 2), - message: decodeHtmlEntities(props.message, 2), - } - return prepareFileFromProps(decodedProps, decodedProps.id.toString(), isDetailed) - }) -} diff --git a/apps/comments/src/services/GetComments.ts b/apps/comments/src/services/GetComments.ts new file mode 100644 index 00000000000..d74e92bce68 --- /dev/null +++ b/apps/comments/src/services/GetComments.ts @@ -0,0 +1,83 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ + * + * @author John Molakvoæ + * + * @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 . + * + */ + +import { parseXML, type DAVResult, type FileStat } 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 + * @return {object[]} the comments list + */ +export const getComments = async function({ commentsType, ressourceId }, options: { offset: number }) { + const ressourcePath = ['', commentsType, ressourceId].join('/') + + const response = await client.customRequest(ressourcePath, Object.assign({ + method: 'REPORT', + data: ` + + ${DEFAULT_LIMIT} + ${options.offset || 0} + `, + }, options)) + + const responseData = await response.text() + const result = await parseXML(responseData) + const stat = getDirectoryFiles(result, true) + return processResponsePayload(response, stat, true) +} + +// https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/operations/directoryContents.ts +const getDirectoryFiles = function( + result: DAVResult, + isDetailed = false, +): Array { + // 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) + }) +} diff --git a/apps/comments/src/utils/cancelableRequest.js b/apps/comments/src/utils/cancelableRequest.js index cdb31441926..1973de38903 100644 --- a/apps/comments/src/utils/cancelableRequest.js +++ b/apps/comments/src/utils/cancelableRequest.js @@ -20,15 +20,6 @@ * */ -import axios from '@nextcloud/axios' - -/** - * Create a cancel token - * - * @return {import('axios').CancelTokenSource} - */ -const createCancelToken = () => axios.CancelToken.source() - /** * Creates a cancelable axios 'request object'. * @@ -36,10 +27,8 @@ const createCancelToken = () => axios.CancelToken.source() * @return {object} */ const cancelableRequest = function(request) { - /** - * Generate an axios cancel token - */ - const cancelToken = createCancelToken() + const controller = new AbortController() + const signal = controller.signal /** * Execute the request @@ -48,15 +37,16 @@ const cancelableRequest = function(request) { * @param {object} [options] optional config for the request */ const fetch = async function(url, options) { - return request( + const response = await request( url, - Object.assign({ cancelToken: cancelToken.token }, options) + Object.assign({ signal }, options) ) + return response } return { request: fetch, - cancel: cancelToken.cancel, + abort: () => controller.abort(), } } diff --git a/apps/comments/src/views/Comments.vue b/apps/comments/src/views/Comments.vue index b3b75a92dca..55f735d566d 100644 --- a/apps/comments/src/views/Comments.vue +++ b/apps/comments/src/views/Comments.vue @@ -94,7 +94,7 @@ import MessageReplyTextIcon from 'vue-material-design-icons/MessageReplyText.vue import AlertCircleOutlineIcon from 'vue-material-design-icons/AlertCircleOutline.vue' import Comment from '../components/Comment.vue' -import getComments, { DEFAULT_LIMIT } from '../services/GetComments.js' +import { getComments, DEFAULT_LIMIT } from '../services/GetComments.ts' import cancelableRequest from '../utils/cancelableRequest.js' Vue.use(VTooltip) @@ -206,14 +206,14 @@ export default { this.error = '' // Init cancellable request - const { request, cancel } = cancelableRequest(getComments) - this.cancelRequest = cancel + const { request, abort } = cancelableRequest(getComments) + this.cancelRequest = abort // Fetch comments - const comments = await request({ + const { data: comments } = await request({ commentsType: this.commentsType, ressourceId: this.ressourceId, - }, { offset: this.offset }) + }, { offset: this.offset }) || { data: [] } this.logger.debug(`Processed ${comments.length} comments`, { comments }) -- cgit v1.2.3