Skip to content

Commit

Permalink
Update the MD sanitize with implemented method and fix the issue in t…
Browse files Browse the repository at this point in the history
…he translation file
  • Loading branch information
Daniel Candia Flores committed Sep 9, 2024
1 parent ad20cfc commit e6b0b26
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
19 changes: 6 additions & 13 deletions bot/modules/dispute/messages.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
const { getDisputeChannel, getDetailedOrder } = require('../../../util');
const { getDisputeChannel, getDetailedOrder, sanitizeMD } = require('../../../util');
const { logger } = require('../../../logger');

const escapeMarkdown = function(text) {
const characterToEscape = '_';
const regex = new RegExp(characterToEscape, 'g');

return text.replace(regex, '\\_');
};

exports.beginDispute = async (ctx, initiator, order, buyer, seller) => {
try {
let initiatorUser = buyer;
Expand Down Expand Up @@ -94,10 +87,10 @@ exports.disputeData = async (
}

const detailedOrder = getDetailedOrder(ctx.i18n, order, buyer, seller);

// Fix Issue 543: Escape underscores in usernames
const escapedInitiatorUsername = escapeMarkdown(initiatorUser.username);
const escapedCounterPartyUsername = escapeMarkdown(counterPartyUser.username);
const escapedInitiatorUsername = sanitizeMD(initiatorUser.username);
const escapedCounterPartyUsername = sanitizeMD(counterPartyUser.username);

await ctx.telegram.sendMessage(
solver.tg_id,
Expand All @@ -122,14 +115,14 @@ exports.disputeData = async (
await ctx.telegram.sendMessage(
buyer.tg_id,
ctx.i18n.t('dispute_solver', {
solver: solver.username,
solver: sanitizeMD(solver.username),
token: order.buyer_dispute_token,
})
);
await ctx.telegram.sendMessage(
seller.tg_id,
ctx.i18n.t('dispute_solver', {
solver: solver.username,
solver: sanitizeMD(solver.username),
token: order.seller_dispute_token,
})
);
Expand Down
4 changes: 2 additions & 2 deletions locales/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ dispute_started_channel: |
Seller Token: ${sellerToken}
Buyer Token: ${buyerToken}
@${buyer.username} ya tiene ${buyerDisputes} disputas
@${seller.username} ya tiene ${sellerDisputes} disputas
@${initiatorUser.username} ya tiene ${buyerDisputes} disputas
@${counterPartyUser.username} ya tiene ${sellerDisputes} disputas
you_started: '🥴 Has iniciado una disputa en tu orden con Id: ${orderId}.'
counterpart_started: '🥴 Tu contraparte ha iniciado una disputa en tu orden con Id: ${orderId}.'
dispute_started: '${who} Un solver te atenderá pronto, cuando él/la solver sea asignado a tu disputa el bot te dirá su username, solo él/ella podrá atenderte. Puedes escribirle directamente, pero si él/ella te contacta primero, debes pedirle que te diga cuál es el token de tu disputa, tu token es: ${token}.'
Expand Down
37 changes: 33 additions & 4 deletions tests/bot/modules/dispute/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ const mockI18n = {
t: sinon.stub((key, params) => {
switch (key) {
case 'dispute_started_channel':
return `Dispute started by ${params.initiatorUser.username} against ${params.counterPartyUser.username} with tokens ${params.sellerToken} and ${params.buyerToken}`;
return `User ${params.type} @${params.initiatorUser.username} TG ID: ${params.initiatorTgId}
has started a dispute with @${params.counterPartyUser.username} TG ID: ${params.counterPartyUserTgId} for the order
${params.detailedOrder}
Seller Token: ${params.sellerToken}
Buyer Token: ${params.buyerToken}
@${params.initiatorUser.username} has been involved in ${params.buyerDisputes} disputes
@${params.counterPartyUser.username} has been involved in ${params.sellerDisputes} disputes`;
case 'seller':
return 'seller';
case 'buyer':
Expand All @@ -40,7 +49,8 @@ const mockOrder = {
};

const mockBuyerUnderscore = { username: 'buyer_user', tg_id: '246802' };
const mockBuyerNormal = { username: 'buyer-user', tg_id: '246802' };
const mockBuyerNormal = { username: 'buyeruser', tg_id: '246802' };
const mockBuyerSpecial = { username: 'buyer-user', tg_id: '246802' };
const mockSeller = { username: 'seller_user', tg_id: '567890' };
const mockSolver = { username: 'solver', tg_id: '123456' };

Expand All @@ -49,7 +59,7 @@ describe('disputeData', () => {
sinon.restore();
});

it('should send a message with escaped underscores in usernames', async () => {
it('should send a message with escaped underscores in username', async () => {
await disputeData(
mockCtx,
mockBuyerUnderscore,
Expand All @@ -68,6 +78,25 @@ describe('disputeData', () => {
));
});

it('should send a message with another escaped character in username', async () => {
await disputeData(
mockCtx,
mockBuyerSpecial,
mockSeller,
mockOrder,
'buyer',
mockSolver,
5,
3
);

assert.isTrue(mockTelegram.sendMessage.calledWith(
mockSolver.tg_id,
sinon.match(/buyer\\-user/),
sinon.match({ parse_mode: 'MarkdownV2' })
));
});

it('should send a message without underscores in usernames', async () => {
await disputeData(
mockCtx,
Expand All @@ -82,7 +111,7 @@ describe('disputeData', () => {

assert.isTrue(mockTelegram.sendMessage.calledWith(
mockSolver.tg_id,
sinon.match('buyer-user'),
sinon.match('buyeruser'),
sinon.match({ parse_mode: 'MarkdownV2' })
));
});
Expand Down

0 comments on commit e6b0b26

Please sign in to comment.