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';
|
2021-06-19 11:52:17 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2021-07-15 16:43:28 +02:00
|
|
|
import { SORT_BY, BLOCK_LEVEL } from 'constants/comment';
|
2021-10-17 10:36:14 +02:00
|
|
|
import Lbry from 'lbry';
|
|
|
|
import { parseURI, buildURI, isURIEqual } from 'util/lbryURI';
|
2022-01-18 03:46:18 +01:00
|
|
|
import { devToast, doFailedSignatureToast, resolveCommentronError } from 'util/commentron-error';
|
2022-01-14 15:50:09 +01:00
|
|
|
import { selectClaimForUri, selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
|
2022-01-26 14:01:42 +01:00
|
|
|
import { doResolveUris, doClaimSearch } from 'redux/actions/claims';
|
2020-10-20 15:29:49 +02:00
|
|
|
import { doToast, doSeeNotifications } from 'redux/actions/notifications';
|
2020-09-29 20:45:28 +02:00
|
|
|
import {
|
2021-10-06 20:40:40 +02:00
|
|
|
selectMyReactsForComment,
|
|
|
|
selectOthersReactsForComment,
|
2020-09-30 17:59:05 +02:00
|
|
|
selectPendingCommentReacts,
|
2021-03-03 19:50:16 +01:00
|
|
|
selectModerationBlockList,
|
2021-05-25 08:17:36 +02:00
|
|
|
selectModerationDelegatorsById,
|
2020-09-29 20:45:28 +02:00
|
|
|
} from 'redux/selectors/comments';
|
2020-10-20 15:29:49 +02:00
|
|
|
import { makeSelectNotificationForCommentId } from 'redux/selectors/notifications';
|
2021-02-09 17:05:56 +01:00
|
|
|
import { selectActiveChannelClaim } from 'redux/selectors/app';
|
2021-02-11 06:12:41 +01:00
|
|
|
import { toHex } from 'util/hex';
|
|
|
|
import Comments from 'comments';
|
2021-06-24 16:33:11 +02:00
|
|
|
import { selectPrefsReady } from 'redux/selectors/sync';
|
|
|
|
import { doAlertWaitingForSync } from 'redux/actions/app';
|
2020-06-23 19:38:18 +02:00
|
|
|
|
2021-07-25 14:52:45 +02:00
|
|
|
const FETCH_API_FAILED_TO_FETCH = 'Failed to fetch';
|
2021-09-21 16:40:44 +02:00
|
|
|
const PROMISE_FULFILLED = 'fulfilled';
|
2021-07-15 16:43:28 +02:00
|
|
|
|
2022-01-14 15:50:09 +01:00
|
|
|
const MENTION_REGEX = /(?:^| |\n)@[^\s=&#$@%?:;/"<>%{}|^~[]*(?::[\w]+)?/gm;
|
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
export function doCommentList(
|
|
|
|
uri: string,
|
|
|
|
parentId: string,
|
|
|
|
page: number = 1,
|
|
|
|
pageSize: number = 99999,
|
|
|
|
sortBy: number = SORT_BY.NEWEST
|
|
|
|
) {
|
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;
|
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
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,
|
2021-07-15 16:43:28 +02:00
|
|
|
data: {
|
|
|
|
parentId,
|
|
|
|
},
|
2020-06-23 19:38:18 +02:00
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2021-06-03 07:57:50 +02:00
|
|
|
// Adding 'channel_id' and 'channel_name' enables "CreatorSettings > commentsEnabled".
|
2021-10-01 14:10:27 +02:00
|
|
|
const creatorChannelClaim = claim.value_type === 'channel' ? claim : claim.signing_channel;
|
2021-06-03 07:57:50 +02:00
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
return Comments.comment_list({
|
2020-06-23 19:38:18 +02:00
|
|
|
page,
|
2021-02-11 06:12:41 +01:00
|
|
|
claim_id: claimId,
|
2020-06-23 19:38:18 +02:00
|
|
|
page_size: pageSize,
|
2021-07-15 16:43:28 +02:00
|
|
|
parent_id: parentId || undefined,
|
|
|
|
top_level: !parentId,
|
2021-10-01 14:10:27 +02:00
|
|
|
channel_id: creatorChannelClaim ? creatorChannelClaim.claim_id : undefined,
|
|
|
|
channel_name: creatorChannelClaim ? creatorChannelClaim.name : undefined,
|
2021-07-15 16:43:28 +02:00
|
|
|
sort_by: sortBy,
|
2020-06-23 19:38:18 +02:00
|
|
|
})
|
|
|
|
.then((result: CommentListResponse) => {
|
2021-07-15 16:43:28 +02:00
|
|
|
const { items: comments, total_items, total_filtered_items, total_pages } = result;
|
2020-06-23 19:38:18 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_COMPLETED,
|
|
|
|
data: {
|
|
|
|
comments,
|
2021-07-15 16:43:28 +02:00
|
|
|
parentId,
|
|
|
|
totalItems: total_items,
|
|
|
|
totalFilteredItems: total_filtered_items,
|
|
|
|
totalPages: total_pages,
|
2020-06-23 19:38:18 +02:00
|
|
|
claimId: claimId,
|
2021-10-01 14:10:27 +02:00
|
|
|
creatorClaimId: creatorChannelClaim ? creatorChannelClaim.claim_id : undefined,
|
2020-06-23 19:38:18 +02:00
|
|
|
uri: uri,
|
|
|
|
},
|
|
|
|
});
|
2021-07-15 16:43:28 +02:00
|
|
|
|
2020-10-20 05:20:38 +02:00
|
|
|
return result;
|
2020-06-23 19:38:18 +02:00
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.catch((error) => {
|
2021-07-25 14:52:45 +02:00
|
|
|
switch (error.message) {
|
|
|
|
case 'comments are disabled by the creator':
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_COMPLETED,
|
|
|
|
data: {
|
2021-10-01 14:10:27 +02:00
|
|
|
creatorClaimId: creatorChannelClaim ? creatorChannelClaim.claim_id : undefined,
|
2021-07-25 14:52:45 +02:00
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FETCH_API_FAILED_TO_FETCH:
|
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
isError: true,
|
2022-02-03 09:48:56 +01:00
|
|
|
message: __('Failed to fetch comments.'),
|
2021-07-25 14:52:45 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error });
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dispatch(doToast({ isError: true, message: `${error.message}` }));
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error });
|
2021-06-03 07:57:50 +02:00
|
|
|
}
|
2020-06-23 19:38:18 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-01 14:10:27 +02:00
|
|
|
export function doCommentListOwn(
|
|
|
|
channelId: string,
|
|
|
|
page: number = 1,
|
|
|
|
pageSize: number = 99999,
|
|
|
|
sortBy: number = SORT_BY.NEWEST_NO_PINS
|
|
|
|
) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myChannelClaims = selectMyChannelClaims(state);
|
|
|
|
if (!myChannelClaims) {
|
|
|
|
console.error('Failed to fetch channel list.'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const channelClaim = myChannelClaims.find((x) => x.claim_id === channelId);
|
|
|
|
if (!channelClaim) {
|
|
|
|
console.error('You do not own this channel.'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const channelSignature = await channelSignName(channelClaim.claim_id, channelClaim.name);
|
|
|
|
if (!channelSignature) {
|
|
|
|
console.error('Failed to sign channel name.'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_STARTED,
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
return Comments.comment_list({
|
|
|
|
page,
|
|
|
|
page_size: pageSize,
|
|
|
|
sort_by: sortBy,
|
|
|
|
author_claim_id: channelId,
|
|
|
|
requestor_channel_name: channelClaim.name,
|
|
|
|
requestor_channel_id: channelClaim.claim_id,
|
|
|
|
signature: channelSignature.signature,
|
|
|
|
signing_ts: channelSignature.signing_ts,
|
|
|
|
})
|
|
|
|
.then((result: CommentListResponse) => {
|
|
|
|
const { items: comments, total_items, total_filtered_items, total_pages } = result;
|
|
|
|
|
|
|
|
if (!comments) {
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: 'No more comments.' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
doClaimSearch({
|
|
|
|
page: 1,
|
|
|
|
page_size: 20,
|
|
|
|
no_totals: true,
|
|
|
|
claim_ids: comments.map((c) => c.claim_id),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_COMPLETED,
|
|
|
|
data: {
|
|
|
|
comments,
|
|
|
|
totalItems: total_items,
|
|
|
|
totalFilteredItems: total_filtered_items,
|
|
|
|
totalPages: total_pages,
|
|
|
|
uri: channelClaim.canonical_url, // hijack "Discussion Page"
|
|
|
|
claimId: channelClaim.claim_id, // hijack "Discussion Page"
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: err });
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
switch (error.message) {
|
|
|
|
case FETCH_API_FAILED_TO_FETCH:
|
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
isError: true,
|
2022-02-03 09:48:56 +01:00
|
|
|
message: __('Failed to fetch comments.'),
|
2021-10-01 14:10:27 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
dispatch(doToast({ isError: true, message: `${error.message}` }));
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error });
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dispatch(doToast({ isError: true, message: `${error.message}` }));
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
export function doCommentById(commentId: string, toastIfNotFound: boolean = true) {
|
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2021-10-01 09:49:37 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_BY_ID_STARTED,
|
|
|
|
});
|
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
return Comments.comment_by_id({ comment_id: commentId, with_ancestors: true })
|
|
|
|
.then((result: CommentByIdResponse) => {
|
|
|
|
const { item, items, ancestors } = result;
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_BY_ID_COMPLETED,
|
|
|
|
data: {
|
|
|
|
comment: item || items, // Requested a change to rename it to 'item'. This covers both.
|
|
|
|
ancestors: ancestors,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2021-08-30 08:12:12 +02:00
|
|
|
const ID_NOT_FOUND_REGEX = /^comment for id (.*) could not be found$/;
|
|
|
|
if (ID_NOT_FOUND_REGEX.test(error.message) && toastIfNotFound) {
|
2021-07-15 16:43:28 +02:00
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
isError: true,
|
|
|
|
message: __('The requested comment is no longer available.'),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
devToast(dispatch, error.message);
|
|
|
|
}
|
2021-08-27 14:03:29 +02:00
|
|
|
|
|
|
|
return error;
|
2021-07-15 16:43:28 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-01 14:10:27 +02:00
|
|
|
export function doCommentReset(claimId: string) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2021-07-15 16:43:28 +02:00
|
|
|
if (!claimId) {
|
2021-10-01 14:10:27 +02:00
|
|
|
console.error(`Failed to reset comments`); //eslint-disable-line
|
2021-07-15 16:43:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_RESET,
|
|
|
|
data: {
|
|
|
|
claimId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
})
|
2021-07-15 16:43:28 +02:00
|
|
|
.then((result: SuperListResponse) => {
|
2021-04-23 21:59:48 +02:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
export function doCommentReactList(commentIds: Array<string>) {
|
2021-07-15 05:24:37 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
const state = getState();
|
2021-02-09 17:05:56 +01:00
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
2020-09-30 17:59:05 +02:00
|
|
|
|
2020-09-29 16:10:23 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACTION_LIST_STARTED,
|
|
|
|
});
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
const params: ReactionListParams = {
|
2020-09-29 16:10:23 +02:00
|
|
|
comment_ids: commentIds.join(','),
|
2020-09-30 17:59:05 +02:00
|
|
|
};
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
if (activeChannelClaim) {
|
2021-07-15 05:24:37 +02:00
|
|
|
const signatureData = await channelSignName(activeChannelClaim.claim_id, activeChannelClaim.name);
|
|
|
|
if (!signatureData) {
|
|
|
|
return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
|
|
|
|
}
|
|
|
|
|
|
|
|
params.channel_name = activeChannelClaim.name;
|
|
|
|
params.channel_id = activeChannelClaim.claim_id;
|
|
|
|
params.signature = signatureData.signature;
|
|
|
|
params.signing_ts = signatureData.signing_ts;
|
2020-09-30 17:59:05 +02:00
|
|
|
}
|
2020-10-06 21:35:13 +02:00
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
return Comments.reaction_list(params)
|
|
|
|
.then((result: ReactionListResponse) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
const { my_reactions: myReactions, others_reactions: othersReactions } = result;
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
|
|
|
|
data: {
|
2021-07-20 08:55:26 +02:00
|
|
|
myReactions,
|
2020-09-29 16:10:23 +02:00
|
|
|
othersReactions,
|
2021-07-15 16:43:28 +02:00
|
|
|
channelId: activeChannelClaim ? activeChannelClaim.claim_id : undefined,
|
2021-07-20 08:55:26 +02:00
|
|
|
commentIds,
|
2020-09-29 16:10:23 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.catch((error) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACTION_LIST_FAILED,
|
2021-07-18 04:54:01 +02:00
|
|
|
data: error,
|
2020-09-29 16:10:23 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCommentReact(commentId: string, type: string) {
|
2021-07-15 05:24:37 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
const state = getState();
|
2021-02-09 17:05:56 +01:00
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
2020-09-30 17:59:05 +02:00
|
|
|
const pendingReacts = selectPendingCommentReacts(state);
|
2020-10-20 15:29:49 +02:00
|
|
|
const notification = makeSelectNotificationForCommentId(commentId)(state);
|
2021-02-09 17:05:56 +01:00
|
|
|
|
|
|
|
if (!activeChannelClaim) {
|
|
|
|
console.error('Unable to react to comment. No activeChannel is set.'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:29:49 +02:00
|
|
|
if (notification && !notification.is_seen) {
|
|
|
|
dispatch(doSeeNotifications([notification.id]));
|
|
|
|
}
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2020-09-30 17:59:05 +02:00
|
|
|
const exclusiveTypes = {
|
|
|
|
[REACTION_TYPES.LIKE]: REACTION_TYPES.DISLIKE,
|
|
|
|
[REACTION_TYPES.DISLIKE]: REACTION_TYPES.LIKE,
|
|
|
|
};
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2020-10-02 02:25:30 +02:00
|
|
|
if (pendingReacts.includes(commentId + exclusiveTypes[type]) || pendingReacts.includes(commentId + type)) {
|
2020-09-30 17:59:05 +02:00
|
|
|
// ignore dislikes during likes, for example
|
|
|
|
return;
|
|
|
|
}
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
const reactKey = `${commentId}:${activeChannelClaim.claim_id}`;
|
2021-10-06 20:40:40 +02:00
|
|
|
const myReacts = selectMyReactsForComment(state, reactKey) || [];
|
|
|
|
const othersReacts = selectOthersReactsForComment(state, reactKey) || {};
|
2021-07-15 05:24:37 +02:00
|
|
|
|
|
|
|
const signatureData = await channelSignName(activeChannelClaim.claim_id, activeChannelClaim.name);
|
|
|
|
if (!signatureData) {
|
|
|
|
return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const params: ReactionReactParams = {
|
2020-09-29 16:10:23 +02:00
|
|
|
comment_ids: commentId,
|
2021-02-09 17:05:56 +01:00
|
|
|
channel_name: activeChannelClaim.name,
|
|
|
|
channel_id: activeChannelClaim.claim_id,
|
2021-07-15 05:24:37 +02:00
|
|
|
signature: signatureData.signature,
|
|
|
|
signing_ts: signatureData.signing_ts,
|
|
|
|
type: type,
|
2020-09-29 16:10:23 +02:00
|
|
|
};
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2020-09-29 16:10:23 +02:00
|
|
|
if (myReacts.includes(type)) {
|
|
|
|
params['remove'] = true;
|
2020-09-29 20:45:28 +02:00
|
|
|
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
|
|
|
}
|
2021-07-15 05:24:37 +02:00
|
|
|
|
2020-09-29 20:45:28 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACT_STARTED,
|
2020-09-30 17:59:05 +02:00
|
|
|
data: commentId + type,
|
2020-09-29 20:45:28 +02:00
|
|
|
});
|
2020-10-02 02:25:30 +02:00
|
|
|
|
2020-09-29 20:45:28 +02:00
|
|
|
// 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
|
|
|
|
2020-10-02 02:25:30 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED,
|
|
|
|
data: {
|
2021-07-15 16:43:28 +02:00
|
|
|
myReactions: { [reactKey]: myReactsObj },
|
|
|
|
othersReactions: { [reactKey]: othersReacts },
|
2020-10-02 02:25:30 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
Comments.reaction_react(params)
|
|
|
|
.then((result: ReactionReactResponse) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACT_COMPLETED,
|
2020-09-30 17:59:05 +02:00
|
|
|
data: commentId + type,
|
2020-09-29 20:45:28 +02:00
|
|
|
});
|
2020-09-29 16:10:23 +02:00
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.catch((error) => {
|
2020-09-29 16:10:23 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_REACT_FAILED,
|
2020-09-30 17:59:05 +02:00
|
|
|
data: commentId + type,
|
2020-09-29 16:10:23 +02:00
|
|
|
});
|
2020-10-02 02:25:30 +02:00
|
|
|
|
|
|
|
const myRevertedReactsObj = myReacts
|
2021-03-03 19:50:16 +01:00
|
|
|
.filter((el) => el !== type)
|
2020-10-02 02:25:30 +02:00
|
|
|
.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
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-06 22:28:29 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param uri
|
|
|
|
* @param livestream
|
2022-02-08 13:15:22 +01:00
|
|
|
* @param params the CommentSubmitParams needed for CommentCreateParams (not the same as they are dealt differently,
|
|
|
|
* like mentionedChannels which is selected after submission)
|
2021-07-06 22:28:29 +02:00
|
|
|
* @returns {(function(Dispatch, GetState): Promise<undefined|void|*>)|*}
|
|
|
|
*/
|
2022-02-07 19:03:10 +01:00
|
|
|
export function doCommentCreate(uri: string, livestream: boolean, params: CommentSubmitParams) {
|
2021-04-23 21:59:48 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2022-02-07 19:03:10 +01:00
|
|
|
const { comment, claim_id, parent_id, txid, payment_intent_id, environment, sticker } = params;
|
|
|
|
|
2020-06-23 19:38:18 +02:00
|
|
|
const state = getState();
|
2021-02-09 17:05:56 +01:00
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
2022-01-27 17:27:54 +01:00
|
|
|
const mentionedChannels: Array<MentionedChannel> = [];
|
2021-02-09 17:05:56 +01:00
|
|
|
|
|
|
|
if (!activeChannelClaim) {
|
|
|
|
console.error('Unable to create comment. No activeChannel is set.'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-14 15:50:09 +01:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
|
|
|
|
// $FlowFixMe
|
|
|
|
const mentionMatches = [...comment.matchAll(MENTION_REGEX)];
|
|
|
|
|
|
|
|
if (mentionMatches.length > 0) {
|
2022-01-26 14:01:42 +01:00
|
|
|
const mentionUrls = [];
|
|
|
|
|
2022-01-14 15:50:09 +01:00
|
|
|
mentionMatches.forEach((match) => {
|
|
|
|
const matchTerm = match[0];
|
|
|
|
const mention = matchTerm.substring(matchTerm.indexOf('@'));
|
|
|
|
const mentionUri = `lbry://${mention}`;
|
|
|
|
|
2022-01-20 19:39:44 +01:00
|
|
|
if (mention.length === 1) return;
|
|
|
|
|
2022-01-14 15:50:09 +01:00
|
|
|
const claim = selectClaimForUri(state, mentionUri);
|
|
|
|
|
|
|
|
if (claim) {
|
2022-01-17 14:44:45 +01:00
|
|
|
mentionedChannels.push({ channel_name: claim.name, channel_id: claim.claim_id });
|
2022-01-27 17:27:54 +01:00
|
|
|
} else {
|
2022-01-26 14:01:42 +01:00
|
|
|
mentionUrls.push(mentionUri);
|
2022-01-14 15:50:09 +01:00
|
|
|
}
|
|
|
|
});
|
2022-01-26 14:01:42 +01:00
|
|
|
|
|
|
|
if (mentionUrls.length > 0) {
|
|
|
|
await dispatch(doResolveUris(mentionUrls, true))
|
|
|
|
.then((response) => {
|
2022-01-27 17:27:54 +01:00
|
|
|
Object.values(response).map((claim) => {
|
|
|
|
if (claim) {
|
|
|
|
// $FlowFixMe
|
|
|
|
mentionedChannels.push({ channel_name: claim.name, channel_id: claim.claim_id });
|
|
|
|
}
|
2022-01-26 14:01:42 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {});
|
|
|
|
}
|
2022-01-14 15:50:09 +01:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_CREATE_STARTED });
|
2020-06-23 19:38:18 +02:00
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
const notification = parent_id && makeSelectNotificationForCommentId(parent_id)(state);
|
|
|
|
if (notification && !notification.is_seen) dispatch(doSeeNotifications([notification.id]));
|
2020-10-20 15:29:49 +02:00
|
|
|
|
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,
|
2021-02-09 17:05:56 +01:00
|
|
|
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,
|
2021-10-28 22:25:34 +02:00
|
|
|
sticker: sticker,
|
2022-01-14 15:50:09 +01:00
|
|
|
mentioned_channels: mentionedChannels,
|
2022-01-07 13:00:23 +01:00
|
|
|
...(txid ? { support_tx_id: txid } : {}),
|
|
|
|
...(payment_intent_id ? { payment_intent_id } : {}),
|
|
|
|
...(environment ? { environment } : {}),
|
2020-06-23 19:38:18 +02:00
|
|
|
})
|
|
|
|
.then((result: CommentCreateResponse) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_CREATE_COMPLETED,
|
|
|
|
data: {
|
2020-09-09 20:53:31 +02:00
|
|
|
uri,
|
2021-03-16 19:37:19 +01:00
|
|
|
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
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.catch((error) => {
|
2021-08-10 04:20:55 +02:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_CREATE_FAILED, data: error });
|
2021-09-15 09:41:57 +02:00
|
|
|
dispatch(doToast(resolveCommentronError(error.message)));
|
2021-04-23 21:59:48 +02:00
|
|
|
return Promise.reject(error);
|
2020-06-23 19:38:18 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:43:28 +02:00
|
|
|
export function doCommentPin(commentId: string, claimId: string, remove: boolean) {
|
2021-07-15 05:24:37 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2020-10-20 05:20:38 +02:00
|
|
|
const state = getState();
|
2021-02-09 17:05:56 +01:00
|
|
|
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
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
const signedCommentId = await channelSignData(activeChannel.claim_id, commentId);
|
|
|
|
if (!signedCommentId) {
|
|
|
|
return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:20:38 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_PIN_STARTED,
|
|
|
|
});
|
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
const params: CommentPinParams = {
|
2021-02-09 17:05:56 +01:00
|
|
|
comment_id: commentId,
|
|
|
|
channel_id: activeChannel.claim_id,
|
2021-07-15 05:24:37 +02:00
|
|
|
channel_name: activeChannel.name,
|
|
|
|
remove: remove,
|
|
|
|
signature: signedCommentId.signature,
|
|
|
|
signing_ts: signedCommentId.signing_ts,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Comments.comment_pin(params)
|
2020-10-20 05:20:38 +02:00
|
|
|
.then((result: CommentPinResponse) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_PIN_COMPLETED,
|
2021-07-15 16:43:28 +02:00
|
|
|
data: {
|
|
|
|
pinnedComment: result.items,
|
|
|
|
claimId,
|
|
|
|
unpin: remove,
|
|
|
|
},
|
2020-10-20 05:20:38 +02:00
|
|
|
});
|
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:22:40 +01:00
|
|
|
/**
|
|
|
|
* Deletes a comment in Commentron.
|
|
|
|
*
|
|
|
|
* @param commentId The comment ID to delete.
|
|
|
|
* @param deleterClaim The channel-claim of the person doing the deletion. Defaults to the active channel if not provided.
|
|
|
|
* @param deleterIsModOrAdmin Is the deleter a mod or admin for the content?
|
|
|
|
* @param creatorClaim The channel-claim for the content where the comment resides. Not required if the deleter owns the comment (i.e. deleting own comment).
|
|
|
|
* @returns {function(Dispatch): *}
|
|
|
|
*/
|
|
|
|
export function doCommentAbandon(
|
|
|
|
commentId: string,
|
|
|
|
deleterClaim?: Claim,
|
|
|
|
deleterIsModOrAdmin?: boolean,
|
|
|
|
creatorClaim?: Claim
|
|
|
|
) {
|
2021-02-11 06:12:41 +01:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2021-11-08 18:22:40 +01:00
|
|
|
if (!deleterClaim) {
|
|
|
|
const state = getState();
|
|
|
|
deleterClaim = selectActiveChannelClaim(state);
|
|
|
|
}
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2020-06-23 19:38:18 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_ABANDON_STARTED,
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2021-11-08 18:22:40 +01:00
|
|
|
const commentIdSignature = await channelSignData(deleterClaim.claim_id, commentId);
|
2021-02-11 06:12:41 +01:00
|
|
|
|
|
|
|
return Comments.comment_abandon({
|
|
|
|
comment_id: commentId,
|
2021-11-08 18:22:40 +01:00
|
|
|
creator_channel_id: creatorClaim ? creatorClaim.claim_id : undefined,
|
|
|
|
creator_channel_name: creatorClaim ? creatorClaim.name : undefined,
|
2021-02-11 06:12:41 +01:00
|
|
|
...(commentIdSignature || {}),
|
2021-11-08 18:22:40 +01:00
|
|
|
mod_channel_id: deleterClaim && deleterIsModOrAdmin ? deleterClaim.claim_id : undefined,
|
|
|
|
mod_channel_name: deleterClaim && deleterIsModOrAdmin ? deleterClaim.name : undefined,
|
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: {
|
2021-02-11 06:12:41 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.catch((error) => {
|
2020-06-23 19:38:18 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_ABANDON_FAILED,
|
|
|
|
data: error,
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
|
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 {
|
2021-07-15 05:24:37 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
|
|
|
if (!activeChannelClaim) {
|
|
|
|
return dispatch(doToast({ isError: true, message: __('No active channel selected.') }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const signedComment = await channelSignData(activeChannelClaim.claim_id, comment);
|
|
|
|
if (!signedComment) {
|
|
|
|
return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') }));
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:38:18 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_UPDATE_STARTED,
|
|
|
|
});
|
2021-07-15 05:24:37 +02:00
|
|
|
|
|
|
|
return Comments.comment_edit({
|
2020-06-23 19:38:18 +02:00
|
|
|
comment_id: comment_id,
|
|
|
|
comment: comment,
|
2021-07-15 05:24:37 +02:00
|
|
|
signature: signedComment.signature,
|
|
|
|
signing_ts: signedComment.signing_ts,
|
2020-06-23 19:38:18 +02:00
|
|
|
})
|
2021-07-15 05:24:37 +02:00
|
|
|
.then((result: CommentEditResponse) => {
|
2020-06-23 19:38:18 +02:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
2021-03-03 19:50:16 +01:00
|
|
|
.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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2021-05-28 03:06:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-15 05:24:37 +02:00
|
|
|
async function channelSignData(channelClaimId: string, data: string) {
|
|
|
|
let signedObject;
|
|
|
|
|
|
|
|
try {
|
|
|
|
signedObject = await Lbry.channel_sign({
|
|
|
|
channel_id: channelClaimId,
|
|
|
|
hexdata: toHex(data),
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return signedObject;
|
|
|
|
}
|
|
|
|
|
2021-09-11 15:03:57 +02:00
|
|
|
function safeParseURI(uri) {
|
|
|
|
try {
|
|
|
|
return parseURI(uri);
|
|
|
|
} catch {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
// 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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: string,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockerIds: Array<string>, // [] = use all my channels
|
|
|
|
blockLevel: string,
|
2021-11-09 15:43:02 +01:00
|
|
|
timeoutSec: ?number,
|
|
|
|
showLink: boolean = false,
|
|
|
|
offendingCommentId: ?string = undefined
|
2021-05-25 08:17:36 +02:00
|
|
|
) {
|
2021-02-11 06:12:41 +01:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
2021-06-24 16:33:11 +02:00
|
|
|
const ready = selectPrefsReady(state);
|
2021-05-25 08:17:36 +02:00
|
|
|
let blockerChannelClaims = selectMyChannelClaims(state);
|
2021-06-24 16:33:11 +02:00
|
|
|
|
|
|
|
if (!ready) {
|
|
|
|
return dispatch(doAlertWaitingForSync());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockerChannelClaims) {
|
|
|
|
return dispatch(
|
|
|
|
doToast({
|
|
|
|
message: __('Create a channel to change this setting.'),
|
|
|
|
isError: false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-11 15:03:57 +02:00
|
|
|
const { channelName, channelClaimId } = parseURI(commenterUri);
|
|
|
|
const { channelName: creatorName, channelClaimId: creatorId } = safeParseURI(creatorUri);
|
|
|
|
|
2021-05-25 08:17:36 +02:00
|
|
|
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 {
|
2021-09-11 15:03:57 +02:00
|
|
|
// Client wants to block for specific channels only. Ensure we own those channels.
|
2021-05-25 08:17:36 +02:00
|
|
|
blockerChannelClaims = blockerChannelClaims.filter((x) => blockerIds.includes(x.claim_id));
|
|
|
|
}
|
|
|
|
|
2021-03-03 19:50:16 +01:00
|
|
|
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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: creatorUri || undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockLevel: blockLevel,
|
2021-03-03 19:50:16 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2021-06-19 04:33:32 +02:00
|
|
|
const commenterIdForAction = channelClaimId;
|
|
|
|
const commenterNameForAction = channelName;
|
2021-03-03 19:50:16 +01:00
|
|
|
|
|
|
|
let channelSignatures = [];
|
2021-02-11 06:12:41 +01:00
|
|
|
|
2021-03-03 19:50:16 +01:00
|
|
|
const sharedModBlockParams = unblock
|
|
|
|
? {
|
2021-05-25 08:17:36 +02:00
|
|
|
un_blocked_channel_id: commenterIdForAction,
|
|
|
|
un_blocked_channel_name: commenterNameForAction,
|
|
|
|
}
|
2021-03-03 19:50:16 +01:00
|
|
|
: {
|
2021-05-25 08:17:36 +02:00
|
|
|
blocked_channel_id: commenterIdForAction,
|
|
|
|
blocked_channel_name: commenterNameForAction,
|
|
|
|
};
|
2021-03-03 19:50:16 +01:00
|
|
|
|
|
|
|
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)))
|
2021-05-28 03:06:07 +02:00
|
|
|
.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
|
2021-05-28 03:06:07 +02:00
|
|
|
mod_channel_id: signatureData.claim_id,
|
2021-05-25 08:17:36 +02:00
|
|
|
// $FlowFixMe
|
2021-05-28 03:06:07 +02:00
|
|
|
mod_channel_name: signatureData.name,
|
2021-05-25 08:17:36 +02:00
|
|
|
// $FlowFixMe
|
2021-05-28 03:06:07 +02:00
|
|
|
signature: signatureData.signature,
|
2021-05-25 08:17:36 +02:00
|
|
|
// $FlowFixMe
|
2021-05-28 03:06:07 +02:00
|
|
|
signing_ts: signatureData.signing_ts,
|
2021-09-11 15:03:57 +02:00
|
|
|
creator_channel_id: creatorUri ? creatorId : undefined,
|
|
|
|
creator_channel_name: creatorUri ? creatorName : undefined,
|
2021-11-09 15:43:02 +01:00
|
|
|
offending_comment_id: offendingCommentId && !unblock ? offendingCommentId : undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
block_all: unblock ? undefined : blockLevel === BLOCK_LEVEL.ADMIN,
|
|
|
|
global_un_block: unblock ? blockLevel === BLOCK_LEVEL.ADMIN : undefined,
|
2021-05-28 03:06:07 +02:00
|
|
|
...sharedModBlockParams,
|
2021-08-20 03:04:48 +02:00
|
|
|
time_out: unblock ? undefined : timeoutSec,
|
2021-05-28 03:06:07 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: creatorUri || undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockLevel: blockLevel,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-28 03:06:07 +02:00
|
|
|
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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: creatorUri || undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockLevel: blockLevel,
|
|
|
|
},
|
2021-05-28 03:06:07 +02:00
|
|
|
});
|
|
|
|
|
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',
|
|
|
|
})
|
|
|
|
);
|
2021-05-28 03:06:07 +02:00
|
|
|
})
|
|
|
|
.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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: creatorUri || undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockLevel: blockLevel,
|
|
|
|
},
|
2021-05-28 03:06:07 +02:00
|
|
|
});
|
|
|
|
});
|
2021-03-03 19:50:16 +01:00
|
|
|
})
|
|
|
|
.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,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: creatorUri || undefined,
|
2021-05-25 08:17:36 +02:00
|
|
|
blockLevel: blockLevel,
|
|
|
|
},
|
2021-03-03 19:50:16 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:17:36 +02:00
|
|
|
/**
|
|
|
|
* Blocks the commenter for all channels that I own.
|
|
|
|
*
|
2021-11-09 15:43:02 +01:00
|
|
|
* Update: the above it not entirely true now. A blocked channel's comment won't
|
|
|
|
* appear for you anywhere since we now filter the comments at the app-side
|
|
|
|
* before showing it.
|
|
|
|
*
|
2021-05-25 08:17:36 +02:00
|
|
|
* @param commenterUri
|
2021-11-09 15:43:02 +01:00
|
|
|
* @param offendingCommentId
|
2021-09-11 15:03:57 +02:00
|
|
|
* @param timeoutSec
|
2021-05-25 08:17:36 +02:00
|
|
|
* @param showLink
|
|
|
|
* @returns {function(Dispatch): *}
|
|
|
|
*/
|
2021-11-09 15:43:02 +01:00
|
|
|
export function doCommentModBlock(
|
|
|
|
commenterUri: string,
|
|
|
|
offendingCommentId: ?string,
|
|
|
|
timeoutSec: ?number,
|
|
|
|
showLink: boolean = true
|
|
|
|
) {
|
2021-05-25 08:17:36 +02:00
|
|
|
return (dispatch: Dispatch) => {
|
2021-11-09 15:43:02 +01:00
|
|
|
return dispatch(
|
|
|
|
doCommentModToggleBlock(false, commenterUri, '', [], BLOCK_LEVEL.SELF, timeoutSec, showLink, offendingCommentId)
|
|
|
|
);
|
2021-05-25 08:17:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks the commenter using the given channel that has Global privileges.
|
|
|
|
*
|
|
|
|
* @param commenterUri
|
2021-11-09 15:43:02 +01:00
|
|
|
* @param offendingCommentId
|
|
|
|
* @param blockerId Your specific channel ID to block with, or pass 'undefined' to block it for all of your channels.
|
2021-09-11 15:03:57 +02:00
|
|
|
* @param timeoutSec
|
2021-05-25 08:17:36 +02:00
|
|
|
* @returns {function(Dispatch): *}
|
|
|
|
*/
|
2021-11-09 15:43:02 +01:00
|
|
|
export function doCommentModBlockAsAdmin(
|
|
|
|
commenterUri: string,
|
|
|
|
offendingCommentId: ?string,
|
|
|
|
blockerId: ?string,
|
|
|
|
timeoutSec: ?number
|
|
|
|
) {
|
2021-05-25 08:17:36 +02:00
|
|
|
return (dispatch: Dispatch) => {
|
2021-08-12 09:10:44 +02:00
|
|
|
return dispatch(
|
2021-11-09 15:43:02 +01:00
|
|
|
doCommentModToggleBlock(
|
|
|
|
false,
|
|
|
|
commenterUri,
|
|
|
|
'',
|
|
|
|
blockerId ? [blockerId] : [],
|
|
|
|
BLOCK_LEVEL.ADMIN,
|
|
|
|
timeoutSec,
|
|
|
|
false,
|
|
|
|
offendingCommentId
|
|
|
|
)
|
2021-08-12 09:10:44 +02:00
|
|
|
);
|
2021-05-25 08:17:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks the commenter using the given channel that has been granted
|
|
|
|
* moderation rights by the creator.
|
|
|
|
*
|
|
|
|
* @param commenterUri
|
2021-11-09 15:43:02 +01:00
|
|
|
* @param offendingCommentId
|
2021-09-11 15:03:57 +02:00
|
|
|
* @param creatorUri
|
2021-11-09 15:43:02 +01:00
|
|
|
* @param blockerId Your specific channel ID to block with, or pass 'undefined' to block it for all of your channels.
|
2021-09-11 15:03:57 +02:00
|
|
|
* @param timeoutSec
|
2021-05-25 08:17:36 +02:00
|
|
|
* @returns {function(Dispatch): *}
|
|
|
|
*/
|
2021-08-12 09:10:44 +02:00
|
|
|
export function doCommentModBlockAsModerator(
|
|
|
|
commenterUri: string,
|
2021-11-09 15:43:02 +01:00
|
|
|
offendingCommentId: ?string,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: string,
|
2021-11-09 15:43:02 +01:00
|
|
|
blockerId: ?string,
|
|
|
|
timeoutSec: ?number
|
2021-08-12 09:10:44 +02:00
|
|
|
) {
|
2021-03-03 19:50:16 +01:00
|
|
|
return (dispatch: Dispatch) => {
|
2021-05-25 08:17:36 +02:00
|
|
|
return dispatch(
|
2021-08-12 09:10:44 +02:00
|
|
|
doCommentModToggleBlock(
|
|
|
|
false,
|
|
|
|
commenterUri,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri,
|
2021-08-12 09:10:44 +02:00
|
|
|
blockerId ? [blockerId] : [],
|
|
|
|
BLOCK_LEVEL.MODERATOR,
|
2021-11-09 15:43:02 +01:00
|
|
|
timeoutSec,
|
|
|
|
false,
|
|
|
|
offendingCommentId
|
2021-08-12 09:10:44 +02:00
|
|
|
)
|
2021-05-25 08:17:36 +02:00
|
|
|
);
|
2021-03-03 19:50:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-03-03 19:50:16 +01:00
|
|
|
return (dispatch: Dispatch) => {
|
2021-08-12 09:10:44 +02:00
|
|
|
return dispatch(doCommentModToggleBlock(true, commenterUri, '', [], BLOCK_LEVEL.SELF, undefined, showLink));
|
2021-05-25 08:17:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2021-09-11 15:03:57 +02:00
|
|
|
* @param creatorUri
|
2021-05-25 08:17:36 +02:00
|
|
|
* @param blockerId
|
|
|
|
* @returns {function(Dispatch): *}
|
|
|
|
*/
|
2021-09-11 15:03:57 +02:00
|
|
|
export function doCommentModUnBlockAsModerator(commenterUri: string, creatorUri: string, blockerId: string) {
|
2021-05-25 08:17:36 +02:00
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
return dispatch(
|
2021-09-11 15:03:57 +02:00
|
|
|
doCommentModToggleBlock(true, commenterUri, creatorUri, blockerId ? [blockerId] : [], BLOCK_LEVEL.MODERATOR)
|
2021-05-25 08:17:36 +02:00
|
|
|
);
|
2021-03-03 19:50:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doFetchModBlockedList() {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myChannels = selectMyChannelClaims(state);
|
2021-09-24 09:01:51 +02:00
|
|
|
if (!myChannels) {
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_FAILED });
|
|
|
|
return;
|
|
|
|
}
|
2021-03-03 19:50:16 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_STARTED,
|
2021-02-11 06:12:41 +01:00
|
|
|
});
|
2021-03-03 19:50:16 +01:00
|
|
|
|
|
|
|
let channelSignatures = [];
|
|
|
|
|
2021-05-28 03:06:07 +02:00
|
|
|
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 = {};
|
|
|
|
|
2021-08-20 09:18:54 +02:00
|
|
|
// These should just be part of the block list above, but it is
|
|
|
|
// separated for now because there are too many clients that we need
|
|
|
|
// to update.
|
|
|
|
const personalTimeoutMap = {};
|
|
|
|
const adminTimeoutMap = {};
|
|
|
|
const moderatorTimeoutMap = {};
|
|
|
|
|
2021-05-25 08:17:36 +02:00
|
|
|
const blockListsPerChannel = res.map((r) => r.value);
|
|
|
|
blockListsPerChannel
|
2021-05-28 03:06:07 +02:00
|
|
|
.sort((a, b) => {
|
|
|
|
return 1;
|
|
|
|
})
|
2021-05-25 08:17:36 +02:00
|
|
|
.forEach((channelBlockLists) => {
|
2021-08-20 09:18:54 +02:00
|
|
|
const storeList = (fetchedList, blockedList, timeoutMap, blockedByMap) => {
|
2021-05-25 08:17:36 +02:00
|
|
|
if (fetchedList) {
|
|
|
|
fetchedList.forEach((blockedChannel) => {
|
|
|
|
if (blockedChannel.blocked_channel_name) {
|
|
|
|
const channelUri = buildURI({
|
|
|
|
channelName: blockedChannel.blocked_channel_name,
|
|
|
|
claimId: blockedChannel.blocked_channel_id,
|
|
|
|
});
|
|
|
|
|
2021-07-15 22:22:44 +02:00
|
|
|
if (!blockedList.find((blockedChannel) => isURIEqual(blockedChannel.channelUri, channelUri))) {
|
2021-05-25 08:17:36 +02:00
|
|
|
blockedList.push({ channelUri, blockedAt: blockedChannel.blocked_at });
|
2021-08-20 09:18:54 +02:00
|
|
|
|
|
|
|
if (blockedChannel.banned_for) {
|
|
|
|
timeoutMap[channelUri] = {
|
|
|
|
blockedAt: blockedChannel.blocked_at,
|
|
|
|
bannedFor: blockedChannel.banned_for,
|
|
|
|
banRemaining: blockedChannel.ban_remaining,
|
|
|
|
};
|
|
|
|
}
|
2021-05-25 08:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-28 03:06:07 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2021-08-20 09:18:54 +02:00
|
|
|
storeList(blocked_channels, personalBlockList, personalTimeoutMap);
|
|
|
|
storeList(globally_blocked_channels, adminBlockList, adminTimeoutMap);
|
|
|
|
storeList(
|
|
|
|
delegated_blocked_channels,
|
|
|
|
moderatorBlockList,
|
|
|
|
moderatorTimeoutMap,
|
|
|
|
moderatorBlockListDelegatorsMap
|
|
|
|
);
|
2021-03-03 19:50:16 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 03:06:07 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_COMPLETED,
|
|
|
|
data: {
|
2021-05-25 08:17:36 +02:00
|
|
|
personalBlockList:
|
|
|
|
personalBlockList.length > 0
|
|
|
|
? personalBlockList
|
2021-05-28 03:06:07 +02:00
|
|
|
.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,
|
2021-08-20 09:18:54 +02:00
|
|
|
personalTimeoutMap,
|
|
|
|
adminTimeoutMap,
|
|
|
|
moderatorTimeoutMap,
|
2021-05-28 03:06:07 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_FAILED,
|
|
|
|
});
|
|
|
|
});
|
2021-03-03 19:50:16 +01:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_FAILED,
|
|
|
|
});
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
};
|
|
|
|
}
|
2021-03-03 19:50:16 +01:00
|
|
|
|
|
|
|
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);
|
2021-10-17 10:36:14 +02:00
|
|
|
if (channelName && channelClaimId) {
|
|
|
|
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-03-03 19:50:16 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
2021-04-20 10:40:53 +02:00
|
|
|
|
2021-05-25 08:17:36 +02:00
|
|
|
export function doCommentModAddDelegate(
|
|
|
|
modChannelId: string,
|
|
|
|
modChannelName: string,
|
2021-06-19 11:52:17 +02:00
|
|
|
creatorChannelClaim: ChannelClaim,
|
|
|
|
showToast: boolean = false
|
2021-05-25 08:17:36 +02:00
|
|
|
) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2022-01-18 03:18:08 +01:00
|
|
|
const signature = await channelSignData(creatorChannelClaim.claim_id, creatorChannelClaim.name);
|
2021-05-25 08:17:36 +02:00
|
|
|
if (!signature) {
|
2022-01-18 03:46:18 +01:00
|
|
|
doFailedSignatureToast(dispatch, creatorChannelClaim.name);
|
2021-05-25 08:17:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Comments.moderation_add_delegate({
|
|
|
|
mod_channel_id: modChannelId,
|
|
|
|
mod_channel_name: modChannelName,
|
2022-01-18 03:18:08 +01:00
|
|
|
channel_id: creatorChannelClaim.claim_id,
|
|
|
|
channel_name: creatorChannelClaim.name,
|
|
|
|
...signature,
|
2021-06-19 11:52:17 +02:00
|
|
|
})
|
|
|
|
.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) => {
|
2022-01-18 03:18:08 +01:00
|
|
|
dispatch(doToast({ message: err.message, isError: true }));
|
2021-06-19 11:52:17 +02:00
|
|
|
});
|
2021-05-25 08:17:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCommentModRemoveDelegate(
|
|
|
|
modChannelId: string,
|
|
|
|
modChannelName: string,
|
|
|
|
creatorChannelClaim: ChannelClaim
|
|
|
|
) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2022-01-18 03:18:08 +01:00
|
|
|
const signature = await channelSignData(creatorChannelClaim.claim_id, creatorChannelClaim.name);
|
2021-05-25 08:17:36 +02:00
|
|
|
if (!signature) {
|
2022-01-18 03:46:18 +01:00
|
|
|
doFailedSignatureToast(dispatch, creatorChannelClaim.name);
|
2021-05-25 08:17:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Comments.moderation_remove_delegate({
|
|
|
|
mod_channel_id: modChannelId,
|
|
|
|
mod_channel_name: modChannelName,
|
2022-01-18 03:18:08 +01:00
|
|
|
channel_id: creatorChannelClaim.claim_id,
|
|
|
|
channel_name: creatorChannelClaim.name,
|
|
|
|
...signature,
|
2021-05-25 08:17:36 +02:00
|
|
|
}).catch((err) => {
|
2022-01-18 03:18:08 +01:00
|
|
|
dispatch(doToast({ message: err.message, isError: true }));
|
2021-05-25 08:17:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCommentModListDelegates(channelClaim: ChannelClaim) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2022-01-18 03:18:08 +01:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_STARTED });
|
2021-05-25 08:17:36 +02:00
|
|
|
|
2022-01-18 03:18:08 +01:00
|
|
|
const signature = await channelSignData(channelClaim.claim_id, channelClaim.name);
|
2021-05-25 08:17:36 +02:00
|
|
|
if (!signature) {
|
2022-01-18 03:46:18 +01:00
|
|
|
doFailedSignatureToast(dispatch, channelClaim.name);
|
2022-01-18 03:18:08 +01:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_FAILED });
|
2021-05-25 08:17:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Comments.moderation_list_delegates({
|
2022-01-18 03:18:08 +01:00
|
|
|
channel_id: channelClaim.claim_id,
|
|
|
|
channel_name: channelClaim.name,
|
|
|
|
...signature,
|
2021-05-25 08:17:36 +02:00
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_COMPLETED,
|
|
|
|
data: {
|
|
|
|
id: channelClaim.claim_id,
|
|
|
|
delegates: response.Delegates,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2022-01-18 03:46:18 +01:00
|
|
|
dispatch(doToast({ message: err.message, isError: true }));
|
2022-01-18 03:18:08 +01:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_FETCH_MODERATION_DELEGATES_FAILED });
|
2021-05-25 08:17:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doFetchCommentModAmIList(channelClaim: ChannelClaim) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myChannels = selectMyChannelClaims(state);
|
2021-09-24 09:01:51 +02:00
|
|
|
if (!myChannels) {
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_FAILED });
|
|
|
|
return;
|
|
|
|
}
|
2021-05-25 08:17:36 +02:00
|
|
|
|
2021-09-21 16:40:44 +02:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_STARTED });
|
2021-05-25 08:17:36 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
2021-09-21 16:40:44 +02:00
|
|
|
.then((results) => {
|
2021-05-25 08:17:36 +02:00
|
|
|
const delegatorsById = {};
|
|
|
|
|
2021-09-21 16:40:44 +02:00
|
|
|
results.forEach((result, index) => {
|
|
|
|
if (result.status === PROMISE_FULFILLED) {
|
|
|
|
const value = result.value;
|
|
|
|
delegatorsById[value.channel_id] = {
|
2021-05-25 08:17:36 +02:00
|
|
|
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) => {
|
2021-09-21 16:40:44 +02:00
|
|
|
devToast(dispatch, `AmI: ${err}`);
|
|
|
|
dispatch({ type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_FAILED });
|
2021-05-25 08:17:36 +02:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-09-21 16:40:44 +02:00
|
|
|
dispatch({ type: ACTIONS.COMMENT_MODERATION_AM_I_LIST_FAILED });
|
2021-05-25 08:17:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:53:36 +02:00
|
|
|
export const doFetchCreatorSettings = (channelId: string) => {
|
2021-04-20 10:40:53 +02:00
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myChannels = selectMyChannelClaims(state);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_FETCH_SETTINGS_STARTED,
|
|
|
|
});
|
|
|
|
|
2021-07-29 16:53:36 +02:00
|
|
|
let signedName;
|
2021-04-20 10:40:53 +02:00
|
|
|
|
2021-07-29 16:53:36 +02:00
|
|
|
if (myChannels) {
|
|
|
|
const index = myChannels.findIndex((myChannel) => myChannel.claim_id === channelId);
|
|
|
|
if (index > -1) {
|
|
|
|
signedName = await channelSignName(channelId, myChannels[index].name);
|
2021-04-20 10:40:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:53:36 +02:00
|
|
|
const cmd = signedName ? Comments.setting_list : Comments.setting_get;
|
2021-04-20 10:40:53 +02:00
|
|
|
|
2021-07-29 16:53:36 +02:00
|
|
|
return cmd({
|
|
|
|
channel_id: channelId,
|
|
|
|
channel_name: (signedName && signedName.name) || undefined,
|
|
|
|
signature: (signedName && signedName.signature) || undefined,
|
|
|
|
signing_ts: (signedName && signedName.signing_ts) || undefined,
|
|
|
|
})
|
|
|
|
.then((response: SettingsResponse) => {
|
2021-04-20 10:40:53 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_FETCH_SETTINGS_COMPLETED,
|
2021-07-29 16:53:36 +02:00
|
|
|
data: {
|
|
|
|
channelId: channelId,
|
|
|
|
settings: response,
|
|
|
|
partialUpdate: !signedName,
|
|
|
|
},
|
2021-04-20 10:40:53 +02:00
|
|
|
});
|
|
|
|
})
|
2021-05-25 04:58:42 +02:00
|
|
|
.catch((err) => {
|
|
|
|
if (err.message === 'validation is disallowed for non controlling channels') {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_FETCH_SETTINGS_COMPLETED,
|
2021-07-29 16:53:36 +02:00
|
|
|
data: {
|
|
|
|
channelId: channelId,
|
|
|
|
settings: null,
|
|
|
|
partialUpdate: !signedName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
devToast(dispatch, `Creator: ${err}`);
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_FETCH_SETTINGS_FAILED,
|
2021-05-25 04:58:42 +02:00
|
|
|
});
|
|
|
|
}
|
2021-04-20 10:40:53 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates creator settings, except for 'Words', which will be handled by
|
|
|
|
* 'doCommentWords, doCommentBlockWords, etc.'
|
|
|
|
*
|
|
|
|
* @param channelClaim
|
|
|
|
* @param settings
|
2021-07-29 16:53:36 +02:00
|
|
|
* @returns {function(Dispatch, GetState): any}
|
2021-04-20 10:40:53 +02:00
|
|
|
*/
|
|
|
|
export const doUpdateCreatorSettings = (channelClaim: ChannelClaim, settings: PerChannelSettings) => {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
2021-07-16 10:11:02 +02:00
|
|
|
const channelSignature = await channelSignName(channelClaim.claim_id, channelClaim.name);
|
2021-04-20 10:40:53 +02:00
|
|
|
if (!channelSignature) {
|
2021-07-16 10:11:02 +02:00
|
|
|
devToast(dispatch, 'doUpdateCreatorSettings: failed to sign channel name');
|
2021-04-20 10:40:53 +02:00
|
|
|
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) => {
|
2021-07-16 10:11:02 +02:00
|
|
|
dispatch(doToast({ message: err.message, isError: true }));
|
2021-04-20 10:40:53 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|