From 322e1bfd339f8ac46238ed1370852db2630edfc6 Mon Sep 17 00:00:00 2001 From: whekin Date: Tue, 24 Mar 2026 04:32:15 +0400 Subject: [PATCH] fix(bot): clear stale reminder draft buttons --- apps/bot/src/ad-hoc-notifications.test.ts | 16 +++++++--- apps/bot/src/ad-hoc-notifications.ts | 38 +++++++++++++++++------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/apps/bot/src/ad-hoc-notifications.test.ts b/apps/bot/src/ad-hoc-notifications.test.ts index 69465a5..314ed1e 100644 --- a/apps/bot/src/ad-hoc-notifications.test.ts +++ b/apps/bot/src/ad-hoc-notifications.test.ts @@ -378,11 +378,19 @@ describe('registerAdHocNotifications', () => { expect(calls[1]?.payload).toMatchObject({ text: `Окей, ${updatedWhen} напомню.` }) + expect(calls[2]?.method).toBe('editMessageReplyMarkup') + expect(calls[2]?.payload).toMatchObject({ + chat_id: -10012345, + message_id: 1, + reply_markup: { + inline_keyboard: [] + } + }) await bot.handleUpdate(reminderMessageUpdate('А вообще, я не буду кушать') as never) expect(draftEditCalls).toBe(2) - expect(calls[2]?.payload).toMatchObject({ + expect(calls[3]?.payload).toMatchObject({ text: 'Окей, тогда не напоминаю.' }) expect(await promptRepository.getPendingAction('-10012345', '10002')).toBeNull() @@ -403,9 +411,9 @@ describe('registerAdHocNotifications', () => { reminderCallbackUpdate(`adhocnotif:confirm:${renewedProposalId}`) as never ) - expect(calls[4]?.method).toBe('answerCallbackQuery') - expect(calls[5]?.method).toBe('editMessageText') - expect(calls[5]?.payload).toMatchObject({ + expect(calls[5]?.method).toBe('answerCallbackQuery') + expect(calls[6]?.method).toBe('editMessageText') + expect(calls[6]?.payload).toMatchObject({ text: `Окей, ${initialWhen} напомню.` }) diff --git a/apps/bot/src/ad-hoc-notifications.ts b/apps/bot/src/ad-hoc-notifications.ts index d8c6e2f..8701522 100644 --- a/apps/bot/src/ad-hoc-notifications.ts +++ b/apps/bot/src/ad-hoc-notifications.ts @@ -45,6 +45,7 @@ type NotificationDraftPayload = | { stage: 'confirm' proposalId: string + confirmationMessageId: number | null householdId: string threadId: string creatorMemberId: string @@ -418,10 +419,10 @@ async function replyInTopic( options?: { parseMode?: 'HTML' } -): Promise { +): Promise { const message = ctx.msg if (!ctx.chat || !message) { - return + return null } const threadId = @@ -429,7 +430,7 @@ async function replyInTopic( ? message.message_thread_id : undefined - await ctx.api.sendMessage(ctx.chat.id, text, { + const sentMessage = await ctx.api.sendMessage(ctx.chat.id, text, { ...(threadId !== undefined ? { message_thread_id: threadId @@ -449,6 +450,8 @@ async function replyInTopic( } : {}) }) + + return sentMessage.message_id } async function resolveReminderTopicContext( @@ -593,7 +596,8 @@ export function registerAdHocNotifications(options: { async function showDraftConfirmation( ctx: Context, - draft: Extract + draft: Extract, + previousConfirmationMessageId?: number | null ) { const reminderContext = await resolveReminderTopicContext( ctx, @@ -603,7 +607,7 @@ export function registerAdHocNotifications(options: { return } - await replyInTopic( + const confirmationMessageId = await replyInTopic( ctx, notificationSummaryText({ locale: reminderContext.locale, @@ -612,6 +616,23 @@ export function registerAdHocNotifications(options: { }), notificationDraftReplyMarkup(reminderContext.locale, draft, reminderContext.members) ) + + if ( + previousConfirmationMessageId && + ctx.chat && + previousConfirmationMessageId !== confirmationMessageId + ) { + await ctx.api.editMessageReplyMarkup(ctx.chat.id, previousConfirmationMessageId, { + reply_markup: { + inline_keyboard: [] + } + }) + } + + await saveDraft(options.promptRepository, ctx, { + ...draft, + confirmationMessageId + }) } async function refreshConfirmationMessage( @@ -791,12 +812,12 @@ export function registerAdHocNotifications(options: { const confirmPayload: Extract = { ...existingDraft, stage: 'confirm', + confirmationMessageId: null, renderedNotificationText, scheduledForIso: schedule.scheduledFor!.toString(), timePrecision: schedule.timePrecision!, viewMode: 'compact' } - await saveDraft(options.promptRepository, ctx, confirmPayload) await showDraftConfirmation(ctx, confirmPayload) return } @@ -932,8 +953,7 @@ export function registerAdHocNotifications(options: { viewMode: 'compact' } - await saveDraft(options.promptRepository, ctx, nextPayload) - await showDraftConfirmation(ctx, nextPayload) + await showDraftConfirmation(ctx, nextPayload, existingDraft.confirmationMessageId) return } @@ -1043,6 +1063,7 @@ export function registerAdHocNotifications(options: { const draft: Extract = { stage: 'confirm', proposalId: createProposalId(), + confirmationMessageId: null, householdId: reminderContext.householdId, threadId: reminderContext.threadId, creatorMemberId: reminderContext.member.id, @@ -1058,7 +1079,6 @@ export function registerAdHocNotifications(options: { viewMode: 'compact' } - await saveDraft(options.promptRepository, ctx, draft) await showDraftConfirmation(ctx, draft) })