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

295 lines
10 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';
2021-02-25 18:43:56 +01:00
import { selectMutedChannels } from 'redux/selectors/blocked';
2020-07-15 20:50:45 +02:00
import { makeSelectClientSetting } from 'redux/selectors/settings';
2020-08-24 19:35:21 +02:00
import { selectBlacklistedOutpointMap, selectFilteredOutpointMap } 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
2021-02-25 18:43:56 +01:00
const selectState = (state) => state.comments || {};
2020-06-23 19:38:18 +02:00
2021-02-25 18:43:56 +01:00
export const selectCommentsById = createSelector(selectState, (state) => state.commentById || {});
export const selectIsFetchingComments = createSelector(selectState, (state) => state.isLoading);
export const selectIsPostingComment = createSelector(selectState, (state) => state.isCommenting);
export const selectIsFetchingReacts = createSelector(selectState, (state) => state.isFetchingReacts);
export const selectOthersReactsById = createSelector(selectState, (state) => state.othersReactsByCommentId);
export const selectModerationBlockList = createSelector(selectState, (state) =>
state.moderationBlockList ? state.moderationBlockList.reverse() : []
2021-02-25 18:43:56 +01:00
);
export const selectBlockingByUri = createSelector(selectState, (state) => state.blockingByUri);
export const selectUnBlockingByUri = createSelector(selectState, (state) => state.unBlockingByUri);
export const selectFetchingModerationBlockList = createSelector(
selectState,
(state) => state.fetchingModerationBlockList
);
2020-06-23 19:38:18 +02:00
export const selectCommentsByClaimId = createSelector(selectState, selectCommentsById, (state, byId) => {
const byClaimId = state.byId || {};
const comments = {};
2020-08-24 19:35:21 +02:00
// replace every comment_id in the list with the actual comment object
Object.keys(byClaimId).forEach((claimId: string) => {
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;
});
export const selectTopLevelCommentsByClaimId = createSelector(selectState, selectCommentsById, (state, byId) => {
const byClaimId = state.topLevelCommentsById || {};
const comments = {};
2020-06-23 19:38:18 +02:00
// replace every comment_id in the list with the actual comment object
2021-02-25 18:43:56 +01:00
Object.keys(byClaimId).forEach((claimId) => {
2020-06-23 19:38:18 +02:00
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;
});
2020-08-24 19:35:21 +02:00
export const makeSelectCommentForCommentId = (commentId: string) =>
2021-02-25 18:43:56 +01:00
createSelector(selectCommentsById, (comments) => comments[commentId]);
2020-08-24 19:35:21 +02:00
export const selectRepliesByParentId = createSelector(selectState, selectCommentsById, (state, byId) => {
const byParentId = state.repliesByParentId || {};
const comments = {};
// replace every comment_id in the list with the actual comment object
2021-02-25 18:43:56 +01:00
Object.keys(byParentId).forEach((id) => {
2020-08-24 19:35:21 +02:00
const commentIds = byParentId[id];
comments[id] = Array(commentIds === null ? 0 : commentIds.length);
for (let i = 0; i < commentIds.length; i++) {
comments[id][i] = byId[commentIds[i]];
}
});
return comments;
});
2020-06-23 19:38:18 +02:00
// previously this used a mapping from claimId -> Array<Comments>
/* export const selectCommentsById = createSelector(
selectState,
state => state.byId || {}
); */
2021-02-25 18:43:56 +01:00
export const selectCommentsByUri = createSelector(selectState, (state) => {
2020-06-23 19:38:18 +02:00
const byUri = state.commentsByUri || {};
const comments = {};
2021-02-25 18:43:56 +01:00
Object.keys(byUri).forEach((uri) => {
2020-06-23 19:38:18 +02:00
const claimId = byUri[uri];
if (claimId === null) {
comments[uri] = null;
} else {
comments[uri] = claimId;
}
});
return comments;
});
2020-09-29 16:10:23 +02:00
export const makeSelectCommentIdsForUri = (uri: string) =>
createSelector(selectState, selectCommentsByUri, selectClaimsById, (state, byUri) => {
const claimId = byUri[uri];
return state.byId[claimId];
});
export const makeSelectMyReactionsForComment = (commentId: string) =>
2021-02-25 18:43:56 +01:00
createSelector(selectState, (state) => {
2020-09-29 21:28:50 +02:00
return state.myReactsByCommentId[commentId] || [];
2020-09-29 16:10:23 +02:00
});
export const makeSelectOthersReactionsForComment = (commentId: string) =>
2021-02-25 18:43:56 +01:00
createSelector(selectState, (state) => {
2020-09-29 16:10:23 +02:00
return state.othersReactsByCommentId[commentId];
});
2021-02-25 18:43:56 +01:00
export const selectPendingCommentReacts = createSelector(selectState, (state) => state.pendingCommentReactions);
2020-06-23 19:38:18 +02:00
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,
2021-02-25 18:43:56 +01:00
selectMutedChannels,
2020-08-24 19:35:21 +02:00
selectBlacklistedOutpointMap,
selectFilteredOutpointMap,
2020-07-15 20:50:45 +02:00
makeSelectClientSetting(SETTINGS.SHOW_MATURE),
2020-08-24 19:35:21 +02:00
(byClaimId, byUri, claimsById, myClaims, blockedChannels, blacklistedMap, filteredMap, showMatureContent) => {
2020-07-15 18:15:00 +02:00
const claimId = byUri[uri];
const comments = byClaimId && byClaimId[claimId];
2020-08-24 19:35:21 +02:00
return comments
2021-02-25 18:43:56 +01:00
? comments.filter((comment) => {
if (!comment) {
// It may have been recently deleted after being blocked
return false;
}
2020-08-24 19:35:21 +02:00
const channelClaim = claimsById[comment.channel_id];
// Return comment if `channelClaim` doesn't exist so the component knows to resolve the author
if (channelClaim) {
if (myClaims && myClaims.size > 0) {
const claimIsMine = channelClaim.is_my_output || myClaims.has(channelClaim.claim_id);
if (claimIsMine) {
return true;
}
}
const outpoint = `${channelClaim.txid}:${channelClaim.nout}`;
if (blacklistedMap[outpoint] || filteredMap[outpoint]) {
return false;
}
if (!showMatureContent) {
const claimIsMature = isClaimNsfw(channelClaim);
if (claimIsMature) {
return false;
}
}
}
return !blockedChannels.includes(comment.channel_url);
})
: [];
}
);
export const makeSelectTopLevelCommentsForUri = (uri: string) =>
createSelector(
selectTopLevelCommentsByClaimId,
selectCommentsByUri,
selectClaimsById,
selectMyActiveClaims,
2021-02-25 18:43:56 +01:00
selectMutedChannels,
2020-08-24 19:35:21 +02:00
selectBlacklistedOutpointMap,
selectFilteredOutpointMap,
makeSelectClientSetting(SETTINGS.SHOW_MATURE),
(byClaimId, byUri, claimsById, myClaims, blockedChannels, blacklistedMap, filteredMap, showMatureContent) => {
const claimId = byUri[uri];
const comments = byClaimId && byClaimId[claimId];
return comments
2021-02-25 18:43:56 +01:00
? comments.filter((comment) => {
if (!comment) {
return false;
}
2020-08-24 19:35:21 +02:00
const channelClaim = claimsById[comment.channel_id];
// Return comment if `channelClaim` doesn't exist so the component knows to resolve the author
if (channelClaim) {
if (myClaims && myClaims.size > 0) {
const claimIsMine = channelClaim.is_my_output || myClaims.has(channelClaim.claim_id);
if (claimIsMine) {
return true;
}
}
const outpoint = `${channelClaim.txid}:${channelClaim.nout}`;
if (blacklistedMap[outpoint] || filteredMap[outpoint]) {
return false;
}
if (!showMatureContent) {
const claimIsMature = isClaimNsfw(channelClaim);
if (claimIsMature) {
return false;
}
}
}
return !blockedChannels.includes(comment.channel_url);
})
: [];
}
);
export const makeSelectRepliesForParentId = (id: string) =>
createSelector(
selectState, // no selectRepliesByParentId
selectCommentsById,
selectClaimsById,
selectMyActiveClaims,
2021-02-25 18:43:56 +01:00
selectMutedChannels,
2020-08-24 19:35:21 +02:00
selectBlacklistedOutpointMap,
selectFilteredOutpointMap,
makeSelectClientSetting(SETTINGS.SHOW_MATURE),
(state, commentsById, claimsById, myClaims, blockedChannels, blacklistedMap, filteredMap, showMatureContent) => {
// const claimId = byUri[uri]; // just parentId (id)
const replyIdsByParentId = state.repliesByParentId;
const replyIdsForParent = replyIdsByParentId[id] || [];
if (!replyIdsForParent.length) return null;
const comments = [];
2021-02-25 18:43:56 +01:00
replyIdsForParent.forEach((cid) => {
2020-08-24 19:35:21 +02:00
comments.push(commentsById[cid]);
});
// const comments = byParentId && byParentId[id];
return comments
2021-02-25 18:43:56 +01:00
? comments.filter((comment) => {
if (!comment) {
return false;
}
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
}
);
2020-09-30 20:46:17 +02:00
export const makeSelectTotalCommentsCountForUri = (uri: string) =>
2021-02-25 18:43:56 +01:00
createSelector(makeSelectCommentsForUri(uri), (comments) => {
2020-09-30 20:46:17 +02:00
return comments ? comments.length : 0;
});
2021-02-25 18:43:56 +01:00
export const makeSelectChannelIsBlocked = (uri: string) =>
createSelector(selectModerationBlockList, (blockedChannelUris) => {
if (!blockedChannelUris || !blockedChannelUris) {
return false;
}
return blockedChannelUris.includes(uri);
});
export const makeSelectUriIsBlockingOrUnBlocking = (uri: string) =>
createSelector(selectBlockingByUri, selectUnBlockingByUri, (blockingByUri, unBlockingByUri) => {
return blockingByUri[uri] || unBlockingByUri[uri];
});