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

1374 lines
42 KiB
JavaScript
Raw Normal View History

2020-06-23 19:38:18 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
2020-09-29 16:10:23 +02:00
import * as REACTION_TYPES from 'constants/reactions';
import * as PAGES from 'constants/pages';
2021-05-25 08:17:36 +02:00
import { BLOCK_LEVEL } from 'constants/comment';
import { Lbry, parseURI, buildURI, selectClaimsById, selectClaimsByUri, selectMyChannelClaims } from 'lbry-redux';
import { doToast, doSeeNotifications } from 'redux/actions/notifications';
import {
makeSelectCommentIdsForUri,
makeSelectMyReactionsForComment,
makeSelectOthersReactionsForComment,
selectPendingCommentReacts,
selectModerationBlockList,
2021-05-25 08:17:36 +02:00
selectModerationDelegatorsById,
} from 'redux/selectors/comments';
import { makeSelectNotificationForCommentId } from 'redux/selectors/notifications';
import { selectActiveChannelClaim } from 'redux/selectors/app';
import { toHex } from 'util/hex';
import Comments from 'comments';
2020-06-23 19:38:18 +02:00
export function doCommentList(uri: string, page: number = 1, pageSize: number = 99999) {
2020-11-16 20:09:00 +01:00
return (dispatch: Dispatch, getState: GetState) => {
2020-06-23 19:38:18 +02:00
const state = getState();
const claim = selectClaimsByUri(state)[uri];
const claimId = claim ? claim.claim_id : null;
if (!claimId) {
dispatch({
type: ACTIONS.COMMENT_LIST_FAILED,
data: 'unable to find claim for uri',
});
return;
}
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_LIST_STARTED,
});
// Adding 'channel_id' and 'channel_name' enables "CreatorSettings > commentsEnabled".
const authorChannelClaim = claim.value_type === 'channel' ? claim : claim.signing_channel;
return Comments.comment_list({
2020-06-23 19:38:18 +02:00
page,
claim_id: claimId,
2020-06-23 19:38:18 +02:00
page_size: pageSize,
channel_id: authorChannelClaim ? authorChannelClaim.claim_id : undefined,
channel_name: authorChannelClaim ? authorChannelClaim.name : undefined,
2020-06-23 19:38:18 +02:00
})
.then((result: CommentListResponse) => {
const { items: comments } = result;
dispatch({
type: ACTIONS.COMMENT_LIST_COMPLETED,
data: {
comments,
claimId: claimId,
authorClaimId: authorChannelClaim ? authorChannelClaim.claim_id : undefined,
2020-06-23 19:38:18 +02:00
uri: uri,
},
});
2020-10-20 05:20:38 +02:00
return result;
2020-06-23 19:38:18 +02:00
})
.catch((error) => {
if (error.message === 'comments are disabled by the creator') {
dispatch({
type: ACTIONS.COMMENT_LIST_COMPLETED,
data: {
authorClaimId: authorChannelClaim ? authorChannelClaim.claim_id : undefined,
disabled: true,
},
});
} else {
dispatch({
type: ACTIONS.COMMENT_LIST_FAILED,
data: error,
});
}
2020-06-23 19:38:18 +02:00
});
};
}
2021-04-23 21:59:48 +02:00
export function doSuperChatList(uri: string) {
return (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const claim = selectClaimsByUri(state)[uri];
const claimId = claim ? claim.claim_id : null;
if (!claimId) {
console.error('No claimId found for uri: ', uri); //eslint-disable-line
return;
}
dispatch({
type: ACTIONS.COMMENT_SUPER_CHAT_LIST_STARTED,
});
return Comments.super_list({
claim_id: claimId,
})
.then((result: CommentListResponse) => {
const { items: comments, total_amount: totalAmount } = result;
dispatch({
type: ACTIONS.COMMENT_SUPER_CHAT_LIST_COMPLETED,
data: {
comments,
totalAmount,
uri: uri,
},
});
})
.catch((error) => {
dispatch({
type: ACTIONS.COMMENT_SUPER_CHAT_LIST_FAILED,
data: error,
});
});
};
}
2020-09-29 16:10:23 +02:00
export function doCommentReactList(uri: string | null, commentId?: string) {
2020-11-16 20:09:00 +01:00
return (dispatch: Dispatch, getState: GetState) => {
2020-09-29 16:10:23 +02:00
const state = getState();
const activeChannelClaim = selectActiveChannelClaim(state);
2020-09-29 16:10:23 +02:00
const commentIds = uri ? makeSelectCommentIdsForUri(uri)(state) : [commentId];
2020-09-29 16:10:23 +02:00
dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_STARTED,
});
const params: { comment_ids: string, channel_name?: string, channel_id?: string } = {
2020-09-29 16:10:23 +02:00
comment_ids: commentIds.join(','),
};
if (activeChannelClaim) {
params['channel_name'] = activeChannelClaim.name;
params['channel_id'] = activeChannelClaim.claim_id;
}
return Lbry.comment_react_list(params)
2020-09-29 16:10:23 +02:00
.then((result: CommentReactListResponse) => {
const { my_reactions: myReactions, others_reactions: othersReactions } = result;
dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
data: {
myReactions: myReactions || {},
2020-09-29 16:10:23 +02:00
othersReactions,
},
});
})
.catch((error) => {
2020-09-29 16:10:23 +02:00
dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_FAILED,
data: error,
});
});
};
}
export function doCommentReact(commentId: string, type: string) {
2020-11-16 20:09:00 +01:00
return (dispatch: Dispatch, getState: GetState) => {
2020-09-29 16:10:23 +02:00
const state = getState();
const activeChannelClaim = selectActiveChannelClaim(state);
const pendingReacts = selectPendingCommentReacts(state);
const notification = makeSelectNotificationForCommentId(commentId)(state);
if (!activeChannelClaim) {
console.error('Unable to react to comment. No activeChannel is set.'); // eslint-disable-line
return;
}
if (notification && !notification.is_seen) {
dispatch(doSeeNotifications([notification.id]));
}
const exclusiveTypes = {
[REACTION_TYPES.LIKE]: REACTION_TYPES.DISLIKE,
[REACTION_TYPES.DISLIKE]: REACTION_TYPES.LIKE,
};
if (pendingReacts.includes(commentId + exclusiveTypes[type]) || pendingReacts.includes(commentId + type)) {
// ignore dislikes during likes, for example
return;
}
let myReacts = makeSelectMyReactionsForComment(commentId)(state);
const othersReacts = makeSelectOthersReactionsForComment(commentId)(state);
2020-09-29 16:10:23 +02:00
const params: CommentReactParams = {
comment_ids: commentId,
channel_name: activeChannelClaim.name,
channel_id: activeChannelClaim.claim_id,
2020-09-29 16:10:23 +02:00
react_type: type,
};
2020-09-29 16:10:23 +02:00
if (myReacts.includes(type)) {
params['remove'] = true;
myReacts.splice(myReacts.indexOf(type), 1);
} else {
myReacts.push(type);
if (Object.keys(exclusiveTypes).includes(type)) {
params['clear_types'] = exclusiveTypes[type];
if (myReacts.indexOf(exclusiveTypes[type]) !== -1) {
myReacts.splice(myReacts.indexOf(exclusiveTypes[type]), 1);
}
}
2020-09-29 16:10:23 +02:00
}
dispatch({
type: ACTIONS.COMMENT_REACT_STARTED,
data: commentId + type,
});
// simulate api return shape: ['like'] -> { 'like': 1 }
const myReactsObj = myReacts.reduce((acc, el) => {
acc[el] = 1;
return acc;
}, {});
2020-09-29 16:10:23 +02:00
dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
data: {
myReactions: { [commentId]: myReactsObj },
othersReactions: { [commentId]: othersReacts },
},
});
2020-09-29 16:10:23 +02:00
Lbry.comment_react(params)
.then((result: CommentReactListResponse) => {
dispatch({
type: ACTIONS.COMMENT_REACT_COMPLETED,
data: commentId + type,
});
2020-09-29 16:10:23 +02:00
})
.catch((error) => {
2020-09-29 16:10:23 +02:00
dispatch({
type: ACTIONS.COMMENT_REACT_FAILED,
data: commentId + type,
2020-09-29 16:10:23 +02:00
});
const myRevertedReactsObj = myReacts
.filter((el) => el !== type)
.reduce((acc, el) => {
acc[el] = 1;
return acc;
}, {});
dispatch({
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
data: {
myReactions: { [commentId]: myRevertedReactsObj },
othersReactions: { [commentId]: othersReacts },
},
});
2020-09-29 16:10:23 +02:00
});
};
}
export function doCommentCreate(
comment: string = '',
claim_id: string = '',
parent_id?: string,
uri: string,
2021-04-23 21:59:48 +02:00
livestream?: boolean = false,
txid?: string
) {
2021-04-23 21:59:48 +02:00
return async (dispatch: Dispatch, getState: GetState) => {
2020-06-23 19:38:18 +02:00
const state = getState();
const activeChannelClaim = selectActiveChannelClaim(state);
if (!activeChannelClaim) {
console.error('Unable to create comment. No activeChannel is set.'); // eslint-disable-line
return;
}
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_CREATE_STARTED,
});
2021-04-23 21:59:48 +02:00
let signatureData;
if (activeChannelClaim) {
try {
signatureData = await Lbry.channel_sign({
channel_id: activeChannelClaim.claim_id,
hexdata: toHex(comment),
});
} catch (e) {}
}
if (parent_id) {
const notification = makeSelectNotificationForCommentId(parent_id)(state);
if (notification && !notification.is_seen) {
dispatch(doSeeNotifications([notification.id]));
}
}
2021-04-23 21:59:48 +02:00
if (!signatureData) {
return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
}
return Comments.comment_create({
2020-06-23 19:38:18 +02:00
comment: comment,
claim_id: claim_id,
channel_id: activeChannelClaim.claim_id,
2021-04-23 21:59:48 +02:00
channel_name: activeChannelClaim.name,
2020-06-23 19:38:18 +02:00
parent_id: parent_id,
2021-04-23 21:59:48 +02:00
signature: signatureData.signature,
signing_ts: signatureData.signing_ts,
...(txid ? { support_tx_id: txid } : {}),
2020-06-23 19:38:18 +02:00
})
.then((result: CommentCreateResponse) => {
dispatch({
type: ACTIONS.COMMENT_CREATE_COMPLETED,
data: {
uri,
livestream,
2020-06-23 19:38:18 +02:00
comment: result,
claimId: claim_id,
},
});
2020-09-30 02:11:48 +02:00
return result;
2020-06-23 19:38:18 +02:00
})
.catch((error) => {
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_CREATE_FAILED,
data: error,
});
let toastMessage = __('Unable to create comment, please try again later.');
if (error && error.message === 'channel is blocked by publisher') {
toastMessage = __('Unable to comment. This channel has blocked you.');
}
if (error) {
const BLOCKED_WORDS_ERR_MSG = 'the comment contents are blocked by';
if (error.message === 'channel is blocked by publisher') {
toastMessage = __('Unable to comment. This channel has blocked you.');
} else if (error.message.startsWith(BLOCKED_WORDS_ERR_MSG)) {
const channelName = error.message.substring(BLOCKED_WORDS_ERR_MSG.length);
toastMessage = __('The comment contains contents that are blocked by %author%', { author: channelName });
}
}
2020-06-23 19:38:18 +02:00
dispatch(
doToast({
message: toastMessage,
2020-06-23 19:38:18 +02:00
isError: true,
})
);
2021-04-23 21:59:48 +02:00
return Promise.reject(error);
2020-06-23 19:38:18 +02:00
});
};
}
2020-10-20 05:20:38 +02:00
export function doCommentPin(commentId: string, remove: boolean) {
2020-11-16 20:09:00 +01:00
return (dispatch: Dispatch, getState: GetState) => {
2020-10-20 05:20:38 +02:00
const state = getState();
const activeChannel = selectActiveChannelClaim(state);
if (!activeChannel) {
console.error('Unable to pin comment. No activeChannel is set.'); // eslint-disable-line
return;
}
2020-10-20 05:20:38 +02:00
dispatch({
type: ACTIONS.COMMENT_PIN_STARTED,
});
return Lbry.comment_pin({
comment_id: commentId,
channel_name: activeChannel.name,
channel_id: activeChannel.claim_id,
...(remove ? { remove: true } : {}),
})
2020-10-20 05:20:38 +02:00
.then((result: CommentPinResponse) => {
dispatch({
type: ACTIONS.COMMENT_PIN_COMPLETED,
data: result,
});
})
.catch((error) => {
2020-10-20 05:20:38 +02:00
dispatch({
type: ACTIONS.COMMENT_PIN_FAILED,
data: error,
});
dispatch(
doToast({
message: 'Unable to pin this comment, please try again later.',
isError: true,
})
);
});
};
}
export function doCommentAbandon(commentId: string, creatorChannelUri?: string) {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const claim = creatorChannelUri ? selectClaimsByUri(state)[creatorChannelUri] : undefined;
const creatorChannelId = claim ? claim.claim_id : null;
const creatorChannelName = claim ? claim.name : null;
const activeChannelClaim = selectActiveChannelClaim(state);
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_ABANDON_STARTED,
});
let commentIdSignature;
if (activeChannelClaim) {
try {
commentIdSignature = await Lbry.channel_sign({
channel_id: activeChannelClaim.claim_id,
hexdata: toHex(commentId),
});
} catch (e) {}
}
return Comments.comment_abandon({
comment_id: commentId,
...(creatorChannelId ? { creator_channel_id: creatorChannelId } : {}),
...(creatorChannelName ? { creator_channel_name: creatorChannelName } : {}),
...(commentIdSignature || {}),
2020-06-23 19:38:18 +02:00
})
.then((result: CommentAbandonResponse) => {
// Comment may not be deleted if the signing channel can't be signed.
// This will happen if the channel was recently created or abandoned.
if (result.abandoned) {
dispatch({
type: ACTIONS.COMMENT_ABANDON_COMPLETED,
data: {
comment_id: commentId,
2020-06-23 19:38:18 +02:00
},
});
} else {
dispatch({
type: ACTIONS.COMMENT_ABANDON_FAILED,
});
dispatch(
doToast({
message: 'Your channel is still being setup, try again in a few moments.',
isError: true,
})
);
}
})
.catch((error) => {
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_ABANDON_FAILED,
data: error,
});
2020-06-23 19:38:18 +02:00
dispatch(
doToast({
message: 'Unable to delete this comment, please try again later.',
isError: true,
})
);
});
};
}
export function doCommentUpdate(comment_id: string, comment: string) {
// if they provided an empty string, they must have wanted to abandon
if (comment === '') {
return doCommentAbandon(comment_id);
} else {
2020-11-16 20:09:00 +01:00
return (dispatch: Dispatch) => {
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_UPDATE_STARTED,
});
return Lbry.comment_update({
comment_id: comment_id,
comment: comment,
})
.then((result: CommentUpdateResponse) => {
if (result != null) {
dispatch({
type: ACTIONS.COMMENT_UPDATE_COMPLETED,
data: {
comment: result,
},
});
} else {
// the result will return null
dispatch({
type: ACTIONS.COMMENT_UPDATE_FAILED,
});
dispatch(
doToast({
message: 'Your channel is still being setup, try again in a few moments.',
isError: true,
})
);
}
})
.catch((error) => {
2020-06-23 19:38:18 +02:00
dispatch({
type: ACTIONS.COMMENT_UPDATE_FAILED,
data: error,
});
dispatch(
doToast({
message: 'Unable to edit this comment, please try again later.',
isError: true,
})
);
});
};
}
}
async function channelSignName(channelClaimId: string, channelName: string) {
let signedObject;
try {
signedObject = await Lbry.channel_sign({
channel_id: channelClaimId,
hexdata: toHex(channelName),
});
signedObject['claim_id'] = channelClaimId;
signedObject['name'] = channelName;
} catch (e) {}
return signedObject;
}
// Hides a users comments from all creator's claims and prevent them from commenting in the future
2021-05-25 08:17:36 +02:00
function doCommentModToggleBlock(
unblock: boolean,
commenterUri: string,
creatorId: string,
blockerIds: Array<string>, // [] = use all my channels
blockLevel: string,
showLink: boolean = false
) {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
2021-05-25 08:17:36 +02:00
let blockerChannelClaims = selectMyChannelClaims(state);
if (blockerIds.length === 0) {
// Specific blockers not provided, so find one based on block-level.
switch (blockLevel) {
case BLOCK_LEVEL.MODERATOR:
{
// Find the first channel that is a moderator for 'creatorId'.
const delegatorsById = selectModerationDelegatorsById(state);
blockerChannelClaims = [
blockerChannelClaims.find((x) => {
const delegatorDataForId = delegatorsById[x.claim_id];
return delegatorDataForId && Object.values(delegatorDataForId.delegators).includes(creatorId);
}),
];
}
break;
case BLOCK_LEVEL.ADMIN:
{
// Find the first admin channel and use that.
const delegatorsById = selectModerationDelegatorsById(state);
blockerChannelClaims = [
blockerChannelClaims.find((x) => delegatorsById[x.claim_id] && delegatorsById[x.claim_id].global),
];
}
break;
}
} else {
blockerChannelClaims = blockerChannelClaims.filter((x) => blockerIds.includes(x.claim_id));
}
const { channelName, channelClaimId } = parseURI(commenterUri);
2021-05-25 08:17:36 +02:00
const creatorClaim = selectClaimsById(state)[creatorId];
if (creatorId && !creatorClaim) {
console.error("Can't find creator claim"); // eslint-disable-line
return;
}
dispatch({
type: unblock ? ACTIONS.COMMENT_MODERATION_UN_BLOCK_STARTED : ACTIONS.COMMENT_MODERATION_BLOCK_STARTED,
data: {
2021-05-25 08:17:36 +02:00
blockedUri: commenterUri,
creatorUri: creatorClaim ? creatorClaim.permanent_url : undefined,
blockLevel: blockLevel,
},
});
const commenterIdForAction = channelClaimId;
const commenterNameForAction = channelName;
let channelSignatures = [];
const sharedModBlockParams = unblock
? {
2021-05-25 08:17:36 +02:00
un_blocked_channel_id: commenterIdForAction,
un_blocked_channel_name: commenterNameForAction,
}
: {
2021-05-25 08:17:36 +02:00
blocked_channel_id: commenterIdForAction,
blocked_channel_name: commenterNameForAction,
};
const commentAction = unblock ? Comments.moderation_unblock : Comments.moderation_block;
2021-05-25 08:17:36 +02:00
return Promise.all(blockerChannelClaims.map((x) => channelSignName(x.claim_id, x.name)))
.then((response) => {
channelSignatures = response;
// $FlowFixMe
return Promise.allSettled(
channelSignatures
.filter((x) => x !== undefined && x !== null)
.map((signatureData) =>
commentAction({
2021-05-25 08:17:36 +02:00
// $FlowFixMe
mod_channel_id: signatureData.claim_id,
2021-05-25 08:17:36 +02:00
// $FlowFixMe
mod_channel_name: signatureData.name,
2021-05-25 08:17:36 +02:00
// $FlowFixMe
signature: signatureData.signature,
2021-05-25 08:17:36 +02:00
// $FlowFixMe
signing_ts: signatureData.signing_ts,
2021-05-25 08:17:36 +02:00
creator_channel_id: creatorClaim ? creatorClaim.claim_id : undefined,
creator_channel_name: creatorClaim ? creatorClaim.name : undefined,
block_all: unblock ? undefined : blockLevel === BLOCK_LEVEL.ADMIN,
global_un_block: unblock ? blockLevel === BLOCK_LEVEL.ADMIN : undefined,
...sharedModBlockParams,
})
)
)
2021-05-25 08:17:36 +02:00
.then((response) => {
const failures = [];
response.forEach((res, index) => {
if (res.status === 'rejected') {
// TODO: This should be error codes
if (res.reason.message !== 'validation is disallowed for non controlling channels') {
// $FlowFixMe
failures.push(channelSignatures[index].name + ': ' + res.reason.message);
}
}
});
if (failures.length !== 0) {
dispatch(doToast({ message: failures.join(), isError: true }));
dispatch({
type: unblock ? ACTIONS.COMMENT_MODERATION_UN_BLOCK_FAILED : ACTIONS.COMMENT_MODERATION_BLOCK_FAILED,
data: {
blockedUri: commenterUri,
creatorUri: creatorClaim ? creatorClaim.permanent_url : undefined,
blockLevel: blockLevel,
},
});
return;
}
dispatch({
type: unblock ? ACTIONS.COMMENT_MODERATION_UN_BLOCK_COMPLETE : ACTIONS.COMMENT_MODERATION_BLOCK_COMPLETE,
2021-05-25 08:17:36 +02:00
data: {
blockedUri: commenterUri,
creatorUri: creatorClaim ? creatorClaim.permanent_url : undefined,
blockLevel: blockLevel,
},
});
2021-05-25 08:17:36 +02:00
dispatch(
doToast({
message: unblock
? __('Channel unblocked!')
: __('Channel "%channel%" blocked.', { channel: commenterNameForAction }),
linkText: __(showLink ? 'See All' : ''),
linkTarget: '/settings/block_and_mute',
})
);
})
.catch(() => {
dispatch({
type: unblock ? ACTIONS.COMMENT_MODERATION_UN_BLOCK_FAILED : ACTIONS.COMMENT_MODERATION_BLOCK_FAILED,
2021-05-25 08:17:36 +02:00
data: {
blockedUri: commenterUri,
creatorUri: creatorClaim ? creatorClaim.permanent_url : undefined,
blockLevel: blockLevel,
},
});
});
})
.catch(() => {
dispatch({
type: unblock ? ACTIONS.COMMENT_MODERATION_UN_BLOCK_FAILED : ACTIONS.COMMENT_MODERATION_BLOCK_FAILED,
2021-05-25 08:17:36 +02:00
data: {
blockedUri: commenterUri,
creatorUri: creatorClaim ? creatorClaim.permanent_url : undefined,
blockLevel: blockLevel,
},
});
});
};
}
2021-05-25 08:17:36 +02:00
/**
* Blocks the commenter for all channels that I own.
*
* @param commenterUri
* @param showLink
* @returns {function(Dispatch): *}
*/
export function doCommentModBlock(commenterUri: string, showLink: boolean = true) {
return (dispatch: Dispatch) => {
return dispatch(doCommentModToggleBlock(false, commenterUri, '', [], BLOCK_LEVEL.SELF, showLink));
};
}
/**
* Blocks the commenter using the given channel that has Global privileges.
*
* @param commenterUri
* @param blockerId
* @returns {function(Dispatch): *}
*/
export function doCommentModBlockAsAdmin(commenterUri: string, blockerId: string) {
return (dispatch: Dispatch) => {
return dispatch(doCommentModToggleBlock(false, commenterUri, '', blockerId ? [blockerId] : [], BLOCK_LEVEL.ADMIN));
};
}
/**
* Blocks the commenter using the given channel that has been granted
* moderation rights by the creator.
*
* @param commenterUri
* @param creatorId
* @param blockerId
* @returns {function(Dispatch): *}
*/
export function doCommentModBlockAsModerator(commenterUri: string, creatorId: string, blockerId: string) {
return (dispatch: Dispatch) => {
2021-05-25 08:17:36 +02:00
return dispatch(
doCommentModToggleBlock(false, commenterUri, creatorId, blockerId ? [blockerId] : [], BLOCK_LEVEL.MODERATOR)
);
};
}
2021-05-25 08:17:36 +02:00
/**
* Unblocks the commenter for all channels that I own.
*
* @param commenterUri
* @param showLink
* @returns {function(Dispatch): *}
*/
export function doCommentModUnBlock(commenterUri: string, showLink: boolean = true) {
return (dispatch: Dispatch) => {
2021-05-25 08:17:36 +02:00
return dispatch(doCommentModToggleBlock(true, commenterUri, '', [], BLOCK_LEVEL.SELF, showLink));
};
}
/**
* Unblocks the commenter using the given channel that has Global privileges.
*
* @param commenterUri
* @param blockerId
* @returns {function(Dispatch): *}
*/
export function doCommentModUnBlockAsAdmin(commenterUri: string, blockerId: string) {
return (dispatch: Dispatch) => {
return dispatch(doCommentModToggleBlock(true, commenterUri, '', blockerId ? [blockerId] : [], BLOCK_LEVEL.ADMIN));
};
}
/**
* Unblocks the commenter using the given channel that has been granted
* moderation rights by the creator.
*
* @param commenterUri
* @param creatorId
* @param blockerId
* @returns {function(Dispatch): *}
*/
export function doCommentModUnBlockAsModerator(commenterUri: string, creatorId: string, blockerId: string) {
return (dispatch: Dispatch) => {
return dispatch(
doCommentModToggleBlock(true, commenterUri, creatorId, blockerId ? [blockerId] : [], BLOCK_LEVEL.MODERATOR)
);
};
}
export function doFetchModBlockedList() {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myChannels = selectMyChannelClaims(state);
dispatch({
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_STARTED,
});
let channelSignatures = [];
return Promise.all(myChannels.map((channel) => channelSignName(channel.claim_id, channel.name)))
.then((response) => {
channelSignatures = response;
// $FlowFixMe
return Promise.allSettled(
channelSignatures
.filter((x) => x !== undefined && x !== null)
.map((signatureData) =>
Comments.moderation_block_list({
mod_channel_id: signatureData.claim_id,
mod_channel_name: signatureData.name,
signature: signatureData.signature,
signing_ts: signatureData.signing_ts,
})
)
)
.then((res) => {
2021-05-25 08:17:36 +02:00
let personalBlockList = [];
let adminBlockList = [];
let moderatorBlockList = [];
let moderatorBlockListDelegatorsMap = {};
const blockListsPerChannel = res.map((r) => r.value);
blockListsPerChannel
.sort((a, b) => {
return 1;
})
2021-05-25 08:17:36 +02:00
.forEach((channelBlockLists) => {
const storeList = (fetchedList, blockedList, blockedByMap) => {
if (fetchedList) {
fetchedList.forEach((blockedChannel) => {
if (blockedChannel.blocked_channel_name) {
const channelUri = buildURI({
channelName: blockedChannel.blocked_channel_name,
claimId: blockedChannel.blocked_channel_id,
});
if (!blockedList.find((blockedChannel) => blockedChannel.channelUri === channelUri)) {
blockedList.push({ channelUri, blockedAt: blockedChannel.blocked_at });
}
if (blockedByMap !== undefined) {
const blockedByChannelUri = buildURI({
channelName: blockedChannel.blocked_by_channel_name,
claimId: blockedChannel.blocked_by_channel_id,
});
if (blockedByMap[channelUri]) {
if (!blockedByMap[channelUri].includes(blockedByChannelUri)) {
blockedByMap[channelUri].push(blockedByChannelUri);
}
} else {
blockedByMap[channelUri] = [blockedByChannelUri];
}
}
}
2021-05-25 08:17:36 +02:00
});
}
};
const blocked_channels = channelBlockLists && channelBlockLists.blocked_channels;
const globally_blocked_channels = channelBlockLists && channelBlockLists.globally_blocked_channels;
const delegated_blocked_channels = channelBlockLists && channelBlockLists.delegated_blocked_channels;
storeList(blocked_channels, personalBlockList);
storeList(globally_blocked_channels, adminBlockList);
storeList(delegated_blocked_channels, moderatorBlockList, moderatorBlockListDelegatorsMap);
});
dispatch({
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_COMPLETED,
data: {
2021-05-25 08:17:36 +02:00
personalBlockList:
personalBlockList.length > 0
? personalBlockList
.sort((a, b) => new Date(a.blockedAt) - new Date(b.blockedAt))
.map((blockedChannel) => blockedChannel.channelUri)
: null,
2021-05-25 08:17:36 +02:00
adminBlockList:
adminBlockList.length > 0
? adminBlockList
.sort((a, b) => new Date(a.blockedAt) - new Date(b.blockedAt))
.map((blockedChannel) => blockedChannel.channelUri)
: null,
moderatorBlockList:
moderatorBlockList.length > 0
? moderatorBlockList
.sort((a, b) => new Date(a.blockedAt) - new Date(b.blockedAt))
.map((blockedChannel) => blockedChannel.channelUri)
: null,
moderatorBlockListDelegatorsMap: moderatorBlockListDelegatorsMap,
},
});
})
.catch(() => {
dispatch({
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_FAILED,
});
});
})
.catch(() => {
dispatch({
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_FAILED,
});
});
};
}
export const doUpdateBlockListForPublishedChannel = (channelClaim: ChannelClaim) => {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const blockedUris = selectModerationBlockList(state);
let channelSignature: ?{
signature: string,
signing_ts: string,
};
try {
channelSignature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
} catch (e) {}
if (!channelSignature) {
return;
}
return Promise.all(
blockedUris.map((uri) => {
const { channelName, channelClaimId } = parseURI(uri);
return Comments.moderation_block({
mod_channel_id: channelClaim.claim_id,
mod_channel_name: channelClaim.name,
// $FlowFixMe
signature: channelSignature.signature,
// $FlowFixMe
signing_ts: channelSignature.signing_ts,
blocked_channel_id: channelClaimId,
blocked_channel_name: channelName,
});
})
);
};
};
2021-04-20 10:40:53 +02:00
2021-05-25 08:17:36 +02:00
export function doCommentModAddDelegate(
modChannelId: string,
modChannelName: string,
creatorChannelClaim: ChannelClaim,
showToast: boolean = false
2021-05-25 08:17:36 +02:00
) {
return async (dispatch: Dispatch, getState: GetState) => {
let signature: ?{
signature: string,
signing_ts: string,
};
try {
signature = await Lbry.channel_sign({
channel_id: creatorChannelClaim.claim_id,
hexdata: toHex(creatorChannelClaim.name),
});
} catch (e) {}
if (!signature) {
return;
}
return Comments.moderation_add_delegate({
mod_channel_id: modChannelId,
mod_channel_name: modChannelName,
creator_channel_id: creatorChannelClaim.claim_id,
creator_channel_name: creatorChannelClaim.name,
signature: signature.signature,
signing_ts: signature.signing_ts,
})
.then(() => {
if (showToast) {
dispatch(
doToast({
message: __('Added %user% as moderator for %myChannel%', {
user: modChannelName,
myChannel: creatorChannelClaim.name,
}),
linkText: __('Manage'),
linkTarget: `/${PAGES.SETTINGS_CREATOR}`,
})
);
}
})
.catch((err) => {
dispatch(
doToast({
message: err.message,
isError: true,
})
);
});
2021-05-25 08:17:36 +02:00
};
}
export function doCommentModRemoveDelegate(
modChannelId: string,
modChannelName: string,
creatorChannelClaim: ChannelClaim
) {
return async (dispatch: Dispatch, getState: GetState) => {
let signature: ?{
signature: string,
signing_ts: string,
};
try {
signature = await Lbry.channel_sign({
channel_id: creatorChannelClaim.claim_id,
hexdata: toHex(creatorChannelClaim.name),
});
} catch (e) {}
if (!signature) {
return;
}
return Comments.moderation_remove_delegate({
mod_channel_id: modChannelId,
mod_channel_name: modChannelName,
creator_channel_id: creatorChannelClaim.claim_id,
creator_channel_name: creatorChannelClaim.name,
signature: signature.signature,
signing_ts: signature.signing_ts,
}).catch((err) => {
dispatch(
doToast({
message: err.message,
isError: true,
})
);
});
};
}
export function doCommentModListDelegates(channelClaim: ChannelClaim) {
return async (dispatch: Dispatch, getState: GetState) => {
dispatch({
type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_STARTED,
});
let signature: ?{
signature: string,
signing_ts: string,
};
try {
signature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
} catch (e) {}
if (!signature) {
dispatch({
type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_FAILED,
});
return;
}
return Comments.moderation_list_delegates({
creator_channel_id: channelClaim.claim_id,
creator_channel_name: channelClaim.name,
signature: signature.signature,
signing_ts: signature.signing_ts,
})
.then((response) => {
dispatch({
type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_COMPLETED,
data: {
id: channelClaim.claim_id,
delegates: response.Delegates,
},
});
})
.catch((err) => {
dispatch({
type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_FAILED,
});
});
};
}
export function doFetchCommentModAmIList(channelClaim: ChannelClaim) {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myChannels = selectMyChannelClaims(state);
dispatch({
type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_STARTED,
});
let channelSignatures = [];
return Promise.all(myChannels.map((channel) => channelSignName(channel.claim_id, channel.name)))
.then((response) => {
channelSignatures = response;
// $FlowFixMe
return Promise.allSettled(
channelSignatures
.filter((x) => x !== undefined && x !== null)
.map((signatureData) =>
Comments.moderation_am_i({
channel_name: signatureData.name,
channel_id: signatureData.claim_id,
signature: signatureData.signature,
signing_ts: signatureData.signing_ts,
})
)
)
.then((res) => {
const delegatorsById = {};
channelSignatures.forEach((chanSig, index) => {
if (chanSig && res[index]) {
const value = res[index].value;
delegatorsById[chanSig.claim_id] = {
global: value ? value.type === 'Global' : false,
delegators: value && value.authorized_channels ? value.authorized_channels : {},
};
}
});
dispatch({
type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_COMPLETED,
data: delegatorsById,
});
})
.catch((err) => {
dispatch({
type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_FAILED,
});
});
})
.catch(() => {
dispatch({
type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_FAILED,
});
});
};
}
2021-04-20 10:40:53 +02:00
export const doFetchCreatorSettings = (channelClaimIds: Array<string> = []) => {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myChannels = selectMyChannelClaims(state);
dispatch({
type: ACTIONS.COMMENT_FETCH_SETTINGS_STARTED,
});
let channelSignatures = [];
if (myChannels) {
for (const channelClaim of myChannels) {
if (channelClaimIds.length !== 0 && !channelClaimIds.includes(channelClaim.claim_id)) {
continue;
}
try {
const channelSignature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
channelSignatures.push({ ...channelSignature, claim_id: channelClaim.claim_id, name: channelClaim.name });
} catch (e) {}
}
}
return Promise.all(
channelSignatures.map((signatureData) =>
Comments.setting_list({
channel_name: signatureData.name,
channel_id: signatureData.claim_id,
signature: signatureData.signature,
signing_ts: signatureData.signing_ts,
})
)
)
.then((settings) => {
const settingsByChannelId = {};
for (let i = 0; i < channelSignatures.length; ++i) {
const channelId = channelSignatures[i].claim_id;
settingsByChannelId[channelId] = settings[i];
settingsByChannelId[channelId].words = settingsByChannelId[channelId].words.split(',');
delete settingsByChannelId[channelId].channel_name;
delete settingsByChannelId[channelId].channel_id;
delete settingsByChannelId[channelId].signature;
delete settingsByChannelId[channelId].signing_ts;
}
dispatch({
type: ACTIONS.COMMENT_FETCH_SETTINGS_COMPLETED,
data: settingsByChannelId,
});
})
.catch((err) => {
// TODO: Use error codes when available.
// TODO: The "validation is disallowed" thing ideally should just be a
// success case that returns a null setting, instead of an error.
// As we are using 'Promise.all', if one channel fails, everyone
// fails. This forces us to remove the batch functionality of this
// function. However, since this "validation is disallowed" thing
// is potentially a temporary one to handle spammers, I retained
// the batch functionality for now.
if (err.message === 'validation is disallowed for non controlling channels') {
const settingsByChannelId = {};
for (let i = 0; i < channelSignatures.length; ++i) {
const channelId = channelSignatures[i].claim_id;
// 'undefined' means "fetching or have not fetched";
// 'null' means "feature not available for this channel";
settingsByChannelId[channelId] = null;
}
dispatch({
type: ACTIONS.COMMENT_FETCH_SETTINGS_COMPLETED,
data: settingsByChannelId,
});
return;
}
2021-04-20 10:40:53 +02:00
dispatch({
type: ACTIONS.COMMENT_FETCH_SETTINGS_FAILED,
});
});
};
};
/**
* Updates creator settings, except for 'Words', which will be handled by
* 'doCommentWords, doCommentBlockWords, etc.'
*
* @param channelClaim
* @param settings
* @returns {function(Dispatch, GetState): Promise<R>|Promise<unknown>|*}
*/
export const doUpdateCreatorSettings = (channelClaim: ChannelClaim, settings: PerChannelSettings) => {
return async (dispatch: Dispatch, getState: GetState) => {
let channelSignature: ?{
signature: string,
signing_ts: string,
};
try {
channelSignature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
} catch (e) {}
if (!channelSignature) {
return;
}
return Comments.setting_update({
channel_name: channelClaim.name,
channel_id: channelClaim.claim_id,
signature: channelSignature.signature,
signing_ts: channelSignature.signing_ts,
...settings,
}).catch((err) => {
dispatch(
doToast({
message: err.message,
isError: true,
})
);
});
};
};
export const doCommentWords = (channelClaim: ChannelClaim, words: Array<string>, isUnblock: boolean) => {
return async (dispatch: Dispatch, getState: GetState) => {
let channelSignature: ?{
signature: string,
signing_ts: string,
};
try {
channelSignature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
} catch (e) {}
if (!channelSignature) {
return;
}
const cmd = isUnblock ? Comments.setting_unblock_word : Comments.setting_block_word;
return cmd({
channel_name: channelClaim.name,
channel_id: channelClaim.claim_id,
words: words.join(','),
signature: channelSignature.signature,
signing_ts: channelSignature.signing_ts,
}).catch((err) => {
dispatch(
doToast({
message: err.message,
isError: true,
})
);
});
};
};
export const doCommentBlockWords = (channelClaim: ChannelClaim, words: Array<string>) => {
return (dispatch: Dispatch) => {
return dispatch(doCommentWords(channelClaim, words, false));
};
};
export const doCommentUnblockWords = (channelClaim: ChannelClaim, words: Array<string>) => {
return (dispatch: Dispatch) => {
return dispatch(doCommentWords(channelClaim, words, true));
};
};
export const doFetchBlockedWords = () => {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const myChannels = selectMyChannelClaims(state);
dispatch({
type: ACTIONS.COMMENT_FETCH_BLOCKED_WORDS_STARTED,
});
let channelSignatures = [];
if (myChannels) {
for (const channelClaim of myChannels) {
try {
const channelSignature = await Lbry.channel_sign({
channel_id: channelClaim.claim_id,
hexdata: toHex(channelClaim.name),
});
channelSignatures.push({ ...channelSignature, claim_id: channelClaim.claim_id, name: channelClaim.name });
} catch (e) {}
}
}
return Promise.all(
channelSignatures.map((signatureData) =>
Comments.setting_list_blocked_words({
channel_name: signatureData.name,
channel_id: signatureData.claim_id,
signature: signatureData.signature,
signing_ts: signatureData.signing_ts,
})
)
)
.then((blockedWords) => {
const blockedWordsByChannelId = {};
for (let i = 0; i < channelSignatures.length; ++i) {
const claim_id = channelSignatures[i].claim_id;
blockedWordsByChannelId[claim_id] = blockedWords[i].word_list;
}
dispatch({
type: ACTIONS.COMMENT_FETCH_BLOCKED_WORDS_COMPLETED,
data: blockedWordsByChannelId,
});
})
.catch(() => {
dispatch({
type: ACTIONS.COMMENT_FETCH_BLOCKED_WORDS_FAILED,
});
});
};
};