2020-07-27 22:04:12 +02:00
|
|
|
// @flow
|
|
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
|
|
|
|
|
|
|
const DEFAULT_SEARCH_RESULT_FROM = 0;
|
|
|
|
const DEFAULT_SEARCH_SIZE = 20;
|
|
|
|
|
|
|
|
export function parseQueryParams(queryString: string) {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (queryString === '') return {};
|
2017-11-24 15:31:05 +01:00
|
|
|
const parts = queryString
|
2017-12-21 18:32:51 +01:00
|
|
|
.split('?')
|
2017-11-24 15:31:05 +01:00
|
|
|
.pop()
|
2017-12-21 18:32:51 +01:00
|
|
|
.split('&')
|
2021-02-12 18:14:53 +01:00
|
|
|
.map((p) => p.split('='));
|
2017-05-07 14:50:32 +02:00
|
|
|
|
|
|
|
const params = {};
|
2021-02-12 18:14:53 +01:00
|
|
|
parts.forEach((array) => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const [first, second] = array;
|
|
|
|
params[first] = second;
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-07 14:50:32 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2019-07-19 16:52:42 +02:00
|
|
|
// https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter
|
2020-07-27 22:04:12 +02:00
|
|
|
export function updateQueryParam(uri: string, key: string, value: string) {
|
2019-07-17 22:49:06 +02:00
|
|
|
const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
|
2019-10-10 07:23:42 +02:00
|
|
|
const separator = uri.includes('?') ? '&' : '?';
|
2019-07-17 22:49:06 +02:00
|
|
|
if (uri.match(re)) {
|
|
|
|
return uri.replace(re, '$1' + key + '=' + value + '$2');
|
|
|
|
} else {
|
|
|
|
return uri + separator + key + '=' + value;
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
export const getSearchQueryString = (query: string, options: any = {}) => {
|
2021-03-23 09:55:37 +01:00
|
|
|
const isSurroundedByQuotes = (str) => str[0] === '"' && str[str.length - 1] === '"';
|
2020-07-27 22:04:12 +02:00
|
|
|
const encodedQuery = encodeURIComponent(query);
|
|
|
|
const queryParams = [
|
2021-03-23 09:55:37 +01:00
|
|
|
options.exact && !isSurroundedByQuotes(encodedQuery) ? `s="${encodedQuery}"` : `s=${encodedQuery}`,
|
2020-07-27 22:04:12 +02:00
|
|
|
`size=${options.size || DEFAULT_SEARCH_SIZE}`,
|
|
|
|
`from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`,
|
|
|
|
];
|
|
|
|
const { isBackgroundSearch } = options;
|
|
|
|
const includeUserOptions = typeof isBackgroundSearch === 'undefined' ? false : !isBackgroundSearch;
|
|
|
|
|
|
|
|
if (includeUserOptions) {
|
|
|
|
const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE];
|
|
|
|
if (claimType) {
|
|
|
|
queryParams.push(`claimType=${claimType}`);
|
|
|
|
|
2021-02-16 17:59:16 +01:00
|
|
|
/*
|
2021-03-23 04:30:38 +01:00
|
|
|
* Due to limitations in lighthouse, we can't pass the mediaType parameter
|
|
|
|
* when searching for channels or "everything".
|
2021-02-16 17:59:16 +01:00
|
|
|
*/
|
|
|
|
if (!claimType.includes(SEARCH_OPTIONS.INCLUDE_CHANNELS)) {
|
2020-07-27 22:04:12 +02:00
|
|
|
queryParams.push(
|
|
|
|
`mediaType=${[
|
|
|
|
SEARCH_OPTIONS.MEDIA_FILE,
|
|
|
|
SEARCH_OPTIONS.MEDIA_AUDIO,
|
|
|
|
SEARCH_OPTIONS.MEDIA_VIDEO,
|
|
|
|
SEARCH_OPTIONS.MEDIA_TEXT,
|
|
|
|
SEARCH_OPTIONS.MEDIA_IMAGE,
|
|
|
|
SEARCH_OPTIONS.MEDIA_APPLICATION,
|
|
|
|
].reduce((acc, currentOption) => (options[currentOption] ? `${acc}${currentOption},` : acc), '')}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 06:52:32 +01:00
|
|
|
|
|
|
|
const sortBy = options[SEARCH_OPTIONS.SORT];
|
|
|
|
if (sortBy) {
|
|
|
|
queryParams.push(`${SEARCH_OPTIONS.SORT}=${sortBy}`);
|
|
|
|
}
|
2021-03-30 07:57:37 +02:00
|
|
|
|
|
|
|
const timeFilter = options[SEARCH_OPTIONS.TIME_FILTER];
|
|
|
|
if (timeFilter) {
|
|
|
|
queryParams.push(`${SEARCH_OPTIONS.TIME_FILTER}=${timeFilter}`);
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const additionalOptions = {};
|
2022-03-15 20:07:31 +01:00
|
|
|
const { related_to, nsfw, free_only, gid, uuid } = options;
|
2021-03-23 06:52:32 +01:00
|
|
|
|
2022-03-15 20:07:31 +01:00
|
|
|
if (related_to) {
|
|
|
|
additionalOptions[SEARCH_OPTIONS.RELATED_TO] = related_to;
|
|
|
|
|
|
|
|
if (gid && uuid) {
|
|
|
|
additionalOptions['gid'] = gid;
|
|
|
|
additionalOptions['uuid'] = uuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_only) {
|
|
|
|
additionalOptions[SEARCH_OPTIONS.PRICE_FILTER_FREE] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsfw === false) {
|
|
|
|
additionalOptions[SEARCH_OPTIONS.INCLUDE_MATURE] = false;
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
if (additionalOptions) {
|
2021-02-12 18:14:53 +01:00
|
|
|
Object.keys(additionalOptions).forEach((key) => {
|
2020-07-27 22:04:12 +02:00
|
|
|
const option = additionalOptions[key];
|
|
|
|
queryParams.push(`${key}=${option}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryParams.join('&');
|
|
|
|
};
|