DRY: fix duplicate code for Recommended key generation
Must use `getRecommendationSearchOptions`, otherwise there is a chance of some clients deriving the wrong key.
This commit is contained in:
parent
d50f51bd3b
commit
539cc062b6
2 changed files with 20 additions and 25 deletions
|
@ -1,7 +1,5 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { getSearchQueryString } from 'util/query-params';
|
|
||||||
import { selectShowMatureContent } from 'redux/selectors/settings';
|
import { selectShowMatureContent } from 'redux/selectors/settings';
|
||||||
import { SEARCH_OPTIONS } from 'constants/search';
|
|
||||||
import {
|
import {
|
||||||
selectClaimsByUri,
|
selectClaimsByUri,
|
||||||
makeSelectClaimForUri,
|
makeSelectClaimForUri,
|
||||||
|
@ -14,11 +12,10 @@ import { parseURI } from 'util/lbryURI';
|
||||||
import { isClaimNsfw } from 'util/claim';
|
import { isClaimNsfw } from 'util/claim';
|
||||||
import { createSelector } from 'reselect';
|
import { createSelector } from 'reselect';
|
||||||
import { createCachedSelector } from 're-reselect';
|
import { createCachedSelector } from 're-reselect';
|
||||||
import { createNormalizedSearchKey, getRecommendationSearchOptions } from 'util/search';
|
import { createNormalizedSearchKey, getRecommendationSearchKey, getRecommendationSearchOptions } from 'util/search';
|
||||||
import { selectMutedChannels } from 'redux/selectors/blocked';
|
import { selectMutedChannels } from 'redux/selectors/blocked';
|
||||||
import { selectHistory } from 'redux/selectors/content';
|
import { selectHistory } from 'redux/selectors/content';
|
||||||
import { selectAllCostInfoByUri } from 'lbryinc';
|
import { selectAllCostInfoByUri } from 'lbryinc';
|
||||||
import { SIMPLE_SITE } from 'config';
|
|
||||||
|
|
||||||
type State = { claims: any, search: SearchState, user: UserState };
|
type State = { claims: any, search: SearchState, user: UserState };
|
||||||
|
|
||||||
|
@ -66,7 +63,6 @@ export const selectRecommendedContentForUri = createCachedSelector(
|
||||||
selectClaimIsNsfwForUri, // (state, uri)
|
selectClaimIsNsfwForUri, // (state, uri)
|
||||||
(uri, history, claimsByUri, matureEnabled, blockedChannels, costInfoByUri, searchUrisByQuery, isMature) => {
|
(uri, history, claimsByUri, matureEnabled, blockedChannels, costInfoByUri, searchUrisByQuery, isMature) => {
|
||||||
const claim = claimsByUri[uri];
|
const claim = claimsByUri[uri];
|
||||||
|
|
||||||
if (!claim) return;
|
if (!claim) return;
|
||||||
|
|
||||||
let recommendedContent;
|
let recommendedContent;
|
||||||
|
@ -74,26 +70,10 @@ export const selectRecommendedContentForUri = createCachedSelector(
|
||||||
const currentClaimId = claim.claim_id;
|
const currentClaimId = claim.claim_id;
|
||||||
|
|
||||||
const { title } = claim.value;
|
const { title } = claim.value;
|
||||||
|
|
||||||
if (!title) return;
|
if (!title) return;
|
||||||
|
|
||||||
const options: {
|
const options = getRecommendationSearchOptions(matureEnabled, isMature, claim.claim_id);
|
||||||
size: number,
|
const normalizedSearchQuery = getRecommendationSearchKey(title, options);
|
||||||
nsfw?: boolean,
|
|
||||||
isBackgroundSearch?: boolean,
|
|
||||||
} = { size: 20, nsfw: matureEnabled, isBackgroundSearch: true };
|
|
||||||
|
|
||||||
if (SIMPLE_SITE) {
|
|
||||||
options[SEARCH_OPTIONS.CLAIM_TYPE] = SEARCH_OPTIONS.INCLUDE_FILES;
|
|
||||||
options[SEARCH_OPTIONS.MEDIA_VIDEO] = true;
|
|
||||||
options[SEARCH_OPTIONS.PRICE_FILTER_FREE] = true;
|
|
||||||
}
|
|
||||||
if (matureEnabled || (!matureEnabled && !isMature)) {
|
|
||||||
options[SEARCH_OPTIONS.RELATED_TO] = claim.claim_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
|
|
||||||
const normalizedSearchQuery = createNormalizedSearchKey(searchQuery);
|
|
||||||
|
|
||||||
let searchResult = searchUrisByQuery[normalizedSearchQuery];
|
let searchResult = searchUrisByQuery[normalizedSearchQuery];
|
||||||
|
|
||||||
|
@ -169,8 +149,7 @@ export const makeSelectRecommendedRecsysIdForClaimId = (claimId: string) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = getRecommendationSearchOptions(matureEnabled, isMature, claimId);
|
const options = getRecommendationSearchOptions(matureEnabled, isMature, claimId);
|
||||||
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
|
const normalizedSearchQuery = getRecommendationSearchKey(title, options);
|
||||||
const normalizedSearchQuery = createNormalizedSearchKey(searchQuery);
|
|
||||||
|
|
||||||
const searchResult = searchUrisByQuery[normalizedSearchQuery];
|
const searchResult = searchUrisByQuery[normalizedSearchQuery];
|
||||||
if (searchResult) {
|
if (searchResult) {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import { isNameValid, isURIValid, normalizeURI, parseURI } from 'util/lbryURI';
|
import { isNameValid, isURIValid, normalizeURI, parseURI } from 'util/lbryURI';
|
||||||
import { URL as SITE_URL, URL_LOCAL, URL_DEV, SIMPLE_SITE } from 'config';
|
import { URL as SITE_URL, URL_LOCAL, URL_DEV, SIMPLE_SITE } from 'config';
|
||||||
import { SEARCH_OPTIONS } from 'constants/search';
|
import { SEARCH_OPTIONS } from 'constants/search';
|
||||||
|
import { getSearchQueryString } from 'util/query-params';
|
||||||
|
|
||||||
export function createNormalizedSearchKey(query: string) {
|
export function createNormalizedSearchKey(query: string) {
|
||||||
const removeParam = (query: string, param: string) => {
|
const removeParam = (query: string, param: string) => {
|
||||||
|
@ -97,6 +98,16 @@ export function getUriForSearchTerm(term: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 'Recommended' search query is used as the key to store the results. This
|
||||||
|
* function ensures all clients derive the same key by making this the only
|
||||||
|
* place to make tweaks.
|
||||||
|
*
|
||||||
|
* @param matureEnabled
|
||||||
|
* @param claimIsMature
|
||||||
|
* @param claimId
|
||||||
|
* @returns {{size: number, nsfw: boolean, isBackgroundSearch: boolean}}
|
||||||
|
*/
|
||||||
export function getRecommendationSearchOptions(matureEnabled: boolean, claimIsMature: boolean, claimId: string) {
|
export function getRecommendationSearchOptions(matureEnabled: boolean, claimIsMature: boolean, claimId: string) {
|
||||||
const options = { size: 20, nsfw: matureEnabled, isBackgroundSearch: true };
|
const options = { size: 20, nsfw: matureEnabled, isBackgroundSearch: true };
|
||||||
|
|
||||||
|
@ -112,3 +123,8 @@ export function getRecommendationSearchOptions(matureEnabled: boolean, claimIsMa
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRecommendationSearchKey(title: string, options: {}) {
|
||||||
|
const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options);
|
||||||
|
return createNormalizedSearchKey(searchQuery);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue