Skip to content

Commit 801f638

Browse files
committed
Remove transcription download from history command
1 parent 07062e2 commit 801f638

3 files changed

Lines changed: 11 additions & 109 deletions

File tree

handlers/max/history.py

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33

44
import aiomax
55

6-
from pathlib import Path
7-
8-
from aiomax.buttons import CallbackButton, KeyboardBuilder
9-
10-
from database.models import PLATFORM_MAX, STATUS_COMPLETED, is_owner
11-
from database.queries import get_recent_transcriptions, get_transcription
12-
from utils.s3 import download_text, object_name_from_url
6+
from database.models import PLATFORM_MAX
7+
from database.queries import get_recent_transcriptions
138
from utils.utils import format_duration, MoscowTimezone
149
from utils.tg import STATUS_EMOJI, fmt_price
1510
from utils.sentry import sentry_bind_user_max, sentry_transaction
16-
from messengers.max import safe_callback_answer, safe_send_document, safe_send_message
11+
from messengers.max import safe_send_message
1712

1813

1914
@sentry_bind_user_max
@@ -36,7 +31,6 @@ async def handle_max_history(message: aiomax.Message, bot: aiomax.Bot) -> None:
3631
return
3732

3833
lines: list[str] = []
39-
doc_buttons: list[CallbackButton] = []
4034
for r in items:
4135
status = r.status if isinstance(r.status, str) else ""
4236
emoji = STATUS_EMOJI[status] if status in STATUS_EMOJI else "•"
@@ -47,51 +41,11 @@ async def handle_max_history(message: aiomax.Message, bot: aiomax.Bot) -> None:
4741
dur = format_duration(r.duration_seconds)
4842
price_for_user = fmt_price(r.price_for_user)
4943
lines.append(f"{emoji} {dt_str} МСК • {dur}{price_for_user}")
50-
if status == STATUS_COMPLETED and r.result_s3_path:
51-
doc_buttons.append(CallbackButton(f"📄 {dt_str}{dur}", f"history_doc:{r.id}"))
5244

5345
msg = (
5446
"Последние 10 распознаваний:\n"
5547
+ "\n".join(lines)
5648
+ "\n\nСтатусы: 🕓 ожидание • ⏳ в работе • ✅ готово • ❌ ошибка • 🚫 отменено"
5749
)
5850

59-
keyboard = None
60-
if doc_buttons:
61-
msg += "\n\nГотовые тексты можно получить ещё раз:"
62-
keyboard = KeyboardBuilder()
63-
for btn in doc_buttons:
64-
keyboard = keyboard.row(btn)
65-
66-
await safe_send_message(bot, msg, chat_id=message.recipient.chat_id, keyboard=keyboard)
67-
68-
69-
@sentry_bind_user_max
70-
@sentry_transaction(name="history.document", op="max.callback")
71-
async def handle_max_history_doc(callback: aiomax.Callback, bot: aiomax.Bot) -> None:
72-
"""Re-send a finished transcription document from S3."""
73-
await safe_callback_answer(callback, notification="")
74-
75-
try:
76-
transcription_id = int(callback.payload.split(":", 1)[1])
77-
user_id = int(callback.user.user_id)
78-
except (IndexError, ValueError, TypeError, AttributeError):
79-
return
80-
81-
transcription = get_transcription(transcription_id)
82-
if not is_owner(transcription, user_id, PLATFORM_MAX):
83-
return
84-
if not transcription.result_s3_path:
85-
return
86-
87-
object_name = object_name_from_url(transcription.result_s3_path)
88-
text = await download_text(object_name)
89-
if not text:
90-
await safe_send_message(bot,
91-
"❌ Не удалось получить текст\n\n"
92-
"Попробуйте ещё раз чуть позже",
93-
chat_id=callback.message.recipient.chat_id,
94-
)
95-
return
96-
97-
await safe_send_document(bot, user_id, text.encode("utf-8"), Path(object_name).name, "")
51+
await safe_send_message(bot, msg, chat_id=message.recipient.chat_id)

handlers/telegram/history.py

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
from pathlib import Path
2-
3-
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, InputFile, Update
1+
from telegram import Update
42
from telegram.ext import ContextTypes
53

6-
from database.models import PLATFORM_TELEGRAM, STATUS_COMPLETED, is_owner
7-
from database.queries import get_recent_transcriptions, get_transcription
4+
from database.models import PLATFORM_TELEGRAM
5+
from database.queries import get_recent_transcriptions
86

9-
from utils.s3 import download_text, object_name_from_url
107
from utils.sentry import sentry_bind_user, sentry_transaction
118
from utils.utils import format_duration, MoscowTimezone
129
from utils.tg import STATUS_EMOJI, fmt_price
13-
from messengers.telegram import safe_query_answer, safe_reply_text, safe_send_document
10+
from messengers.telegram import safe_reply_text
1411

1512

1613
@sentry_bind_user
@@ -27,9 +24,7 @@ async def handle_history(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
2724
)
2825
return
2926

30-
3127
lines: list[str] = []
32-
buttons: list[list[InlineKeyboardButton]] = []
3328
for r in items:
3429
status = r.status if isinstance(r.status, str) else ""
3530
emoji = STATUS_EMOJI[status] if status in STATUS_EMOJI else "•"
@@ -40,53 +35,11 @@ async def handle_history(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
4035
dur = format_duration(r.duration_seconds)
4136
price_for_user = fmt_price(r.price_for_user)
4237
lines.append(f"{emoji} {dt_str} МСК • {dur}{price_for_user}")
43-
if status == STATUS_COMPLETED and r.result_s3_path:
44-
buttons.append([InlineKeyboardButton(
45-
f"📄 {dt_str}{dur}", callback_data=f"history_doc:{r.id}"
46-
)])
4738

4839
msg = (
4940
"Последние 10 распознаваний:\n"
5041
+ "\n".join(lines)
5142
+ "\n\nСтатусы: 🕓 ожидание • ⏳ в работе • ✅ готово • ❌ ошибка • 🚫 отменено"
5243
)
53-
if buttons:
54-
msg += "\n\nГотовые тексты можно получить ещё раз:"
55-
56-
await safe_reply_text(
57-
update.message, msg,
58-
reply_markup=InlineKeyboardMarkup(buttons) if buttons else None,
59-
)
6044

61-
62-
@sentry_bind_user
63-
@sentry_transaction(name="history.document", op="telegram.callback")
64-
async def handle_history_doc(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
65-
"""Re-send a finished transcription document from S3."""
66-
query = update.callback_query
67-
await safe_query_answer(query)
68-
69-
transcription_id = int(query.data.split(":", 1)[1])
70-
transcription = get_transcription(transcription_id)
71-
if not is_owner(transcription, query.from_user.id, PLATFORM_TELEGRAM):
72-
return
73-
if not transcription.result_s3_path:
74-
return
75-
76-
object_name = object_name_from_url(transcription.result_s3_path)
77-
text = await download_text(object_name)
78-
if not text:
79-
await safe_reply_text(
80-
query.message,
81-
"❌ Не удалось получить текст\n\n"
82-
"Попробуйте ещё раз чуть позже"
83-
)
84-
return
85-
86-
await safe_send_document(
87-
context.bot,
88-
query.message.chat_id,
89-
query.message.message_id,
90-
InputFile(text.encode("utf-8"), filename=Path(object_name).name),
91-
"",
92-
)
45+
await safe_reply_text(update.message, msg)

main.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from handlers.telegram.cancel_task import handle_cancel_task
3030
from handlers.telegram.create_task import handle_create_task
3131
from handlers.telegram.file import handle_file
32-
from handlers.telegram.history import handle_history, handle_history_doc
32+
from handlers.telegram.history import handle_history
3333
from handlers.telegram.price import handle_price
3434
from handlers.telegram.text import handle_text, handle_unsupported
3535
from handlers.telegram.topup import handle_topup, handle_topup_callback, handle_cancel_payment
@@ -43,7 +43,7 @@
4343
from handlers.max.cancel_task import handle_max_cancel_task
4444
from handlers.max.create_task import handle_max_create_task
4545
from handlers.max.file import handle_max_file
46-
from handlers.max.history import handle_max_history, handle_max_history_doc
46+
from handlers.max.history import handle_max_history
4747
from handlers.max.price import handle_max_price
4848
from handlers.max.rate_transcription import handle_max_rate
4949
from handlers.max.summarize import handle_max_summarize
@@ -158,9 +158,6 @@ async def run_bots() -> None:
158158
application.add_handler(
159159
CallbackQueryHandler(handle_timecodes_format, pattern=r"^tc_fmt:\d+:(txt|srt|vtt)$")
160160
)
161-
application.add_handler(
162-
CallbackQueryHandler(handle_history_doc, pattern=r"^history_doc:\d+$")
163-
)
164161

165162
# --- Build Max bot (optional) ---
166163
max_bot = None
@@ -263,8 +260,6 @@ async def _on_max_callback(callback: aiomax.Callback) -> None:
263260
await handle_max_send_as_text(callback, max_bot)
264261
elif payload.startswith("improve:"):
265262
await handle_max_improve(callback, max_bot)
266-
elif payload.startswith("history_doc:"):
267-
await handle_max_history_doc(callback, max_bot)
268263
elif payload.startswith("tc_fmt:"):
269264
await handle_max_timecodes_format(callback, max_bot)
270265
elif payload.startswith("tc_back:"):

0 commit comments

Comments
 (0)