2020-07-27 22:04:12 +02:00
|
|
|
// @flow
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-07-19 18:38:53 +02:00
|
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
2021-09-16 22:00:44 +02:00
|
|
|
import { selectShowMatureContent } from 'redux/selectors/settings';
|
|
|
|
import {
|
|
|
|
buildURI,
|
|
|
|
doResolveUris,
|
|
|
|
batchActions,
|
|
|
|
isURIValid,
|
|
|
|
makeSelectClaimForUri,
|
|
|
|
makeSelectClaimIsNsfw,
|
|
|
|
} from 'lbry-redux';
|
2021-08-17 16:03:25 +02:00
|
|
|
import { makeSelectSearchUrisForQuery, selectSearchValue } from 'redux/selectors/search';
|
2020-07-27 22:04:12 +02:00
|
|
|
import handleFetchResponse from 'util/handle-fetch';
|
2021-07-13 08:28:09 +02:00
|
|
|
import { getSearchQueryString } from 'util/query-params';
|
2021-07-29 19:19:12 +02:00
|
|
|
import { SIMPLE_SITE, SEARCH_SERVER_API } from 'config';
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
type Dispatch = (action: any) => any;
|
|
|
|
type GetState = () => { search: SearchState };
|
|
|
|
|
|
|
|
type SearchOptions = {
|
|
|
|
size?: number,
|
|
|
|
from?: number,
|
|
|
|
related_to?: string,
|
|
|
|
nsfw?: boolean,
|
|
|
|
isBackgroundSearch?: boolean,
|
|
|
|
};
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
let lighthouse = {
|
2021-07-29 19:19:12 +02:00
|
|
|
CONNECTION_STRING: SEARCH_SERVER_API,
|
2020-12-03 18:29:47 +01:00
|
|
|
search: (queryString: string) => fetch(`${lighthouse.CONNECTION_STRING}?${queryString}`).then(handleFetchResponse),
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
export const setSearchApi = (endpoint: string) => {
|
|
|
|
lighthouse.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end;
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const doSearch = (rawQuery: string, searchOptions: SearchOptions) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
|
|
|
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
|
|
|
|
|
|
|
|
if (!query) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_FAIL,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
|
2021-07-13 08:28:09 +02:00
|
|
|
const queryWithOptions = getSearchQueryString(query, searchOptions);
|
2021-03-26 09:33:30 +01:00
|
|
|
|
2021-07-13 08:28:09 +02:00
|
|
|
const size = searchOptions.size;
|
2021-03-26 09:33:30 +01:00
|
|
|
const from = searchOptions.from;
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
// If we have already searched for something, we don't need to do anything
|
2021-08-17 16:03:25 +02:00
|
|
|
const urisForQuery = makeSelectSearchUrisForQuery(queryWithOptions)(state);
|
2020-07-27 22:04:12 +02:00
|
|
|
if (urisForQuery && !!urisForQuery.length) {
|
2021-03-26 09:33:30 +01:00
|
|
|
if (!size || !from || from + size < urisForQuery.length) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_START,
|
|
|
|
});
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
lighthouse
|
|
|
|
.search(queryWithOptions)
|
2021-08-17 16:03:25 +02:00
|
|
|
.then((data: { body: Array<{ name: string, claimId: string }>, poweredBy: string }) => {
|
|
|
|
const { body: result, poweredBy } = data;
|
2020-07-27 22:04:12 +02:00
|
|
|
const uris = [];
|
|
|
|
const actions = [];
|
|
|
|
|
2021-08-17 16:03:25 +02:00
|
|
|
result.forEach((item) => {
|
|
|
|
if (item) {
|
|
|
|
const { name, claimId } = item;
|
2020-07-27 22:04:12 +02:00
|
|
|
const urlObj: LbryUrlObj = {};
|
|
|
|
|
|
|
|
if (name.startsWith('@')) {
|
|
|
|
urlObj.channelName = name;
|
|
|
|
urlObj.channelClaimId = claimId;
|
|
|
|
} else {
|
|
|
|
urlObj.streamName = name;
|
|
|
|
urlObj.streamClaimId = claimId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = buildURI(urlObj);
|
2021-01-28 20:03:37 +01:00
|
|
|
if (isURIValid(url)) {
|
|
|
|
uris.push(url);
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-12 19:02:19 +02:00
|
|
|
actions.push(doResolveUris(uris));
|
|
|
|
|
2020-07-27 22:04:12 +02:00
|
|
|
actions.push({
|
|
|
|
type: ACTIONS.SEARCH_SUCCESS,
|
|
|
|
data: {
|
|
|
|
query: queryWithOptions,
|
2021-04-07 12:50:15 +02:00
|
|
|
from: from,
|
2021-03-26 09:33:30 +01:00
|
|
|
size: size,
|
2020-07-27 22:04:12 +02:00
|
|
|
uris,
|
2021-08-17 16:03:25 +02:00
|
|
|
recsys: poweredBy,
|
2020-07-27 22:04:12 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
dispatch(batchActions(...actions));
|
|
|
|
})
|
2021-07-13 08:28:09 +02:00
|
|
|
.catch(() => {
|
2020-07-27 22:04:12 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_FAIL,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doUpdateSearchOptions = (newOptions: SearchOptions, additionalOptions: SearchOptions) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
|
|
|
const state = getState();
|
|
|
|
const searchValue = selectSearchValue(state);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_SEARCH_OPTIONS,
|
|
|
|
data: newOptions,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (searchValue) {
|
|
|
|
// After updating, perform a search with the new options
|
|
|
|
dispatch(doSearch(searchValue, additionalOptions));
|
|
|
|
}
|
|
|
|
};
|
2020-12-03 18:29:47 +01:00
|
|
|
|
2021-09-16 22:00:44 +02:00
|
|
|
export const doFetchRecommendedContent = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
|
2021-03-19 16:04:12 +01:00
|
|
|
const state = getState();
|
|
|
|
const claim = makeSelectClaimForUri(uri)(state);
|
2021-09-16 22:00:44 +02:00
|
|
|
const matureEnabled = selectShowMatureContent(state);
|
|
|
|
const claimIsMature = makeSelectClaimIsNsfw(uri)(state);
|
2021-03-19 16:04:12 +01:00
|
|
|
|
|
|
|
if (claim && claim.value && claim.claim_id) {
|
2021-09-16 22:00:44 +02:00
|
|
|
const options: SearchOptions = { size: 20, nsfw: matureEnabled, isBackgroundSearch: true };
|
2021-07-19 18:38:53 +02:00
|
|
|
|
|
|
|
if (SIMPLE_SITE) {
|
|
|
|
options[SEARCH_OPTIONS.CLAIM_TYPE] = SEARCH_OPTIONS.INCLUDE_FILES;
|
|
|
|
options[SEARCH_OPTIONS.MEDIA_VIDEO] = true;
|
2021-09-16 22:00:44 +02:00
|
|
|
options[SEARCH_OPTIONS.PRICE_FILTER_FREE] = true;
|
|
|
|
}
|
|
|
|
if (matureEnabled || !claimIsMature) {
|
|
|
|
options[SEARCH_OPTIONS.RELATED_TO] = claim.claim_id;
|
2021-07-19 18:38:53 +02:00
|
|
|
}
|
2021-09-16 22:00:44 +02:00
|
|
|
|
2021-03-19 16:04:12 +01:00
|
|
|
const { title } = claim.value;
|
2021-09-16 22:00:44 +02:00
|
|
|
|
2021-03-19 16:04:12 +01:00
|
|
|
if (title && options) {
|
|
|
|
dispatch(doSearch(title, options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
export { lighthouse };
|