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

124 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-06-23 19:38:18 +02:00
// @flow
2020-07-15 20:50:45 +02:00
import * as SETTINGS from 'constants/settings';
2020-06-23 19:38:18 +02:00
import { createSelector } from 'reselect';
2020-07-15 18:15:00 +02:00
import { selectBlockedChannels } from 'redux/selectors/blocked';
2020-07-15 20:50:45 +02:00
import { makeSelectClientSetting } from 'redux/selectors/settings';
2020-07-20 19:19:45 +02:00
import { selectBlackListedOutpoints, selectFilteredOutpoints } from 'lbryinc';
2020-07-15 20:50:45 +02:00
import { selectClaimsById, isClaimNsfw, selectMyActiveClaims } from 'lbry-redux';
2020-06-23 19:38:18 +02:00
const selectState = state => state.comments || {};
export const selectCommentsById = createSelector(selectState, state => state.commentById || {});
export const selectIsFetchingComments = createSelector(selectState, state => state.isLoading);
export const selectCommentsByClaimId = createSelector(selectState, selectCommentsById, (state, byId) => {
const byClaimId = state.byId || {};
const comments = {};
// replace every comment_id in the list with the actual comment object
Object.keys(byClaimId).forEach(claimId => {
const commentIds = byClaimId[claimId];
comments[claimId] = Array(commentIds === null ? 0 : commentIds.length);
for (let i = 0; i < commentIds.length; i++) {
comments[claimId][i] = byId[commentIds[i]];
}
});
return comments;
});
// previously this used a mapping from claimId -> Array<Comments>
/* export const selectCommentsById = createSelector(
selectState,
state => state.byId || {}
); */
export const selectCommentsByUri = createSelector(selectState, state => {
const byUri = state.commentsByUri || {};
const comments = {};
Object.keys(byUri).forEach(uri => {
const claimId = byUri[uri];
if (claimId === null) {
comments[uri] = null;
} else {
comments[uri] = claimId;
}
});
return comments;
});
export const makeSelectCommentsForUri = (uri: string) =>
2020-07-15 18:15:00 +02:00
createSelector(
selectCommentsByClaimId,
selectCommentsByUri,
selectClaimsById,
2020-07-15 20:50:45 +02:00
selectMyActiveClaims,
2020-07-15 18:15:00 +02:00
selectBlockedChannels,
selectBlackListedOutpoints,
2020-07-20 19:19:45 +02:00
selectFilteredOutpoints,
2020-07-15 20:50:45 +02:00
makeSelectClientSetting(SETTINGS.SHOW_MATURE),
2020-07-20 19:19:45 +02:00
(
byClaimId,
byUri,
claimsById,
myClaims,
blockedChannels,
blacklistedOutpoints,
filteredOutpoints,
showMatureContent
) => {
2020-07-15 18:15:00 +02:00
const claimId = byUri[uri];
const comments = byClaimId && byClaimId[claimId];
2020-07-20 19:19:45 +02:00
const blacklistedMap = blacklistedOutpoints
? blacklistedOutpoints.reduce((acc, val) => {
const outpoint = `${val.txid}:${val.nout}`;
return {
...acc,
[outpoint]: 1,
};
}, {})
: {};
2020-07-20 19:19:45 +02:00
const filteredMap = filteredOutpoints
? filteredOutpoints.reduce((acc, val) => {
const outpoint = `${val.txid}:${val.nout}`;
return {
...acc,
[outpoint]: 1,
};
}, {})
: {};
return comments
? comments.filter(comment => {
const channelClaim = claimsById[comment.channel_id];
// Return comment if `channelClaim` doesn't exist so the component knows to resolve the author
if (channelClaim) {
2020-07-20 19:19:45 +02:00
if (myClaims && myClaims.size > 0) {
2020-07-15 20:50:45 +02:00
const claimIsMine = channelClaim.is_my_output || myClaims.has(channelClaim.claim_id);
if (claimIsMine) {
return true;
}
}
const outpoint = `${channelClaim.txid}:${channelClaim.nout}`;
2020-07-20 19:19:45 +02:00
if (blacklistedMap[outpoint] || filteredMap[outpoint]) {
return false;
}
2020-07-15 20:50:45 +02:00
if (!showMatureContent) {
const claimIsMature = isClaimNsfw(channelClaim);
if (claimIsMature) {
return false;
}
}
}
return !blockedChannels.includes(comment.channel_url);
})
: [];
2020-07-15 18:15:00 +02:00
}
);