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-02-09 17:05:56 +01:00
|
|
|
import { Lbry, selectClaimsByUri } from 'lbry-redux';
|
2020-10-20 15:29:49 +02:00
|
|
|
import { doToast, doSeeNotifications } from 'redux/actions/notifications';
|
2020-09-29 20:45:28 +02:00
|
|
|
import {
|
|
|
|
makeSelectCommentIdsForUri,
|
|
|
|
makeSelectMyReactionsForComment,
|
|
|
|
makeSelectOthersReactionsForComment,
|
2020-09-30 17:59:05 +02:00
|
|
|
selectPendingCommentReacts,
|
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';
|
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;
|
|
|
|
|
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-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,
|
|
|
|
})
|
|
|
|
.then((result: CommentListResponse) => {
|
|
|
|
const { items: comments } = result;
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_LIST_COMPLETED,
|
|
|
|
data: {
|
|
|
|
comments,
|
|
|
|
claimId: claimId,
|
|
|
|
uri: uri,
|
|
|
|
},
|
|
|
|
});
|
2020-10-20 05:20:38 +02:00
|
|
|
return result;
|
2020-06-23 19:38:18 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_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();
|
2021-02-09 17:05:56 +01:00
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
2020-09-29 16:10:23 +02:00
|
|
|
const commentIds = uri ? makeSelectCommentIdsForUri(uri)(state) : [commentId];
|
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
|
|
|
|
2020-09-30 17:59:05 +02:00
|
|
|
const params: { comment_ids: string, channel_name?: string, channel_id?: string } = {
|
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) {
|
|
|
|
params['channel_name'] = activeChannelClaim.name;
|
|
|
|
params['channel_id'] = activeChannelClaim.claim_id;
|
2020-09-30 17:59:05 +02:00
|
|
|
}
|
2020-10-06 21:35:13 +02:00
|
|
|
|
|
|
|
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: {
|
2020-09-30 17:59:05 +02:00
|
|
|
myReactions: myReactions || {},
|
2020-09-29 16:10:23 +02:00
|
|
|
othersReactions,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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();
|
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
|
|
|
|
2020-09-29 20:45:28 +02:00
|
|
|
let myReacts = makeSelectMyReactionsForComment(commentId)(state);
|
|
|
|
const othersReacts = makeSelectOthersReactionsForComment(commentId)(state);
|
2020-09-29 16:10:23 +02:00
|
|
|
const params: CommentReactParams = {
|
|
|
|
comment_ids: commentId,
|
2021-02-09 17:05:56 +01:00
|
|
|
channel_name: activeChannelClaim.name,
|
|
|
|
channel_id: activeChannelClaim.claim_id,
|
2020-09-29 16:10:23 +02:00
|
|
|
react_type: type,
|
|
|
|
};
|
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
|
|
|
}
|
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: {
|
|
|
|
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,
|
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
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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
|
|
|
|
.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
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
export function doCommentCreate(comment: string = '', claim_id: string = '', parent_id?: string, uri: string) {
|
2020-11-16 20:09:00 +01:00
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2020-06-23 19:38:18 +02:00
|
|
|
const state = getState();
|
2021-02-09 17:05:56 +01:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2020-10-20 15:29:49 +02:00
|
|
|
if (parent_id) {
|
|
|
|
const notification = makeSelectNotificationForCommentId(parent_id)(state);
|
|
|
|
if (notification && !notification.is_seen) {
|
|
|
|
dispatch(doSeeNotifications([notification.id]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:38:18 +02:00
|
|
|
return Lbry.comment_create({
|
|
|
|
comment: comment,
|
|
|
|
claim_id: claim_id,
|
2021-02-09 17:05:56 +01:00
|
|
|
channel_id: activeChannelClaim.claim_id,
|
2020-06-23 19:38:18 +02:00
|
|
|
parent_id: parent_id,
|
|
|
|
})
|
|
|
|
.then((result: CommentCreateResponse) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_CREATE_COMPLETED,
|
|
|
|
data: {
|
2020-09-09 20:53:31 +02:00
|
|
|
uri,
|
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 => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_CREATE_FAILED,
|
|
|
|
data: error,
|
|
|
|
});
|
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
message: 'Unable to create comment, please try again later.',
|
|
|
|
isError: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_PIN_STARTED,
|
|
|
|
});
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
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 => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_PIN_FAILED,
|
|
|
|
data: error,
|
|
|
|
});
|
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
message: 'Unable to pin this comment, please try again later.',
|
|
|
|
isError: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
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,
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
|
|
|
|
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: {
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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 {
|
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 => {
|
|
|
|
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
|
|
|
|
|
|
|
// Hides a users comments from all creator's claims and prevent them from commenting in the future
|
|
|
|
export function doCommentModBlock(commentAuthor: string) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const claim = selectClaimsByUri(state)[commentAuthor];
|
|
|
|
|
|
|
|
if (!claim) {
|
|
|
|
console.error("Can't find claim to block"); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const creatorIdToBan = claim ? claim.claim_id : null;
|
|
|
|
const creatorNameToBan = claim ? claim.name : null;
|
|
|
|
const activeChannelClaim = selectActiveChannelClaim(state);
|
|
|
|
|
|
|
|
let channelSignature = {};
|
|
|
|
if (activeChannelClaim) {
|
|
|
|
try {
|
|
|
|
channelSignature = await Lbry.channel_sign({
|
|
|
|
channel_id: activeChannelClaim.claim_id,
|
|
|
|
hexdata: toHex(activeChannelClaim.name),
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Comments.moderation_block({
|
|
|
|
mod_channel_id: activeChannelClaim.claim_id,
|
|
|
|
mod_channel_name: activeChannelClaim.name,
|
|
|
|
signature: channelSignature.signature,
|
|
|
|
signing_ts: channelSignature.signing_ts,
|
|
|
|
banned_channel_id: creatorIdToBan,
|
|
|
|
banned_channel_name: creatorNameToBan,
|
|
|
|
delete_all: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|