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.

UnifiedSearchService.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @copyright 2020, John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. import { generateOcsUrl } from '@nextcloud/router'
  26. import { loadState } from '@nextcloud/initial-state'
  27. import axios from '@nextcloud/axios'
  28. export const defaultLimit = loadState('unified-search', 'limit-default')
  29. export const minSearchLength = 2
  30. export const regexFilterIn = /[^-]in:([a-z_-]+)/ig
  31. export const regexFilterNot = /-in:([a-z_-]+)/ig
  32. /**
  33. * Create a cancel token
  34. *
  35. * @return {import('axios').CancelTokenSource}
  36. */
  37. const createCancelToken = () => axios.CancelToken.source()
  38. /**
  39. * Get the list of available search providers
  40. *
  41. * @return {Promise<Array>}
  42. */
  43. export async function getTypes() {
  44. try {
  45. const { data } = await axios.get(generateOcsUrl('search/providers'), {
  46. params: {
  47. // Sending which location we're currently at
  48. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  49. },
  50. })
  51. if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) {
  52. // Providers are sorted by the api based on their order key
  53. return data.ocs.data
  54. }
  55. } catch (error) {
  56. console.error(error)
  57. }
  58. return []
  59. }
  60. /**
  61. * Get the list of available search providers
  62. *
  63. * @param {object} options destructuring object
  64. * @param {string} options.type the type to search
  65. * @param {string} options.query the search
  66. * @param {number|string|undefined} options.cursor the offset for paginated searches
  67. * @return {object} {request: Promise, cancel: Promise}
  68. */
  69. export function search({ type, query, cursor }) {
  70. /**
  71. * Generate an axios cancel token
  72. */
  73. const cancelToken = createCancelToken()
  74. const request = async () => axios.get(generateOcsUrl('search/providers/{type}/search', { type }), {
  75. cancelToken: cancelToken.token,
  76. params: {
  77. term: query,
  78. cursor,
  79. // Sending which location we're currently at
  80. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  81. },
  82. })
  83. return {
  84. request,
  85. cancel: cancelToken.cancel,
  86. }
  87. }