Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions workers/paymaster/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const paidUntil = new Date(currentDate.getTime());

paidUntil.setMonth(paidUntil.getMonth() + 3);
Expand Down Expand Up @@ -590,7 +590,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const paidUntil = new Date(currentDate.getTime());

paidUntil.setDate(paidUntil.getDate() + 1);
Expand Down Expand Up @@ -647,7 +647,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const lastChargeDate = new Date(currentDate.getTime());

lastChargeDate.setMonth(lastChargeDate.getMonth() - 1); // Set last charge date to 1 month ago
Expand Down
13 changes: 11 additions & 2 deletions workers/telegram/src/templates/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { EventsTemplateVariables, TemplateEventData } from 'hawk-worker-sender/types/template-variables';

/**
* Escape characters that break Telegram legacy Markdown parsing
*
* @param text - raw text
*/
export function escapeMarkdown(text: string): string {
return text.replace(/[_*`[]/g, '\\$&');
}

/**
* Return tpl with data substitutions
*
Expand All @@ -17,10 +26,10 @@ export default function render(tplData: EventsTemplateVariables): string {
location = '\n' + '```\n' + location + '\n```'; // markdown code block
}

return ''.concat(event.payload.title,
return ''.concat(escapeMarkdown(event.payload.title),
Comment thread
Kuchizu marked this conversation as resolved.
Resolved
'\n',
location || '',
'\n',
`[View details](${eventURL}) `, `| *${tplData.project.name}*`, ` | ${eventInfo.newCount} new (${eventInfo.event.totalCount} total)`
`[View details](${eventURL}) `, `| ${escapeMarkdown(tplData.project.name)}`, ` | ${eventInfo.newCount} new (${eventInfo.event.totalCount} total)`
);
}
5 changes: 3 additions & 2 deletions workers/telegram/src/templates/several-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EventsTemplateVariables } from 'hawk-worker-sender/types/template-variables';
import { escapeMarkdown } from './event';

/**
* Return tpl with data substitutions
Expand All @@ -10,10 +11,10 @@ export default function render(tplData: EventsTemplateVariables): string {
let message = tplData.events.length + ' new events\n\n';

tplData.events.forEach(({ event, newCount }) => {
message += `(${newCount}) ${event.payload.title} \n`;
message += `(${newCount}) ${escapeMarkdown(event.payload.title)} \n`;
});

message += `\n[View all events](${projectUrl}) | *${tplData.project.name}*`;
message += `\n[View all events](${projectUrl}) | ${escapeMarkdown(tplData.project.name)}`;

return message;
}
7 changes: 6 additions & 1 deletion workers/telegram/tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventNotification, SeveralEventsNotification } from 'hawk-worker-sender
import { DecodedGroupedEvent, ProjectDBScheme } from '@hawk.so/types';
import TelegramProvider from 'hawk-worker-telegram/src/provider';
import templates from '../src/templates';
import EventTpl from '../src/templates/event';
import EventTpl, { escapeMarkdown } from '../src/templates/event';
import { ObjectId } from 'mongodb';

/**
Expand All @@ -13,6 +13,11 @@ describe('TelegramProvider', () => {
* Check templates rendering
*/
describe('templates', () => {
it('should escape Markdown entities in event titles', () => {
expect(escapeMarkdown('No such file: /home/market_static/*.csv [`x`]'))
.toBe('No such file: /home/market\\_static/\\*.csv \\[\\`x\\`]');
});

/**
* Check that rendering of a single event message works without errors
*/
Expand Down
Loading