diff --git a/auth.py b/auth.py index d24d50f..edab3e8 100644 --- a/auth.py +++ b/auth.py @@ -73,11 +73,11 @@ async def verify_telegram_initdata(init_data: str): user_id = user_data.get('id') username = user_data.get('username', '') - first_name = user_data.get('first_name', '') - + # 🔧 ФИКС (25.09.2026): unused_first_name — удалена мёртвая переменная. + if not user_id: raise HTTPException(401, "User ID not found in initData") - + return user_id, username except HTTPException: diff --git a/database.py b/database.py index 4b94aeb..2ad9899 100644 --- a/database.py +++ b/database.py @@ -131,15 +131,18 @@ async def init_db(): ALTER COLUMN user_id TYPE BIGINT """) - # 🔧 ФИКС (24.09.2026): add_pk_on_duplicates — - # удаляем дубли перед ADD PRIMARY KEY. - # Оставляем строку с максимальным completed_at (или первую). + # 🔧 ФИКС (25.09.2026): duplicate_delete_keeps_wrong_row — + # оставляем строку с максимальным completed_at, затем score. + # Удаляем все остальные дубли по (user_id, level_id). await conn.execute(""" - DELETE FROM user_progress a - USING user_progress b - WHERE a.ctid < b.ctid - AND a.user_id = b.user_id - AND a.level_id = b.level_id + DELETE FROM user_progress + WHERE ctid NOT IN ( + SELECT DISTINCT ON (user_id, level_id) ctid + FROM user_progress + ORDER BY user_id, level_id, + completed_at DESC NULLS LAST, + score DESC + ) """) await conn.execute(""" diff --git a/main.py b/main.py index af86e55..7201437 100644 --- a/main.py +++ b/main.py @@ -126,23 +126,25 @@ class RateLimiter: rate_limiter = RateLimiter() -# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof — -# проверяем, что IP клиента входит в список Cloudflare. +# 🔧 ФИКС (25.09.2026): cf_ip_check_per_request — +# компилируем CF_IPS один раз при старте, а не парсим 22 CIDR на каждый запрос. +_CF_NETWORKS = [] +for _cidr in CF_IPS: + try: + _CF_NETWORKS.append(ipaddress.ip_network(_cidr, strict=False)) + except ValueError: + logger.warning(f"Invalid CF_IPS entry skipped: {_cidr}") + + def _is_cloudflare_ip(ip_str: str) -> bool: - """Проверяет, что IP входит в CF_IPS (Cloudflare).""" + """Проверяет, что IP входит в предкомпилированные сети Cloudflare.""" if not ip_str: return False try: ip = ipaddress.ip_address(ip_str) except ValueError: return False - for cidr in CF_IPS: - try: - if ip in ipaddress.ip_network(cidr, strict=False): - return True - except ValueError: - continue - return False + return any(ip in net for net in _CF_NETWORKS) # ============ Фоновое обновление кэша рейтинга ============ @@ -171,10 +173,9 @@ async def lifespan(app: FastAPI): await init_achievements() await init_learning_data() - # 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled — - # сохраняем ссылку и отменяем в shutdown. - cleanup_task = asyncio.create_task(rate_limiter.start_cleanup()) - + # 🔧 ФИКС (25.09.2026): cleanup_task_leak_on_startup_failure — + # создаём cleanup_task ПОСЛЕ успешной инициализации БД и matview. + # Если что-то упадёт до этого — задача не создастся, утечки не будет. from database import get_pool pool = await get_pool() async with pool.acquire() as conn: @@ -227,6 +228,9 @@ async def lifespan(app: FastAPI): logger.info("Leaderboard cache created") refresh_task = asyncio.create_task(refresh_leaderboard_cache_loop()) + # 🔧 ФИКС (25.09.2026): cleanup_task_leak_on_startup_failure — + # создаём cleanup_task только после успешной инициализации. + cleanup_task = asyncio.create_task(rate_limiter.start_cleanup()) logger.info(f"Filwords API v{APP_VERSION} started") @@ -251,7 +255,18 @@ async def lifespan(app: FastAPI): logger.info(f"Filwords API v{APP_VERSION} stopped") -app = FastAPI(title="Filwords PL API", lifespan=lifespan) +# 🔧 ФИКС (25.09.2026): fastapi_docs_exposed — +# в проде скрываем /docs, /redoc, /openapi.json. +_ENV = os.getenv("ENV", "production") +_is_prod = _ENV == "production" + +app = FastAPI( + title="Filwords PL API", + lifespan=lifespan, + docs_url=None if _is_prod else "/docs", + redoc_url=None if _is_prod else "/redoc", + openapi_url=None if _is_prod else "/openapi.json", +) # ============ Rate Limiting Middleware ============ diff --git a/routers/admin_content.py b/routers/admin_content.py index 568100c..9622be0 100644 --- a/routers/admin_content.py +++ b/routers/admin_content.py @@ -647,37 +647,53 @@ async def get_lesson_levels(request: Request): ORDER BY sort_order """) + if not level_rows: + return {"levels": []} + + level_codes = [lvl["code"] for lvl in level_rows] + + # 🔧 ФИКС (25.09.2026): admin_content_lesson_levels_n_plus_1 — + # 3 GROUP BY запроса вместо 3×N. + topics_rows = await conn.fetch(""" + SELECT level_code, COUNT(*) as cnt + FROM learning_topics + WHERE level_code = ANY($1::text[]) AND is_active = TRUE + GROUP BY level_code + """, level_codes) + topics_map = {r["level_code"]: r["cnt"] for r in topics_rows} + + lessons_rows = await conn.fetch(""" + SELECT t.level_code, COUNT(*) as cnt + FROM learning_lessons l + JOIN learning_topics t ON l.topic_id = t.topic_id + WHERE t.level_code = ANY($1::text[]) AND l.is_active = TRUE + GROUP BY t.level_code + """, level_codes) + lessons_map = {r["level_code"]: r["cnt"] for r in lessons_rows} + + last_edited_rows = await conn.fetch(""" + SELECT level_code, MAX(edited_at) as last_edited + FROM ( + SELECT t.level_code, t.edited_at FROM learning_topics t + WHERE t.level_code = ANY($1::text[]) + UNION ALL + SELECT t.level_code, l.edited_at FROM learning_lessons l + JOIN learning_topics t ON l.topic_id = t.topic_id + WHERE t.level_code = ANY($1::text[]) + ) sub + GROUP BY level_code + """, level_codes) + last_edited_map = {r["level_code"]: r["last_edited"] for r in last_edited_rows} + result = [] for lvl in level_rows: code = lvl["code"] - - topics_count = await conn.fetchval(""" - SELECT COUNT(*) FROM learning_topics - WHERE level_code = $1 AND is_active = TRUE - """, code) or 0 - - lessons_count = await conn.fetchval(""" - SELECT COUNT(*) FROM learning_lessons l - JOIN learning_topics t ON l.topic_id = t.topic_id - WHERE t.level_code = $1 AND l.is_active = TRUE - """, code) or 0 - - last_edited = await conn.fetchval(""" - SELECT MAX(edited_at) FROM ( - SELECT t.edited_at FROM learning_topics t - WHERE t.level_code = $1 - UNION ALL - SELECT l.edited_at FROM learning_lessons l - JOIN learning_topics t ON l.topic_id = t.topic_id - WHERE t.level_code = $1 - ) sub - """, code) - + last_edited = last_edited_map.get(code) result.append({ "code": code, "name_ru": lvl["name_ru"], - "topics_count": topics_count, - "lessons_count": lessons_count, + "topics_count": topics_map.get(code, 0), + "lessons_count": lessons_map.get(code, 0), "last_edited": last_edited.isoformat() if last_edited else None, }) @@ -709,20 +725,32 @@ async def get_lesson_topics(request: Request, level: str): ORDER BY order_number ASC """, level) + if not rows: + return {"level": level, "topics": []} + + topic_ids = [r["topic_id"] for r in rows] + + # 🔧 ФИКС (25.09.2026): admin_content_lesson_topics_n_plus_1 — + # 2 GROUP BY запроса вместо 2×N. + lessons_rows = await conn.fetch(""" + SELECT topic_id, COUNT(*) as cnt + FROM learning_lessons + WHERE topic_id = ANY($1::text[]) AND is_active = TRUE + GROUP BY topic_id + """, topic_ids) + lessons_map = {r["topic_id"]: r["cnt"] for r in lessons_rows} + + test_rows = await conn.fetch(""" + SELECT topic_id, COUNT(*) as cnt + FROM learning_questions + WHERE topic_id = ANY($1::text[]) AND is_test_question = TRUE + GROUP BY topic_id + """, topic_ids) + test_map = {r["topic_id"]: r["cnt"] for r in test_rows} + result = [] for r in rows: topic_id = r["topic_id"] - - lessons_count = await conn.fetchval(""" - SELECT COUNT(*) FROM learning_lessons - WHERE topic_id = $1 AND is_active = TRUE - """, topic_id) or 0 - - test_questions_count = await conn.fetchval(""" - SELECT COUNT(*) FROM learning_questions - WHERE topic_id = $1 AND is_test_question = TRUE - """, topic_id) or 0 - result.append({ "topic_id": topic_id, "order_number": r["order_number"], @@ -730,8 +758,8 @@ async def get_lesson_topics(request: Request, level: str): "name_ru": r["name_ru"], "name_uk": r["name_uk"], "name_en": r["name_en"], - "lessons_count": lessons_count, - "test_questions_count": test_questions_count, + "lessons_count": lessons_map.get(topic_id, 0), + "test_questions_count": test_map.get(topic_id, 0), }) return {"level": level, "topics": result} @@ -876,15 +904,26 @@ async def get_lesson(request: Request, lesson_id: str): """) wb_map = {w["wrong_because"]: w["explanation_ru"] for w in wb_rows} - questions = [] - for q in questions_rows: - options_rows = await conn.fetch(""" - SELECT option_key, text_pl, text_ru, text_uk, text_en, + # 🔧 ФИКС (25.09.2026): admin_content_get_lesson_n_plus_1 — + # один запрос ANY + группировка. + question_ids = [q["question_id"] for q in questions_rows] + all_options = [] + if question_ids: + all_options = await conn.fetch(""" + SELECT question_id, option_key, text_pl, text_ru, text_uk, text_en, is_correct, wrong_because FROM question_options - WHERE question_id = $1 - ORDER BY option_key ASC - """, q["question_id"]) + WHERE question_id = ANY($1::text[]) + ORDER BY question_id, option_key ASC + """, question_ids) + + options_by_q: dict = {} + for o in all_options: + options_by_q.setdefault(o["question_id"], []).append(o) + + questions = [] + for q in questions_rows: + options_rows = options_by_q.get(q["question_id"], []) options = [] for o in options_rows: diff --git a/routers/hints.py b/routers/hints.py index 9ed3e3c..a7944bc 100644 --- a/routers/hints.py +++ b/routers/hints.py @@ -38,6 +38,17 @@ async def get_first_letter_hint(request: Request, level_id: int): if hints_already_used >= MAX_HINTS: raise HTTPException(403, "Hint limit reached") + # 🔧 ФИКС (25.09.2026): hint_consumed_before_word_check — + # сначала проверяем, что у уровня ЕСТЬ слова, и только потом + # инкрементим hints_used. Раньше при 404 hints_used уже +1. + words = await conn.fetch( + "SELECT word_pl FROM level_words WHERE level_id = $1", level_id + ) + + if not words: + raise HTTPException(404, "No words found for this level") + + # Теперь безопасно инкрементим счётчик if progress: await conn.execute( "UPDATE user_progress SET hints_used = hints_used + 1 WHERE user_id = $1 AND level_id = $2", @@ -50,14 +61,6 @@ async def get_first_letter_hint(request: Request, level_id: int): ) hints_already_used += 1 - # Получаем все слова уровня - words = await conn.fetch( - "SELECT word_pl FROM level_words WHERE level_id = $1", level_id - ) - - if not words: - raise HTTPException(404, "No words found for this level") - word_paths = level["word_paths"] if isinstance(word_paths, str): import json @@ -76,10 +79,11 @@ async def get_first_letter_hint(request: Request, level_id: int): first_cell = word_paths[word][0] break + # 🔧 ФИКС (25.09.2026): hint_fake_first_cell — + # если ни у одного слова нет пути в word_paths, подсказка + # бессмысленна. Возвращаем 404, а не фейковую координату [0,0]. if not found_word: - # Если пути не нашлись — берём любое слово - found_word = all_level_words[0] - first_cell = [0, 0] + raise HTTPException(404, "No word paths found for this level") return { "status": "ok", diff --git a/routers/leaderboard.py b/routers/leaderboard.py index aef948d..7b6ee81 100644 --- a/routers/leaderboard.py +++ b/routers/leaderboard.py @@ -52,14 +52,27 @@ async def get_leaderboard(request: Request, limit: int = 50): "sentences_completed": 0, }) - # Место пользователя - user_rank = await conn.fetchval(""" - SELECT COUNT(*) + 1 FROM leaderboard_cache - WHERE total_score > ( - SELECT COALESCE(total_score, 0) FROM leaderboard_cache WHERE telegram_id = $1 - ) + # 🔧 ФИКС (25.09.2026): leaderboard_rank_wrong_for_uncached_user — + # если пользователя нет в leaderboard_cache, подзапрос вернёт NULL, + # и COUNT(*) + 1 = 1 (юзер видит себя первым, хотя у него 0 очков). + # Теперь: если user нет в кэше — считаем его место по количеству + # записей с total_score > 0 + 1 (в конец списка). + my_total_in_cache = await conn.fetchval(""" + SELECT total_score FROM leaderboard_cache WHERE telegram_id = $1 """, telegram_id) + if my_total_in_cache is None: + # Пользователя нет в кэше — считаем по количеству с > 0 + user_rank = await conn.fetchval(""" + SELECT COUNT(*) + 1 FROM leaderboard_cache + WHERE total_score > 0 + """) or 1 + else: + user_rank = await conn.fetchval(""" + SELECT COUNT(*) + 1 FROM leaderboard_cache + WHERE total_score > $1 + """, my_total_in_cache) + # Персональная статистика пользователя my_row = await conn.fetchrow(""" SELECT * FROM leaderboard_cache WHERE telegram_id = $1 diff --git a/routers/orchestrator.py b/routers/orchestrator.py index 4f6a31c..75c1f53 100644 --- a/routers/orchestrator.py +++ b/routers/orchestrator.py @@ -169,17 +169,28 @@ async def get_lesson_by_id(conn, lesson_id: str) -> Optional[Dict[str, Any]]: WHERE lesson_id = $1 AND is_test_question = FALSE ORDER BY id """, lesson_id) - + + # 🔧 ФИКС (25.09.2026): orchestrator_n_plus_1_options — + # один запрос ANY + группировка в Python. + question_ids = [q["question_id"] for q in questions] + all_options = [] + if question_ids: + all_options = await conn.fetch(""" + SELECT * FROM question_options + WHERE question_id = ANY($1::text[]) + """, question_ids) + + options_by_q: dict = {} + for o in all_options: + options_by_q.setdefault(o["question_id"], []).append(dict(o)) + questions_data = [] for q in questions: - options = await conn.fetch(""" - SELECT * FROM question_options WHERE question_id = $1 - """, q["question_id"]) questions_data.append({ "question": dict(q), - "options": [dict(o) for o in options], + "options": options_by_q.get(q["question_id"], []), }) - + return { "lesson": dict(lesson), "examples": [dict(e) for e in examples], @@ -195,17 +206,26 @@ async def get_lesson_questions(conn, lesson_id: str) -> list: WHERE lesson_id = $1 AND is_test_question = FALSE ORDER BY id """, lesson_id) - + + question_ids = [q["question_id"] for q in questions] + all_options = [] + if question_ids: + all_options = await conn.fetch(""" + SELECT * FROM question_options + WHERE question_id = ANY($1::text[]) + """, question_ids) + + options_by_q: dict = {} + for o in all_options: + options_by_q.setdefault(o["question_id"], []).append(dict(o)) + questions_data = [] for q in questions: - options = await conn.fetch(""" - SELECT * FROM question_options WHERE question_id = $1 - """, q["question_id"]) questions_data.append({ "question": dict(q), - "options": [dict(o) for o in options], + "options": options_by_q.get(q["question_id"], []), }) - + return questions_data @@ -217,17 +237,26 @@ async def get_test_questions(conn, topic_id: str) -> list: WHERE topic_id = $1 AND is_test_question = TRUE ORDER BY test_order """, topic_id) - + + question_ids = [q["question_id"] for q in questions] + all_options = [] + if question_ids: + all_options = await conn.fetch(""" + SELECT * FROM question_options + WHERE question_id = ANY($1::text[]) + """, question_ids) + + options_by_q: dict = {} + for o in all_options: + options_by_q.setdefault(o["question_id"], []).append(dict(o)) + questions_data = [] for q in questions: - options = await conn.fetch(""" - SELECT * FROM question_options WHERE question_id = $1 - """, q["question_id"]) questions_data.append({ "question": dict(q), - "options": [dict(o) for o in options], + "options": options_by_q.get(q["question_id"], []), }) - + return questions_data diff --git a/routers/teacher.py b/routers/teacher.py index d058ffb..ed8825a 100644 --- a/routers/teacher.py +++ b/routers/teacher.py @@ -288,7 +288,20 @@ async def get_wrong_answer_ai_explanation(telegram_id: int, lang: str, prompt_te async with pool.acquire() as conn: await increment_teacher_ai_usage(conn, telegram_id) else: - ai_response = get_ai_limit_message(lang) if not await _check_ai_limit_for_fallback(telegram_id, lang) else "" + # 🔧 ФИКС (25.09.2026): teacher_ai_explanation_empty_on_failure — + # если AI упал и лимит НЕ исчерпан, возвращаем нейтральный fallback, + # а не пустую строку (иначе ученик не понимает, почему ошибся). + if not await _check_ai_limit_for_fallback(telegram_id, lang): + ai_response = get_ai_limit_message(lang) + else: + # Нейтральный fallback на 4 языках + fallbacks = { + "pl": "Zastanów się nad tym pytaniem jeszcze raz.", + "ru": "Подумай над этим вопросом ещё раз.", + "uk": "Поміркуй над цим питанням ще раз.", + "en": "Think about this question again.", + } + ai_response = fallbacks.get(lang, fallbacks["en"]) return ai_response @@ -1028,17 +1041,29 @@ async def teacher_diagnostic_test(request: Request): "SELECT current_level FROM users WHERE telegram_id = $1", telegram_id ) or "A1" session_id = await start_learning_session(conn, telegram_id, user_level, "", "diagnostic_test") - + + # 🔧 ФИКС (25.09.2026): teacher_diagnostic_test_n_plus_1 — + # один запрос ANY + группировка в Python. + question_ids = [q["question_id"] for q in questions] + + all_options = [] + if question_ids: + all_options = await conn.fetch(""" + SELECT * FROM question_options + WHERE question_id = ANY($1::text[]) + """, question_ids) + + options_by_q: dict = {} + for o in all_options: + options_by_q.setdefault(o["question_id"], []).append(dict(o)) + questions_data = [] for q in questions: - options = await conn.fetch(""" - SELECT * FROM question_options WHERE question_id = $1 - """, q["question_id"]) questions_data.append({ "question": dict(q), - "options": [dict(o) for o in options], + "options": options_by_q.get(q["question_id"], []), }) - + return { "session_id": session_id, "questions": questions_data, @@ -1103,34 +1128,47 @@ async def teacher_progress(request: Request): WHERE is_active = TRUE ORDER BY sort_order """) - + + if not levels: + return {"levels": []} + + level_codes = [lvl["code"] for lvl in levels] + + # 🔧 ФИКС (25.09.2026): teacher_progress_n_plus_1 — + # 2 запроса вместо 2×N. + topics_total_rows = await conn.fetch(""" + SELECT level_code, COUNT(*) as cnt + FROM learning_topics + WHERE level_code = ANY($1::text[]) AND is_active = TRUE + GROUP BY level_code + """, level_codes) + topics_total_map = {r["level_code"]: r["cnt"] for r in topics_total_rows} + + topics_completed_rows = await conn.fetch(""" + SELECT t.level_code, COUNT(*) as cnt + FROM user_topic_progress utp + JOIN learning_topics t ON utp.topic_id = t.topic_id + WHERE utp.user_id = $1 + AND utp.test_completed = TRUE + AND t.is_active = TRUE + AND t.level_code = ANY($2::text[]) + GROUP BY t.level_code + """, telegram_id, level_codes) + topics_completed_map = {r["level_code"]: r["cnt"] for r in topics_completed_rows} + result = [] - for level in levels: code = level["code"] - - topics_total = await conn.fetchval(""" - SELECT COUNT(*) FROM learning_topics - WHERE level_code = $1 AND is_active = TRUE - """, code) or 0 - - topics_completed = await conn.fetchval(""" - SELECT COUNT(*) FROM user_topic_progress - WHERE user_id = $1 AND topic_id IN ( - SELECT topic_id FROM learning_topics WHERE level_code = $2 AND is_active = TRUE - ) AND test_completed = TRUE - """, telegram_id, code) or 0 - result.append({ "code": code, "name_pl": level["name_pl"], "name_ru": level["name_ru"], "name_uk": level["name_uk"], "name_en": level["name_en"], - "topics_total": topics_total, - "topics_completed": topics_completed, + "topics_total": topics_total_map.get(code, 0), + "topics_completed": topics_completed_map.get(code, 0), }) - + return {"levels": result}