Skip to content

Commit

Permalink
Merge pull request #37 from beabee-communityrm/fix/abort-controller
Browse files Browse the repository at this point in the history
fix(abort-controller): Fix abort signal handling
  • Loading branch information
JumpLink authored May 16, 2024
2 parents 69b70de + 17bc225 commit d863996
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 66 deletions.
2 changes: 1 addition & 1 deletion beabee-common
3 changes: 2 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions telegram-bot/commands/list.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@ export class ListCommand extends BaseCommand {

// Handle the /list command
public async action(ctx: AppContext, force = false) {
const session = await ctx.session;
const successful = await this.checkAction(ctx, force);
if (!force && !successful) {
return false;
}

const signal = this.stateMachine.setSessionState(
session,
// await this.keyboard.removeLastInlineKeyboard(ctx);

const signal = await this.stateMachine.setSessionState(
ctx,
ChatState.CalloutList,
true,
);

await this.keyboard.removeLastInlineKeyboard(ctx);
if (!signal) {
throw new Error("The AbortSignal is required!");
}

const callouts = await this.callout.list();
const render = this.calloutRenderer.listItems(callouts);
await this.communication.sendAndReceiveAll(ctx, render, signal);
return successful;
return signal?.aborted ? false : successful;
}
}
24 changes: 13 additions & 11 deletions telegram-bot/commands/reset.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class ResetCommand extends BaseCommand {
// Always allow this command to reset the state even if an error occurs, so we not use `this.checkAction` here
const session = await ctx.session;
const abortController = session._data.abortController;
let successful = true;

if (abortController) {
// Already cancelled
Expand Down Expand Up @@ -74,19 +75,20 @@ export class ResetCommand extends BaseCommand {
await this.messageRenderer.continueList(),
);

const successful = await this.listCommand.action(ctx, true);
return successful;
}

// Otherwise show continue help and set the state to start
// Reset the state and unsubscribe all events
// successful = successful && await this.stateMachine.resetSessionState(ctx);

const successful = await this.stateMachine.resetSessionState(ctx);
successful = successful && await this.listCommand.action(ctx, true);
} else {
// Otherwise show continue help
await this.communication.send(
ctx,
await this.messageRenderer.continueHelp(session.state),
);

// Use this after the reset to show the right help message for the current state
await this.communication.send(
ctx,
await this.messageRenderer.continueHelp(session.state),
);
// Reset the state and unsubscribe all events
await this.stateMachine.setSessionState(ctx, ChatState.Start, true);
}

return successful;
}
Expand Down
10 changes: 7 additions & 3 deletions telegram-bot/commands/show.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ export class ShowCommand extends BaseCommand {
}

try {
const session = await ctx.session;
// const session = await ctx.session;
const callout = await this.callout.get(slug);
const render = await this.calloutRenderer.calloutDetails(callout);

const signal = this.stateMachine.setSessionState(
session,
const signal = await this.stateMachine.setSessionState(
ctx,
ChatState.CalloutDetails,
true,
);

if (!signal) {
throw new Error("The AbortSignal is required!");
}

await this.communication.sendAndReceiveAll(ctx, render, signal);
} catch (error) {
console.error("Error sending callout", error);
Expand Down
2 changes: 1 addition & 1 deletion telegram-bot/commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class StartCommand extends BaseCommand {

// Otherwise show initial help and set the state to start

this.stateMachine.setSessionState(session, ChatState.Start, false);
await this.stateMachine.setSessionState(ctx, ChatState.Start, false);

await this.communication.send(
ctx,
Expand Down
38 changes: 37 additions & 1 deletion telegram-bot/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion telegram-bot/deps/grammy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://deno.land/x/grammy_parse_mode@1.9.0/mod.ts";
export * from "https://deno.land/x/grammy_parse_mode@1.10.0/mod.ts";
export type * from "https://deno.land/x/[email protected]/types.deno.ts";
export type * from "https://deno.land/x/[email protected]/message.ts";
12 changes: 8 additions & 4 deletions telegram-bot/event-managers/callout-response.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ export class CalloutResponseEventManager extends BaseEventManager {
ctx,
);

const abortSignal = this.stateMachine.setSessionState(
session,
const abortSignal = await this.stateMachine.setSessionState(
ctx,
ChatState.CalloutAnswer,
true,
);

if (!abortSignal) {
throw new Error("The AbortSignal is required!");
}

// Wait for all responses
const responses = await this.communication.sendAndReceiveAll(
ctx,
Expand Down Expand Up @@ -158,8 +162,8 @@ export class CalloutResponseEventManager extends BaseEventManager {
}

try {
this.stateMachine.setSessionState(
session,
await this.stateMachine.setSessionState(
ctx,
ChatState.CalloutAnswered,
false,
);
Expand Down
9 changes: 6 additions & 3 deletions telegram-bot/event-managers/callout.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class CalloutEventManager extends BaseEventManager {

protected async onCalloutSelectionKeyboardPressed(ctx: AppContext) {
const shortSlug = ctx.callbackQuery?.data?.split(":")[1];
const session = await ctx.session;

// Remove the inline keyboard
await this.keyboard.removeInlineKeyboard(ctx);
Expand All @@ -67,12 +66,16 @@ export class CalloutEventManager extends BaseEventManager {
callout,
);

const signal = this.stateMachine.setSessionState(
session,
const signal = await this.stateMachine.setSessionState(
ctx,
ChatState.CalloutDetails,
true,
);

if (!signal) {
throw new Error("The AbortSignal is required!");
}

await this.communication.sendAndReceiveAll(
ctx,
calloutFormRender,
Expand Down
22 changes: 13 additions & 9 deletions telegram-bot/renderer/message.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export class MessageRenderer {
let markdown = "";

for (const command of commands) {
markdown += `${("/" + command.command)}: _${command.description}_\n`;
markdown += `/${escapeMd(command.command)}: ${
escapeMd(command.description)
}\n`;
}

const result: RenderMarkdown = {
Expand Down Expand Up @@ -159,36 +161,38 @@ export class MessageRenderer {
* Render the help message
*/
public help(state: ChatState): RenderMarkdown {
const tKey = "bot.info.messages.help";
const key = "bot.info.messages.help";
const generalContentPlaceholders = this
.getGeneralContentPlaceholdersMarkdown();
const commands = this.commands(state).markdown;
const intro = this.i18n.t(tKey, {
const markdown = this.i18n.t(key, {
...generalContentPlaceholders,
commands: commands,
}, { escapeMd: true });

console.debug("help markdown:", markdown);

const result: RenderMarkdown = {
type: RenderType.MARKDOWN,
markdown: intro,
key: tKey,
markdown,
key,
...this.noResponse(),
};
return result;
}

public async continueList(): Promise<RenderMarkdown> {
const tKey = "bot.info.messages.continueList";
const key = "bot.info.messages.continueList";
const generalContentPlaceholders = await this
.getGeneralContentPlaceholdersMarkdown();
const intro = this.i18n.t(tKey, {
const markdown = this.i18n.t(key, {
...generalContentPlaceholders,
}, { escapeMd: true });

const result: RenderMarkdown = {
type: RenderType.MARKDOWN,
markdown: intro,
key: tKey,
markdown,
key,
keyboard: this.keyboard.empty(), // To replace the old one
...this.noResponse(),
};
Expand Down
4 changes: 3 additions & 1 deletion telegram-bot/services/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ export class CommandService extends BaseService {

if (session.state === ChatState.Start) {
// Cancel old process for the case there is one
await this.stateMachine.resetSessionState(
await this.stateMachine.setSessionState(
ctx,
ChatState.Start,
true,
);
}

Expand Down
Loading

0 comments on commit d863996

Please sign in to comment.