From e8c973c048c64c1b0d2df39ee522c558bfae75a0 Mon Sep 17 00:00:00 2001 From: webwarrior-ws Date: Tue, 18 Feb 2025 22:18:16 +0100 Subject: [PATCH] bot/modules/user: convert to TS (#631) Convert bot/modules/user to TypeScript. --- bot/modules/community/communityContext.ts | 2 + bot/modules/user/{index.js => index.ts} | 12 ++- bot/modules/user/scenes/index.js | 1 - bot/modules/user/scenes/index.ts | 3 + .../user/scenes/{settings.js => settings.ts} | 78 +++++++++++-------- 5 files changed, 60 insertions(+), 36 deletions(-) rename bot/modules/user/{index.js => index.ts} (53%) delete mode 100644 bot/modules/user/scenes/index.js create mode 100644 bot/modules/user/scenes/index.ts rename bot/modules/user/scenes/{settings.js => settings.ts} (50%) diff --git a/bot/modules/community/communityContext.ts b/bot/modules/community/communityContext.ts index b47656a0..7c891bfe 100644 --- a/bot/modules/community/communityContext.ts +++ b/bot/modules/community/communityContext.ts @@ -25,6 +25,8 @@ export interface CommunityWizardState { bot: Telegraf; message: Message.TextMessage | undefined; error?: any; + feedback?: any; + language: string; updateUI: (() => Promise); handler?: ((ctx: CommunityContext) => Promise); } diff --git a/bot/modules/user/index.js b/bot/modules/user/index.ts similarity index 53% rename from bot/modules/user/index.js rename to bot/modules/user/index.ts index ac279d18..e6b412fc 100644 --- a/bot/modules/user/index.js +++ b/bot/modules/user/index.ts @@ -1,15 +1,21 @@ // @ts-check const { userMiddleware } = require('../../middleware/user'); -const Scenes = (exports.Scenes = require('./scenes')); +import { Telegraf } from 'telegraf'; +import Scenes from './scenes'; +import { CommunityContext } from '../community/communityContext'; -exports.configure = bot => { +export const configure = (bot: Telegraf) => { bot.command('/settings', userMiddleware, async ctx => { try { const { user } = ctx; await ctx.scene.enter(Scenes.Settings.id, { user }); - } catch (err) { + } catch (err: any) { ctx.reply(err.message); } }); }; + +export { + Scenes +} diff --git a/bot/modules/user/scenes/index.js b/bot/modules/user/scenes/index.js deleted file mode 100644 index fe02c8bf..00000000 --- a/bot/modules/user/scenes/index.js +++ /dev/null @@ -1 +0,0 @@ -exports.Settings = require('./settings'); diff --git a/bot/modules/user/scenes/index.ts b/bot/modules/user/scenes/index.ts new file mode 100644 index 00000000..a7b51e55 --- /dev/null +++ b/bot/modules/user/scenes/index.ts @@ -0,0 +1,3 @@ +import Settings from './settings'; + +export default { Settings } diff --git a/bot/modules/user/scenes/settings.js b/bot/modules/user/scenes/settings.ts similarity index 50% rename from bot/modules/user/scenes/settings.js rename to bot/modules/user/scenes/settings.ts index 36cdbbfb..5bfab963 100644 --- a/bot/modules/user/scenes/settings.js +++ b/bot/modules/user/scenes/settings.ts @@ -1,25 +1,31 @@ -const { Scenes } = require('telegraf'); -const { Community } = require('../../../../models'); -const { getLanguageFlag } = require('../../../../util'); -const NostrLib = require('../../nostr/lib'); +import { Scenes } from 'telegraf'; +import { Community } from '../../../../models'; +import { getLanguageFlag } from '../../../../util'; +import * as NostrLib from '../../nostr/lib'; +import { CommunityContext, CommunityWizardState } from '../../community/communityContext'; +import { Message } from 'telegraf/typings/core/types/typegram'; function make() { - const resetMessage = async (ctx, next) => { - delete ctx.scene.state.feedback; - delete ctx.scene.state.error; + const resetMessage = async (ctx: CommunityContext, next: () => void) => { + const state = ctx.scene.state as CommunityWizardState; + delete state.feedback; + delete state.error; next(); }; - async function mainData(ctx) { - const { user } = ctx.scene.state; + async function mainData(ctx: CommunityContext) { + const state = ctx.scene.state as CommunityWizardState; + const { user } = state; const data = { user, - language: getLanguageFlag(ctx.scene.state.language), + language: getLanguageFlag(state.language), npub: '', community: '', lightning_address: '', }; if (user.default_community_id) { const community = await Community.findById(user.default_community_id); + if(community == null) + throw new Error("community not found") data.community = community.group; } if (user.nostr_public_key) { @@ -32,10 +38,14 @@ function make() { return data; } - async function updateMessage(ctx) { + async function updateMessage(ctx: CommunityContext) { try { - ctx.i18n.locale(ctx.scene.state.language); // i18n locale resets if user executes unknown action - const { message, error } = ctx.scene.state; + const state = ctx.scene.state as CommunityWizardState; + ctx.i18n.locale(state.language); // i18n locale resets if user executes unknown action + const { message, error } = state; + + if(message === undefined) + throw new Error("message is undefined"); const errorText = (error => { if (!error) return; @@ -45,7 +55,7 @@ function make() { if (!feedback) return; if (typeof feedback === 'string') return feedback; return ctx.i18n.t(feedback.i18n, feedback); - })(ctx.scene.state.feedback); + })(state.feedback); const extras = [errorText, feedbackText].filter(e => e); const main = ctx.i18n.t('user_settings', await mainData(ctx)); @@ -57,30 +67,31 @@ function make() { const msg = await ctx.telegram.editMessageText( message.chat.id, message.message_id, - null, + undefined, str, { parse_mode: 'HTML', - disable_web_page_preview: true, - } + link_preview_options: { is_disabled: true }, + } as any ); - ctx.scene.state.message = msg; - ctx.scene.state.message.text = str; + state.message = msg as Message.TextMessage; + state.message.text = str; } catch (err) {} } - async function initHandler(ctx) { + async function initHandler(ctx: CommunityContext) { try { - const { user } = ctx.scene.state; - ctx.scene.state.language = user.lang || ctx.from?.language_code; + const state = ctx.scene.state as CommunityWizardState; + const { user } = state; + state.language = user.lang || ctx.from?.language_code; const str = ctx.i18n.t('user_settings', await mainData(ctx)); const msg = await ctx.reply(str, { parse_mode: 'HTML' }); - ctx.scene.state.message = msg; - ctx.scene.state.message.text = str; + state.message = msg; + state.message.text = str; } catch (err) {} } - const scene = new Scenes.WizardScene('USER_SETTINGS', async ctx => { - ctx.user = ctx.scene.state.user; - const { state } = ctx.scene; + const scene = new Scenes.WizardScene('USER_SETTINGS', async (ctx: CommunityContext) => { + const state = ctx.scene.state as CommunityWizardState; + ctx.user = state.user; if (!state.message) return initHandler(ctx); await ctx.deleteMessage(); state.error = { @@ -89,22 +100,25 @@ function make() { await updateMessage(ctx); }); - scene.command('/setnpub', resetMessage, async ctx => { + scene.command('/setnpub', resetMessage, async (ctx: CommunityContext) => { try { await ctx.deleteMessage(); + const state = ctx.scene.state as CommunityWizardState; + if(ctx.message === undefined) + throw new Error("ctx.message is undefined"); const [, npub] = ctx.message.text.trim().split(' '); const hex = NostrLib.decodeNpub(npub); if (!hex) throw new Error('NpubNotValid'); - const user = ctx.scene.state.user; + const user = state.user; user.nostr_public_key = hex; await user.save(); - ctx.scene.state.feedback = { + state.feedback = { i18n: 'user_npub_updated', npub, }; await updateMessage(ctx); } catch (err) { - ctx.scene.state.error = { + (ctx.scene.state as CommunityWizardState).error = { i18n: 'npub_not_valid', }; await updateMessage(ctx); @@ -114,4 +128,4 @@ function make() { return scene; } -module.exports = make(); +export default make();