From 8e8b28103f913037386b7a47213f5f02868bad3b Mon Sep 17 00:00:00 2001 From: miko <34790748+keikari@users.noreply.github.com> Date: Sun, 26 Jan 2025 08:48:45 +0200 Subject: [PATCH] DryRun second try (#3216) * DryRun the comment creation before submitting the tip (#3214) * DryRun comment creation before submitting the tip * Include commentron to the dryRun * Remove lint change --------- Co-authored-by: miko * Add "amount" for LBC tips in comment create * Fix dryRun hyperchat issue * Fix Send-button flicker * Minor adjustment --------- Co-authored-by: miko --- flow-typed/Comment.js | 2 ++ ui/component/commentCreate/view.jsx | 29 ++++++++++++++++++++++++++--- ui/redux/actions/comments.js | 26 ++++++++++++++++++-------- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/flow-typed/Comment.js b/flow-typed/Comment.js index 1999f82716..36c970fac9 100644 --- a/flow-typed/Comment.js +++ b/flow-typed/Comment.js @@ -33,6 +33,8 @@ declare type CommentSubmitParams = { environment?: ?string, sticker: boolean, is_protected?: boolean, + amount?: number, + dry_run?: boolean, }; // **************************************************************************** diff --git a/ui/component/commentCreate/view.jsx b/ui/component/commentCreate/view.jsx index faffde4fcc..bd14c5e7d6 100644 --- a/ui/component/commentCreate/view.jsx +++ b/ui/component/commentCreate/view.jsx @@ -398,7 +398,14 @@ export function CommentCreate(props: Props) { return; } - doSubmitTip(); + // DryRun comment creation before submitting the tip + handleCreateComment(undefined, undefined, undefined, true).then((res) => { + if (res !== 'success') { + setSubmitting(false); + return; + } + doSubmitTip(); + }); }) .catch(() => { doToast({ @@ -480,7 +487,7 @@ export function CommentCreate(props: Props) { * @param {string} [environment] Optional environment for Stripe (test|live) * @param {boolean} [is_protected] Whether are not the content has a protected chat */ - async function handleCreateComment(txid, payment_intent_id, environment) { + async function handleCreateComment(txid, payment_intent_id, environment, dryRun = false) { if (isSubmitting || disableInput || !claimId) return; // do another creator settings fetch here to make sure that on submit, the setting did not change @@ -500,7 +507,15 @@ export function CommentCreate(props: Props) { const stickerValue = selectedSticker && buildValidSticker(selectedSticker.name); - doCommentCreate(uri, isLivestream, { + if (dryRun) { + if (activeTab === TAB_LBC) { + txid = 'dummy_txid'; + } else if (activeTab === TAB_FIAT) { + payment_intent_id = 'dummy_payment_intent_id'; + } + } + + return doCommentCreate(uri, isLivestream, { comment: stickerValue || commentValue, claim_id: claimId, parent_id: parentId, @@ -509,9 +524,14 @@ export function CommentCreate(props: Props) { environment, sticker: !!stickerValue, is_protected: Boolean(isLivestreamChatMembersOnly || areCommentsMembersOnly), + amount: activeTab === TAB_LBC ? tipAmount * 100000000 : undefined, + dry_run: dryRun, }) .then((res) => { setSubmitting(false); + if (dryRun) { + return res.comment_id ? 'success' : 'fail'; + } if (setQuickReply) setQuickReply(res); if (res && res.signature) { @@ -527,6 +547,9 @@ export function CommentCreate(props: Props) { }) .catch(() => { setSubmitting(false); + if (dryRun) { + return; + } setCommentFailure(true); if (channelClaimId) { diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index f017977bb2..2f371d5b25 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -704,7 +704,7 @@ export function doCommentReact(commentId: string, type: string) { export function doCommentCreate(uri: string, livestream: boolean, params: CommentSubmitParams) { return async (dispatch: Dispatch, getState: GetState) => { - const { comment, claim_id, parent_id, txid, payment_intent_id, sticker, is_protected } = params; + const { comment, claim_id, parent_id, txid, payment_intent_id, sticker, is_protected, amount, dry_run } = params; const state = getState(); const activeChannelClaim = selectActiveChannelClaim(state); @@ -802,18 +802,21 @@ export function doCommentCreate(uri: string, livestream: boolean, params: Commen } } - dispatch({ type: ACTIONS.COMMENT_CREATE_STARTED }); + const signatureData = await ChannelSign.sign(activeChannelClaim.claim_id, comment, false); + if (!signatureData) { + dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') })); + return; + } + + if (!dry_run) { + dispatch({ type: ACTIONS.COMMENT_CREATE_STARTED }); + } const notification = parent_id && makeSelectNotificationForCommentId(parent_id)(state); if (notification && !notification.is_seen) { dispatch(doSeeNotifications([notification.id])); } - const signatureData = await ChannelSign.sign(activeChannelClaim.claim_id, comment, false); - if (!signatureData) { - return dispatch(doToast({ isError: true, message: __('Unable to verify your channel. Please try again.') })); - } - return Comments.comment_create({ comment: comment, claim_id: claim_id, @@ -826,10 +829,15 @@ export function doCommentCreate(uri: string, livestream: boolean, params: Commen mentioned_channels: mentionedChannels, environment: stripeEnvironment, is_protected: is_protected || undefined, + amount: amount, + dry_run: dry_run, ...(txid ? { support_tx_id: txid } : {}), ...(payment_intent_id ? { payment_intent_id } : {}), }) .then((result: CommentCreateResponse) => { + if (dry_run) { + return result; + } const previousCommenterChannel = { claim_id: activeChannelClaim.claim_id, name: activeChannelClaim.name, @@ -860,7 +868,9 @@ export function doCommentCreate(uri: string, livestream: boolean, params: Commen return result; }) .catch((error) => { - dispatch({ type: ACTIONS.COMMENT_CREATE_FAILED, data: error }); + if (!dry_run) { + dispatch({ type: ACTIONS.COMMENT_CREATE_FAILED, data: error }); + } dispatchToast(dispatch, resolveApiMessage(error.message)); return Promise.reject(error); });