lbry-desktop/ui/redux/selectors/search.js

207 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2020-07-27 22:04:12 +02:00
// @flow
import { getSearchQueryString } from 'util/query-params';
2021-03-31 22:55:26 +02:00
import { selectShowMatureContent } from 'redux/selectors/settings';
import {
parseURI,
makeSelectClaimForUri,
makeSelectClaimForClaimId,
makeSelectClaimIsNsfw,
buildURI,
isClaimNsfw,
makeSelectPendingClaimForUri,
2020-12-16 19:31:07 +01:00
makeSelectIsUriResolving,
} from 'lbry-redux';
2020-07-27 22:04:12 +02:00
import { createSelector } from 'reselect';
2021-03-26 09:33:30 +01:00
import { createNormalizedSearchKey } from 'util/search';
2020-07-27 22:04:12 +02:00
type State = { search: SearchState };
export const selectState = (state: State): SearchState => state.search;
export const selectSearchValue: (state: State) => string = createSelector(selectState, (state) => state.searchQuery);
2020-07-27 22:04:12 +02:00
export const selectSearchOptions: (state: State) => SearchOptions = createSelector(
selectState,
(state) => state.options
);
2020-07-27 22:04:12 +02:00
export const selectIsSearching: (state: State) => boolean = createSelector(selectState, (state) => state.searching);
2020-07-27 22:04:12 +02:00
export const selectSearchResultByQuery: (state: State) => { [string]: Array<string> } = createSelector(
2020-07-27 22:04:12 +02:00
selectState,
(state) => state.resultsByQuery
2020-07-27 22:04:12 +02:00
);
2021-03-26 09:33:30 +01:00
export const selectHasReachedMaxResultsLength: (state: State) => { [boolean]: Array<boolean> } = createSelector(
selectState,
(state) => state.hasReachedMaxResultsLength
);
export const makeSelectSearchUrisForQuery = (query: string): ((state: State) => Array<string>) =>
2020-07-27 22:04:12 +02:00
// replace statement below is kind of ugly, and repeated in doSearch action
createSelector(selectSearchResultByQuery, (byQuery) => {
2021-03-26 09:33:30 +01:00
if (query) {
query = query.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
const normalizedQuery = createNormalizedSearchKey(query);
return byQuery[normalizedQuery] && byQuery[normalizedQuery]['uris'];
2021-03-26 09:33:30 +01:00
}
return byQuery[query] && byQuery[query]['uris'];
2021-03-26 09:33:30 +01:00
});
export const makeSelectHasReachedMaxResultsLength = (query: string): ((state: State) => boolean) =>
createSelector(selectHasReachedMaxResultsLength, (hasReachedMaxResultsLength) => {
if (query) {
query = query.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
const normalizedQuery = createNormalizedSearchKey(query);
return hasReachedMaxResultsLength[normalizedQuery];
}
return hasReachedMaxResultsLength[query];
});
2020-07-27 22:04:12 +02:00
export const makeSelectRecommendedContentForUri = (uri: string) =>
createSelector(
makeSelectClaimForUri(uri),
selectSearchResultByQuery,
2020-07-27 22:04:12 +02:00
makeSelectClaimIsNsfw(uri),
(claim, searchUrisByQuery, isMature) => {
let recommendedContent;
if (claim) {
// always grab full URL - this can change once search returns canonical
const currentUri = buildURI({ streamClaimId: claim.claim_id, streamName: claim.name });
const { title } = claim.value;
if (!title) {
return;
}
const options: {
related_to?: string,
nsfw?: boolean,
isBackgroundSearch?: boolean,
} = { related_to: claim.claim_id, isBackgroundSearch: true };
2021-01-08 23:02:40 +01:00
options['nsfw'] = isMature;
2020-07-27 22:04:12 +02:00
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
2021-03-26 09:33:30 +01:00
const normalizedSearchQuery = createNormalizedSearchKey(searchQuery);
2020-07-27 22:04:12 +02:00
let searchResult = searchUrisByQuery[normalizedSearchQuery];
if (searchResult) {
recommendedContent = searchResult['uris'].filter((searchUri) => searchUri !== currentUri);
2020-07-27 22:04:12 +02:00
}
}
return recommendedContent;
}
);
export const makeSelectRecommendedRecsysIdForClaimId = (claimId: string) =>
createSelector(makeSelectClaimForClaimId(claimId), selectSearchResultByQuery, (claim, searchUrisByQuery) => {
// TODO: DRY this out.
let poweredBy;
if (claim) {
const isMature = isClaimNsfw(claim);
const { title } = claim.value;
if (!title) {
return;
}
const options: {
related_to?: string,
nsfw?: boolean,
isBackgroundSearch?: boolean,
} = { related_to: claim.claim_id, isBackgroundSearch: true };
options['nsfw'] = isMature;
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
const normalizedSearchQuery = createNormalizedSearchKey(searchQuery);
let searchResult = searchUrisByQuery[normalizedSearchQuery];
if (searchResult) {
poweredBy = searchResult.recsys;
} else {
return normalizedSearchQuery;
}
}
return poweredBy;
});
export const makeSelectWinningUriForQuery = (query: string) => {
const uriFromQuery = `lbry://${query}`;
let channelUriFromQuery;
try {
const { isChannel } = parseURI(uriFromQuery);
if (!isChannel) {
channelUriFromQuery = `lbry://@${query}`;
}
} catch (e) {}
return createSelector(
2021-03-31 22:55:26 +02:00
selectShowMatureContent,
makeSelectPendingClaimForUri(uriFromQuery),
makeSelectClaimForUri(uriFromQuery),
makeSelectClaimForUri(channelUriFromQuery),
(matureEnabled, pendingClaim, claim1, claim2) => {
2020-12-03 18:29:47 +01:00
const claim1Mature = claim1 && isClaimNsfw(claim1);
const claim2Mature = claim2 && isClaimNsfw(claim2);
let pendingAmount = pendingClaim && pendingClaim.amount;
2020-12-03 18:29:47 +01:00
if (!claim1 && !claim2) {
return undefined;
} else if (!claim1 && claim2) {
2020-12-03 18:29:47 +01:00
return matureEnabled ? claim2.canonical_url : claim2Mature ? undefined : claim2.canonical_url;
} else if (claim1 && !claim2) {
return matureEnabled
? claim1.repost_url || claim1.canonical_url
: claim1Mature
? undefined
: claim1.repost_url || claim1.canonical_url;
}
const effectiveAmount1 = claim1 && (claim1.repost_bid_amount || claim1.meta.effective_amount);
// claim2 will never have a repost_bid_amount because reposts never start with "@"
const effectiveAmount2 = claim2 && claim2.meta.effective_amount;
2020-11-04 20:39:16 +01:00
2020-12-03 18:29:47 +01:00
if (!matureEnabled) {
if (claim1Mature && !claim2Mature) {
return claim2.canonical_url;
} else if (claim2Mature && !claim1Mature) {
return claim1.repost_url || claim1.canonical_url;
2020-12-03 18:29:47 +01:00
} else if (claim1Mature && claim2Mature) {
return undefined;
}
}
const returnBeforePending =
2021-01-13 16:44:44 +01:00
Number(effectiveAmount1) > Number(effectiveAmount2)
? claim1.repost_url || claim1.canonical_url
: claim2.canonical_url;
if (pendingAmount && pendingAmount > effectiveAmount1 && pendingAmount > effectiveAmount2) {
return pendingAmount.permanent_url;
} else {
return returnBeforePending;
}
}
);
};
2020-12-16 19:31:07 +01:00
export const makeSelectIsResolvingWinningUri = (query: string = '') => {
const uriFromQuery = `lbry://${query}`;
let channelUriFromQuery;
try {
const { isChannel } = parseURI(uriFromQuery);
if (!isChannel) {
channelUriFromQuery = `lbry://@${query}`;
}
} catch (e) {}
return createSelector(
makeSelectIsUriResolving(uriFromQuery),
channelUriFromQuery ? makeSelectIsUriResolving(channelUriFromQuery) : () => {},
(claim1IsResolving, claim2IsResolving) => {
return claim1IsResolving || claim2IsResolving;
}
);
};