diff --git a/i18n/de.yaml b/i18n/de.yaml index 590d2cb2b53..7936700d87d 100644 --- a/i18n/de.yaml +++ b/i18n/de.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: Sprache auswählen SET_LANG_TO: Sprache einstellen auf LANG_USAGE: 'Verwendung: /lang Sprache ändern' CHOOSE_UNSUB: Wähle das Abonnement, das du abbestellen möchtest +EXPORT_SUCCESS: Export abgeschlossen +RESPONSE_TOO_LONG: Das Ergebnis ist zu lang, um es anzuzeigen. diff --git a/i18n/en.yaml b/i18n/en.yaml index 3ebea0d8f21..21b7b97ae88 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: Number of active feeds with error TOTAL_ACTIVE_FEED_COUNT: Total number of active feeds CREATOR_ONLY: Bot Craetor only ALLOW_LIST_NOT_INCLUDE: This bot has enabled allow list and you are blocked +EXPORT_SUCCESS: Export complete +RESPONSE_TOO_LONG: The result is too long to display. diff --git a/i18n/es-es.yaml b/i18n/es-es.yaml index 40638fe3e01..87cbe2fbf39 100644 --- a/i18n/es-es.yaml +++ b/i18n/es-es.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: Seleccione idiomas SET_LANG_TO: Cambiar idioma a LANG_USAGE: 'UTILIZACIÓN: /lang cambiar idioma' CHOOSE_UNSUB: Elija la suscripción que desee eliminar +EXPORT_SUCCESS: Exportación completada +RESPONSE_TOO_LONG: El resultado es demasiado largo para mostrarlo. diff --git a/i18n/fa.yaml b/i18n/fa.yaml index 852908a6cc2..e16a0d605a3 100644 --- a/i18n/fa.yaml +++ b/i18n/fa.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: تعداد فیدهای فعال با خطا TOTAL_ACTIVE_FEED_COUNT: تعداد فیدهای فعال CREATOR_ONLY: فقط سازنده ربات ALLOW_LIST_NOT_INCLUDE: این ربات لیست مجاز را فعال کرده است و شما مسدود شده اید +EXPORT_SUCCESS: با موفقیت صادر شد +RESPONSE_TOO_LONG: نتیجه برای نمایش بیش از حد طولانی است. diff --git a/i18n/id.yaml b/i18n/id.yaml index 7da28f7221a..5109317e390 100644 --- a/i18n/id.yaml +++ b/i18n/id.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: Banyaknya feed aktif yang error TOTAL_ACTIVE_FEED_COUNT: Banyaknya feed yang aktif CREATOR_ONLY: Hanya pemilik bot ALLOW_LIST_NOT_INCLUDE: Bot ini telah mengaktifkan daftar yang diijinkan dan anda diblok +EXPORT_SUCCESS: Ekspor selesai +RESPONSE_TOO_LONG: Hasilnya terlalu panjang untuk ditampilkan. diff --git a/i18n/it.yaml b/i18n/it.yaml index dfe38e88466..18bc7c24607 100644 --- a/i18n/it.yaml +++ b/i18n/it.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: Scegli la lingua SET_LANG_TO: Cambia lingua in LANG_USAGE: 'UTILIZZO: /lang cambia lingua' CHOOSE_UNSUB: Scegli l'iscrizione che desideri annullare +EXPORT_SUCCESS: Esportazione completata +RESPONSE_TOO_LONG: Il risultato è troppo lungo per essere visualizzato. diff --git a/i18n/ka.yaml b/i18n/ka.yaml index 534af348e7a..c0bcdb4487f 100644 --- a/i18n/ka.yaml +++ b/i18n/ka.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: ენის არჩევა SET_LANG_TO: ენის დაყენება LANG_USAGE: 'გამოყენება: /lang ენის შეცვლა' CHOOSE_UNSUB: აირჩიეთ გამოწერა რომლის გაუქმებაც გსურთ +EXPORT_SUCCESS: ექსპორტი დასრულდა +RESPONSE_TOO_LONG: შედეგი ზედმეტად გრძელია და ვერ გამოჩნდება. diff --git a/i18n/lt.yaml b/i18n/lt.yaml index 9a0927d2b15..aa50feb4c35 100644 --- a/i18n/lt.yaml +++ b/i18n/lt.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: Pasirinkti kalbą SET_LANG_TO: Nustatyti kalbą LANG_USAGE: 'NAUDOJIMAS: /lang keisti kalbą' CHOOSE_UNSUB: Pasirinkite kanalą, kurio prenumeratą norite atšaukti +EXPORT_SUCCESS: Eksportas baigtas +RESPONSE_TOO_LONG: Rezultatas per ilgas, kad jį būtų galima parodyti. diff --git a/i18n/pt-br.yaml b/i18n/pt-br.yaml index 6eb9bb4c291..31ef88a0326 100644 --- a/i18n/pt-br.yaml +++ b/i18n/pt-br.yaml @@ -47,3 +47,5 @@ CHOOSE_LANG: Escolha a língua SET_LANG_TO: Configurar língua para LANG_USAGE: 'USO: /lang alterar língua' CHOOSE_UNSUB: Escolha a inscrição que vocês deseja remover +EXPORT_SUCCESS: Exportação concluída +RESPONSE_TOO_LONG: O resultado é longo demais para ser exibido. diff --git a/i18n/ru.yaml b/i18n/ru.yaml index 7e59298eb56..e4c6a6125e8 100644 --- a/i18n/ru.yaml +++ b/i18n/ru.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: Число активных каналов с ош TOTAL_ACTIVE_FEED_COUNT: Число активных каналов CREATOR_ONLY: Только для создателя бота ALLOW_LIST_NOT_INCLUDE: У этого бота включён лист разрешенных и вы в него не входите +EXPORT_SUCCESS: Экспорт завершён +RESPONSE_TOO_LONG: Результат слишком длинный для отображения. diff --git a/i18n/zh-cn.yaml b/i18n/zh-cn.yaml index 179aa4e6228..5b6cce8e852 100644 --- a/i18n/zh-cn.yaml +++ b/i18n/zh-cn.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: 错误的活跃订阅源数 TOTAL_ACTIVE_FEED_COUNT: 总活跃订阅源数 CREATOR_ONLY: 只限 Bot 创建者使用 ALLOW_LIST_NOT_INCLUDE: 该 Bot 已启用白名单,你已经被禁用 +EXPORT_SUCCESS: 已导出 +RESPONSE_TOO_LONG: 结果过长,无法显示。 diff --git a/i18n/zh-tw.yaml b/i18n/zh-tw.yaml index 13e865d4242..a348761502d 100644 --- a/i18n/zh-tw.yaml +++ b/i18n/zh-tw.yaml @@ -51,3 +51,5 @@ ACTIVE_FEED_COUNT_WITH_ERROR: 錯誤的活躍訂閱源數 TOTAL_ACTIVE_FEED_COUNT: 總活躍訂閱源數 CREATOR_ONLY: 只限 Bot 創建者使用 ALLOW_LIST_NOT_INCLUDE: 該 Bot 已啟用白名單,妳已經被禁用 +EXPORT_SUCCESS: 已匯出 +RESPONSE_TOO_LONG: 結果過長,無法顯示。 diff --git a/source/controlers/language.ts b/source/controlers/language.ts index eb8030c54cb..1fbcbcd49c7 100644 --- a/source/controlers/language.ts +++ b/source/controlers/language.ts @@ -1,3 +1,4 @@ +import editResponse from '../utils/edit-response'; import { setLangById } from '../proxies/users'; import i18n from '../i18n'; import { MContext, TNextFn } from '../types/ctx'; @@ -20,19 +21,15 @@ export async function replyKeyboard( callback_data: `CHANGE_LANG_${i}_${ctx.state.chat.id}` }; }); - await ctx.telegram.sendMessage( - ctx.chat.id, - i18n[ctx.state.lang]['CHOOSE_LANG'], - { - parse_mode: 'HTML', - link_preview_options: { - is_disabled: true - }, - reply_markup: { - inline_keyboard: chunk(kbs, 5) - } + await editResponse(ctx, i18n[ctx.state.lang]['CHOOSE_LANG'], { + parse_mode: 'HTML', + link_preview_options: { + is_disabled: true + }, + reply_markup: { + inline_keyboard: chunk(kbs, 5) } - ); + }); await next(); } @@ -46,14 +43,7 @@ export async function changeLangCallback( const lang = data[data.length - 2]; const id = data[data.length - 1]; await setLangById(parseInt(id), lang); - ctx.telegram.answerCbQuery( - cb.id, - i18n[lang]['SET_LANG_TO'] + ' ' + lang - ); - await ctx.telegram.deleteMessage( - cb.message.chat.id, - cb.message.message_id - ); + await editResponse(ctx, i18n[lang]['SET_LANG_TO'] + ' ' + lang); await next(); } } diff --git a/source/controlers/rss.ts b/source/controlers/rss.ts index 429f73f25d0..8290295ca1a 100644 --- a/source/controlers/rss.ts +++ b/source/controlers/rss.ts @@ -1,3 +1,5 @@ +import { limitText, MESSAGE_LIMIT } from '../utils/limit-text'; +import editResponse from '../utils/edit-response'; import * as RSS from '../proxies/rss-feed'; import i18n from '../i18n'; import twoKeyReply from '../utils/two-key-reply'; @@ -8,6 +10,12 @@ import { decodeUrl, encodeUrl } from '../utils/urlencode'; import sanitize from '../utils/sanitize'; import escapeHtml from '../utils/escape-html'; +function feedLink(label: string, feedUrl: string, feedTitle: string): string { + const href = escapeHtml(encodeUrl(feedUrl)); + const budget = MESSAGE_LIMIT - label.length - 1; + return `${label} ${sanitize(limitText(feedTitle, budget))}`; +} + export async function sub(ctx: MContext, next: TNextFn): Promise { const { feedUrl, chat, lang } = ctx.state; const feedTitle = ctx.state.feed.feed_title; @@ -15,44 +23,37 @@ export async function sub(ctx: MContext, next: TNextFn): Promise { const userId = chat.id; try { await RSS.sub(userId, feedUrl, feedTitle, ttl); - await ctx.telegram.deleteMessage(ctx.chat.id, ctx.state.processMsgId); - ctx.state.processMsgId = null; - ctx.replyWithMarkdown(` - ${i18n[lang]['SUB_SUCCESS']} [${ctx.state.feed.feed_title}](${encodeUrl( - ctx.state.feedUrl - )})`); } catch (e) { if (e instanceof errors.ControllableError) throw e; throw errors.newCtrlErr('DB_ERROR', e); } + await editResponse( + ctx, + feedLink(i18n[lang]['SUB_SUCCESS'], feedUrl, feedTitle), + { + parse_mode: 'HTML' + } + ); await next(); } export async function unsub(ctx: MContext, next: TNextFn): Promise { const { feedUrl, chat, lang } = ctx.state; const userId = chat.id; + const feed = await RSS.getFeedByUrl(feedUrl); + if (isNone(feed)) throw errors.newCtrlErr('DID_NOT_SUB'); try { - const feed = await RSS.getFeedByUrl(feedUrl); - if (isNone(feed)) throw errors.newCtrlErr('DID_NOT_SUB'); - await RSS.unsub(userId, feed.value.feed_id); - - await ctx.telegram.deleteMessage(ctx.chat.id, ctx.state.processMsgId); - ctx.state.processMsgId = null; - ctx.replyWithMarkdown( - ` - ${i18n[lang]['UNSUB_SUCCESS']} [${feed.value.feed_title}](${encodeUrl( - ctx.state.feedUrl - )})`, - { - reply_markup: { - remove_keyboard: true - } - } - ); + const deleted = await RSS.unsub(userId, feed.value.feed_id); + if (!deleted) throw errors.newCtrlErr('DID_NOT_SUB'); } catch (e) { if (e instanceof errors.ControllableError) throw e; throw errors.newCtrlErr('DB_ERROR', e); } + await editResponse( + ctx, + feedLink(i18n[lang]['UNSUB_SUCCESS'], feedUrl, feed.value.feed_title), + { parse_mode: 'HTML' } + ); await next(); } @@ -115,14 +116,10 @@ export async function unsubAll(ctx: MContext, next: TNextFn): Promise { const userId = ctx.state.chat.id; const lang = ctx.state.lang; await RSS.unsubAll(userId); - await ctx.telegram.sendMessage( - ctx.chat.id, - i18n[lang]['UNSUB_ALL_SUCCESS'], - { - parse_mode: 'HTML', - link_preview_options: { is_disabled: true } - } - ); + await editResponse(ctx, i18n[lang]['UNSUB_ALL_SUCCESS'], { + parse_mode: 'HTML', + link_preview_options: { is_disabled: true } + }); await next(); } @@ -170,6 +167,7 @@ export async function getUrlById( const { text } = ctx.message; const feed_id = text.match(/^\[(\d+)] (.+)/)[1]; const feed = await RSS.getFeedById(parseInt(feed_id)); + if (!feed) throw errors.newCtrlErr('DID_NOT_SUB'); ctx.state.feedUrl = decodeUrl(feed.url); await next(); } @@ -190,9 +188,7 @@ export async function getActiveFeedWithErrorCount( }: ${count} ${i18n[ctx.state.lang]['TOTAL_ACTIVE_FEED_COUNT']}: ${ feedsWithErrorCount.length }`; - ctx.reply(heath); - await ctx.telegram.deleteMessage(ctx.chat.id, ctx.state.processMsgId); - ctx.state.processMsgId = null; + await editResponse(ctx, heath); await next(); } diff --git a/source/index.ts b/source/index.ts index 727e523c23f..fd7b6ad3c56 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,3 +1,4 @@ +import editResponse from './utils/edit-response'; import { Telegraf } from 'telegraf'; import { telegrafThrottler } from 'telegraf-throttler'; import { fork } from 'child_process'; @@ -35,7 +36,7 @@ const { import getUrl from './middlewares/get-url'; import getUrlByTitle from './middlewares/get-url-by-title'; import getFileLink from './middlewares/get-file-link'; -import sendError from './middlewares/send-error'; +import sendError, { handleErrors, processing } from './middlewares/send-error'; import testUrl from './middlewares/test-url'; import isAdmin from './middlewares/is-admin'; import onlyPrivateChat from './middlewares/only-private-chat'; @@ -89,7 +90,7 @@ bot.command('start', sendError, async (ctx) => { builder.push(i18n[lang]['USB_ALL_USAGE']); builder.push(i18n[lang]['LANG_USAGE']); if (view_all) builder.push(i18n[lang]['VIEW_ALL_USAGE']); - await ctx.replyWithMarkdown(builder.join('\n')); + await editResponse(ctx, builder.join('\n'), { parse_mode: 'Markdown' }); }); bot.command('help', sendError, async (ctx) => { @@ -106,12 +107,12 @@ bot.command('help', sendError, async (ctx) => { builder.push( `[https://github.com/fengkx/NodeRSSBot/blob/master/README.md](https://github.com/fengkx/NodeRSSBot/blob/master/README.md)` ); - await ctx.replyWithMarkdown(builder.join('\n')); + await editResponse(ctx, builder.join('\n'), { parse_mode: 'Markdown' }); }); bot.command('sub', sendError, isAdmin, getUrl, testUrl, sub); -bot.command('unsub', sendError, isAdmin, getUrl, unsub); +bot.command('unsub', handleErrors, isAdmin, getUrl, processing, unsub); bot.command('unsubthis', sendError, isAdmin, getUrlByTitle, unsub); @@ -191,7 +192,7 @@ bot.command( getActiveFeedWithErrorCount ); -bot.action(/^CHANGE_LANG[\w_]+/, changeLangCallback); +bot.action(/^CHANGE_LANG[\w_]+/, sendError, changeLangCallback); bot.hears( /(((https?:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w\-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)/gm, @@ -210,24 +211,19 @@ bot.hears( subMultiUrl ); -bot.hears(/^\[(\d+)] (.+)/, sendError, isAdmin, getUrlById, unsub); - -bot.action( - 'UNSUB_ALL_YES', - sendError, +bot.hears( + /^\[(\d+)] (.+)/, + handleErrors, + (ctx, next) => processing(ctx, next, { removeKeyboard: true }), isAdmin, - unsubAll, - async (ctx: MContext, next) => { - const cb = ctx.callbackQuery; - ctx.telegram.answerCbQuery(cb.id); - await next(); - } + getUrlById, + unsub ); -bot.action('UNSUB_ALL_NO', async (ctx: MContext, next) => { - const cb = ctx.callbackQuery; - ctx.telegram.answerCbQuery(cb.id, i18n[lang]['CANCEL']); - await ctx.telegram.deleteMessage(cb.message.chat.id, cb.message.message_id); +bot.action('UNSUB_ALL_YES', sendError, isAdmin, unsubAll); + +bot.action('UNSUB_ALL_NO', sendError, async (ctx: MContext, next) => { + await editResponse(ctx, i18n[ctx.state.lang]['CANCEL']); await next(); }); @@ -239,10 +235,6 @@ bot.action( const cb = ctx.callbackQuery; if ('data' in cb) { ctx.state.viewallPage = parseInt(cb.data.split('_')[1]); - await ctx.telegram.deleteMessage( - cb.message.chat.id, - cb.message.message_id - ); await next(); } }, @@ -259,10 +251,6 @@ bot.action( const splitStr = cb.data.split('_'); if (splitStr[1] === 'RAW') ctx.state.showRaw = true; ctx.state.rssPage = parseInt(splitStr[splitStr.length - 1]); - await ctx.telegram.deleteMessage( - cb.message.chat.id, - cb.message.message_id - ); await next(); } }, diff --git a/source/middlewares/export-to-opml.ts b/source/middlewares/export-to-opml.ts index 4698ed71f14..8265a7e4a23 100644 --- a/source/middlewares/export-to-opml.ts +++ b/source/middlewares/export-to-opml.ts @@ -1,3 +1,5 @@ +import { badRequestDescription, cannotEdit } from '../utils/edit-response'; +import i18n from '../i18n'; import { getSubscribedFeedsByUserId } from '../proxies/rss-feed'; import errors from '../utils/errors'; import * as path from 'path'; @@ -5,7 +7,6 @@ import * as ejs from 'ejs'; import * as fs from 'fs'; import { MContext, TNextFn } from '../types/ctx'; import { Feed } from '../types/feed'; -import { config } from '../config'; import { escapeHTML } from 'fast-escape-html'; function readFilePromise(path: string): Promise { @@ -39,13 +40,21 @@ const render = async (feeds: Feed[]): Promise => { return template({ feeds }); }; -function remove(path: string): Promise { - return new Promise((resolve, reject) => { - fs.unlink(path, function (err) { - if (err) reject(err); - resolve(); - }); - }); +/** Replace the still-visible processing message with a completion note. */ +async function markProcessingMessageExported( + ctx: MContext, + messageId: number +): Promise { + try { + await ctx.telegram.editMessageText( + ctx.chat.id, + messageId, + undefined, + i18n[ctx.state.lang]['EXPORT_SUCCESS'] + ); + } catch (error) { + if (!cannotEdit(badRequestDescription(error))) throw error; + } } export default async (ctx: MContext, next: TNextFn): Promise => { @@ -54,21 +63,28 @@ export default async (ctx: MContext, next: TNextFn): Promise => { if (feeds.length === 0) { throw errors.newCtrlErr('NOT_SUB'); } + let opml: string; try { - const opml = await render(feeds); - const filePath = path.join( - config['PKG_ROOT'], - 'data', - `${chat.id.toString()}.opml` - ); - fs.writeFileSync(filePath, opml); - await ctx.replyWithDocument({ - source: fs.readFileSync(filePath), - filename: 'export.opml' - }); - await remove(filePath); + opml = await render(feeds); } catch (e) { throw errors.newCtrlErr('FILESYSTEM_ERROR', e); } + const document = { source: Buffer.from(opml), filename: 'export.opml' }; + const messageId = ctx.state.processMsgId; + try { + await ctx.telegram.editMessageMedia(ctx.chat.id, messageId, undefined, { + type: 'document', + media: document + }); + } catch (error) { + // A timeout can arrive after delivery. Only resend after an explicit rejection. + const description = badRequestDescription(error); + if (!description) throw error; + const sent = await ctx.replyWithDocument(document); + ctx.state.processMsgId = sent.message_id; + if (description !== 'message to edit not found') { + await markProcessingMessageExported(ctx, messageId); + } + } await next(); }; diff --git a/source/middlewares/import-from-opml.ts b/source/middlewares/import-from-opml.ts index 60d8e0360f7..6bfef8551c8 100644 --- a/source/middlewares/import-from-opml.ts +++ b/source/middlewares/import-from-opml.ts @@ -1,3 +1,5 @@ +import { MESSAGE_LIMIT } from '../utils/limit-text'; +import editResponse from '../utils/edit-response'; import { Outline } from '../types/outline'; import got from '../utils/got'; import { transform } from 'camaro'; @@ -26,6 +28,7 @@ export const _getOutlines = getOutlines; export default async (ctx: MContext, next: TNextFn): Promise => { const { fileLink, lang } = ctx.state; + let text: string; try { const res = await got(fileLink); const opmlStr = await res.textConverted(); @@ -42,17 +45,14 @@ export default async (ctx: MContext, next: TNextFn): Promise => { } }) ); - let text = `${i18n[lang]['IMPORT_SUCCESS']}`; + text = `${i18n[lang]['IMPORT_SUCCESS']}`; outlines.forEach((outline) => { // URL is escaped for href, title text is sanitized for display text += `\n${sanitize(outline.text)}`; }); - if (text.length > 4096) { + if (text.length > MESSAGE_LIMIT) { text = `${i18n[lang]['IMPORT_SUCCESS']}\n`; } - ctx.telegram.deleteMessage(ctx.state.chat.id, ctx.state.processMsgId); - ctx.state.processMsgId = null; - ctx.replyWithHTML(text); } catch (e) { if (e.response) { throw errors.newCtrlErr('NETWORK_ERROR', e); @@ -62,5 +62,6 @@ export default async (ctx: MContext, next: TNextFn): Promise => { throw errors.newCtrlErr('OPML_PARSE_ERROR', e); } } + await editResponse(ctx, text, { parse_mode: 'HTML' }); await next(); }; diff --git a/source/middlewares/send-error.ts b/source/middlewares/send-error.ts index 5eb58218b2a..1a7b215a201 100644 --- a/source/middlewares/send-error.ts +++ b/source/middlewares/send-error.ts @@ -1,28 +1,53 @@ +import { CB_QUERY_TEXT_LIMIT, limitText } from '../utils/limit-text'; import i18n from '../i18n'; import logger from '../utils/logger'; import errors from '../utils/errors'; +import editResponse from '../utils/edit-response'; import { MContext, TNextFn } from '../types/ctx'; -export default async (ctx: MContext, next: TNextFn): Promise => { - const { lang } = ctx.state; - const m = await ctx.reply(i18n[lang]['PROCESSING'], { - reply_markup: { - remove_keyboard: true - } - }); - ctx.state.processMsgId = m.message_id; - logger.debug(ctx.message); +export async function handleErrors( + ctx: MContext, + next: TNextFn +): Promise { try { await next(); } catch (e) { - if (e instanceof errors.ControllableError) { - if (ctx.state.processMsgId) { - ctx.telegram.deleteMessage( - ctx.state.chat.id, - ctx.state.processMsgId - ); - } - ctx.reply(e.toString(lang)); - } else throw e; + if (!(e instanceof errors.ControllableError)) throw e; + const text = e.toString(ctx.state.lang); + if (ctx.callbackQuery) + await ctx.answerCbQuery(limitText(text, CB_QUERY_TEXT_LIMIT)); + else if (ctx.state.processMsgId) await editResponse(ctx, text); + else await ctx.reply(text); + } +} + +interface ProcessingOptions { + /** Attach remove_keyboard to the processing reply, clearing a picker keyboard. */ + removeKeyboard?: boolean; +} + +export async function processing( + ctx: MContext, + next: TNextFn, + { removeKeyboard = false }: ProcessingOptions = {} +): Promise { + const callbackMessage = ctx.callbackQuery?.message; + if (callbackMessage) { + ctx.state.processMsgId = callbackMessage.message_id; + } else { + const reply = await ctx.reply( + i18n[ctx.state.lang]['PROCESSING'], + removeKeyboard + ? { reply_markup: { remove_keyboard: true } } + : undefined + ); + ctx.state.processMsgId = reply.message_id; } + logger.debug(ctx.message); + await next(); + if (ctx.callbackQuery) await ctx.answerCbQuery(); +} + +export default async (ctx: MContext, next: TNextFn): Promise => { + await handleErrors(ctx, () => processing(ctx, next)); }; diff --git a/source/middlewares/sub-multi-url.ts b/source/middlewares/sub-multi-url.ts index 87677270be8..f4d9e57d40d 100644 --- a/source/middlewares/sub-multi-url.ts +++ b/source/middlewares/sub-multi-url.ts @@ -1,3 +1,5 @@ +import { limitText, MESSAGE_LIMIT } from '../utils/limit-text'; +import editResponse from '../utils/edit-response'; import errors from '../utils/errors'; import got from '../utils/got'; import { getFeedByUrl, sub } from '../proxies/rss-feed'; @@ -19,7 +21,7 @@ export default async ( ); const { lang } = ctx.state; const feedsReady = await Promise.all( - urls.map(async (url): Promise> => { + urls.map(async (url): Promise | string> => { url = decodeUrl(url); // decode first const feed = await getFeedByUrl(url); if (isSome(feed)) { @@ -35,37 +37,50 @@ export default async ( ttl: Number.isNaN(rssFeed.ttl) ? 0 : rssFeed.ttl }; } catch (e) { - ctx.reply( - `${url} ${i18n[lang]['FETCH_ERROR']} ${e.message}` + return escapeHtml( + limitText( + `${url} ${i18n[lang]['FETCH_ERROR']} ${e.message}` + ) ); - return undefined; } } }) ); - const builder = [i18n[lang]['SUB_SUCCESS']]; - feedsReady - .filter((i) => i !== undefined) - .forEach(function (feed: { - feed_title: string; - url: string; - ttl: number; - }) { - try { - sub(ctx.state.chat.id, feed.url, feed.feed_title, feed.ttl); - } catch (e) { - if (e.message !== 'ALREADY_SUB') - throw errors.newCtrlErr('DB_ERROR'); - } - // URL is percent-encoded for href, title is sanitized for display - builder.push( - `${sanitize(feed.feed_title)}` - ); - }); - if (builder.length > 1) { - // some feed did sub successfully - ctx.replyWithHTML(builder.join('\n')); + const successes: string[] = []; + const failures: string[] = []; + for (const feed of feedsReady) { + if (typeof feed === 'string') { + failures.push(feed); + continue; + } + try { + await sub(ctx.state.chat.id, feed.url, feed.feed_title, feed.ttl); + } catch (e) { + if (e.code !== 'ALREADY_SUB') + throw errors.newCtrlErr('DB_ERROR', e); + } + successes.push( + `${sanitize(limitText(feed.feed_title))}` + ); + } + const lines = successes.length + ? [i18n[lang]['SUB_SUCCESS'], ...successes, ...failures] + : failures; + const messages: string[] = []; + for (const line of lines) { + const last = messages.length - 1; + // +1 accounts for the '\n' joining the lines + if ( + last >= 0 && + messages[last].length + line.length + 1 <= MESSAGE_LIMIT + ) { + messages[last] += '\n' + line; + } else { + messages.push(line); + } } + await editResponse(ctx, messages[0], { parse_mode: 'HTML' }); + for (const message of messages.slice(1)) await ctx.replyWithHTML(message); await next(); }; diff --git a/source/middlewares/test-url.ts b/source/middlewares/test-url.ts index ccba0335178..7ebf546aa53 100644 --- a/source/middlewares/test-url.ts +++ b/source/middlewares/test-url.ts @@ -1,3 +1,4 @@ +import editResponse from '../utils/edit-response'; import got from '../utils/got'; import { findFeed, isFeedValid } from '../utils/feed'; import errors from '../utils/errors'; @@ -14,7 +15,6 @@ export default async (ctx: MContext, next: TNextFn): Promise => { if (isSome(feedOption)) { // feed is in database; ctx.state.feed = feedOption.value; - await next(); } else { try { const res = await got(url); @@ -38,17 +38,6 @@ export default async (ctx: MContext, next: TNextFn): Promise => { feed_title: realFeed.title }; ctx.state.feedUrl = ctx.state.feedUrls[0]; - await next(); // next - break; - default: - const builder = []; - builder.push( - `**${ - i18n[ctx.state.lang]['FOUND_FEED_MORE_THAN_ONE'] - }:**` - ); - builder.push(...ctx.state.feedUrls); - ctx.replyWithMarkdown(builder.join('\n')); break; } } else { @@ -57,11 +46,20 @@ export default async (ctx: MContext, next: TNextFn): Promise => { feed_title: feedOption.value.title, ttl: feedOption.value.ttl }; - await next(); } } catch (e) { if (e instanceof errors.ControllableError) throw e; throw errors.newCtrlErr('FETCH_ERROR', e); } + if (ctx.state.feedUrls?.length > 1) { + const heading = i18n[ctx.state.lang]['FOUND_FEED_MORE_THAN_ONE']; + await editResponse( + ctx, + [`**${heading}:**`, ...ctx.state.feedUrls].join('\n'), + { parse_mode: 'Markdown' } + ); + return; + } } + await next(); }; diff --git a/source/proxies/rss-feed.ts b/source/proxies/rss-feed.ts index f9f0b7b5224..4f1acaa16e2 100644 --- a/source/proxies/rss-feed.ts +++ b/source/proxies/rss-feed.ts @@ -80,9 +80,9 @@ export async function getFeedById(id: number): Promise { } } -export async function unsub(userId: number, feedId: number): Promise { +export async function unsub(userId: number, feedId: number): Promise { try { - await db('subscribes') + return await db('subscribes') .where('feed_id', feedId) .andWhere('user_id', userId) .del(); diff --git a/source/utils/edit-response.ts b/source/utils/edit-response.ts new file mode 100644 index 00000000000..af40a174b5c --- /dev/null +++ b/source/utils/edit-response.ts @@ -0,0 +1,47 @@ +import { TelegramError } from 'telegraf'; +import type { Convenience } from 'telegraf/types'; +import { MContext } from '../types/ctx'; +import errors from './errors'; + +export function badRequestDescription(error: unknown): string { + return error instanceof TelegramError && error.code === 400 + ? error.description + .toLowerCase() + .replace(/^bad request: /, '') + .replaceAll('_', ' ') + // Telegram uses both "MESSAGE_TOO_LONG" and "message is too long" + .replace('is too long', 'too long') + : ''; +} + +export function cannotEdit(description: string): boolean { + return ( + description === 'message to edit not found' || + description === "message can't be edited" + ); +} + +export default async function editResponse( + ctx: MContext, + text: string, + extra?: Convenience.ExtraEditMessageText +): Promise { + try { + await ctx.telegram.editMessageText( + ctx.chat.id, + ctx.state.processMsgId, + undefined, + text, + extra + ); + } catch (error) { + const description = badRequestDescription(error); + if (description.startsWith('message is not modified')) return; + if (description === 'message too long') { + throw errors.newCtrlErr('RESPONSE_TOO_LONG'); + } + if (!cannotEdit(description)) throw error; + const message = await ctx.reply(text, extra); + ctx.state.processMsgId = message.message_id; + } +} diff --git a/source/utils/limit-text.ts b/source/utils/limit-text.ts new file mode 100644 index 00000000000..4e5e7f5e98f --- /dev/null +++ b/source/utils/limit-text.ts @@ -0,0 +1,16 @@ +/** Telegram message text limit, counted in UTF-16 code units. */ +export const MESSAGE_LIMIT = 4096; +/** Telegram answerCbQuery toast text limit. */ +export const CB_QUERY_TEXT_LIMIT = 200; + +const graphemes = new Intl.Segmenter(); + +export function limitText(text: string, limit = MESSAGE_LIMIT): string { + if (text.length <= limit) return text; + for (const { index, segment } of graphemes.segment(text)) { + if (index + segment.length > limit - 1) { + return text.slice(0, index) + '…'; + } + } + return text; +} diff --git a/source/utils/two-key-reply.ts b/source/utils/two-key-reply.ts index 3b7c9c89580..363916da6f5 100644 --- a/source/utils/two-key-reply.ts +++ b/source/utils/two-key-reply.ts @@ -1,3 +1,4 @@ +import editResponse from './edit-response'; import { InlineKeyboardButton } from 'telegraf/types'; import { MContext, TNextFn } from '../types/ctx'; @@ -7,26 +8,15 @@ interface Middleware { export default (kbs: InlineKeyboardButton[], text?: string): Middleware => { // text is optional default to ctx.state.replyText return async (ctx, next) => { - if (ctx.state.processMsgId) { - await ctx.telegram.deleteMessage( - ctx.chat.id, - ctx.state.processMsgId - ); - ctx.state.processMsgId = null; - } - await ctx.telegram.sendMessage( - ctx.chat.id, - text || ctx.state.replyText, - { - parse_mode: 'HTML', - link_preview_options: { - is_disabled: true - }, - reply_markup: { - inline_keyboard: [[...kbs]] - } + await editResponse(ctx, text || ctx.state.replyText, { + parse_mode: 'HTML', + link_preview_options: { + is_disabled: true + }, + reply_markup: { + inline_keyboard: kbs.length ? [kbs] : [] } - ); + }); await next(); }; }; diff --git a/test/command-responses.spec.ts b/test/command-responses.spec.ts new file mode 100644 index 00000000000..364714651d5 --- /dev/null +++ b/test/command-responses.spec.ts @@ -0,0 +1,256 @@ +import { TelegramError } from 'telegraf'; +import { MContext } from '../source/types/ctx'; +import { Optional } from '../source/types/option'; +import i18n from '../source/i18n'; +import sendError, { processing } from '../source/middlewares/send-error'; +import editResponse from '../source/utils/edit-response'; +import exportToOpml from '../source/middlewares/export-to-opml'; +import subMultiUrl from '../source/middlewares/sub-multi-url'; +import testUrl from '../source/middlewares/test-url'; +import { sub, unsub } from '../source/controlers/rss'; +import { changeLangCallback } from '../source/controlers/language'; +import * as RSS from '../source/proxies/rss-feed'; +import got from '../source/utils/got'; +import { isFeedValid } from '../source/utils/feed'; + +jest.mock('../source/proxies/rss-feed'); +jest.mock('../source/proxies/users'); +jest.mock('../source/utils/got'); +jest.mock('../source/utils/feed'); + +const url = 'https://example.org/feed'; +const badRequest = (description: string) => + new TelegramError({ + error_code: 400, + description: 'Bad Request: ' + description + }); +const next = jest.fn(); + +function setup() { + const ctx = { + state: { chat: { id: 1 }, lang: 'en', processMsgId: 2, feedUrl: url }, + chat: { id: 1 }, + message: { text: url }, + telegram: { editMessageText: jest.fn(), editMessageMedia: jest.fn() }, + reply: jest.fn().mockResolvedValue({ message_id: 3 }), + replyWithDocument: jest.fn().mockResolvedValue({ message_id: 4 }), + replyWithHTML: jest.fn(), + answerCbQuery: jest.fn() + }; + return { + ctx, + context: ctx as unknown as MContext & { message: { text: string } } + }; +} + +test.each(["message can't be edited", 'message to edit not found'])( + 'recovers a list after %s', + async (description) => { + const { ctx, context } = setup(); + const options = { + parse_mode: 'HTML' as const, + reply_markup: { + inline_keyboard: [[{ text: 'Next', callback_data: 'RSS_2' }]] + } + }; + ctx.telegram.editMessageText.mockRejectedValueOnce( + badRequest(description) + ); + await editResponse(context, 'list', options); + expect(ctx.reply).toHaveBeenCalledWith('list', options); + await editResponse(context, 'next page'); + expect(ctx.telegram.editMessageText).toHaveBeenLastCalledWith( + 1, + 3, + undefined, + 'next page', + undefined + ); + } +); + +test('acknowledges a repeated page without sending another message', async () => { + const { ctx, context } = setup(); + Object.assign(ctx, { callbackQuery: { message: { message_id: 5 } } }); + ctx.telegram.editMessageText.mockRejectedValueOnce( + badRequest('message is not modified: unchanged') + ); + await sendError(context, () => editResponse(context, 'page')); + expect(ctx.answerCbQuery).toHaveBeenCalledWith(); + expect(ctx.reply).not.toHaveBeenCalled(); +}); + +test('clears the picker when sending processing, then edits that response', async () => { + const { ctx, context } = setup(); + await processing(context, () => editResponse(context, 'done'), { + removeKeyboard: true + }); + expect(ctx.reply).toHaveBeenCalledWith(i18n.en.PROCESSING, { + reply_markup: { remove_keyboard: true } + }); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 1, + 3, + undefined, + 'done', + undefined + ); +}); + +test.each(['MESSAGE_TOO_LONG', 'message is too long'])( + 'reports %s with a short localized response', + async (description) => { + const { ctx, context } = setup(); + ctx.telegram.editMessageText.mockRejectedValueOnce( + badRequest(description) + ); + await sendError(context, () => editResponse(context, 'x'.repeat(4097))); + expect(ctx.reply).toHaveBeenCalledTimes(1); + expect(ctx.telegram.editMessageText).toHaveBeenLastCalledWith( + 1, + 3, + undefined, + i18n.en.RESPONSE_TOO_LONG, + undefined + ); + } +); + +describe('OPML export', () => { + beforeEach(() => { + jest.mocked(RSS.getSubscribedFeedsByUserId).mockResolvedValue([ + { feed_title: 'Example', url } + ] as Awaited>); + }); + test('attaches the document to the processing message', async () => { + const { ctx, context } = setup(); + await exportToOpml(context, next); + const [chat, id, , media] = ctx.telegram.editMessageMedia.mock.calls[0]; + expect([chat, id, media.type, media.media.filename]).toEqual([ + 1, + 2, + 'document', + 'export.opml' + ]); + expect(media.media.source.toString()).toContain(url); + expect(ctx.replyWithDocument).not.toHaveBeenCalled(); + }); + test('sends the same file once and marks the old message complete after a rejection', async () => { + const { ctx, context } = setup(); + ctx.telegram.editMessageMedia.mockRejectedValueOnce( + badRequest('there is no media in the message to edit') + ); + await exportToOpml(context, next); + expect(ctx.replyWithDocument).toHaveBeenCalledTimes(1); + expect(ctx.replyWithDocument).toHaveBeenCalledWith( + ctx.telegram.editMessageMedia.mock.calls[0][3].media + ); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 1, + 2, + undefined, + i18n.en.EXPORT_SUCCESS + ); + }); + test('does not resend a file after an ambiguous timeout', async () => { + const { ctx, context } = setup(); + const timeout = new Error('timeout'); + ctx.telegram.editMessageMedia.mockRejectedValueOnce(timeout); + await expect(exportToOpml(context, next)).rejects.toBe(timeout); + expect(ctx.replyWithDocument).not.toHaveBeenCalled(); + }); +}); + +test('reports an already removed subscription even when the feed exists', async () => { + const { context } = setup(); + jest.mocked(RSS.getFeedByUrl).mockResolvedValue( + Optional({ feed_id: 5 }) as Awaited> + ); + jest.mocked(RSS.unsub).mockResolvedValue(0); + await expect(unsub(context, next)).rejects.toMatchObject({ + code: 'DID_NOT_SUB' + }); +}); + +test('acknowledges a language change without duplicating its confirmation', async () => { + const { ctx, context } = setup(); + Object.assign(ctx, { + callbackQuery: { data: 'CHANGE_LANG_en_1', message: { message_id: 9 } } + }); + await sendError(context, () => changeLangCallback(context, next)); + expect(ctx.answerCbQuery).toHaveBeenCalledWith(); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 1, + 9, + undefined, + i18n.en.SET_LANG_TO + ' en', + undefined + ); +}); + +test('preserves a short feed title when its link target is long', async () => { + const { ctx, context } = setup(); + context.state.feedUrl = url + '?q=' + 'x'.repeat(4060); + context.state.feed = { feed_title: 'Complete feed title' }; + + await sub(context, next); + + expect(ctx.telegram.editMessageText.mock.calls[0][3]).toContain( + '>Complete feed title' + ); +}); + +test('limits the visible confirmation before escaping a long feed title', async () => { + const { ctx, context } = setup(); + context.state.feed = { feed_title: '<' + '👩‍💻'.repeat(2500) }; + await sub(context, next); + const html = ctx.telegram.editMessageText.mock.calls[0][3]; + expect(html).toMatch(/<(👩‍💻)+…<\/a>$/u); + expect( + html.replace(/<[^>]*>/g, '').replace('<', '<').length + ).toBeLessThanOrEqual(4096); +}); + +test('delivers both an oversized title and an oversized fetch error in a batch', async () => { + const { ctx, context } = setup(); + ctx.message.text += '\nhttps://example.org/broken'; + jest.mocked(RSS.getFeedByUrl) + .mockResolvedValueOnce( + Optional({ feed_title: 'X'.repeat(5000), url }) as Awaited< + ReturnType + > + ) + .mockResolvedValueOnce(Optional(undefined)); + jest.mocked(got).mockRejectedValueOnce(new Error('<'.repeat(6000))); + await subMultiUrl(context, next); + const messages = [ + ctx.telegram.editMessageText.mock.calls[0][3], + ...ctx.replyWithHTML.mock.calls.map(([html]) => html) + ]; + expect(messages.join('\n')).toContain('…'); + expect(messages.join('\n')).toContain('<…'); + for (const html of messages) + expect( + html.replace(/<[^>]*>/g, '').replaceAll('<', '<').length + ).toBeLessThanOrEqual(4096); +}); + +test('preserves downstream errors after discovering a new feed', async () => { + const { context } = setup(); + jest.mocked(RSS.getFeedByUrl).mockResolvedValueOnce(Optional(undefined)); + jest.mocked(got).mockResolvedValueOnce({ + url, + textConverted: async () => 'feed' + } as Awaited>); + jest.mocked(isFeedValid).mockResolvedValueOnce( + Optional({ title: 'Example' }) as Awaited< + ReturnType + > + ); + const error = new Error('Telegram timeout'); + await expect( + testUrl(context, async () => { + throw error; + }) + ).rejects.toBe(error); +}); diff --git a/test/import-from-opml.spec.ts b/test/import-from-opml.spec.ts index ce0cdbc7ff0..c85df20a7a7 100644 --- a/test/import-from-opml.spec.ts +++ b/test/import-from-opml.spec.ts @@ -36,5 +36,11 @@ test('import from opml', async () => { return { type: 'rss', text: item.feed_title, xmlUrl: item.url }; }); expect(ctx).toHaveProperty('state.outlines'); - expect(ctx).toHaveProperty('state.processMsgId'); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 233233233, + 42, + undefined, + expect.stringContaining(' { }); test('RSS unsub', async () => { - await RSS.unsub(userId, 1); + expect(await RSS.unsub(userId, 1)).toBe(1); + expect(await RSS.unsub(userId, 1)).toBe(0); // subscription deleted const subscribe = await SUBSCRIBES.getSubscribersByFeedId(1); expect(subscribe).toHaveProperty('length', 0); diff --git a/test/send-error.spec.ts b/test/send-error.spec.ts index a73447ace73..9db94864c39 100644 --- a/test/send-error.spec.ts +++ b/test/send-error.spec.ts @@ -1,5 +1,8 @@ /* eslint @typescript-eslint/no-empty-function:0 */ /* eslint no-empty-function:0 */ +import errors from '../source/utils/errors'; +import twoKeyReply from '../source/utils/two-key-reply'; +import i18n from '../source/i18n'; import sendError from '../source/middlewares/send-error'; import userAllowList from '../source/middlewares/user_allow_list'; import { MContext } from '../source/types/ctx'; @@ -23,3 +26,50 @@ test('sendError@message', async () => { expect(ctx).toHaveProperty('state.processMsgId', 233); expect(ctx).toHaveProperty('state.lang', 'en'); }); + +test('edits the processing reply in the command chat on error', async () => { + const ctx = { + chat: { id: 123 }, + state: { lang: 'en', chat: { id: -456 } }, + reply: jest.fn().mockResolvedValue({ message_id: 7 }), + telegram: { editMessageText: jest.fn() } + }; + await sendError(ctx as unknown as MContext, () => { + throw errors.newCtrlErr('CHANNEL_ADMIN_REQUIRE'); + }); + expect(ctx.reply).toHaveBeenCalledTimes(1); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 123, + 7, + undefined, + i18n.en.CHANNEL_ADMIN_REQUIRE, + undefined + ); +}); + +test('pagination edits the existing list and acknowledges the button', async () => { + const ctx = { + chat: { id: 123 }, + state: { lang: 'en' }, + callbackQuery: { message: { message_id: 8 } }, + reply: jest.fn(), + answerCbQuery: jest.fn(), + telegram: { editMessageText: jest.fn() } + }; + const context = ctx as unknown as MContext; + const buttons = [{ text: 'Back', callback_data: 'RSS_1' }]; + await sendError(context, () => + twoKeyReply(buttons, 'Page 2')(context, jest.fn()) + ); + expect(ctx.reply).not.toHaveBeenCalled(); + expect(ctx.telegram.editMessageText).toHaveBeenCalledWith( + 123, + 8, + undefined, + 'Page 2', + expect.objectContaining({ + reply_markup: { inline_keyboard: [buttons] } + }) + ); + expect(ctx.answerCbQuery).toHaveBeenCalledTimes(1); +}); diff --git a/test/test-data/ctx/import-from-opml-ctx.ts b/test/test-data/ctx/import-from-opml-ctx.ts index c76b043f2c3..6d288146a6e 100644 --- a/test/test-data/ctx/import-from-opml-ctx.ts +++ b/test/test-data/ctx/import-from-opml-ctx.ts @@ -1,5 +1,7 @@ export default { + chat: { id: 233233233 }, state: { + processMsgId: 42, lang: 'en', fileLink: '', chat: { @@ -11,7 +13,6 @@ export default { } }, telegram: { - deleteMessage: jest.fn() - }, - replyWithHTML: jest.fn() + editMessageText: jest.fn() + } };