Blacklist: use existing map instead of looping (#400)

We already have a pre-calculated map, but not used except for comments.

At the expense of pre-calculating it, the subsequent queries are instantaneous compared to the loop.

We are still not perfect in term of reducing re-renders, so this helps a lot.
This commit is contained in:
infinite-persistence 2021-12-01 07:24:27 -08:00 committed by GitHub
parent 7e9e213974
commit 787ebd9588
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 42 deletions

View file

@ -8,17 +8,17 @@ import { createCachedSelector } from 're-reselect';
import { selectClaimForUri } from 'redux/selectors/claims'; import { selectClaimForUri } from 'redux/selectors/claims';
import { selectMutedChannels } from 'redux/selectors/blocked'; import { selectMutedChannels } from 'redux/selectors/blocked';
import { selectModerationBlockList } from 'redux/selectors/comments'; import { selectModerationBlockList } from 'redux/selectors/comments';
import { selectBlackListedOutpoints, selectFilteredOutpoints } from 'lbryinc'; import { selectBlacklistedOutpointMap, selectFilteredOutpointMap } from 'lbryinc';
import { getChannelFromClaim } from 'util/claim'; import { getChannelFromClaim } from 'util/claim';
import { isURIEqual } from 'util/lbryURI'; import { isURIEqual } from 'util/lbryURI';
export const selectBanStateForUri = createCachedSelector( export const selectBanStateForUri = createCachedSelector(
selectClaimForUri, selectClaimForUri,
selectBlackListedOutpoints, selectBlacklistedOutpointMap,
selectFilteredOutpoints, selectFilteredOutpointMap,
selectMutedChannels, selectMutedChannels,
selectModerationBlockList, selectModerationBlockList,
(claim, blackListedOutpoints, filteredOutpoints, mutedChannelUris, personalBlocklist) => { (claim, blackListedOutpointMap, filteredOutpointMap, mutedChannelUris, personalBlocklist) => {
const banState = {}; const banState = {};
if (!claim) { if (!claim) {
@ -28,13 +28,10 @@ export const selectBanStateForUri = createCachedSelector(
const channelClaim = getChannelFromClaim(claim); const channelClaim = getChannelFromClaim(claim);
// This will be replaced once blocking is done at the wallet server level. // This will be replaced once blocking is done at the wallet server level.
if (blackListedOutpoints) { if (blackListedOutpointMap) {
if ( if (
blackListedOutpoints.some( (channelClaim && blackListedOutpointMap[`${channelClaim.txid}:${channelClaim.nout}`]) ||
(outpoint) => blackListedOutpointMap[`${claim.txid}:${claim.nout}`]
(channelClaim && outpoint.txid === channelClaim.txid && outpoint.nout === channelClaim.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
)
) { ) {
banState['blacklisted'] = true; banState['blacklisted'] = true;
} }
@ -42,13 +39,10 @@ export const selectBanStateForUri = createCachedSelector(
// We're checking to see if the stream outpoint or signing channel outpoint // We're checking to see if the stream outpoint or signing channel outpoint
// is in the filter list. // is in the filter list.
if (filteredOutpoints) { if (filteredOutpointMap) {
if ( if (
filteredOutpoints.some( (channelClaim && filteredOutpointMap[`${channelClaim.txid}:${channelClaim.nout}`]) ||
(outpoint) => filteredOutpointMap[`${claim.txid}:${claim.nout}`]
(channelClaim && outpoint.txid === channelClaim.txid && outpoint.nout === channelClaim.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
)
) { ) {
banState['filtered'] = true; banState['filtered'] = true;
} }

View file

@ -9,7 +9,7 @@ import {
makeSelectClaimIsPending, makeSelectClaimIsPending,
} from 'redux/selectors/claims'; } from 'redux/selectors/claims';
import { selectMyUnpublishedCollections } from 'redux/selectors/collections'; import { selectMyUnpublishedCollections } from 'redux/selectors/collections';
import { selectBlackListedOutpoints, doFetchSubCount, selectSubCountForUri } from 'lbryinc'; import { selectBlacklistedOutpointMap, doFetchSubCount, selectSubCountForUri } from 'lbryinc';
import { selectYoutubeChannels } from 'redux/selectors/user'; import { selectYoutubeChannels } from 'redux/selectors/user';
import { selectIsSubscribedForUri } from 'redux/selectors/subscriptions'; import { selectIsSubscribedForUri } from 'redux/selectors/subscriptions';
import { selectModerationBlockList } from 'redux/selectors/comments'; import { selectModerationBlockList } from 'redux/selectors/comments';
@ -28,7 +28,7 @@ const select = (state, props) => {
page: selectCurrentChannelPage(state), page: selectCurrentChannelPage(state),
claim, claim,
isSubscribed: selectIsSubscribedForUri(state, props.uri), isSubscribed: selectIsSubscribedForUri(state, props.uri),
blackListedOutpoints: selectBlackListedOutpoints(state), blackListedOutpointMap: selectBlacklistedOutpointMap(state),
subCount: selectSubCountForUri(state, props.uri), subCount: selectSubCountForUri(state, props.uri),
pending: makeSelectClaimIsPending(props.uri)(state), pending: makeSelectClaimIsPending(props.uri)(state),
youtubeChannels: selectYoutubeChannels(state), youtubeChannels: selectYoutubeChannels(state),

View file

@ -50,10 +50,7 @@ type Props = {
channelIsMine: boolean, channelIsMine: boolean,
isSubscribed: boolean, isSubscribed: boolean,
channelIsBlocked: boolean, channelIsBlocked: boolean,
blackListedOutpoints: Array<{ blackListedOutpointMap: { [string]: number },
txid: string,
nout: number,
}>,
fetchSubCount: (string) => void, fetchSubCount: (string) => void,
subCount: number, subCount: number,
pending: boolean, pending: boolean,
@ -72,7 +69,7 @@ function ChannelPage(props: Props) {
// page, ?page= may come back some day? // page, ?page= may come back some day?
channelIsMine, channelIsMine,
isSubscribed, isSubscribed,
blackListedOutpoints, blackListedOutpointMap,
fetchSubCount, fetchSubCount,
subCount, subCount,
pending, pending,
@ -137,10 +134,8 @@ function ChannelPage(props: Props) {
let channelIsBlackListed = false; let channelIsBlackListed = false;
if (claim && blackListedOutpoints) { if (claim && blackListedOutpointMap) {
channelIsBlackListed = blackListedOutpoints.some( channelIsBlackListed = blackListedOutpointMap[`${claim.txid}:${claim.nout}`];
(outpoint) => outpoint.txid === claim.txid && outpoint.nout === claim.nout
);
} }
// If a user changes tabs, update the url so it stays on the same page if they refresh. // If a user changes tabs, update the url so it stays on the same page if they refresh.

View file

@ -24,7 +24,7 @@ import { normalizeURI } from 'util/lbryURI';
import * as COLLECTIONS_CONSTS from 'constants/collections'; import * as COLLECTIONS_CONSTS from 'constants/collections';
import { push } from 'connected-react-router'; import { push } from 'connected-react-router';
import { makeSelectChannelInSubscriptions } from 'redux/selectors/subscriptions'; import { makeSelectChannelInSubscriptions } from 'redux/selectors/subscriptions';
import { selectBlackListedOutpoints } from 'lbryinc'; import { selectBlacklistedOutpointMap } from 'lbryinc';
import ShowPage from './view'; import ShowPage from './view';
const select = (state, props) => { const select = (state, props) => {
@ -73,7 +73,7 @@ const select = (state, props) => {
uri, uri,
claim, claim,
isResolvingUri: selectIsUriResolving(state, uri), isResolvingUri: selectIsUriResolving(state, uri),
blackListedOutpoints: selectBlackListedOutpoints(state), blackListedOutpointMap: selectBlacklistedOutpointMap(state),
totalPages: makeSelectTotalPagesForChannel(uri, PAGE_SIZE)(state), totalPages: makeSelectTotalPagesForChannel(uri, PAGE_SIZE)(state),
isSubscribed: makeSelectChannelInSubscriptions(uri)(state), isSubscribed: makeSelectChannelInSubscriptions(uri)(state),
title: selectTitleForUri(state, uri), title: selectTitleForUri(state, uri),

View file

@ -28,10 +28,7 @@ type Props = {
uri: string, uri: string,
claim: StreamClaim, claim: StreamClaim,
location: UrlLocation, location: UrlLocation,
blackListedOutpoints: Array<{ blackListedOutpointMap: { [string]: number },
txid: string,
nout: number,
}>,
title: string, title: string,
claimIsMine: boolean, claimIsMine: boolean,
claimIsPending: boolean, claimIsPending: boolean,
@ -50,7 +47,7 @@ function ShowPage(props: Props) {
resolveUri, resolveUri,
uri, uri,
claim, claim,
blackListedOutpoints, blackListedOutpointMap,
location, location,
claimIsMine, claimIsMine,
isSubscribed, isSubscribed,
@ -181,14 +178,11 @@ function ShowPage(props: Props) {
} else if (claim.name.length && claim.name[0] === '@') { } else if (claim.name.length && claim.name[0] === '@') {
innerContent = <ChannelPage uri={uri} location={location} />; innerContent = <ChannelPage uri={uri} location={location} />;
} else if (claim) { } else if (claim) {
let isClaimBlackListed = false; const isClaimBlackListed =
blackListedOutpointMap &&
isClaimBlackListed = Boolean(
blackListedOutpoints && (signingChannel && blackListedOutpointMap[`${signingChannel.txid}:${signingChannel.nout}`]) ||
blackListedOutpoints.some( blackListedOutpointMap[`${claim.txid}:${claim.nout}`]
(outpoint) =>
(signingChannel && outpoint.txid === signingChannel.txid && outpoint.nout === signingChannel.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
); );
if (isClaimBlackListed && !claimIsMine) { if (isClaimBlackListed && !claimIsMine) {