2020-08-03 13:54:37 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2020, John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
2021-03-31 13:15:40 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-08-03 13:54:37 +03:00
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* 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
|
2021-03-31 13:15:40 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-08-03 13:54:37 +03:00
|
|
|
*/
|
|
|
|
|
2020-09-07 11:47:10 +03:00
|
|
|
import { generateOcsUrl } from '@nextcloud/router'
|
2020-08-03 13:54:37 +03:00
|
|
|
import { loadState } from '@nextcloud/initial-state'
|
|
|
|
import axios from '@nextcloud/axios'
|
|
|
|
|
|
|
|
export const defaultLimit = loadState('unified-search', 'limit-default')
|
2020-08-05 10:49:18 +03:00
|
|
|
export const minSearchLength = 2
|
2020-08-05 13:56:11 +03:00
|
|
|
export const regexFilterIn = /[^-]in:([a-z_-]+)/ig
|
|
|
|
export const regexFilterNot = /-in:([a-z_-]+)/ig
|
|
|
|
|
2020-09-15 18:10:44 +03:00
|
|
|
/**
|
|
|
|
* Create a cancel token
|
|
|
|
* @returns {CancelTokenSource}
|
|
|
|
*/
|
|
|
|
const createCancelToken = () => axios.CancelToken.source()
|
|
|
|
|
2020-08-03 13:54:37 +03:00
|
|
|
/**
|
|
|
|
* Get the list of available search providers
|
2020-08-04 11:00:27 +03:00
|
|
|
*
|
|
|
|
* @returns {Array}
|
2020-08-03 13:54:37 +03:00
|
|
|
*/
|
|
|
|
export async function getTypes() {
|
|
|
|
try {
|
2020-09-07 11:47:10 +03:00
|
|
|
const { data } = await axios.get(generateOcsUrl('search', 2) + 'providers', {
|
2020-08-04 19:15:09 +03:00
|
|
|
params: {
|
|
|
|
// Sending which location we're currently at
|
|
|
|
from: window.location.pathname.replace('/index.php', '') + window.location.search,
|
|
|
|
},
|
|
|
|
})
|
2020-09-07 11:47:10 +03:00
|
|
|
if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) {
|
2020-08-04 19:15:09 +03:00
|
|
|
// Providers are sorted by the api based on their order key
|
2020-09-07 11:47:10 +03:00
|
|
|
return data.ocs.data
|
2020-08-03 13:54:37 +03:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of available search providers
|
|
|
|
*
|
2020-09-15 18:10:44 +03:00
|
|
|
* @param {Object} options destructuring object
|
|
|
|
* @param {string} options.type the type to search
|
|
|
|
* @param {string} options.query the search
|
|
|
|
* @param {int|string|undefined} options.cursor the offset for paginated searches
|
|
|
|
* @returns {Object} {request: Promise, cancel: Promise}
|
2020-08-03 13:54:37 +03:00
|
|
|
*/
|
2020-09-15 18:10:44 +03:00
|
|
|
export function search({ type, query, cursor }) {
|
|
|
|
/**
|
|
|
|
* Generate an axios cancel token
|
|
|
|
*/
|
|
|
|
const cancelToken = createCancelToken()
|
|
|
|
|
|
|
|
const request = async() => axios.get(generateOcsUrl('search', 2) + `providers/${type}/search`, {
|
|
|
|
cancelToken: cancelToken.token,
|
2020-08-25 18:01:10 +03:00
|
|
|
params: {
|
2020-09-07 11:47:10 +03:00
|
|
|
term: query,
|
2020-08-25 18:01:10 +03:00
|
|
|
cursor,
|
2020-08-25 18:16:12 +03:00
|
|
|
// Sending which location we're currently at
|
|
|
|
from: window.location.pathname.replace('/index.php', '') + window.location.search,
|
2020-09-02 05:24:03 +03:00
|
|
|
},
|
2020-08-25 18:01:10 +03:00
|
|
|
})
|
2020-09-15 18:10:44 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
request,
|
|
|
|
cancel: cancelToken.cancel,
|
|
|
|
}
|
2020-08-03 13:54:37 +03:00
|
|
|
}
|