1e67a5cc7f
* Factor out lighthouse-result processing code for FYP re-use. The FYP results will be in the same format as LH. * Recsys: add ability to pass in specific uuid to use For FYP, we want to pass the UUID as a param when searching for recommendations. The search comes before the recsys entry creation, so we need to generate the UUID first when searching, and then tell recsys to use that specific ID. * Redux: fetch and store FYP Note that the gid cannot be used as "hash" for the uri list -- it doesn't necessarily change when the list changes, so we can't use it to optimize redux. For now, just always update/render when re-fetched. * UI for FYP * Mark rendered FYPs * Pass the FYP ID down the same way as Collection ID Not ideal, but at least it's in the same pattern as existing code for now. The whole prop-drilling problem with the claim components will be fixed together later. * Include 'gid' and 'uuid' in recommendation search * Allow users to mark recommendations that they dislike * Pass auth-token to all FYP requests + remove beacon use beacons are unreliable and often blocked * Only show FYP for members * FYP readme page * small fixes * fyp Co-authored-by: Thomas Zarebczan <thomas.zarebczan@gmail.com>
107 lines
3.2 KiB
JavaScript
107 lines
3.2 KiB
JavaScript
// @flow
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
|
|
|
const DEFAULT_SEARCH_RESULT_FROM = 0;
|
|
const DEFAULT_SEARCH_SIZE = 20;
|
|
|
|
export function parseQueryParams(queryString: string) {
|
|
if (queryString === '') return {};
|
|
const parts = queryString
|
|
.split('?')
|
|
.pop()
|
|
.split('&')
|
|
.map((p) => p.split('='));
|
|
|
|
const params = {};
|
|
parts.forEach((array) => {
|
|
const [first, second] = array;
|
|
params[first] = second;
|
|
});
|
|
return params;
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter
|
|
export function updateQueryParam(uri: string, key: string, value: string) {
|
|
const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
|
|
const separator = uri.includes('?') ? '&' : '?';
|
|
if (uri.match(re)) {
|
|
return uri.replace(re, '$1' + key + '=' + value + '$2');
|
|
} else {
|
|
return uri + separator + key + '=' + value;
|
|
}
|
|
}
|
|
|
|
export const getSearchQueryString = (query: string, options: any = {}) => {
|
|
const isSurroundedByQuotes = (str) => str[0] === '"' && str[str.length - 1] === '"';
|
|
const encodedQuery = encodeURIComponent(query);
|
|
const queryParams = [
|
|
options.exact && !isSurroundedByQuotes(encodedQuery) ? `s="${encodedQuery}"` : `s=${encodedQuery}`,
|
|
`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}`);
|
|
|
|
/*
|
|
* Due to limitations in lighthouse, we can't pass the mediaType parameter
|
|
* when searching for channels or "everything".
|
|
*/
|
|
if (!claimType.includes(SEARCH_OPTIONS.INCLUDE_CHANNELS)) {
|
|
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), '')}`
|
|
);
|
|
}
|
|
}
|
|
|
|
const sortBy = options[SEARCH_OPTIONS.SORT];
|
|
if (sortBy) {
|
|
queryParams.push(`${SEARCH_OPTIONS.SORT}=${sortBy}`);
|
|
}
|
|
|
|
const timeFilter = options[SEARCH_OPTIONS.TIME_FILTER];
|
|
if (timeFilter) {
|
|
queryParams.push(`${SEARCH_OPTIONS.TIME_FILTER}=${timeFilter}`);
|
|
}
|
|
}
|
|
|
|
const additionalOptions = {};
|
|
const { related_to, nsfw, free_only, gid, uuid } = options;
|
|
|
|
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;
|
|
}
|
|
|
|
if (additionalOptions) {
|
|
Object.keys(additionalOptions).forEach((key) => {
|
|
const option = additionalOptions[key];
|
|
queryParams.push(`${key}=${option}`);
|
|
});
|
|
}
|
|
|
|
return queryParams.join('&');
|
|
};
|