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

183 lines
5 KiB
JavaScript
Raw Normal View History

2019-03-05 05:46:57 +01:00
// @flow
import { createSelector } from 'reselect';
2019-08-02 08:28:14 +02:00
import {
makeSelectClaimForUri,
selectClaimsByUri,
makeSelectClaimsInChannelForCurrentPageState,
makeSelectClaimIsNsfw,
2019-09-09 19:31:00 +02:00
makeSelectRecommendedContentForUri,
makeSelectMediaTypeForUri,
selectBlockedChannels,
parseURI,
2019-08-02 08:28:14 +02:00
} from 'lbry-redux';
import { selectAllCostInfoByUri } from 'lbryinc';
2019-08-02 08:28:14 +02:00
import { selectShowMatureContent } from 'redux/selectors/settings';
2019-04-01 01:04:01 +02:00
const RECENT_HISTORY_AMOUNT = 10;
const HISTORY_ITEMS_PER_PAGE = 50;
2019-03-05 05:46:57 +01:00
export const selectState = (state: any) => state.content || {};
export const selectPlayingUri = createSelector(
selectState,
state => state.playingUri
);
2019-08-02 08:28:14 +02:00
export const makeSelectIsPlaying = (uri: string) =>
createSelector(
selectPlayingUri,
playingUri => playingUri === uri
);
2019-03-05 05:46:57 +01:00
export const makeSelectContentPositionForUri = (uri: string) =>
createSelector(
selectState,
makeSelectClaimForUri(uri),
(state, claim) => {
if (!claim) {
return null;
}
const outpoint = `${claim.txid}:${claim.nout}`;
const id = claim.claim_id;
return state.positions[id] ? state.positions[id][outpoint] : null;
}
);
2019-04-01 01:04:01 +02:00
export const selectHistory = createSelector(
2019-03-05 05:46:57 +01:00
selectState,
2019-04-01 01:04:01 +02:00
state => state.history || []
);
export const selectHistoryPageCount = createSelector(
selectHistory,
history => Math.ceil(history.length / HISTORY_ITEMS_PER_PAGE)
2019-03-05 05:46:57 +01:00
);
export const makeSelectHistoryForPage = (page: number) =>
createSelector(
2019-04-01 01:04:01 +02:00
selectHistory,
2019-03-05 05:46:57 +01:00
selectClaimsByUri,
2019-04-01 01:04:01 +02:00
(history, claimsByUri) => {
2019-03-05 05:46:57 +01:00
const left = page * HISTORY_ITEMS_PER_PAGE;
2019-04-01 01:04:01 +02:00
const historyItemsForPage = history.slice(left, left + HISTORY_ITEMS_PER_PAGE);
return historyItemsForPage;
2019-03-05 05:46:57 +01:00
}
);
export const makeSelectHistoryForUri = (uri: string) =>
createSelector(
2019-04-01 01:04:01 +02:00
selectHistory,
history => history.find(i => i.uri === uri)
2019-03-05 05:46:57 +01:00
);
2019-07-11 20:06:25 +02:00
export const makeSelectHasVisitedUri = (uri: string) =>
createSelector(
makeSelectHistoryForUri(uri),
history => Boolean(history)
);
2019-09-09 19:31:00 +02:00
export const makeSelectNextUnplayedRecommended = (uri: string) =>
createSelector(
makeSelectRecommendedContentForUri(uri),
selectHistory,
selectClaimsByUri,
selectAllCostInfoByUri,
selectBlockedChannels,
(
recommendedForUri: Array<string>,
history: Array<{ uri: string }>,
claimsByUri: { [string]: ?Claim },
costInfoByUri: { [string]: { cost: 0 | string } },
blockedChannels: Array<string>
) => {
if (recommendedForUri) {
// Make sure we don't autoplay paid content, channels, or content from blocked channels
for (let i = 0; i < recommendedForUri.length; i++) {
const recommendedUri = recommendedForUri[i];
2020-02-05 16:07:48 +01:00
const claim = claimsByUri[recommendedUri];
if (!claim) {
continue;
}
const { isChannel } = parseURI(recommendedUri);
if (isChannel) {
continue;
}
const costInfo = costInfoByUri[recommendedUri];
if (!costInfo || costInfo.cost !== 0) {
continue;
}
2020-02-05 16:07:48 +01:00
// We already check if it's a channel above
// $FlowFixMe
const isVideo = claim.value && claim.value.stream_type === 'video';
2020-02-05 21:46:44 +01:00
// $FlowFixMe
const isAudio = claim.value && claim.value.stream_type === 'audio';
2020-02-07 07:22:48 +01:00
if (!isVideo && !isAudio) {
2020-02-05 16:07:48 +01:00
continue;
}
const channel = claim && claim.signing_channel;
2020-01-29 17:16:03 +01:00
if (channel && blockedChannels.some(blockedUri => blockedUri === channel.permanent_url)) {
continue;
}
2020-01-29 17:16:03 +01:00
if (!history.some(item => item.uri === recommendedForUri[i])) {
return recommendedForUri[i];
2019-09-09 19:31:00 +02:00
}
}
}
}
);
2019-04-01 01:04:01 +02:00
export const selectRecentHistory = createSelector(
selectHistory,
history => {
return history.slice(0, RECENT_HISTORY_AMOUNT);
}
);
2019-03-05 05:46:57 +01:00
export const makeSelectCategoryListUris = (uris: ?Array<string>, channel: string) =>
createSelector(
2019-03-28 17:53:13 +01:00
makeSelectClaimsInChannelForCurrentPageState(channel),
2019-03-05 05:46:57 +01:00
channelClaims => {
if (uris) return uris;
if (channelClaims) {
const CATEGORY_LIST_SIZE = 10;
2019-05-07 23:38:29 +02:00
return channelClaims.slice(0, CATEGORY_LIST_SIZE).map(({ name, claim_id: claimId }) => `${name}#${claimId}`);
2019-03-05 05:46:57 +01:00
}
return null;
}
);
2019-08-02 08:28:14 +02:00
export const makeSelectShouldObscurePreview = (uri: string) =>
createSelector(
selectShowMatureContent,
makeSelectClaimIsNsfw(uri),
(showMatureContent, isClaimMature) => {
return isClaimMature && !showMatureContent;
}
);
export const makeSelectCanAutoplay = (uri: string) =>
createSelector(
makeSelectMediaTypeForUri(uri),
mediaType => {
const canAutoPlay = ['audio', 'video', 'image', 'text', 'document'].includes(mediaType);
return canAutoPlay;
}
);
2020-01-06 21:57:49 +01:00
export const makeSelectIsText = (uri: string) =>
createSelector(
makeSelectMediaTypeForUri(uri),
mediaType => {
2020-01-26 05:09:23 +01:00
const isText = ['text', 'document', 'script'].includes(mediaType);
2020-01-06 21:57:49 +01:00
return isText;
}
);