Skip to content

Commit

Permalink
DryRun second try (#3216)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Add "amount" for LBC tips in comment create

* Fix dryRun hyperchat issue

* Fix Send-button flicker

* Minor adjustment

---------

Co-authored-by: miko <[email protected]>
  • Loading branch information
keikari and miko authored Jan 26, 2025
1 parent 3d41d9a commit 8e8b281
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
2 changes: 2 additions & 0 deletions flow-typed/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ declare type CommentSubmitParams = {
environment?: ?string,
sticker: boolean,
is_protected?: boolean,
amount?: number,
dry_run?: boolean,
};

// ****************************************************************************
Expand Down
29 changes: 26 additions & 3 deletions ui/component/commentCreate/view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -527,6 +547,9 @@ export function CommentCreate(props: Props) {
})
.catch(() => {
setSubmitting(false);
if (dryRun) {
return;
}
setCommentFailure(true);

if (channelClaimId) {
Expand Down
26 changes: 18 additions & 8 deletions ui/redux/actions/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 8e8b281

Please sign in to comment.