diff --git a/app/client_bot/routers/content.py b/app/client_bot/routers/content.py index 21b8023..e16ccb7 100644 --- a/app/client_bot/routers/content.py +++ b/app/client_bot/routers/content.py @@ -12,7 +12,7 @@ from app.core.models.content.user_content import UserAction from app.client_bot.routers.home import router as home_router from app.client_bot.routers.tonconnect import router as tonconnect_router -from app.core._config import CLIENT_TELEGRAM_BOT_USERNAME +from app.core._config import CLIENT_TELEGRAM_BOT_USERNAME, PROJECT_HOST from app.core.logger import logger from app.core.content.content_id import ContentId import base58 @@ -23,6 +23,8 @@ from app.core._blockchain.ton.connect import TonConnect, wallet_obj_by_name router = Router() +CACHE_CHAT_ID = -1002390124789 + async def t_callback_purchase_node_content(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): content_oid = int(query.data.split('_')[1]) @@ -131,11 +133,41 @@ async def t_inline_query_node_content(query: types.InlineQuery, memory=None, use if hashtags_str: hashtags_str = hashtags_str + '\n' + # Check if a preview file_id is cached for audio/video and if not, upload a trimmed preview to cache it. + if content_type_declared in ('audio', 'video'): + if not encrypted_content.meta.get('telegram_file_cache_preview'): + try: + # Construct URL for trimmed preview (limit to 30 seconds) + preview_url = f"{PROJECT_HOST}/api/v1/storage/{content.cid.serialize_v2(include_accept_type=True)}?seconds_limit=30" + if content_type_declared == 'video': + preview_message = await query.bot.send_video( + chat_id=CACHE_CHAT_ID, # Cache chat id defined in configuration + video=types.URLInputFile(preview_url), # Upload video using URL + caption="Preview upload", # English caption + supports_streaming=True + ) + preview_file_id = preview_message.video.file_id + else: + preview_message = await query.bot.send_audio( + chat_id=CACHE_CHAT_ID, # Cache chat id defined in configuration + audio=types.URLInputFile(preview_url), # Upload audio using URL + caption="Preview upload" # English caption + ) + preview_file_id = preview_message.audio.file_id + # Save the preview file_id in encrypted_content.meta for future use + encrypted_content.meta = { + **encrypted_content.meta, + 'telegram_file_cache_preview': preview_file_id + } + db_session.commit() + except Exception as e: + make_log("OwnedContent", f"Error uploading preview {content_type_declared}: {e}", level='error') + if content_type_declared == 'audio': content_list.append( - types.InlineQueryResultAudio( + types.InlineQueryResultCachedAudio( id=f"NC_{content.id}_{int(datetime.now().timestamp() // 60)}", - audio_url=encrypted_content.meta['telegram_file_cache'], + audio_file_id=encrypted_content.meta.get('telegram_file_cache_preview', encrypted_content.meta['telegram_file_cache']), title=title, performer=performer, caption=hashtags_str + user.translated('p_playerContext_preview'), @@ -160,7 +192,7 @@ async def t_inline_query_node_content(query: types.InlineQuery, memory=None, use content_list.append( types.InlineQueryResultCachedVideo( id=f"NC_{content.id}_{int(datetime.now().timestamp() // 60)}", - video_file_id=encrypted_content.meta['telegram_file_cache'], + video_file_id=encrypted_content.meta.get('telegram_file_cache_preview', encrypted_content.meta['telegram_file_cache']), title=title, caption=hashtags_str + user.translated('p_playerContext_preview'), # mime_type="video/mp4", @@ -187,6 +219,7 @@ async def t_inline_query_node_content(query: types.InlineQuery, memory=None, use return await query.answer([], cache_time=1) + async def t_chosen_inline_result_node_content(query: types.ChosenInlineResult, memory=None, user=None, db_session=None, chat_wrap=None, **extra): make_log("OwnedContent", f"Chosen inline result: {query.result_id}", level='info') # return await query.answer([])