From 7a0fa19a552897073b1a271e04e79c0b17be1b68 Mon Sep 17 00:00:00 2001 From: Damir Modyarov Date: Tue, 9 Jul 2024 22:27:50 +0300 Subject: [PATCH] feat: Improve URL extraction --- locales/en.ftl | 2 +- locales/ru.ftl | 2 +- src/core/data/request.ts | 18 +++++++++++++++--- src/telegram/bot/download.ts | 7 +++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/locales/en.ftl b/locales/en.ftl index beb6e00..43a0601 100644 --- a/locales/en.ftl +++ b/locales/en.ftl @@ -2,7 +2,7 @@ start = hii! just send me a link and i'll download it. (ᵔᵕᵔ)◜ error-title = error error = { error-title }: { $message } { $error-emoticon } -error-not-url = doesn't look like a url to me +error-not-url = i couldn't find url in your message error-request-not-found = looks like i forgot your link, try sending it again error-not-button-owner = looks like this button is not yours (¬_¬") error-too-large = sorry, but this file is too big - telegram doesn't allow me to upload it diff --git a/locales/ru.ftl b/locales/ru.ftl index a04214f..3b4260c 100644 --- a/locales/ru.ftl +++ b/locales/ru.ftl @@ -2,7 +2,7 @@ start = привет! просто отправь мне ссылку и я её error-title = ошибка error = { error-title }: { $message } { $error-emoticon } -error-not-url = не похоже на ссылку +error-not-url = я не шаёл ссылки в твоём сообщении error-request-not-found = похоже я потерял твою ссылку, можешь отправить её снова? error-not-button-owner = похоже что эта кнопка не твоя (¬_¬") error-too-large = этот файл слишком большой, к сожалению тг не даёт его загрузить diff --git a/src/core/data/request.ts b/src/core/data/request.ts index f092d80..e6363aa 100644 --- a/src/core/data/request.ts +++ b/src/core/data/request.ts @@ -8,6 +8,18 @@ import { db } from "#core/data/db/database" import { fetchMedia, fetchStream, SuccessfulCobaltMediaResponse } from "#core/data/cobalt" const mediaUrlSchema = z.string().url() +const tryParseUrl = (url: string) => { + const originalParsed = mediaUrlSchema.safeParse(url) + if (originalParsed.success) return originalParsed.data + + const domain = url.split("/")[0] + if (!domain.includes(".") || domain.includes(" ") || domain.includes(":")) return null + + const withHttpsParsed = mediaUrlSchema.safeParse(`https://${url}`) + if (withHttpsParsed.success) return withHttpsParsed.data + + return null +} export type MediaRequest = InferSelectModel @@ -15,14 +27,14 @@ export const createRequest = async ( userInput: string, authorId: number, ): Promise> => { - const url = mediaUrlSchema.safeParse(userInput) - if (!url.success) return error(translatable("error-not-url")) + const url = tryParseUrl(userInput) + if (!url) return error(translatable("error-not-url")) const id = randomUUID() const req = { id, authorId, - url: url.data, + url, } await db .insert(requests) diff --git a/src/telegram/bot/download.ts b/src/telegram/bot/download.ts index 1249be2..69b73dd 100644 --- a/src/telegram/bot/download.ts +++ b/src/telegram/bot/download.ts @@ -17,7 +17,10 @@ export const downloadDp = Dispatcher.child() downloadDp.onNewMessage(filters.chat("private"), async (msg) => { const { e, t } = await evaluatorsFor(msg.sender) - const req = await createRequest(msg.text, msg.sender.id) + const urlEntity = msg.entities.find(e => e.is("text_link") || e.is("url")) + const extractedUrl = urlEntity && (urlEntity.is("text_link") ? urlEntity.params.url : urlEntity.text) + const req = await createRequest(extractedUrl || msg.text, msg.sender.id) + if (!req.success) { await msg.replyText(t("error", { message: e(req.error) })) return @@ -55,7 +58,7 @@ downloadDp.onInlineQuery(async (ctx) => { return } - const req = await createRequest(ctx.query, ctx.user.id) + const req = await createRequest(ctx.query.trim(), ctx.user.id) if (!req.success) { await ctx.answer([ BotInline.article(randomUUID(), {