Factor out geo-restriction logic

No functional change.

Impetus is to make the logic re-usable for comment-blocking.
This commit is contained in:
infinite-persistence 2022-05-24 21:12:52 +08:00 committed by Thomas Zarebczan
parent ce9a1c128c
commit 426d1ea0c9
2 changed files with 50 additions and 29 deletions

View file

@ -15,6 +15,7 @@ import {
} from 'util/claim'; } from 'util/claim';
import * as CLAIM from 'constants/claim'; import * as CLAIM from 'constants/claim';
import { INTERNAL_TAGS } from 'constants/tags'; import { INTERNAL_TAGS } from 'constants/tags';
import { getGeoRestrictionForClaim } from 'util/geoRestriction';
type State = { claims: any, user: UserState }; type State = { claims: any, user: UserState };
@ -858,34 +859,6 @@ export const selectGeoRestrictionForUri = createCachedSelector(
selectGeoBlockLists, selectGeoBlockLists,
selectUserLocale, selectUserLocale,
(claim, geoBlockLists, locale: LocaleInfo) => { (claim, geoBlockLists, locale: LocaleInfo) => {
if (locale && geoBlockLists && claim) { return getGeoRestrictionForClaim(claim, locale, geoBlockLists);
const claimId: ?string = claim.claim_id;
const channelId: ?string = getChannelIdFromClaim(claim);
let geoConfig: ?GeoConfig;
// --- livestreams
if (isStreamPlaceholderClaim(claim) && geoBlockLists.livestreams) {
geoConfig = geoBlockLists.livestreams[channelId] || geoBlockLists.livestreams[claimId];
}
// --- videos (a.k.a everything else)
else if (geoBlockLists.videos) {
geoConfig = geoBlockLists.videos[channelId] || geoBlockLists.videos[claimId];
}
if (geoConfig) {
const specials = geoConfig.specials || [];
const countries = geoConfig.countries || [];
const continents = geoConfig.continents || [];
return (
specials.find((x: GeoRestriction) => x.id === 'EU-ONLY' && locale.is_eu_member) ||
countries.find((x: GeoRestriction) => x.id === locale.country) ||
continents.find((x: GeoRestriction) => x.id === locale.continent)
);
}
}
return null;
} }
)((state, uri) => String(uri)); )((state, uri) => String(uri));

48
ui/util/geoRestriction.js Normal file
View file

@ -0,0 +1,48 @@
// @flow
import { getChannelIdFromClaim, isStreamPlaceholderClaim } from 'util/claim';
/**
* Returns the geo restriction for the given claim, or null if not restricted.
*
* @param claim
* @param locale
* @param geoBlockLists
* @returns {{id: string, trigger?: string, reason?: string, message?:
* string}|null}
*/
export function getGeoRestrictionForClaim(claim: ?StreamClaim, locale: LocaleInfo, geoBlockLists: ?GBL) {
if (locale && geoBlockLists && claim) {
const claimId: ?string = claim.claim_id;
const channelId: ?string = getChannelIdFromClaim(claim);
if (!claimId || !channelId) {
console.log('Claim with blank data:', claim); // eslint-disable-line no-console
return null;
}
let geoConfig: ?GeoConfig;
// --- livestreams
if (isStreamPlaceholderClaim(claim) && geoBlockLists.livestreams) {
geoConfig = geoBlockLists.livestreams[channelId] || geoBlockLists.livestreams[claimId];
}
// --- videos (actually, everything else)
else if (geoBlockLists.videos) {
geoConfig = geoBlockLists.videos[channelId] || geoBlockLists.videos[claimId];
}
if (geoConfig) {
const specials = geoConfig.specials || [];
const countries = geoConfig.countries || [];
const continents = geoConfig.continents || [];
return (
specials.find((x: GeoRestriction) => x.id === 'EU-ONLY' && locale.is_eu_member) ||
countries.find((x: GeoRestriction) => x.id === locale.country) ||
continents.find((x: GeoRestriction) => x.id === locale.continent)
);
}
}
return null;
}