2020-08-03 13:54:37 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2020, John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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-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
|
|
|
|
*
|
|
|
|
* @param {string} type the type to search
|
|
|
|
* @param {string} query the search
|
2020-08-25 18:01:10 +03:00
|
|
|
* @param {int|string|undefined} cursor the offset for paginated searches
|
2020-08-03 13:54:37 +03:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2020-08-25 18:01:10 +03:00
|
|
|
export function search(type, query, cursor) {
|
2020-09-07 11:47:10 +03:00
|
|
|
return axios.get(generateOcsUrl('search', 2) + `providers/${type}/search`, {
|
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-08-03 13:54:37 +03:00
|
|
|
}
|