Skip to content

Commit

Permalink
fix: use filter queries in form controls
Browse files Browse the repository at this point in the history
  • Loading branch information
roziscoding committed Aug 27, 2024
1 parent 9725eca commit 6d10121
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"https://x.nest.land": true,
"https://crux.land": true,
"https://lib.deno.dev": false
}
},
"editor.defaultFormatter": "denoland.vscode-deno"
}
36 changes: 19 additions & 17 deletions src/form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Context } from "./deps.deno.ts";
import { OtherwiseConfig } from "./conversation.ts";
import { type Context, Filter, FilterQuery } from "./deps.deno.ts";

/**
* Form building utilities that are exposed on `conversation.form`.
Expand All @@ -17,6 +18,10 @@ export class ConversationForm<C extends Context> {
constructor(
private readonly conversation: {
wait: () => Promise<C>;
waitFor: <Q extends FilterQuery>(
query: Q,
opts?: OtherwiseConfig<C>,
) => Promise<Filter<C, Q>>;
skip: () => Promise<never>;
},
) {}
Expand All @@ -30,14 +35,9 @@ export class ConversationForm<C extends Context> {
* @param otherwise Handler that will be run for skipped updates
* @returns The received text
*/
async text(otherwise?: (ctx: C) => unknown | Promise<unknown>) {
const ctx = await this.conversation.wait();
const text = ctx.msg?.text ?? ctx.msg?.caption;
if (text === undefined) {
await otherwise?.(ctx);
return await this.conversation.skip();
}
return text;
text(otherwise?: (ctx: C) => unknown | Promise<unknown>) {
return this.conversation.waitFor(":text", { otherwise })
.then((ctx) => ctx.msg.text);
}

// TODO: add match which returns `ctx.match` after `ctx.hasMatch`
Expand All @@ -53,8 +53,8 @@ export class ConversationForm<C extends Context> {
* @returns The received number
*/
async number(otherwise?: (ctx: C) => unknown | Promise<unknown>) {
const ctx = await this.conversation.wait();
const num = parseFloat(ctx.msg?.text ?? ctx.msg?.caption ?? "NaN");
const ctx = await this.conversation.waitFor(":text", { otherwise });
const num = parseFloat(ctx.msg.text);
if (isNaN(num)) {
await otherwise?.(ctx);
return await this.conversation.skip();
Expand Down Expand Up @@ -88,8 +88,8 @@ export class ConversationForm<C extends Context> {
: typeof options === "function"
? { otherwise: options }
: options;
const ctx = await this.conversation.wait();
const text = ctx.msg?.text ?? ctx.msg?.caption ?? "NaN";
const ctx = await this.conversation.waitFor(":text", { otherwise });
const text = ctx.msg.text;
const num = parseInt(text, radix);
if (isNaN(num)) {
await otherwise?.(ctx);
Expand All @@ -115,8 +115,8 @@ export class ConversationForm<C extends Context> {
otherwise?: (ctx: C) => Promise<unknown> | unknown,
) {
const opts: string[] = options;
const ctx = await this.conversation.wait();
const text = ctx.msg?.text ?? ctx.msg?.caption;
const ctx = await this.conversation.waitFor(":text", { otherwise });
const text = ctx.msg.text;
if (text === undefined || !opts.includes(text)) {
await otherwise?.(ctx);
return await this.conversation.skip();
Expand All @@ -137,8 +137,10 @@ export class ConversationForm<C extends Context> {
* @returns The received number
*/
async url(otherwise?: (ctx: C) => unknown | Promise<unknown>) {
const ctx = await this.conversation.wait();
const text = ctx.msg?.text ?? ctx.msg?.caption ?? "";
const ctx = await this.conversation.waitFor(":entities:url", {
otherwise,
});
const [{ text }] = ctx.entities("url");
let url: URL;
try {
url = new URL(text);
Expand Down

0 comments on commit 6d10121

Please sign in to comment.