livestream fixes from odysee

This commit is contained in:
zeppi 2021-05-04 22:36:14 -04:00 committed by jessopb
parent de8d139351
commit c24c016eb7

View file

@ -7,6 +7,14 @@ import { createNormalizedClaimSearchKey, MATURE_TAGS } from 'lbry-redux';
import ClaimPreviewTile from 'component/claimPreviewTile'; import ClaimPreviewTile from 'component/claimPreviewTile';
import { useHistory } from 'react-router'; import { useHistory } from 'react-router';
const getNoSourceOptions = (options) => {
const newOptions = Object.assign({}, options);
delete newOptions.has_source;
delete newOptions.stream_types;
newOptions.has_no_source = true;
return newOptions;
};
/** /**
* Updates 'uris' by adding and/or moving active livestreams to the front of * Updates 'uris' by adding and/or moving active livestreams to the front of
* list. * list.
@ -49,7 +57,7 @@ export function prioritizeActiveLivestreams(
let liveChannelIds = Object.keys(livestreamMap); let liveChannelIds = Object.keys(livestreamMap);
// 1. Collect active livestreams from the primary search to put in front. // 1. Collect active livestreams from the primary search to put in front.
uris.forEach(uri => { uris.forEach((uri) => {
const claim = claimsByUri[uri]; const claim = claimsByUri[uri];
if (claimIsLive(claim, liveChannelIds)) { if (claimIsLive(claim, liveChannelIds)) {
liveUris.push(uri); liveUris.push(uri);
@ -60,13 +68,10 @@ export function prioritizeActiveLivestreams(
// 2. Now, repeat on the secondary search. // 2. Now, repeat on the secondary search.
if (options) { if (options) {
const livestreamsOnlySearchCacheQuery = createNormalizedClaimSearchKey({ const livestreamsOnlySearchCacheQuery = createNormalizedClaimSearchKey(getNoSourceOptions(options));
...options,
has_no_source: true,
});
const livestreamsOnlyUris = claimSearchByQuery[livestreamsOnlySearchCacheQuery]; const livestreamsOnlyUris = claimSearchByQuery[livestreamsOnlySearchCacheQuery];
if (livestreamsOnlyUris) { if (livestreamsOnlyUris) {
livestreamsOnlyUris.forEach(uri => { livestreamsOnlyUris.forEach((uri) => {
const claim = claimsByUri[uri]; const claim = claimsByUri[uri];
if (!uris.includes(uri) && claimIsLive(claim, liveChannelIds)) { if (!uris.includes(uri) && claimIsLive(claim, liveChannelIds)) {
liveUris.push(uri); liveUris.push(uri);
@ -78,7 +83,7 @@ export function prioritizeActiveLivestreams(
} }
// 3. Finalize uris by putting live livestreams in front. // 3. Finalize uris by putting live livestreams in front.
const newUris = liveUris.concat(uris.filter(uri => !liveUris.includes(uri))); const newUris = liveUris.concat(uris.filter((uri) => !liveUris.includes(uri)));
uris.splice(0, uris.length, ...newUris); uris.splice(0, uris.length, ...newUris);
} }
@ -92,7 +97,7 @@ type Props = {
doClaimSearch: ({}) => void, doClaimSearch: ({}) => void,
showNsfw: boolean, showNsfw: boolean,
hideReposts: boolean, hideReposts: boolean,
history: { action: string, push: string => void, replace: string => void }, history: { action: string, push: (string) => void, replace: (string) => void },
claimSearchByQuery: { [string]: Array<string> }, claimSearchByQuery: { [string]: Array<string> },
fetchingClaimSearchByQuery: { [string]: boolean }, fetchingClaimSearchByQuery: { [string]: boolean },
claimsByUri: { [string]: any }, claimsByUri: { [string]: any },
@ -113,9 +118,10 @@ type Props = {
limitClaimsPerChannel?: number, limitClaimsPerChannel?: number,
hasSource?: boolean, hasSource?: boolean,
hasNoSource?: boolean, hasNoSource?: boolean,
renderProperties?: Claim => ?Node, renderProperties?: (Claim) => ?Node,
liveLivestreamsFirst?: boolean, liveLivestreamsFirst?: boolean,
livestreamMap?: { [string]: any }, livestreamMap?: { [string]: any },
pin?: boolean,
}; };
function ClaimTilesDiscover(props: Props) { function ClaimTilesDiscover(props: Props) {
@ -135,7 +141,6 @@ function ClaimTilesDiscover(props: Props) {
languages, languages,
claimType, claimType,
streamTypes, streamTypes,
prefixUris,
timestamp, timestamp,
feeAmount, feeAmount,
limitClaimsPerChannel, limitClaimsPerChannel,
@ -147,13 +152,15 @@ function ClaimTilesDiscover(props: Props) {
mutedUris, mutedUris,
liveLivestreamsFirst, liveLivestreamsFirst,
livestreamMap, livestreamMap,
// pin,
prefixUris,
} = props; } = props;
const { location } = useHistory(); const { location } = useHistory();
const urlParams = new URLSearchParams(location.search); const urlParams = new URLSearchParams(location.search);
const feeAmountInUrl = urlParams.get('fee_amount'); const feeAmountInUrl = urlParams.get('fee_amount');
const feeAmountParam = feeAmountInUrl || feeAmount; const feeAmountParam = feeAmountInUrl || feeAmount;
const mutedAndBlockedChannelIds = Array.from(new Set(mutedUris.concat(blockedUris).map(uri => uri.split('#')[1]))); const mutedAndBlockedChannelIds = Array.from(new Set(mutedUris.concat(blockedUris).map((uri) => uri.split('#')[1])));
const liveUris = []; const liveUris = [];
const options: { const options: {
@ -211,7 +218,7 @@ function ClaimTilesDiscover(props: Props) {
// https://github.com/lbryio/lbry-desktop/issues/3774 // https://github.com/lbryio/lbry-desktop/issues/3774
if (hideReposts) { if (hideReposts) {
if (Array.isArray(options.claim_type)) { if (Array.isArray(options.claim_type)) {
options.claim_type = options.claim_type.filter(claimType => claimType !== 'repost'); options.claim_type = options.claim_type.filter((claimType) => claimType !== 'repost');
} else { } else {
options.claim_type = ['stream', 'channel']; options.claim_type = ['stream', 'channel'];
} }
@ -232,13 +239,13 @@ function ClaimTilesDiscover(props: Props) {
const claimSearchCacheQuery = createNormalizedClaimSearchKey(options); const claimSearchCacheQuery = createNormalizedClaimSearchKey(options);
const uris = (prefixUris || []).concat(claimSearchByQuery[claimSearchCacheQuery] || []); const uris = (prefixUris || []).concat(claimSearchByQuery[claimSearchCacheQuery] || []);
const isLoading = fetchingClaimSearchByQuery[claimSearchCacheQuery];
if (liveLivestreamsFirst && livestreamMap) { if (liveLivestreamsFirst && livestreamMap) {
prioritizeActiveLivestreams(uris, liveUris, livestreamMap, claimsByUri, claimSearchByQuery, options); prioritizeActiveLivestreams(uris, liveUris, livestreamMap, claimsByUri, claimSearchByQuery, options);
} }
// Don't use the query from createNormalizedClaimSearchKey for the effect since that doesn't include page & release_time // Don't use the query from createNormalizedClaimSearchKey for the effect since that doesn't include page & release_time
const optionsStringForEffect = JSON.stringify(options); const optionsStringForEffect = JSON.stringify(options);
const isLoading = fetchingClaimSearchByQuery[claimSearchCacheQuery];
const shouldPerformSearch = !isLoading && uris.length === 0; const shouldPerformSearch = !isLoading && uris.length === 0;
React.useEffect(() => { React.useEffect(() => {
@ -247,14 +254,12 @@ function ClaimTilesDiscover(props: Props) {
doClaimSearch(searchOptions); doClaimSearch(searchOptions);
if (liveLivestreamsFirst) { if (liveLivestreamsFirst) {
delete searchOptions.has_source; doClaimSearch(getNoSourceOptions(searchOptions));
delete searchOptions.stream_types;
doClaimSearch({ ...searchOptions, has_no_source: true });
} }
} }
}, [doClaimSearch, shouldPerformSearch, optionsStringForEffect, liveLivestreamsFirst]); }, [doClaimSearch, shouldPerformSearch, optionsStringForEffect, liveLivestreamsFirst]);
const resolveLive = index => { const resolveLive = (index) => {
if (liveLivestreamsFirst && livestreamMap && index < liveUris.length) { if (liveLivestreamsFirst && livestreamMap && index < liveUris.length) {
return true; return true;
} }