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.

api.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  3. *
  4. * @author Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  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 axios from 'nextcloud-axios'
  23. import { generateRemoteUrl } from 'nextcloud-router'
  24. const xmlToJson = (xml) => {
  25. let obj = {}
  26. if (xml.nodeType === 1) {
  27. if (xml.attributes.length > 0) {
  28. obj['@attributes'] = {}
  29. for (let j = 0; j < xml.attributes.length; j++) {
  30. const attribute = xml.attributes.item(j)
  31. obj['@attributes'][attribute.nodeName] = attribute.nodeValue
  32. }
  33. }
  34. } else if (xml.nodeType === 3) {
  35. obj = xml.nodeValue
  36. }
  37. if (xml.hasChildNodes()) {
  38. for (let i = 0; i < xml.childNodes.length; i++) {
  39. const item = xml.childNodes.item(i)
  40. const nodeName = item.nodeName
  41. if (typeof (obj[nodeName]) === 'undefined') {
  42. obj[nodeName] = xmlToJson(item)
  43. } else {
  44. if (typeof obj[nodeName].push === 'undefined') {
  45. var old = obj[nodeName]
  46. obj[nodeName] = []
  47. obj[nodeName].push(old)
  48. }
  49. obj[nodeName].push(xmlToJson(item))
  50. }
  51. }
  52. }
  53. return obj
  54. }
  55. const parseXml = (xml) => {
  56. let dom = null
  57. try {
  58. dom = (new DOMParser()).parseFromString(xml, 'text/xml')
  59. } catch (e) {
  60. console.error('Failed to parse xml document', e)
  61. }
  62. return dom
  63. }
  64. const xmlToTagList = (xml) => {
  65. const json = xmlToJson(parseXml(xml))
  66. const list = json['d:multistatus']['d:response']
  67. const result = []
  68. for (const index in list) {
  69. const tag = list[index]['d:propstat']
  70. if (tag['d:status']['#text'] !== 'HTTP/1.1 200 OK') {
  71. continue
  72. }
  73. result.push({
  74. id: tag['d:prop']['oc:id']['#text'],
  75. displayName: tag['d:prop']['oc:display-name']['#text'],
  76. canAssign: tag['d:prop']['oc:can-assign']['#text'] === 'true',
  77. userAssignable: tag['d:prop']['oc:user-assignable']['#text'] === 'true',
  78. userVisible: tag['d:prop']['oc:user-visible']['#text'] === 'true'
  79. })
  80. }
  81. return result
  82. }
  83. const searchTags = function() {
  84. return axios({
  85. method: 'PROPFIND',
  86. url: generateRemoteUrl('dav') + '/systemtags/',
  87. data: `<?xml version="1.0"?>
  88. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  89. <d:prop>
  90. <oc:id />
  91. <oc:display-name />
  92. <oc:user-visible />
  93. <oc:user-assignable />
  94. <oc:can-assign />
  95. </d:prop>
  96. </d:propfind>`
  97. }).then((response) => {
  98. return xmlToTagList(response.data)
  99. })
  100. }
  101. export {
  102. searchTags
  103. }