You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GetComments.js 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { parseXML, prepareFileFromProps } from 'webdav/dist/node/tools/dav'
  23. import { processResponsePayload } from 'webdav/dist/node/response'
  24. import { decodeHtmlEntities } from '../utils/decodeHtmlEntities'
  25. import client from './DavClient'
  26. export const DEFAULT_LIMIT = 20
  27. /**
  28. * Retrieve the comments list
  29. *
  30. * @param {object} data destructuring object
  31. * @param {string} data.commentsType the ressource type
  32. * @param {number} data.ressourceId the ressource ID
  33. * @param {object} [options] optional options for axios
  34. * @return {object[]} the comments list
  35. */
  36. export default async function({ commentsType, ressourceId }, options = {}) {
  37. let response = null
  38. const ressourcePath = ['', commentsType, ressourceId].join('/')
  39. return await client.customRequest(ressourcePath, Object.assign({
  40. method: 'REPORT',
  41. data: `<?xml version="1.0"?>
  42. <oc:filter-comments
  43. xmlns:d="DAV:"
  44. xmlns:oc="http://owncloud.org/ns"
  45. xmlns:nc="http://nextcloud.org/ns"
  46. xmlns:ocs="http://open-collaboration-services.org/ns">
  47. <oc:limit>${DEFAULT_LIMIT}</oc:limit>
  48. <oc:offset>${options.offset || 0}</oc:offset>
  49. </oc:filter-comments>`,
  50. }, options))
  51. // See example on how it's done normally
  52. // https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/stat.js#L19
  53. // Waiting for proper REPORT integration https://github.com/perry-mitchell/webdav-client/issues/207
  54. .then(res => {
  55. response = res
  56. return res.data
  57. })
  58. .then(parseXML)
  59. .then(xml => processMultistatus(xml, true))
  60. .then(comments => processResponsePayload(response, comments, true))
  61. .then(response => response.data)
  62. }
  63. // https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/directoryContents.js#L32
  64. /**
  65. * @param {any} result -
  66. * @param {any} isDetailed -
  67. */
  68. function processMultistatus(result, isDetailed = false) {
  69. // Extract the response items (directory contents)
  70. const {
  71. multistatus: { response: responseItems },
  72. } = result
  73. return responseItems.map(item => {
  74. // Each item should contain a stat object
  75. const {
  76. propstat: { prop: props },
  77. } = item
  78. // Decode HTML entities
  79. const decodedProps = {
  80. ...props,
  81. // Decode twice to handle potentially double-encoded entities
  82. // FIXME Remove this once https://github.com/nextcloud/server/issues/29306 is resolved
  83. actorDisplayName: decodeHtmlEntities(props.actorDisplayName, 2),
  84. message: decodeHtmlEntities(props.message, 2),
  85. }
  86. return prepareFileFromProps(decodedProps, decodedProps.id.toString(), isDetailed)
  87. })
  88. }