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.

LegacyUnifiedSearchService.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 AGPL-3.0-or-later
  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 = loadState('unified-search', 'min-search-length', 1)
  30. export const enableLiveSearch = loadState('unified-search', 'live-search', true)
  31. export const regexFilterIn = /(^|\s)in:([a-z_-]+)/ig
  32. export const regexFilterNot = /(^|\s)-in:([a-z_-]+)/ig
  33. /**
  34. * Create a cancel token
  35. *
  36. * @return {import('axios').CancelTokenSource}
  37. */
  38. const createCancelToken = () => axios.CancelToken.source()
  39. /**
  40. * Get the list of available search providers
  41. *
  42. * @return {Promise<Array>}
  43. */
  44. export async function getTypes() {
  45. try {
  46. const { data } = await axios.get(generateOcsUrl('search/providers'), {
  47. params: {
  48. // Sending which location we're currently at
  49. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  50. },
  51. })
  52. if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) {
  53. // Providers are sorted by the api based on their order key
  54. return data.ocs.data
  55. }
  56. } catch (error) {
  57. console.error(error)
  58. }
  59. return []
  60. }
  61. /**
  62. * Get the list of available search providers
  63. *
  64. * @param {object} options destructuring object
  65. * @param {string} options.type the type to search
  66. * @param {string} options.query the search
  67. * @param {number|string|undefined} options.cursor the offset for paginated searches
  68. * @return {object} {request: Promise, cancel: Promise}
  69. */
  70. export function search({ type, query, cursor }) {
  71. /**
  72. * Generate an axios cancel token
  73. */
  74. const cancelToken = createCancelToken()
  75. const request = async () => axios.get(generateOcsUrl('search/providers/{type}/search', { type }), {
  76. cancelToken: cancelToken.token,
  77. params: {
  78. term: query,
  79. cursor,
  80. // Sending which location we're currently at
  81. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  82. },
  83. })
  84. return {
  85. request,
  86. cancel: cancelToken.cancel,
  87. }
  88. }