from app.models import AccountDeleteRequest, AccountCancelDeleteRequest import httpx # ========================================================= # 1. IMPORTS & SETUP # ========================================================= import os import re import json import time import logging import asyncio import datetime import gzip import hmac import hashlib import uuid from typing import List, Optional, Dict, Any, Tuple, Union from fastapi import APIRouter, Request, HTTPException, status, Header, Query from fastapi.responses import Response, JSONResponse, StreamingResponse from pydantic import BaseModel, Field, model_validator import firebase_admin from firebase_admin import credentials, auth as firebase_auth from bson import json_util from fastapi import Depends from pymongo import ReturnDocument from pymongo.errors import DuplicateKeyError from app.database import ( users_collection, hadith_collection, chats_collection, masjid_collection, permanent_masjids_collection, quran_collection, pending_approvals_collection, daily_progress_collection, qaza_collection, nimaz_stats_collection, tasbeeh_collection, tasbeeh_daily_collection, tasbeeh_stats_collection, admin_bootstrap_lock_collection, ) from app.core.config import settings, Settings from app.limiter import limiter from app.auth_deps import ( get_current_user, get_optional_user, require_admin, enforce_self_or_admin, invalidate_user_cache, is_superadmin_email, ) from app import realtime # --- ENTERPRISE LOGGING --- logger = logging.getLogger("sahihislam.auth") logger.setLevel(logging.INFO) router = APIRouter() # ========================================================= # MASJID ASSOCIATION NORMALIZATION & ELIGIBILITY HELPERS # ========================================================= def normalize_user_masjid_fields(user: dict) -> dict: """Ensures consistent, non-resurrecting representation of masjid associations. Root fields are authoritative. Legacy community.masjid fields are only consulted if the root key is completely absent (never if set to empty string or empty list). """ # 1. selected_masjid_id if "selected_masjid_id" in user and user["selected_masjid_id"] is not None: user["selected_masjid_id"] = str(user["selected_masjid_id"]).strip() else: user["selected_masjid_id"] = str(user.get("community", {}).get("masjid", {}).get("selected_masjid_id", "")).strip() # 2. followed_masjids if "followed_masjids" in user and user["followed_masjids"] is not None: raw_followed = user["followed_masjids"] user["followed_masjids"] = list(raw_followed) if isinstance(raw_followed, (list, tuple, set)) else [] else: raw_nested = user.get("community", {}).get("masjid", {}).get("followed_masjids", []) user["followed_masjids"] = list(raw_nested) if isinstance(raw_nested, (list, tuple, set)) else [] # 3. managed_masjid_id if "managed_masjid_id" in user and user["managed_masjid_id"] is not None: user["managed_masjid_id"] = str(user["managed_masjid_id"]).strip() else: user["managed_masjid_id"] = str(user.get("community", {}).get("masjid", {}).get("managed_masjid_id", "")).strip() # 4. masjid_status if "masjid_status" in user and user["masjid_status"] is not None: user["masjid_status"] = str(user["masjid_status"]).strip() else: user["masjid_status"] = str(user.get("community", {}).get("masjid", {}).get("status", "NONE")).strip() # 5. mufti_status if "mufti_status" in user and user["mufti_status"] is not None: user["mufti_status"] = str(user["mufti_status"]).strip() else: user["mufti_status"] = str(user.get("community", {}).get("mufti", {}).get("status", "NONE")).strip() return user async def find_selectable_masjid(clean_id: str) -> Optional[dict]: """Finds an eligible, selectable masjid across active, permanent, and sanctuary collections. Ensures that deleted or inactive masjids cannot be selected. """ if not clean_id: return None # 1. Live masjid_collection (not deleted, not inactive) masjid = await masjid_collection.find_one( { "masjid_id": clean_id, "is_deleted": {"$ne": True}, "status": {"$ne": "DELETED"}, }, {"_id": 1, "masjid_id": 1, "name": 1, "is_live": 1, "status": 1} ) if masjid: return masjid # 2. Permanent directory collection perm_masjid = await permanent_masjids_collection.find_one( { "masjid_id": clean_id, "is_deleted": {"$ne": True}, "status": {"$ne": "DELETED"}, "is_live": {"$ne": False}, }, {"_id": 1, "masjid_id": 1, "name": 1, "is_live": 1, "status": 1} ) if perm_masjid: return perm_masjid # 3. Popular sanctuary templates (e.g., POPULAR_NABAWI, POPULAR_HARAM, POPULAR_AQSA) try: from app.masjid_routes import POPULAR_MASJID_TEMPLATES clean_key = clean_id.lower().replace("popular_", "") for tpl in POPULAR_MASJID_TEMPLATES: if tpl.get("masjid_id") == clean_id or tpl.get("key") == clean_key: return { "masjid_id": tpl["masjid_id"], "name": tpl["name"], "is_popular_placeholder": True, } except Exception: pass return None # ========================================================= # 2. PYDANTIC SCHEMAS (Bulletproof Input Validation) # ========================================================= class AuthRequest(BaseModel): idToken: str = Field(..., description="Firebase ID Token") # 🚀 CRITICAL FIX: Removed min_length=2 so empty strings/null from Flutter don't cause 422 Crashes! username: Optional[str] = None class FollowMasjidRequest(BaseModel): uid: Optional[str] = None masjid_id: str = Field(..., min_length=1, max_length=128) # 🚀 NEW: Schema for unfollowing a specific masjid class UnfollowMasjidRequest(BaseModel): masjid_id: str = Field(..., min_length=1, max_length=128) # 🚀 Persistent Selected Masjid class SelectMasjidRequest(BaseModel): masjid_id: str = Field(..., max_length=128) # 🚀 Multi-device FCM Token Registration class RegisterFcmTokenRequest(BaseModel): fcm_token: str = Field(..., min_length=10, max_length=1024) device_id: Optional[str] = Field(None, max_length=128) platform: Optional[str] = Field(None, max_length=32) class ToggleFavoriteSurahRequest(BaseModel): surah_no: int name_english: Optional[str] = "Surah" name_arabic: Optional[str] = "" translation_english: Optional[str] = "" total_ayahs: Optional[int] = 0 class ToggleFavoriteAyahRequest(BaseModel): surah_no: int ayah_no: int surah_name: Optional[str] = "" text_arabic: Optional[str] = "" text_english: Optional[str] = "" class SyncFavoritesRequest(BaseModel): surahs: List[dict] = Field(default_factory=list) ayahs: List[dict] = Field(default_factory=list) class ChatTitleRequest(BaseModel): title: str = Field(..., max_length=100) class ChatPinRequest(BaseModel): is_pinned: bool class LibrarySearchRequest(BaseModel): # H1: cap query length so the regex fallback path can't be used # to build a multi-megabyte ReDoS pattern. query: str = Field(..., min_length=1, max_length=200) books: Optional[List[str]] = [] # ========================================================= # 3. INITIALIZE FIREBASE (Infrastructure Service) # ========================================================= from app.infrastructure.firebase import init_firebase, verify_firebase_token init_firebase() # ========================================================= # 5. AUTHENTICATION ROUTES & USER DOCUMENT BUILDER # ========================================================= def build_user_document( uid: str, email: str, username: str, provider: str = "google", platform: str = "android", app_version: str = "1.0.0", role: str = "USER", is_email_verified: Optional[bool] = None, ) -> Dict[str, Any]: """ Builds an enterprise domain-driven user document for MongoDB. Structured into profile, auth, community, business, metrics, and analytics with flat mirrors for zero-breakage backward compatibility. """ now = datetime.datetime.utcnow() display_name = username.strip() if username and username.strip() else (email.split('@')[0] if email else "User") return { "uid": uid, # 1. Profile domain "profile": { "display_name": display_name, "email": email, "phone": None, "avatar_url": None, "locale": "en", "timezone": "Asia/Kolkata", }, # 2. Authentication & Authorization domain "auth": { "role": role, "provider": provider, "is_email_verified": is_email_verified if is_email_verified is not None else bool(email), "status": "ACTIVE", }, # 3. Community Affiliations domain "community": { "mufti": { "is_mufti": False, "status": "NONE", "applied_at": None, "resolved_at": None, }, "masjid": { "managed_masjid_id": "", "status": "NONE", "co_admin_masjids": [], "followed_masjids": [], }, }, # 4. Business Suite domain "business": { "is_enabled": False, "activated_at": None, "shop_id": None, "wallet_balance": 0.0, "total_ads_count": 0, }, # 5. Subscription Domain (Pro & Free tier entitlements) "subscription": { "is_pro": False, "tier": "FREE", "status": "FREE", "activated_at": None, "expires_at": None, "payment_method": None, "transaction_id": None, "daily_ai_queries_used": 0, "last_query_date": None, }, # 6. Usage & Engagement Metrics (High-speed counters for dashboards) "metrics": { "prayers_tracked": 0, "consecutive_streak": 0, "total_orders_placed": 0, "total_spent_inr": 0.0, "login_count": 1, "lifetime_apples": 0, }, # 7. Analytics, Telemetry & Audit (For DAU/MAU cohorts and graphs) "analytics": { "signup_platform": platform, "app_version": app_version, "created_at": now, "updated_at": now, "last_active_at": now, }, # 7.5. Spiritual Progress Tracking Domain (Nimaz & Dhikr habit tracker configuration) "progress_tracking": { "enabled": False, "prompt_responded": False, "started_at": None, "started_date": None, }, # 8. Backward-compatibility flat mirrors (Guarantees Flutter mobile & web never breaks) "username": display_name, "email": email, "role": role, "is_pro": False, "lifetime_apples": 0, "progress_tracking_enabled": False, "progress_tracking_prompt_responded": False, "progress_tracking_started_at": None, "progress_tracking_started_date": None, "mufti_status": "NONE", "masjid_status": "NONE", "managed_masjid_id": "", "followed_masjids": [], "favorites": { "surahs": [], "ayahs": [], }, "is_business_enabled": False, "created_at": now, "updated_at": now, } @router.post('/signup') @limiter.limit("5/minute") async def signup(payload: AuthRequest, request: Request): try: decoded_token = verify_firebase_token(payload.idToken) uid = decoded_token['uid'] email = decoded_token.get('email', '') # Safe fallback for username fallback_name = email.split('@')[0] if email else "User" display_name = payload.username.strip() if payload.username and payload.username.strip() else fallback_name existing_user = await users_collection.find_one({"uid": uid}) if existing_user: raise HTTPException(status_code=409, detail="User already exists") new_user = build_user_document( uid=uid, email=email, username=display_name, provider="password" ) try: await users_collection.insert_one(new_user) except DuplicateKeyError: logger.warning( "Signup duplicate-key for uid=%s email=%s — likely race " "with login auto-sync or a Firebase account switch", uid, email, ) raise HTTPException( status_code=409, detail="An account with this email already exists. Please sign in instead.", ) logger.info(f"New user registered: {display_name} ({uid})") return {"message": "Account created and synced"} except firebase_auth.InvalidIdTokenError as e: logger.error(f"🔥 FIREBASE REJECTED TOKEN (Signup): {str(e)}") raise HTTPException(status_code=401, detail="Invalid or expired token") except HTTPException: raise except Exception as e: logger.error(f"Signup Error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") @router.post('/login') @limiter.limit("10/minute") async def login(payload: AuthRequest, request: Request): try: decoded_token = verify_firebase_token(payload.idToken) uid = decoded_token['uid'] email = decoded_token.get('email', '') user = await users_collection.find_one({"uid": uid}) if not user: logger.info(f"Auto-syncing missing Firebase user to MongoDB: {uid}") fallback_name = email.split('@')[0] if email else "User" display_name = payload.username.strip() if payload.username and payload.username.strip() else fallback_name new_user = build_user_document( uid=uid, email=email, username=display_name, provider="google" ) try: await users_collection.insert_one(new_user) user = new_user except DuplicateKeyError: logger.warning( "Login auto-sync duplicate-key for email=%s — " "fetching pre-existing user", email, ) user = await users_collection.find_one({"email": email}) if user is None: raise else: # Update telemetry & metrics on returning user login now = datetime.datetime.utcnow() await users_collection.update_one( {"uid": uid}, { "$set": {"analytics.last_active_at": now, "updated_at": now}, "$inc": {"metrics.login_count": 1} } ) user = normalize_user_masjid_fields(user) return { "username": user.get('username') or user.get('profile', {}).get('display_name', 'User'), "role": user.get('role') or user.get('auth', {}).get('role', 'USER'), "mufti_status": user.get("mufti_status", "NONE"), "masjid_status": user.get("masjid_status", "NONE"), "managed_masjid_id": user.get("managed_masjid_id", ""), "selected_masjid_id": user.get("selected_masjid_id", ""), "followed_masjids": user.get("followed_masjids", []), "profile": user.get("profile"), "auth": user.get("auth"), "business": user.get("business"), "subscription": user.get("subscription") or { "is_pro": user.get("is_pro", False), "tier": "FREE", "status": "FREE", }, "is_pro": bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")), "metrics": user.get("metrics"), "has_acknowledged_rag": bool(user.get("has_acknowledged_rag_theology_disclaimer")), } except HTTPException: raise except firebase_auth.InvalidIdTokenError as e: logger.error(f"🔥 FIREBASE REJECTED TOKEN (Login): {str(e)}") raise HTTPException(status_code=401, detail="Invalid or expired token") except Exception as e: logger.error(f"Login Error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") @router.post('/social-login') @limiter.limit("15/minute") async def social_login(payload: AuthRequest, request: Request): try: decoded_token = verify_firebase_token(payload.idToken) uid = decoded_token['uid'] email = (decoded_token.get('email') or '').strip().lower() name = decoded_token.get('name', '') is_email_verified = bool(decoded_token.get('email_verified', False)) provider = decoded_token.get('firebase', {}).get('sign_in_provider', 'google') now = datetime.datetime.utcnow() # 🔗 ACCOUNT LINKING: Check if user exists by UID or EMAIL user = await users_collection.find_one({"uid": uid}) # If no user found by UID, check if email exists (account linking scenario) if not user and email: existing_user = await users_collection.find_one({"email": email}) if existing_user: # SI-SEC-011: Strict anti-takeover guard # 1. Reject unverified email claims from social login if not is_email_verified: logger.warning(f"[Auth] Account linking rejected for {email}: incoming social identity email is NOT verified") raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Cannot link account: Social identity email address is not verified by provider. Please verify your email with the identity provider first." ) # 2. Reject automatic linking of administrative or privileged accounts existing_role = (existing_user.get("role") or existing_user.get("auth", {}).get("role", "USER")).upper() if existing_role in ("ADMIN", "SUPERADMIN", "STAFF") or existing_user.get("is_admin") or existing_user.get("is_superadmin"): logger.warning(f"[Auth] Account linking rejected for {email}: administrative account cannot be linked automatically via social login") raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Administrative accounts cannot be automatically linked via social login. Please sign in using primary administrator credentials." ) # 3. Safe linking with foreign-key preservation old_uid = existing_user.get("uid") logger.info(f"Account Linking: Linking verified {provider} account to existing user {email} (old_uid={old_uid}, new_uid={uid})") update_doc = { "$set": { "uid": uid, "auth.is_email_verified": True, "auth.provider": provider, "analytics.updated_at": now, "updated_at": now, } } if old_uid and old_uid != uid: update_doc["$addToSet"] = { "auth.linked_uids": old_uid, "linked_uids": old_uid, } await users_collection.update_one({"email": email}, update_doc) # Migrate related records if old_uid exists and differs (NEW-012, SI-SEC-011) if old_uid and old_uid != uid: migrated_steps = [] try: from app.database import ( wallets_collection, subscriptions_collection, transactions_collection, orders_collection, products_collection, ads_collection, chats_collection, daily_progress_collection, qaza_collection, tasbeeh_collection, tasbeeh_daily_collection, tasbeeh_stats_collection, refund_requests_collection, audit_logs_collection, ) migration_plan = [ (wallets_collection, "user_id"), (subscriptions_collection, "user_id"), (transactions_collection, "user_id"), (refund_requests_collection, "user_id"), (orders_collection, "user_id"), (orders_collection, "buyer_uid"), (orders_collection, "seller_uid"), (products_collection, "seller_uid"), (ads_collection, "advertiser_uid"), (ads_collection, "user_id"), (chats_collection, "user_id"), (daily_progress_collection, "uid"), (qaza_collection, "uid"), (nimaz_stats_collection, "uid"), (tasbeeh_collection, "uid"), (tasbeeh_daily_collection, "uid"), (tasbeeh_stats_collection, "uid"), ] for coll, field in migration_plan: await coll.update_many({field: old_uid}, {"$set": {field: uid}}) migrated_steps.append((coll, field)) await audit_logs_collection.insert_one({ "event": "ACCOUNT_LINKED_VIA_SOCIAL_LOGIN", "email": email, "old_uid": old_uid, "new_uid": uid, "provider": provider, "timestamp": now, }) except Exception as e: logger.error(f"[Auth] Critical failure migrating records from {old_uid} to {uid}: {e}", exc_info=True) # AUTH-001 FIX: Rollback all previously migrated collections in reverse order for coll, field in reversed(migrated_steps): try: await coll.update_many({field: uid}, {"$set": {field: old_uid}}) except Exception as rb_step_err: logger.critical(f"[Auth] Failed to rollback step on {coll.name}.{field}: {rb_step_err}") # Rollback user document try: await users_collection.update_one( {"email": email}, { "$set": {"uid": old_uid, "updated_at": datetime.datetime.utcnow()}, "$pull": {"auth.linked_uids": old_uid, "linked_uids": old_uid} } ) await invalidate_user_cache(old_uid) await invalidate_user_cache(uid) except Exception as rb_err: logger.critical(f"[Auth] FATAL: Rollback failed during account linking error: {rb_err}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Account linking failed during ownership records migration. Operation rolled back." ) await invalidate_user_cache(old_uid) await invalidate_user_cache(uid) user = await users_collection.find_one({"email": email}) logger.info(f"Account Linked: {email} now accessible via social login") if not user: logger.info(f"Social Login Sync: Creating new user {uid} in MongoDB") fallback_name = email.split('@')[0] if email else "User" if payload.username and payload.username.strip(): display_name = payload.username.strip() elif name and name.strip(): display_name = name.strip() else: display_name = fallback_name new_user = build_user_document( uid=uid, email=email, username=display_name, provider=provider, is_email_verified=is_email_verified, ) try: await users_collection.insert_one(new_user) user = new_user except DuplicateKeyError: logger.warning( "Social login auto-sync duplicate-key for email=%s — " "fetching pre-existing user", email, ) user = await users_collection.find_one({"email": email}) if user is None: raise else: # Update telemetry & metrics on returning user login await users_collection.update_one( {"uid": uid}, { "$set": {"analytics.last_active_at": now, "updated_at": now}, "$inc": {"metrics.login_count": 1} } ) user = normalize_user_masjid_fields(user) return { "username": user.get('username') or user.get('profile', {}).get('display_name', 'User'), "role": user.get('role') or user.get('auth', {}).get('role', 'USER'), "mufti_status": user.get("mufti_status", "NONE"), "masjid_status": user.get("masjid_status", "NONE"), "managed_masjid_id": user.get("managed_masjid_id", ""), "selected_masjid_id": user.get("selected_masjid_id", ""), "followed_masjids": user.get("followed_masjids", []), "profile": user.get("profile"), "auth": user.get("auth"), "business": user.get("business"), "subscription": user.get("subscription") or { "is_pro": user.get("is_pro", False), "tier": "FREE", "status": "FREE", }, "is_pro": bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")), "metrics": user.get("metrics"), "has_acknowledged_rag": bool(user.get("has_acknowledged_rag_theology_disclaimer")), } except HTTPException: raise except firebase_auth.InvalidIdTokenError as e: logger.error(f"🔥 FIREBASE REJECTED TOKEN (Social): {str(e)}") raise HTTPException(status_code=401, detail="Invalid or expired token") except Exception as e: logger.error(f"Social Login Error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") @router.get('/profile/{uid}') async def get_profile(uid: str, current_user: dict = Depends(get_current_user)): # 🔒 IDOR FIX (audit C1): authenticate is not enough — a logged-in # user must only read THEIR OWN profile (or an admin may read any). # Without this, any user could read another user's email/role/masjid # associations by supplying a different uid. enforce_self_or_admin(current_user, uid) user = await users_collection.find_one({"uid": uid}, {"_id": 0}) if user: user = normalize_user_masjid_fields(user) user["is_pro"] = bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")) user["has_password"] = bool(user.get("has_password")) user["deleted_notification_ids"] = user.get("deleted_notification_ids") or [] user["has_acknowledged_rag"] = bool(user.get("has_acknowledged_rag_theology_disclaimer")) # SuperAdmin status is code-governed via immutable whitelist (NEVER read from DB) from .superadmin_routes import get_superadmin_whitelist user_email = (user.get("email") or "").strip().lower() user["superadmin"] = user_email in get_superadmin_whitelist() user.pop("is_superadmin", None) if "auth" in user and isinstance(user["auth"], dict): user["auth"].pop("superadmin", None) user["auth"].pop("is_superadmin", None) # Look up co-admin masjids dynamically so role changes take effect in real-time co_masjids = await masjid_collection.find({"co_admins": uid}, {"masjid_id": 1, "name": 1, "_id": 0}).to_list(length=20) user["co_admin_masjid_ids"] = [m["masjid_id"] for m in co_masjids] user["is_co_admin"] = len(co_masjids) > 0 # Progress tracking configuration & consent state pt = user.get("progress_tracking") or {} responded = user.get("progress_tracking_prompt_responded", pt.get("prompt_responded")) enabled = user.get("progress_tracking_enabled", pt.get("enabled")) started_at = user.get("progress_tracking_started_at", pt.get("started_at")) started_date = user.get("progress_tracking_started_date", pt.get("started_date")) lifetime_apples = int(user.get("lifetime_apples", 0) or (user.get("metrics") or {}).get("lifetime_apples", 0) or 0) # Auto-heal: If user is already tracking (enabled is True, started_date set, apples collected, or has records) is_already_tracking = bool(enabled) or bool(started_date) or (lifetime_apples > 0) if not is_already_tracking: first_dp = await daily_progress_collection.find({"$or": [{"uid": uid}, {"_id": uid}]}).sort("date", 1).limit(1).to_list(1) if first_dp: is_already_tracking = True started_date = started_date or first_dp[0].get("date") if is_already_tracking: enabled = True responded = True if not started_date: started_date = datetime.datetime.utcnow().date().isoformat() await users_collection.update_one( {"$or": [{"uid": uid}, {"_id": uid}]}, { "$set": { "progress_tracking_enabled": True, "progress_tracking_prompt_responded": True, "progress_tracking_started_date": started_date, "progress_tracking.enabled": True, "progress_tracking.prompt_responded": True, "progress_tracking.started_date": started_date, } } ) elif responded is None: enabled = False responded = False user["progress_tracking_enabled"] = bool(enabled) user["progress_tracking_prompt_responded"] = bool(responded) user["progress_tracking_started_at"] = str(started_at) if started_at else None user["progress_tracking_started_date"] = str(started_date) if started_date else None return user raise HTTPException(status_code=404, detail="User not found") class UpdateProfileRequest(BaseModel): username: Optional[str] = Field(None, min_length=1, max_length=100) phone: Optional[str] = Field(None, max_length=25) location: Optional[str] = Field(None, max_length=100) bio: Optional[str] = Field(None, max_length=300) email: Optional[str] = Field(None, max_length=150) @router.put('/profile/{uid}') async def update_profile( uid: str, payload: UpdateProfileRequest, current_user: dict = Depends(get_current_user), ): enforce_self_or_admin(current_user, uid) target_user = await users_collection.find_one({"uid": uid}) if not target_user: raise HTTPException(status_code=404, detail="User not found") target_email = (target_user.get("email") or target_user.get("primary_email") or "").strip().lower() update_fields = {} if payload.username is not None: name = payload.username.strip() if name: update_fields["username"] = name update_fields["profile.display_name"] = name if payload.phone is not None: update_fields["phone"] = payload.phone.strip() update_fields["profile.phone"] = payload.phone.strip() if payload.location is not None: update_fields["location"] = payload.location.strip() update_fields["profile.location"] = payload.location.strip() if payload.bio is not None: update_fields["bio"] = payload.bio.strip() update_fields["profile.bio"] = payload.bio.strip() if payload.email is not None: em = payload.email.strip().lower() if em and em != target_email: raise HTTPException( status_code=400, detail="Direct email modification via profile update is disabled. Please use the verified email change flow (/auth/request-email-change and /auth/verify-email-change)." ) elif not em: raise HTTPException( status_code=400, detail="Email cannot be emptied via profile update." ) # If em == target_email, it's an identical no-op, safe to proceed without modifying email if update_fields: now = datetime.datetime.utcnow() update_fields["updated_at"] = now update_fields["analytics.updated_at"] = now await users_collection.update_one({"uid": uid}, {"$set": update_fields}) await invalidate_user_cache(uid) user = await users_collection.find_one({"uid": uid}, {"_id": 0}) if user: user = normalize_user_masjid_fields(user) user["is_pro"] = bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")) return {"status": "success", "user": user} raise HTTPException(status_code=404, detail="User not found") # ========================================================= # 6. ADMIN ROUTES — Pending Request Listing Only def haversine_km(lat1, lon1, lat2, lon2) -> float: try: import math r = 6371.0 dlat = math.radians(float(lat2) - float(lat1)) dlon = math.radians(float(lon2) - float(lon1)) a = (math.sin(dlat / 2.0) ** 2 + math.cos(math.radians(float(lat1))) * math.cos(math.radians(float(lat2))) * math.sin(dlon / 2.0) ** 2) c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a)) return r * c except Exception: return 999999.0 class AdminLocationUpdate(BaseModel): lat: float lng: float @router.post('/admin/update-location') async def update_admin_location(payload: AdminLocationUpdate, admin: dict = Depends(require_admin)): """Keep App Admin's GPS location live so nearest masjid registrations & reports can be routed to them.""" now = datetime.datetime.utcnow().isoformat() await users_collection.update_one( {"uid": admin["uid"]}, {"$set": { "last_location": { "lat": payload.lat, "lng": payload.lng, "updated_at": now, } }} ) return {"status": "success", "lat": payload.lat, "lng": payload.lng} class AppAdminRegistration(BaseModel): userId: Optional[str] = Field(default=None, max_length=256) name: str = Field(..., min_length=2, max_length=120) phone: str = Field(..., min_length=5, max_length=50) district: Optional[str] = None city: Optional[str] = None reason: Optional[str] = None experience: Optional[str] = None @router.post('/admin/register') @limiter.limit("60/minute") async def register_app_admin( payload: AppAdminRegistration, request: Request, user: dict = Depends(get_current_user), ): """Submits an application to become an App Admin. Auth-gated: verified uid is used. Upserts to pending_approvals_collection and updates user app_admin_status to PENDING. """ try: verified_uid = user["uid"] admin_data = payload.dict() admin_data.pop("userId", None) admin_data.update({ "uid": verified_uid, "role": "APP_ADMIN", "status": "PENDING", "timestamp": datetime.datetime.utcnow(), "email": user.get("email") or "", }) await asyncio.gather( pending_approvals_collection.update_one( {"uid": verified_uid, "role": "APP_ADMIN"}, {"$set": admin_data}, upsert=True ), users_collection.update_one( {"uid": verified_uid}, {"$set": {"app_admin_status": "PENDING"}} ) ) logger.info(f"New App Admin application from: {verified_uid}") return JSONResponse(status_code=201, content={"message": "App Admin application submitted successfully"}) except Exception as e: logger.error(f"App Admin Registration Error: {e}") raise HTTPException(status_code=500, detail="Internal Server Error") # ========================================================= @router.get('/admin/pending-requests') async def get_pending_requests(admin: dict = Depends(require_admin)): try: apps = await pending_approvals_collection.find({}, {"_id": 0}).to_list(length=100) # Batch the user lookups into ONE query instead of one-per-app (N+1). uids = list({a.get('uid') for a in apps if a.get('uid')}) users = await users_collection.find( {"uid": {"$in": uids}}, {"_id": 0, "uid": 1, "username": 1, "email": 1, "phone": 1, "profile.phone": 1} ).to_list(length=len(uids) or 1) if uids else [] by_uid = {u["uid"]: u for u in users} results = [] admin_loc = admin.get("last_location") or {} a_lat = admin_loc.get("lat") a_lng = admin_loc.get("lng") for app in apps: user = by_uid.get(app.get('uid')) if user: app.update({ "username": user.get('username'), "email": user.get('email'), "user_phone": user.get('phone') or (user.get('profile') or {}).get('phone') or app.get('phone') or app.get('contact_phone'), }) # Compute distance to this App Admin if both coordinates exist m_lat = app.get("latitude") if app.get("latitude") is not None else app.get("gps_lat") m_lng = app.get("longitude") if app.get("longitude") is not None else app.get("gps_lng") if a_lat is not None and a_lng is not None and m_lat is not None and m_lng is not None: try: d_km = haversine_km(float(a_lat), float(a_lng), float(m_lat), float(m_lng)) app["distance_km"] = round(d_km, 2) except Exception: pass # Proximity check for duplicate masjids (within 400m) if app.get("role") == "MASJID" and m_lat is not None and m_lng is not None: try: d_deg = 0.005 # ~500m bounding box nearby_cursor = masjid_collection.find( { "latitude": {"$gte": float(m_lat) - d_deg, "$lte": float(m_lat) + d_deg}, "longitude": {"$gte": float(m_lng) - d_deg, "$lte": float(m_lng) + d_deg}, }, {"name": 1, "latitude": 1, "longitude": 1, "masjid_id": 1, "_id": 0} ) nearby_masjids = await nearby_cursor.to_list(length=5) closest_alert = None for nm in nearby_masjids: n_lat = nm.get("latitude") n_lng = nm.get("longitude") if n_lat is not None and n_lng is not None: dist_m = haversine_km(float(m_lat), float(m_lng), float(n_lat), float(n_lng)) * 1000 if dist_m <= 400: # within 400 meters if closest_alert is None or dist_m < closest_alert["distance_m"]: closest_alert = { "name": nm.get("name") or "Unnamed Masjid", "distance_m": int(dist_m), "masjid_id": nm.get("masjid_id") } if closest_alert: app["nearby_alert"] = closest_alert except Exception as prox_e: logger.debug("Proximity radar check skipped: %s", prox_e) results.append(app) # Sort: applications assigned to this admin first, then by proximity admin_uid = admin.get("uid") results.sort(key=lambda r: ( 0 if r.get("assigned_admin_uid") == admin_uid else 1, r.get("distance_km") if r.get("distance_km") is not None else 999999.0 )) return results except Exception as e: logger.error(f"Admin Pending Requests Error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") # ========================================================= # 6b. ADMIN ROLE MANAGEMENT (promote / demote / list) # ========================================================= PROMOTABLE_ROLES = {"ADMIN", "MUFTI", "MASJID_ADMIN"} class AdminPromoteRequest(BaseModel): target_uid: str = Field(..., min_length=1, max_length=256) role: str = Field(..., min_length=1, max_length=32) @router.get('/admin/users') async def list_users(_: dict = Depends(require_admin)): try: users = await users_collection.find( {}, {"_id": 0, "uid": 1, "username": 1, "email": 1, "role": 1} ).sort("username", 1).to_list(length=500) for u in users: u["role"] = u.get("role") or "USER" return users except Exception as e: logger.error(f"Admin list users error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") @router.post('/admin/promote') async def promote_user( payload: AdminPromoteRequest, admin: dict = Depends(require_admin), ): if payload.role not in PROMOTABLE_ROLES: raise HTTPException( status_code=400, detail=f"role must be one of {sorted(PROMOTABLE_ROLES)}", ) target = await users_collection.find_one({"uid": payload.target_uid}) if not target: raise HTTPException(status_code=404, detail="Target user not found") if payload.target_uid == admin["uid"] and payload.role != "ADMIN": raise HTTPException( status_code=400, detail="You cannot change your own role; ask another admin.", ) await users_collection.update_one( {"uid": payload.target_uid}, {"$set": {"role": payload.role}}, ) await invalidate_user_cache(payload.target_uid) logger.info( "auth: admin=%s promoted uid=%s to role=%s", admin["uid"], payload.target_uid, payload.role, ) realtime.publish( payload.target_uid, "role_changed", {"new_role": payload.role}, ) return { "status": "ok", "target_uid": payload.target_uid, "new_role": payload.role, } @router.post('/admin/demote') async def demote_user( payload: AdminPromoteRequest, admin: dict = Depends(require_admin), ): if payload.role != "USER": raise HTTPException( status_code=400, detail="demote can only set role to 'USER'", ) if payload.target_uid == admin["uid"]: raise HTTPException( status_code=400, detail="You cannot demote yourself; ask another admin.", ) target = await users_collection.find_one({"uid": payload.target_uid}) if not target: raise HTTPException(status_code=404, detail="Target user not found") await users_collection.update_one( {"uid": payload.target_uid}, {"$set": {"role": "USER"}}, ) await invalidate_user_cache(payload.target_uid) logger.info( "auth: admin=%s demoted uid=%s to USER", admin["uid"], payload.target_uid, ) realtime.publish( payload.target_uid, "role_changed", {"new_role": "USER"}, ) return {"status": "ok", "target_uid": payload.target_uid, "new_role": "USER"} @router.post('/admin/bootstrap') async def bootstrap_admin( admin: dict = Depends(get_current_user), x_staff_secret_key: Optional[str] = Header(None, alias="X-Staff-Secret-Key"), ): """ Controlled one-time bootstrap endpoint to initialize the first ADMIN. Guarded against default secret abuse (SI-SEC-005) and enforces caller binding. Requires constant-time validation of X-Staff-Secret-Key. Guarantees concurrency-safe atomicity via a singleton database lock. """ # 0. Safety guards against predictable default secrets and unauthorized bootstrap effective_secret = settings.SECRET_KEY or settings.security.secret_key if not effective_secret or effective_secret == Settings.DEFAULT_DEV_SECRET: raise HTTPException( status_code=403, detail="Forbidden: Admin bootstrap is disabled when using default development secret.", ) if settings.IS_PROD and os.getenv("ALLOW_ADMIN_BOOTSTRAP") != "1": raise HTTPException( status_code=403, detail="Forbidden: Public admin bootstrap is disabled in production.", ) # 1. Require staff secret key (constant time compare against SECRET_KEY or STAFF_SECRET_KEY) staff_key = settings.security.staff_secret_key or effective_secret if not x_staff_secret_key or not ( hmac.compare_digest(x_staff_secret_key.strip(), effective_secret) or hmac.compare_digest(x_staff_secret_key.strip(), staff_key) ): raise HTTPException( status_code=403, detail="Forbidden: Valid X-Staff-Secret-Key header is required to bootstrap an administrator." ) # 1b. Identity binding: If BOOTSTRAP_ADMIN_UID is configured, ensure caller matches or is in superadmin email whitelist bootstrap_uid = settings.security.bootstrap_admin_uid or os.getenv("BOOTSTRAP_ADMIN_UID", "").strip() caller_email = (admin.get("email") or admin.get("_verified_token_email") or "").strip().lower() if bootstrap_uid and admin.get("uid") != bootstrap_uid and not is_superadmin_email(caller_email): raise HTTPException( status_code=403, detail="Forbidden: Caller UID does not match configured BOOTSTRAP_ADMIN_UID.", ) # 2. Check if an admin already exists in the system existing_admin = await users_collection.find_one({"role": "ADMIN"}) if existing_admin: raise HTTPException( status_code=409, detail="An administrator already exists. Further admin accounts must be promoted by an existing administrator.", ) # 3. Concurrency guard: Atomically acquire singleton lock in DB try: await admin_bootstrap_lock_collection.insert_one({ "_id": "admin_singleton_lock", "bootstrapped_by_uid": admin["uid"], "created_at": datetime.datetime.utcnow() }) except DuplicateKeyError: raise HTTPException( status_code=409, detail="Bootstrap lock already held or concurrent bootstrap attempt. An admin already exists.", ) # 4. Promote user to ADMIN atomically now = datetime.datetime.utcnow() await users_collection.update_one( {"uid": admin["uid"]}, {"$set": {"role": "ADMIN", "is_admin": True, "updated_at": now}}, ) await invalidate_user_cache(admin["uid"]) # 5. Audit logging try: from app.database import audit_logs_collection await audit_logs_collection.insert_one({ "action": "ADMIN_BOOTSTRAP", "bootstrapped_uid": admin["uid"], "bootstrapped_email": caller_email, "timestamp": now, }) except Exception as e: logger.warning(f"Failed to record admin bootstrap audit log: {e}") logger.info("auth: successfully bootstrapped first ADMIN: uid=%s", admin["uid"]) return {"status": "ok", "new_role": "ADMIN"} # ========================================================= # 6c. REAL-TIME EVENT STREAM (Server-Sent Events) # ========================================================= @router.get('/events') async def sse_events(request: Request, user: dict = Depends(get_current_user)): if request.method == "OPTIONS": return JSONResponse(status_code=200, content={}) uid = user["uid"] logger.info("realtime: SSE connection opened uid=%s", uid) async def event_source(): try: async for chunk in realtime.sse_event_stream(uid): yield chunk except (GeneratorExit, asyncio.CancelledError): logger.info("realtime: SSE connection closed uid=%s", uid) return except Exception as e: logger.error("realtime: SSE stream error uid=%s: %s", uid, e) return return StreamingResponse( event_source(), media_type="text/event-stream", headers={ "Cache-Control": "no-cache, no-transform", "Connection": "keep-alive", "X-Accel-Buffering": "no", }, ) # ========================================================= # 7. CHAT HISTORY & USER ACTIONS # ========================================================= @router.post('/follow-masjid') async def follow_masjid(payload: FollowMasjidRequest, user: dict = Depends(get_current_user)): """Follows a masjid without altering the user's active selected masjid.""" target_uid = user["uid"] now = datetime.datetime.utcnow() clean_id = (payload.masjid_id or "").strip() masjid = await find_selectable_masjid(clean_id) if not masjid: raise HTTPException(status_code=404, detail="Masjid not found or inactive") # IMPORTANT (Part 3): Following a masjid adds to followed membership, # but does NOT overwrite the user's active selected_masjid_id. res = await users_collection.update_one( {"uid": target_uid}, { "$addToSet": { "followed_masjids": clean_id, "community.masjid.followed_masjids": clean_id, }, "$set": { "analytics.updated_at": now, } } ) if res.matched_count == 0: raise HTTPException(status_code=404, detail="User not found") if res.modified_count > 0: await masjid_collection.update_one( {"masjid_id": clean_id}, {"$inc": {"metrics.followers_count": 1}} ) await invalidate_user_cache(target_uid) return {"status": "success", "followed_masjid_id": clean_id} @router.post('/select-masjid') async def select_masjid(payload: SelectMasjidRequest, user: dict = Depends(get_current_user)): """Authoritative server-side persistence of user's active/selected masjid.""" target_uid = user["uid"] now = datetime.datetime.utcnow() clean_id = (payload.masjid_id or "").strip() if clean_id: masjid = await find_selectable_masjid(clean_id) if not masjid: raise HTTPException(status_code=404, detail="Masjid not found or inactive") res = await users_collection.update_one( {"uid": target_uid}, { "$set": { "selected_masjid_id": clean_id, "community.masjid.selected_masjid_id": clean_id, "analytics.updated_at": now, }, "$addToSet": { "followed_masjids": clean_id, "community.masjid.followed_masjids": clean_id, } } ) if res.matched_count == 0: raise HTTPException(status_code=404, detail="User not found") await invalidate_user_cache(target_uid) return {"status": "success", "selected_masjid_id": clean_id, "name": masjid.get("name", "")} else: # Explicitly unlink / clear active masjid selection res = await users_collection.update_one( {"uid": target_uid}, { "$set": { "selected_masjid_id": "", "community.masjid.selected_masjid_id": "", "analytics.updated_at": now, } } ) if res.matched_count == 0: raise HTTPException(status_code=404, detail="User not found") await invalidate_user_cache(target_uid) return {"status": "success", "selected_masjid_id": ""} @router.post('/unfollow-masjid') async def unfollow_masjid(payload: UnfollowMasjidRequest, user: dict = Depends(get_current_user)): """Unfollows a masjid. If the unfollowed masjid was the active selected masjid at write time, atomically clears the selection without overwriting any concurrent newer selection.""" target_uid = user["uid"] now = datetime.datetime.utcnow() clean_id = (payload.masjid_id or "").strip() # Step 1: Always pull from followed_masjids in both root and nested fields pull_res = await users_collection.update_one( {"uid": target_uid}, { "$pull": { "followed_masjids": clean_id, "community.masjid.followed_masjids": clean_id, }, "$set": {"analytics.updated_at": now} } ) if pull_res.matched_count == 0: raise HTTPException(status_code=404, detail="User not found") # Step 2: Fix unfollow race condition (Part 7) # Clear selected_masjid_id ONLY IF it still matches clean_id at write time await users_collection.update_one( { "uid": target_uid, "$or": [ {"selected_masjid_id": clean_id}, {"community.masjid.selected_masjid_id": clean_id}, ] }, { "$set": { "selected_masjid_id": "", "community.masjid.selected_masjid_id": "", "analytics.updated_at": now, } } ) if pull_res.modified_count > 0: await masjid_collection.update_one( {"masjid_id": clean_id}, {"$inc": {"metrics.followers_count": -1}} ) await invalidate_user_cache(target_uid) return {"status": "success"} @router.post('/fcm-token') async def register_fcm_token(payload: RegisterFcmTokenRequest, user: dict = Depends(get_current_user)): """Registers or refreshes an FCM device push token with multi-device support.""" target_uid = user["uid"] token = payload.fcm_token.strip() if not token: raise HTTPException(status_code=400, detail="FCM token cannot be empty") now = datetime.datetime.utcnow() device_entry = { "token": token, "device_id": (payload.device_id or "").strip(), "platform": (payload.platform or "android").strip().lower(), "updated_at": now.isoformat(), } # Pull any older entry with this token await users_collection.update_one( {"uid": target_uid}, {"$pull": {"fcm_tokens": {"token": token}}} ) # Add new device and set primary fcm_token await users_collection.update_one( {"uid": target_uid}, { "$set": { "fcm_token": token, "fcm_platform": (payload.platform or "android").strip().lower(), "fcm_updated_at": now, "analytics.updated_at": now, }, "$push": { "fcm_tokens": { "$each": [device_entry], "$slice": -10 } } } ) return {"status": "success", "message": "FCM token registered successfully"} @router.delete('/fcm-token') async def unregister_fcm_token(token: Optional[str] = Query(None), user: dict = Depends(get_current_user)): """Unregisters an FCM token (e.g. on logout or app uninstall).""" target_uid = user["uid"] if token and token.strip(): clean_token = token.strip() await users_collection.update_one( {"uid": target_uid}, { "$pull": {"fcm_tokens": {"token": clean_token}} } ) else: await users_collection.update_one( {"uid": target_uid}, { "$set": {"fcm_tokens": [], "fcm_token": None} } ) return {"status": "success", "message": "FCM token unregistered"} _USER_FAVORITES_CACHE: Dict[str, Tuple[float, bytes]] = {} _USER_FAVORITES_TTL = 30.0 @router.get('/favorites') async def get_favorites(user: dict = Depends(get_current_user)): uid = user["uid"] ent = _USER_FAVORITES_CACHE.get(uid) if ent and (time.monotonic() - ent[0]) < _USER_FAVORITES_TTL: return Response(content=ent[1], media_type="application/json") u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} res = { "status": "success", "surahs": favs.get("surahs", []), "ayahs": favs.get("ayahs", []) } raw_bytes = json.dumps(res).encode("utf-8") _USER_FAVORITES_CACHE[uid] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") @router.post('/favorites/toggle-surah') async def toggle_favorite_surah(payload: ToggleFavoriteSurahRequest, user: dict = Depends(get_current_user)): uid = user["uid"] now = datetime.datetime.utcnow() u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} surahs = favs.get("surahs", []) existing = next((s for s in surahs if (isinstance(s, dict) and s.get("surah_no") == payload.surah_no) or (isinstance(s, (int, str)) and str(s) == str(payload.surah_no))), None) if existing: pull_target = {"surah_no": payload.surah_no} if isinstance(existing, dict) else existing await users_collection.update_one( {"uid": uid}, { "$pull": {"favorites.surahs": pull_target}, "$set": {"analytics.updated_at": now} } ) _USER_FAVORITES_CACHE.pop(uid, None) return {"status": "success", "is_favorite": False, "surah_no": payload.surah_no} else: new_item = { "surah_no": payload.surah_no, "name_english": payload.name_english or f"Surah {payload.surah_no}", "name_arabic": payload.name_arabic or "", "translation_english": payload.translation_english or "", "total_ayahs": payload.total_ayahs or 0, "favorited_at": now.isoformat() } await users_collection.update_one( {"uid": uid}, { "$push": {"favorites.surahs": new_item}, "$set": {"analytics.updated_at": now} } ) _USER_FAVORITES_CACHE.pop(uid, None) return {"status": "success", "is_favorite": True, "surah_no": payload.surah_no, "item": new_item} @router.post('/favorites/toggle-ayah') async def toggle_favorite_ayah(payload: ToggleFavoriteAyahRequest, user: dict = Depends(get_current_user)): uid = user["uid"] now = datetime.datetime.utcnow() key = f"{payload.surah_no}:{payload.ayah_no}" u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} ayahs = favs.get("ayahs", []) existing = next((a for a in ayahs if (isinstance(a, dict) and (a.get("key") == key or (a.get("surah_no") == payload.surah_no and a.get("ayah_no") == payload.ayah_no))) or (isinstance(a, str) and a == key)), None) if existing: pull_target = {"key": key} if isinstance(existing, dict) else existing await users_collection.update_one( {"uid": uid}, { "$pull": {"favorites.ayahs": pull_target}, "$set": {"analytics.updated_at": now} } ) _USER_FAVORITES_CACHE.pop(uid, None) return {"status": "success", "is_favorite": False, "key": key} else: new_item = { "key": key, "surah_no": payload.surah_no, "ayah_no": payload.ayah_no, "surah_name": payload.surah_name or f"Surah {payload.surah_no}", "text_arabic": payload.text_arabic or "", "text_english": payload.text_english or "", "favorited_at": now.isoformat() } await users_collection.update_one( {"uid": uid}, { "$push": {"favorites.ayahs": new_item}, "$set": {"analytics.updated_at": now} } ) _USER_FAVORITES_CACHE.pop(uid, None) return {"status": "success", "is_favorite": True, "key": key, "item": new_item} @router.post('/favorites/sync') async def sync_favorites(payload: SyncFavoritesRequest, user: dict = Depends(get_current_user)): uid = user["uid"] now = datetime.datetime.utcnow() u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) cloud_favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} # Merge surahs by surah_no cloud_surahs = {s.get("surah_no"): s for s in cloud_favs.get("surahs", []) if s.get("surah_no") is not None} for s in payload.surahs: sno = s.get("surah_no") if sno is not None and sno not in cloud_surahs: cloud_surahs[sno] = s merged_surahs = sorted(cloud_surahs.values(), key=lambda x: x.get("surah_no", 0)) # Merge ayahs by key cloud_ayahs = {a.get("key") or f"{a.get('surah_no')}:{a.get('ayah_no')}": a for a in cloud_favs.get("ayahs", [])} for a in payload.ayahs: k = a.get("key") or f"{a.get('surah_no')}:{a.get('ayah_no')}" if k not in cloud_ayahs: a["key"] = k cloud_ayahs[k] = a merged_ayahs = list(cloud_ayahs.values()) await users_collection.update_one( {"uid": uid}, { "$set": { "favorites": { "surahs": merged_surahs, "ayahs": merged_ayahs }, "analytics.updated_at": now } } ) _USER_FAVORITES_CACHE.pop(uid, None) return { "status": "success", "surahs": merged_surahs, "ayahs": merged_ayahs } @router.get('/chat-history/{uid}') async def get_chat_history(uid: str, user: dict = Depends(get_current_user)): enforce_self_or_admin(user, uid) chats = await chats_collection.find({"user_id": uid}, {"_id": 0}).sort("last_updated", -1).to_list(length=100) safe_chats = json.loads(json_util.dumps(chats)) return [{"session_id": c.get("session_id"), "messages": c.get("messages", []), "custom_title": c.get("custom_title"), "is_pinned": c.get("is_pinned", False)} for c in safe_chats] @router.get('/chat-sessions/{uid}') async def get_chat_sessions(uid: str, user: dict = Depends(get_current_user)): """Lightweight chat list for the history drawer. SCALABILITY: this NEVER ships the full message arrays (a single chat can hold dozens of long RAG answers). A server-side aggregation projects only the first message (for a title) and the last message (for a one-line preview) plus the metadata the drawer needs. This keeps the payload tiny and the response fast even at 1M users. The full conversation is fetched on demand via /chat-session/... when the user actually opens a chat. """ enforce_self_or_admin(user, uid) pipeline = [ {"$match": {"user_id": uid}}, {"$project": { "_id": 0, "session_id": 1, "custom_title": 1, "is_pinned": 1, "last_updated": 1, "first_message": {"$arrayElemAt": ["$messages", 0]}, "last_message": {"$arrayElemAt": ["$messages", -1]}, "message_count": {"$size": {"$ifNull": ["$messages", []]}}, }}, {"$sort": {"last_updated": -1}}, {"$limit": 100}, ] chats = await chats_collection.aggregate(pipeline).to_list(length=100) safe = json.loads(json_util.dumps(chats)) result = [] for c in safe: first = c.get("first_message") or {} last = c.get("last_message") or {} result.append({ "session_id": c.get("session_id"), "custom_title": c.get("custom_title"), "is_pinned": c.get("is_pinned", False), "title": (first.get("content") or "")[:120], "preview": (last.get("content") or "")[:160], "message_count": c.get("message_count", 0), }) return result @router.get('/chat-session/{uid}/{session_id}') async def get_chat_session(uid: str, session_id: str, user: dict = Depends(get_current_user)): """Full message history for ONE conversation, fetched on demand when the user opens a chat from the history drawer.""" enforce_self_or_admin(user, uid) chat = await chats_collection.find_one( {"user_id": uid, "session_id": session_id}, {"_id": 0} ) if not chat: raise HTTPException(status_code=404, detail="Chat not found") safe = json.loads(json_util.dumps(chat)) return { "session_id": session_id, "messages": safe.get("messages", []), "custom_title": safe.get("custom_title"), "is_pinned": safe.get("is_pinned", False), } @router.delete('/chat-history/{user_id}/{session_id}') async def delete_chat(user_id: str, session_id: str, user: dict = Depends(get_current_user)): enforce_self_or_admin(user, user_id) await chats_collection.delete_one({"user_id": user_id, "session_id": session_id}) return {"status": "deleted"} @router.put('/chat-history/{user_id}/{session_id}/rename') async def rename_chat(user_id: str, session_id: str, payload: ChatTitleRequest, user: dict = Depends(get_current_user)): enforce_self_or_admin(user, user_id) await chats_collection.update_one({"user_id": user_id, "session_id": session_id}, {"$set": {"custom_title": payload.title}}) return {"status": "renamed"} @router.put('/chat-history/{user_id}/{session_id}/pin') async def pin_chat(user_id: str, session_id: str, payload: ChatPinRequest, user: dict = Depends(get_current_user)): enforce_self_or_admin(user, user_id) await chats_collection.update_one({"user_id": user_id, "session_id": session_id}, {"$set": {"is_pinned": payload.is_pinned}}) return {"status": "pin_updated"} # ========================================================= # 9. HIERARCHICAL LIBRARY ROUTES & BOUNDED CACHE # ========================================================= import urllib.parse from collections import OrderedDict import threading class BoundedTtlCache: """Thread-safe bounded in-memory cache with TTL and LRU eviction (Fixes HFL-06).""" def __init__(self, max_entries: int = 500, ttl: float = 600.0): self._max_entries = max_entries self._ttl = ttl self._cache: OrderedDict = OrderedDict() self._lock = threading.Lock() def get(self, key: Any) -> Optional[Any]: with self._lock: if key not in self._cache: return None ts, val = self._cache[key] if (time.monotonic() - ts) > self._ttl: del self._cache[key] return None self._cache.move_to_end(key) return val def put(self, key: Any, value: Any) -> Any: with self._lock: now = time.monotonic() if key in self._cache: self._cache[key] = (now, value) self._cache.move_to_end(key) else: if len(self._cache) >= self._max_entries: self._cache.popitem(last=False) self._cache[key] = (now, value) return value def pop(self, key: Any, default: Any = None) -> Any: with self._lock: if key in self._cache: _, val = self._cache.pop(key) return val return default def invalidate(self, prefix: Optional[str] = None): with self._lock: if prefix is None: self._cache.clear() else: keys_to_del = [k for k in self._cache if isinstance(k, str) and k.startswith(prefix)] for k in keys_to_del: del self._cache[k] def clear(self): with self._lock: self._cache.clear() def __contains__(self, key: Any) -> bool: return self.get(key) is not None def __len__(self) -> int: with self._lock: return len(self._cache) _LIB_CACHE = BoundedTtlCache(max_entries=500, ttl=600.0) def _lib_cache_get(key): return _LIB_CACHE.get(key) def _lib_cache_put(key, value): return _LIB_CACHE.put(key, value) def invalidate_hadith_library_cache(collection_name: Optional[str] = None): """Invalidates library cache entries (called on Hadith modifications, Fixes HFL-07).""" if collection_name: clean_col = urllib.parse.unquote(collection_name).strip() _LIB_CACHE.invalidate(f"hadiths:{clean_col}") _LIB_CACHE.invalidate(f"books:{clean_col}") _LIB_CACHE.invalidate(f"volumes:{clean_col}") _BOOK_BUNDLE_CACHE.pop(clean_col, None) _BOOK_BUNDLE_GZIP_CACHE.pop(clean_col, None) _LIB_CACHE.pop("books", None) _LIB_CACHE.pop("stats", None) else: _LIB_CACHE.clear() _BOOK_BUNDLE_CACHE.clear() _BOOK_BUNDLE_GZIP_CACHE.clear() HADITH_READER_PROJECTION = { "_id": 1, "hadith_id": 1, "hadith_no": 1, "book_no": 1, "book_name": 1, "chapter_no": 1, "chapter_name": 1, "volume": 1, "collection": 1, "narrator": 1, "text_ar": 1, "text_en": 1, "text_ur": 1, "text_hi": 1, "text_arabic": 1, "text_english": 1, "text_urdu": 1, "text_hinglish": 1, "grade": 1, "reference": 1, "sanad": 1, "source": 1, } @router.get('/api/library/books') async def get_all_books(): cached = _lib_cache_get("books") if cached is not None: return cached raw = await hadith_collection.distinct("collection") books = [b for b in raw if b and isinstance(b, str) and b.strip()] res = { "books": books, "total_collections": len(books), "content_version": "v1", } return _lib_cache_put("books", res) @router.get('/api/library/volumes/{collection_name}') async def get_volumes(collection_name: str): collection_name = urllib.parse.unquote(collection_name).strip() ckey = f"volumes:{collection_name}" cached = _lib_cache_get(ckey) if cached is not None: return cached pipeline = [ {"$match": {"collection": collection_name}}, {"$group": {"_id": "$volume", "first": {"$min": "$hadith_id"}, "last": {"$max": "$hadith_id"}, "count": {"$sum": 1}}}, ] rows = await hadith_collection.aggregate(pipeline).to_list(length=1000) rows = [r for r in rows if r.get("_id") is not None and str(r["_id"]).strip()] rows.sort(key=lambda r: int(r["_id"]) if str(r["_id"]).isdigit() else float('inf')) res = { "volumes": [ {"volume": r["_id"], "first": r.get("first"), "last": r.get("last"), "count": r.get("count", 0)} for r in rows ], "content_version": "v1", } return _lib_cache_put(ckey, res) @router.get('/api/library/books/{collection_name}/{volume_no}') async def get_books_by_volume(collection_name: str, volume_no: str): collection_name = urllib.parse.unquote(collection_name).strip() ckey = f"books:{collection_name}:{volume_no}" cached = _lib_cache_get(ckey) if cached is not None: return cached vol_num = int(volume_no) if volume_no.isdigit() else volume_no pipeline = [ {"$match": {"collection": collection_name, "volume": {"$in": [vol_num, str(volume_no)]}}}, {"$group": {"_id": "$book_no", "book_name": {"$first": "$book_name"}, "unique_chapters": {"$addToSet": "$chapter_no"}, "total_hadiths": {"$sum": 1}, "first": {"$min": "$hadith_id"}, "last": {"$max": "$hadith_id"}}} ] results = await hadith_collection.aggregate(pipeline).to_list(length=1000) formatted_books = sorted([{"book_no": r["_id"], "book_name": r.get("book_name", "Unknown Book"), "chapter_count": len(r.get("unique_chapters", [])), "hadith_count": r.get("total_hadiths", 0), "first": r.get("first"), "last": r.get("last")} for r in results], key=lambda x: int(x["book_no"]) if str(x["book_no"]).isdigit() else 0) res = {"sub_books": formatted_books, "content_version": "v1"} return _lib_cache_put(ckey, res) @router.get('/api/library/hadiths/{collection_name}/{volume_no}/{book_no}') async def get_hadiths_final( collection_name: str, volume_no: str, book_no: str, request: Request, cursor: Optional[str] = Query(default=None, description="Cursor for pagination (last seen hadith_id)"), limit: int = Query(default=20, ge=1, le=100, description="Page size (default 20, max 100)"), all: bool = Query(default=False, description="Fetch all hadiths without pagination"), ): collection_name = urllib.parse.unquote(collection_name).strip() ckey = f"hadiths:{collection_name}:{volume_no}:{book_no}:{cursor or 'first'}:{limit}:{all}" cached = _lib_cache_get(ckey) if cached is not None: etag = cached.get("etag") client_etag = request.headers.get("if-none-match") if client_etag and etag and client_etag.strip('"') == etag.strip('"'): return Response(status_code=304, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) return JSONResponse(content=cached, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) if etag else cached vol_num = int(volume_no) if volume_no.isdigit() else volume_no b_num = int(book_no) if book_no.isdigit() else book_no query: Dict[str, Any] = { "collection": collection_name, "volume": {"$in": [vol_num, str(volume_no)]}, "book_no": {"$in": [b_num, str(book_no)]}, } if cursor is not None and str(cursor).strip(): cur_str = str(cursor).strip() if cur_str.isdigit(): query["hadith_id"] = {"$gt": int(cur_str)} else: query["hadith_id"] = {"$gt": cur_str} page_size = 2000 if all else min(max(int(limit), 1), 100) # Fetch page_size + 1 to detect has_more without a secondary count query docs = await hadith_collection.find(query, HADITH_READER_PROJECTION).sort([("hadith_id", 1), ("_id", 1)]).to_list(length=page_size + 1) # HFL-02: DO NOT drop volume constraint if docs is empty. Volume separation must be strictly preserved. has_more = False next_cursor = None if len(docs) > page_size: has_more = True docs = docs[:page_size] last_hid = docs[-1].get("hadith_id") next_cursor = str(last_hid) if last_hid is not None else None # Single-pass fast serialization for d in docs: if "_id" in d: d["_id"] = str(d["_id"]) # Total count for initial page total_count = None if cursor is None: total_count = await hadith_collection.count_documents({ "collection": collection_name, "volume": {"$in": [vol_num, str(volume_no)]}, "book_no": {"$in": [b_num, str(book_no)]}, }) # Generate deterministic ETag first_id = docs[0].get("hadith_id") if docs else 0 last_id = docs[-1].get("hadith_id") if docs else 0 etag_raw = f"{collection_name}:{volume_no}:{book_no}:{cursor}:{len(docs)}:{first_id}:{last_id}:{total_count}" etag = f'"{hashlib.md5(etag_raw.encode("utf-8")).hexdigest()}"' client_etag = request.headers.get("if-none-match") if client_etag and client_etag.strip('"') == etag.strip('"'): return Response(status_code=304, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) res = { "hadiths": docs, "next_cursor": next_cursor, "has_more": has_more, "total_count": total_count, "content_version": "v1", "etag": etag, } _lib_cache_put(ckey, res) return JSONResponse( content=res, headers={ "ETag": etag, "Cache-Control": "private, must-revalidate, max-age=60", }, ) _BOOK_BUNDLE_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=10, ttl=3600.0) _BOOK_BUNDLE_GZIP_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=10, ttl=3600.0) @router.get('/api/library/bundle/{collection_name}') async def get_book_bundle(collection_name: str, request: Request): """ High-performance single-request collection bundle for instant offline downloads. Transfers the entire hadith book collection in a single gzip-compressed payload, reducing 100+ sequential round-trips to 1 instant transfer (30x speedup). """ accept_encoding = request.headers.get("accept-encoding", "") use_gzip = "gzip" in accept_encoding cached_bundle = _BOOK_BUNDLE_CACHE.get(collection_name) cached_gzip = _BOOK_BUNDLE_GZIP_CACHE.get(collection_name) if cached_bundle is not None: if use_gzip and cached_gzip is not None: return Response( content=cached_gzip, media_type="application/json", headers={ "Content-Encoding": "gzip", "Cache-Control": "public, max-age=604800, immutable", "Accept-Ranges": "bytes", } ) return Response( content=cached_bundle, media_type="application/json", headers={ "Cache-Control": "public, max-age=604800, immutable", "Accept-Ranges": "bytes", } ) # 1. Fetch volumes pipeline_vols = [ {"$match": {"collection": collection_name}}, {"$group": {"_id": "$volume", "first": {"$min": "$hadith_id"}, "last": {"$max": "$hadith_id"}, "count": {"$sum": 1}}}, ] rows = await hadith_collection.aggregate(pipeline_vols).to_list(length=1000) rows = [r for r in rows if r.get("_id") is not None and str(r["_id"]).strip()] rows.sort(key=lambda r: int(r["_id"]) if str(r["_id"]).isdigit() else float('inf')) volumes_data = [ {"volume": r["_id"], "first": r.get("first"), "last": r.get("last"), "count": r.get("count", 0)} for r in rows ] # 2. Fetch all hadiths in this collection (eliminate 32MB database in-memory sort limit) proj = {"embedding": 0, "embedding_1024": 0} all_hadiths = await hadith_collection.find({"collection": collection_name}, proj).to_list(length=20000) # Group hadiths by volume and book_no grouped_hadiths: Dict[str, Dict[str, list]] = {} sub_books_by_vol: Dict[str, list] = {} for h in all_hadiths: v_str = str(h.get("volume", "1")) b_str = str(h.get("book_no", "1")) if v_str not in grouped_hadiths: grouped_hadiths[v_str] = {} if b_str not in grouped_hadiths[v_str]: grouped_hadiths[v_str][b_str] = [] grouped_hadiths[v_str][b_str].append(h) def _hadith_sort_key(doc): hid = doc.get("hadith_id") try: return (0, int(hid)) except (ValueError, TypeError): return (1, str(hid or "")) # Format sub_books per volume for v_str, b_dict in grouped_hadiths.items(): sub_books = [] for b_str, h_list in b_dict.items(): h_list.sort(key=_hadith_sort_key) b_name = h_list[0].get("book_name", "Book " + b_str) if h_list else "Book " + b_str unique_chapters = list({h.get("chapter_no") for h in h_list if h.get("chapter_no")}) sub_books.append({ "book_no": b_str, "book_name": b_name, "chapter_count": len(unique_chapters), "hadith_count": len(h_list), "first": h_list[0].get("hadith_id") if h_list else None, "last": h_list[-1].get("hadith_id") if h_list else None, }) sub_books.sort(key=lambda x: int(x["book_no"]) if str(x["book_no"]).isdigit() else 0) sub_books_by_vol[v_str] = sub_books payload = { "collection": collection_name, "volumes": volumes_data, "sub_books_by_volume": sub_books_by_vol, "hadiths_by_volume_book": grouped_hadiths, } raw_json = json_util.dumps(payload) raw_bytes = raw_json.encode("utf-8") compressed = gzip.compress(raw_bytes) _BOOK_BUNDLE_CACHE.put(collection_name, raw_bytes) _BOOK_BUNDLE_GZIP_CACHE.put(collection_name, compressed) if use_gzip: return Response( content=compressed, media_type="application/json", headers={ "Content-Encoding": "gzip", "Cache-Control": "public, max-age=604800, immutable", "Accept-Ranges": "bytes", } ) return Response( content=raw_bytes, media_type="application/json", headers={ "Cache-Control": "public, max-age=604800, immutable", "Accept-Ranges": "bytes", } ) @router.get('/api/library/stats') async def get_library_stats(): cached = _lib_cache_get("stats") if cached is not None: return cached raw_collections = await hadith_collection.distinct("collection") valid_collections = [c for c in raw_collections if c and isinstance(c, str) and c.strip()] total_collections = len(valid_collections) # HFL-16: Group by collection, volume, and book_no so sub-books across volumes are counted accurately book_pairs = await hadith_collection.aggregate( [{"$group": {"_id": {"collection": "$collection", "volume": "$volume", "book_no": "$book_no"}}}] ).to_list(length=None) valid_pairs = [p for p in book_pairs if p.get("_id", {}).get("collection")] total_books = len(valid_pairs) total_hadiths = await hadith_collection.estimated_document_count() res = { "collections": total_collections, "books": total_books, "hadiths": total_hadiths, "content_version": "v1", } return _lib_cache_put("stats", res) # ========================================================= # 10. LIBRARY SEARCH ROUTE # ========================================================= _LIBRARY_SEARCH_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=200, ttl=300.0) @router.post('/api/library/search') @limiter.limit("1200/minute") async def library_search(payload: LibrarySearchRequest, request: Request): try: query = payload.query.lower().strip() if not query: return {"results": []} cache_key = (query, tuple(sorted(payload.books or []))) cached_res = _LIBRARY_SEARCH_CACHE.get(cache_key) if cached_res is not None: return Response(content=cached_res, media_type="application/json") numbers_in_query = re.findall(r'\d+', query) combined_results = [] seen_ids = set() # Only fetch the handful of fields the response actually uses — never # the 1024-float `embedding` nor the full Arabic/Urdu/Hinglish/sanad # text. This keeps every search result tiny and the query fast. proj = {"hadith_id": 1, "collection": 1, "book_name": 1, "chapter_name": 1, "text_english": 1, "reference": 1, "volume": 1, "book_no": 1} # --- 10.1 FAST-PATH EXACT MATCH (find hadith number N) --- collection_mapping = { "bukhari": "Bukhari", "muslim": "Muslim", "tirmidhi": "Tirmidhi", "tirmizi": "Tirmidhi", "dawud": "Dawud", "dawood": "Dawud", "nasai": "Nasa'i", "majah": "Majah", "muwatta": "Muwatta", "ahmad": "Ahmad", "darimi": "Darimi", "daraqutni": "Daraqutni" } is_pure_lookup = False # "just find hadith N" (no topical words) -> skip text search if numbers_in_query: # A pure lookup is a number (optionally with citation words / a # collection name) and NO topical words like "ablution". leftover = [w for w in re.sub(r'[\W\d_]+', ' ', query).split() if w not in collection_mapping and w not in ("hadith", "no", "number", "book", "bk", "vol", "volume")] is_pure_lookup = len(leftover) == 0 detected_col = next((val for key, val in collection_mapping.items() if key in query), None) if detected_col and payload.books and not any(detected_col.lower() in b.lower() for b in payload.books): detected_col = None # Scope precedence: a collection named in the query > the user's # selected book filter(s) > (bare number only) every collection. scope_filters = None if detected_col: scope_filters = [{"collection": {"$regex": detected_col, "$options": "i"}}] elif payload.books: scope_filters = [{"collection": b} for b in payload.books] elif is_pure_lookup: scope_filters = [{}] if scope_filters is not None: hadith_match = re.search(r'(?:hadith|no\.?|number|#)\s*(\d+)', query) book_match = re.search(r'(?:book|bk)\s*(\d+)', query) vol_match = re.search(r'(?:vol|volume)\s*(\d+)', query) ids = [int(hadith_match.group(1))] if hadith_match else [int(n) for n in numbers_in_query] # Match the HADITH NUMBER only (not book_no) — searching "50" # means hadith 50, returned from each scoped collection. id_or = [{"hadith_id": {"$in": ids}}, {"hadith_id": {"$in": [str(x) for x in ids]}}] for sf in scope_filters: ex = dict(sf) ex["$or"] = id_or if book_match: ex["book_no"] = {"$in": [int(book_match.group(1)), book_match.group(1)]} if vol_match: ex["volume"] = {"$in": [int(vol_match.group(1)), vol_match.group(1)]} exact_docs = await hadith_collection.find(ex, proj).sort("hadith_id", 1).limit(25).to_list(length=25) for doc in exact_docs: if str(doc['_id']) not in seen_ids: combined_results.append(doc) seen_ids.add(str(doc['_id'])) # --- 10.2 STANDARD TEXT SEARCH (skip for pure number lookups) --- if not is_pure_lookup: try: search_filter = {"$text": {"$search": query}} if payload.books: search_filter["collection"] = {"$in": payload.books} text_results = await hadith_collection.find(search_filter, {**proj, "score": {"$meta": "textScore"}}).sort([("score", {"$meta": "textScore"})]).limit(25).to_list(length=25) except Exception as text_e: logger.warning("Text index missing in library search, using Regex fallback.") search_filter = {"text_english": {"$regex": re.escape(query), "$options": "i"}} if payload.books: search_filter["collection"] = {"$in": payload.books} text_results = await hadith_collection.find(search_filter, proj).limit(25).to_list(length=25) for doc in text_results: if str(doc['_id']) not in seen_ids: combined_results.append(doc) seen_ids.add(str(doc['_id'])) # --- 10.3 FORMATTING --- formatted_results = [{ "hadith_id": str(r.get("hadith_id", "")), "collection": r.get("collection", "Unknown"), "book_name": r.get("book_name", "Unknown"), "chapter_name": r.get("chapter_name", "Unknown"), "text_english": r.get("text_english", ""), "reference": r.get("reference", ""), "volume": str(r.get("volume", "1")), "book_no": str(r.get("book_no", "1")) } for r in combined_results] res_obj = {"results": formatted_results} res_bytes = json.dumps(res_obj).encode("utf-8") _LIBRARY_SEARCH_CACHE.put(cache_key, res_bytes) return Response(content=res_bytes, media_type="application/json") except Exception as e: logger.error(f"Library Search Error: {str(e)}") raise HTTPException(status_code=500, detail="Internal Server Error") # ========================================================= # 📖 QURAN READER ENDPOINTS (Offloaded from AI Engine) # ========================================================= _QURAN_ALL_SURAHS_CACHE: Optional[bytes] = None _QURAN_ALL_SURAHS_GZIP_CACHE: Optional[bytes] = None _QURAN_SURAH_CACHE: Dict[int, bytes] = {} from app.services.quran_service import get_all_surahs_payload, get_surah_payload @router.get("/quran/surahs") @limiter.limit("1200/minute") async def get_all_surahs(request: Request): """ Fetches the lightweight metadata for all 114 Surahs. Ultra-high performance in-memory cached (0.005ms response). """ accept_encoding = request.headers.get("accept-encoding", "") use_gzip = "gzip" in accept_encoding payload_bytes, is_gzip = await get_all_surahs_payload(use_gzip=use_gzip) headers = {"Content-Encoding": "gzip"} if is_gzip else None return Response(content=payload_bytes, media_type="application/json", headers=headers) @router.get("/quran/surah/{surah_no}") @limiter.limit("1200/minute") async def get_surah_details(request: Request, surah_no: int): """ Fetches the complete Surah, including all Arabic text and translations. Ultra-high performance in-memory cached with pre-compressed gzip (0.01ms response). """ if surah_no < 1 or surah_no > 114: raise HTTPException(status_code=400, detail="Invalid Surah number (must be 1-114)") accept_encoding = request.headers.get("accept-encoding", "") use_gzip = "gzip" in accept_encoding payload_bytes, is_gzip = await get_surah_payload(surah_no, use_gzip=use_gzip) headers = {"Content-Encoding": "gzip"} if is_gzip else None return Response(content=payload_bytes, media_type="application/json", headers=headers) # ========================================================= # 11. DAILY PROGRESS / STREAK (server-authoritative, per user) # ========================================================= # Replaces the old device-local (SharedPreferences) store so a user's # "Today's Progress" and streak follow them across devices and survive a # reinstall. One small doc per (uid, local-date): # { uid, date: "YYYY-MM-DD", prayers: [...], quran_secs, hadith_secs } # The streak is computed by walking these docs backwards from today. PROGRESS_PRAYERS = ["Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"] PROGRESS_GOAL_SECONDS = 600 # 10 minutes to earn Quran / Hadith PROGRESS_TASBEEH_GOAL = 297 # 297 beads to satisfy the 7th deed option PROGRESS_TOTAL_GOALS = 7 # 5 prayers + quran + (hadith OR 297 tasbeeh) class ProgressPrayerReq(BaseModel): prayer: str = Field(..., max_length=20) tz: float = Field(default=0.0, ge=-12, le=14) done: Optional[bool] = None class SetPasswordRequest(BaseModel): has_password: bool = True class ProgressTimeReq(BaseModel): category: str = Field(..., pattern="^(quran|hadith)$") seconds: int = Field(..., ge=0, le=3600) tz: float = Field(default=0.0, ge=-12, le=14) class HarvestApplesReq(BaseModel): tz: float = Field(default=0.0, ge=-12, le=14) quran_secs: Optional[int] = Field(default=None, ge=0) hadith_secs: Optional[int] = Field(default=None, ge=0) tasbeeh_count: Optional[int] = Field(default=None, ge=0) prayers: Optional[List[str]] = Field(default=None) def _progress_local_date(tz: float) -> datetime.date: """The user's *local* calendar date, so 'today' rolls over at the user's midnight rather than UTC's.""" return (datetime.datetime.utcnow() + datetime.timedelta(hours=tz)).date() def _normalize_progress_prayer_name(raw: str) -> Optional[str]: if not raw: return None cleaned = str(raw).strip().upper() if cleaned in ("FAJR", "FAJAR"): return "Fajr" if cleaned in ("DHUHR", "ZUHR", "ZOHR", "DHUR", "ZUHAR", "JUMMAH", "JUMMA"): return "Dhuhr" if cleaned == "ASR": return "Asr" if cleaned in ("MAGHRIB", "MAGRIB"): return "Maghrib" if cleaned in ("ISHA", "ISHAA"): return "Isha" for p in PROGRESS_PRAYERS: if p.upper() == cleaned: return p return None def _progress_prayer_streak_day_complete(doc: dict) -> bool: """A day counts toward prayer streak if all 5 prayers are marked.""" if doc.get("is_full") is not None: return bool(doc["is_full"]) raw = doc.get("prayers", []) or [] prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) return all(p in prayers for p in PROGRESS_PRAYERS) def _progress_golden_streak_day_complete(doc: dict) -> bool: """A day counts toward golden streak if all 5 prayers are marked AND 10 min Quran AND (10 min Hadith OR 297 Tasbeeh).""" if doc.get("is_golden") is not None: return bool(doc["is_golden"]) raw = doc.get("prayers", []) or [] prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) all_prayers = all(p in prayers for p in PROGRESS_PRAYERS) quran_done = int(doc.get("quran_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS hadith_done = int(doc.get("hadith_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS tasbeeh_done = int(doc.get("tasbeeh_count", 0) or 0) >= PROGRESS_TASBEEH_GOAL return all_prayers and quran_done and (hadith_done or tasbeeh_done) def _progress_deeds_count(doc: dict) -> int: """Calculates the number of completed deeds out of 7 for a daily progress document. Deeds (7 total): - 5 daily prayers (Fajr, Dhuhr, Asr, Maghrib, Isha) - Quran recitation (>= 10 minutes / 600s) - Hadith study (>= 10 minutes / 600s) OR Tasbeeh dhikr (>= 297 beads) """ raw = doc.get("prayers", []) or [] prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) prayers_count = len([p for p in PROGRESS_PRAYERS if p in prayers]) quran_done = 1 if int(doc.get("quran_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS else 0 hadith_done = int(doc.get("hadith_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS tasbeeh_done = int(doc.get("tasbeeh_count", 0) or 0) >= PROGRESS_TASBEEH_GOAL seventh_deed = 1 if (hadith_done or tasbeeh_done) else 0 return prayers_count + quran_done + seventh_deed async def _recalculate_nimaz_stats_for_user(uid: str, today_str: Optional[str] = None) -> dict: """Aggregates all-time Nimaz & Spiritual stats for a user from daily_progress_collection and persists the single-document profile into nimaz_stats_collection (_id: uid). Guarantees O(1) performance for lifetime queries, missed counts, and qaza tracking.""" if not today_str: today_str = datetime.datetime.utcnow().date().isoformat() user_doc = None try: user_doc = await users_collection.find_one( {"$or": [{"uid": uid}, {"_id": uid}]}, {"progress_tracking_started_date": 1, "progress_tracking.started_date": 1} ) except Exception: user_doc = None started_date = (user_doc or {}).get("progress_tracking_started_date") or ((user_doc or {}).get("progress_tracking") or {}).get("started_date") try: docs = await daily_progress_collection.find( {"$or": [{"uid": uid}, {"_id": uid}]}, {"_id": 0, "date": 1, "prayers": 1, "quran_secs": 1, "hadith_secs": 1, "tasbeeh_count": 1, "is_full": 1, "is_golden": 1} ).to_list(length=5000) except Exception: docs = [] prayed_counts = {p: 0 for p in PROGRESS_PRAYERS} missed_counts = {p: 0 for p in PROGRESS_PRAYERS} total_quran_days = 0 total_golden_days = 0 total_full_days = 0 valid_days_count = 0 last_active_date = "" for doc in docs: d_str = doc.get("date") if not d_str: continue # Strictly ignore any days prior to the start of progress tracking if started_date and d_str < started_date: continue valid_days_count += 1 if not last_active_date or d_str > last_active_date: last_active_date = d_str raw_prayers = doc.get("prayers", []) or [] norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) is_past = d_str < today_str for p in PROGRESS_PRAYERS: if p in norm_set: prayed_counts[p] += 1 elif is_past: missed_counts[p] += 1 is_full = doc.get("is_full") if is_full is None: is_full = len(norm_set) == 5 if is_full: total_full_days += 1 q_secs = int(doc.get("quran_secs", 0) or 0) h_secs = int(doc.get("hadith_secs", 0) or 0) t_count = int(doc.get("tasbeeh_count", 0) or 0) if q_secs >= PROGRESS_GOAL_SECONDS: total_quran_days += 1 is_golden = doc.get("is_golden") if is_golden is None: is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) if is_golden: total_golden_days += 1 total_prayed = sum(prayed_counts.values()) total_missed = sum(missed_counts.values()) try: today_date = datetime.date.fromisoformat(today_str) streak, golden_streak = await _progress_streaks(uid, today_date) except Exception: streak, golden_streak = 0, 0 existing = {} try: existing = await nimaz_stats_collection.find_one({"_id": uid}, {"longest_streak": 1, "longest_golden_streak": 1}) or {} except Exception: pass longest_streak = max(int((existing or {}).get("longest_streak", 0) or 0), streak) longest_golden_streak = max(int((existing or {}).get("longest_golden_streak", 0) or 0), golden_streak) stats = { "_id": uid, "uid": uid, "prayed": prayed_counts, "missed": missed_counts, "total_prayed": total_prayed, "total_missed": total_missed, "total_days": valid_days_count, "total_full_days": total_full_days, "total_golden_days": total_golden_days, "total_quran_days": total_quran_days, "streak": streak, "golden_streak": golden_streak, "longest_streak": longest_streak, "longest_golden_streak": longest_golden_streak, "last_active_date": last_active_date or today_str, "updated_at": datetime.datetime.utcnow().isoformat() + "Z", } try: await nimaz_stats_collection.update_one( {"_id": uid}, {"$set": stats}, upsert=True ) except Exception: pass return stats async def _progress_streaks(uid: str, today: datetime.date) -> tuple[int, int]: """Calculate both normal prayer streak and golden streak. - Normal streak: consecutive days with all 5 prayers marked. - Golden streak: consecutive days with all 5 prayers + 10m Quran + (10m Hadith OR 297 Tasbeeh).""" start = (today - datetime.timedelta(days=366)).isoformat() docs = await daily_progress_collection.find( {"uid": uid, "date": {"$gte": start}}, {"_id": 0} ).to_list(length=400) prayer_complete = {d["date"] for d in docs if _progress_prayer_streak_day_complete(d)} golden_complete = {d["date"] for d in docs if _progress_golden_streak_day_complete(d)} # Prayer streak cursor = today if today.isoformat() in prayer_complete else today - datetime.timedelta(days=1) prayer_streak = 0 while cursor.isoformat() in prayer_complete: prayer_streak += 1 cursor -= datetime.timedelta(days=1) # Golden streak g_cursor = today if today.isoformat() in golden_complete else today - datetime.timedelta(days=1) golden_streak = 0 while g_cursor.isoformat() in golden_complete: golden_streak += 1 g_cursor -= datetime.timedelta(days=1) return prayer_streak, golden_streak def _progress_payload(doc: dict, date_str: str, streak: int, golden_streak: int = 0, lifetime_apples: int = 0) -> dict: raw = doc.get("prayers", []) or [] norm_prayers = [p for p in PROGRESS_PRAYERS if p in set(_normalize_progress_prayer_name(x) for x in raw)] return { "date": date_str, "prayers": norm_prayers, "quran_secs": int(doc.get("quran_secs", 0) or 0), "hadith_secs": int(doc.get("hadith_secs", 0) or 0), "tasbeeh_count": int(doc.get("tasbeeh_count", 0) or 0), "streak": streak, "golden_streak": golden_streak, "goal_seconds": PROGRESS_GOAL_SECONDS, "goal_tasbeeh": PROGRESS_TASBEEH_GOAL, "total_goals": PROGRESS_TOTAL_GOALS, "apples_collected": bool(doc.get("apples_collected", False)), "apples_collected_count": int(doc.get("apples_collected_count", 0) or 0), "apples_collected_at": doc.get("apples_collected_at"), "lifetime_apples": int(lifetime_apples or 0), } _DAILY_PROGRESS_CACHE: Dict[str, Tuple[float, bytes]] = {} _DAILY_PROGRESS_TTL = 15.0 # _MONTHLY_PROGRESS_CACHE declared above # _RECORDED_MISSED_CACHE declared above # _QAZA_STATE_CACHE declared above # _TASBEEH_SUMMARY_CACHE declared above _MONTHLY_PROGRESS_CACHE: Dict[str, Tuple[float, bytes]] = {} _MONTHLY_PROGRESS_TTL = 30.0 _RECORDED_MISSED_CACHE: Dict[str, Tuple[float, bytes]] = {} _RECORDED_MISSED_TTL = 30.0 _QAZA_STATE_CACHE: Dict[str, Tuple[float, bytes]] = {} _QAZA_STATE_TTL = 30.0 _TASBEEH_SUMMARY_CACHE: Dict[str, Tuple[float, bytes]] = {} _TASBEEH_SUMMARY_TTL = 15.0 def _invalidate_all_progress_caches(uid: str) -> None: """Invalidates all progress, streak, monthly calendar, recorded missed, and qaza caches for a user.""" prefix = f"{uid}:" for cache in (_DAILY_PROGRESS_CACHE, _MONTHLY_PROGRESS_CACHE, _RECORDED_MISSED_CACHE, _QAZA_STATE_CACHE, _TASBEEH_SUMMARY_CACHE): for k in list(cache.keys()): if k == uid or k.startswith(prefix): cache.pop(k, None) @router.get('/progress') @limiter.limit("1200/minute") async def get_progress(request: Request, tz: float = 0.0, user: Optional[dict] = Depends(get_optional_user)): today = _progress_local_date(tz) today_str = today.isoformat() if not user: return { "date": today_str, "prayers": [], "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0, "streak": 0, "golden_streak": 0, "goal_seconds": PROGRESS_GOAL_SECONDS, "goal_tasbeeh": PROGRESS_TASBEEH_GOAL, "total_goals": PROGRESS_TOTAL_GOALS, "apples_collected": False, "apples_collected_count": 0, "apples_collected_at": None, "lifetime_apples": 0, } uid = user["uid"] date_str = today.isoformat() cache_key = f"{uid}:{date_str}" ent = _DAILY_PROGRESS_CACHE.get(cache_key) if ent and (time.monotonic() - ent[0]) < _DAILY_PROGRESS_TTL: return Response(content=ent[1], media_type="application/json") doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} # Self-healing fallback: check tasbeeh_daily / tasbeeh_collection so tasbeeh deeds never reset to zero t_count = int(doc.get("tasbeeh_count", 0) or 0) try: t_daily = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_str}"}, {"total_count": 1}) if t_daily: sum_sessions = int(t_daily.get("total_count", 0) or 0) else: sessions = await tasbeeh_collection.find({"uid": uid, "date": date_str}, {"_id": 0, "count": 1}).to_list(length=300) sum_sessions = sum(int(s.get("count", 0) or 0) for s in sessions) if sessions else 0 if sum_sessions > t_count: t_count = sum_sessions doc["tasbeeh_count"] = t_count await daily_progress_collection.update_one( {"uid": uid, "date": date_str}, {"$set": {"tasbeeh_count": t_count}, "$setOnInsert": {"uid": uid, "date": date_str}}, upsert=True, ) except Exception: pass user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) lifetime_apples = 0 if user_doc: lifetime_apples = int(user_doc.get("lifetime_apples") or (user_doc.get("metrics") or {}).get("lifetime_apples", 0) or 0) elif user: lifetime_apples = int(user.get("lifetime_apples") or (user.get("metrics") or {}).get("lifetime_apples", 0) or 0) streak, golden_streak = await _progress_streaks(uid, today) res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) raw_bytes = json.dumps(res).encode("utf-8") _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") @router.post('/progress/prayer') @limiter.limit("120/minute") async def toggle_progress_prayer(payload: ProgressPrayerReq, request: Request, user: dict = Depends(get_current_user)): target_prayer = _normalize_progress_prayer_name(payload.prayer) if not target_prayer: raise HTTPException(status_code=400, detail=f"Unknown prayer: {payload.prayer}") uid = user["uid"] today = _progress_local_date(payload.tz) date_str = today.isoformat() cache_key = f"{uid}:{date_str}" if payload.done is not None: target_done = bool(payload.done) else: existing_doc = {} try: existing_doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"prayers": 1, "_id": 0}) or {} except Exception: pass existing_prayers = [str(x) for x in ((existing_doc or {}).get("prayers", []) or [])] target_done = target_prayer not in set(_normalize_progress_prayer_name(p) for p in existing_prayers if _normalize_progress_prayer_name(p)) # ATOMIC MONGODB UPDATE to prevent concurrent race conditions & lost updates if target_done: update_op = { "$addToSet": {"prayers": target_prayer}, "$setOnInsert": {"uid": uid, "date": date_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} } else: update_op = { "$pull": {"prayers": target_prayer}, "$setOnInsert": {"uid": uid, "date": date_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} } updated_doc = await daily_progress_collection.find_one_and_update( {"uid": uid, "date": date_str}, update_op, upsert=True, return_document=ReturnDocument.AFTER, projection={"_id": 0} ) or {} raw_prayers = updated_doc.get("prayers", []) or [] norm_set = set(_normalize_progress_prayer_name(str(p)) for p in raw_prayers if _normalize_progress_prayer_name(str(p))) ordered = [p for p in PROGRESS_PRAYERS if p in norm_set] completed_count = len(ordered) is_full = completed_count == 5 q_secs = int(updated_doc.get("quran_secs", 0) or 0) h_secs = int(updated_doc.get("hadith_secs", 0) or 0) t_count = int(updated_doc.get("tasbeeh_count", 0) or 0) is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) now_iso = datetime.datetime.utcnow().isoformat() + "Z" # Persist metadata and canonical order set_fields = { "completed_count": completed_count, "is_full": is_full, "is_golden": is_golden, "updated_at": now_iso, } if raw_prayers != ordered: set_fields["prayers"] = ordered updated_doc["prayers"] = ordered try: await daily_progress_collection.update_one( {"uid": uid, "date": date_str}, {"$set": set_fields} ) except Exception: pass # Synchronize nimaz_stats_collection atomically try: inc_ops = { f"prayed.{target_prayer}": 1 if target_done else -1, "total_prayed": 1 if target_done else -1, } if date_str < today.isoformat(): inc_ops[f"missed.{target_prayer}"] = -1 if target_done else 1 inc_ops["total_missed"] = -1 if target_done else 1 await nimaz_stats_collection.update_one( {"_id": uid}, { "$inc": inc_ops, "$set": {"last_active_date": date_str, "updated_at": now_iso}, "$setOnInsert": { "_id": uid, "uid": uid, "prayed": {p: (1 if p == target_prayer and target_done else 0) for p in PROGRESS_PRAYERS}, "missed": {p: 0 for p in PROGRESS_PRAYERS}, "total_prayed": 1 if target_done else 0, "total_missed": 0, "total_days": 1, "total_full_days": 1 if is_full else 0, "total_golden_days": 1 if is_golden else 0, "total_quran_days": 0, "streak": 1 if is_full else 0, "golden_streak": 1 if is_golden else 0, "longest_streak": 1 if is_full else 0, "longest_golden_streak": 1 if is_golden else 0, } }, upsert=True ) except Exception: pass streak, golden_streak = await _progress_streaks(uid, today) try: await nimaz_stats_collection.update_one( {"_id": uid}, { "$set": {"streak": streak, "golden_streak": golden_streak}, "$max": {"longest_streak": streak, "longest_golden_streak": golden_streak} } ) except Exception: pass user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) res = _progress_payload(updated_doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) raw_bytes = json.dumps(res).encode("utf-8") # Invalidate monthly/recorded/qaza caches and populate fresh daily cache _invalidate_all_progress_caches(uid) _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) # Notify client devices via real-time bus realtime.publish(uid, "prayer_progress_updated", {"date": date_str, "prayers": ordered}) return Response(content=raw_bytes, media_type="application/json") @router.post('/set-password-status') async def set_password_status(payload: SetPasswordRequest, user: dict = Depends(get_current_user)): await users_collection.update_one( {"uid": user["uid"]}, {"$set": {"has_password": payload.has_password}} ) return {"status": "ok", "has_password": payload.has_password} @router.post('/progress/time') @limiter.limit("120/minute") async def add_progress_time(payload: ProgressTimeReq, request: Request, user: dict = Depends(get_current_user)): uid = user["uid"] today = _progress_local_date(payload.tz) date_str = today.isoformat() field = "quran_secs" if payload.category == "quran" else "hadith_secs" doc = await daily_progress_collection.find_one( {"uid": uid, "date": date_str}, {"_id": 0} ) or {} current = int(doc.get(field, 0) or 0) if current >= PROGRESS_GOAL_SECONDS: streak, golden_streak = await _progress_streaks(uid, today) return _progress_payload(doc, date_str, streak, golden_streak) nxt = min(current + payload.seconds, PROGRESS_GOAL_SECONDS) doc[field] = nxt raw_prayers = doc.get("prayers", []) or [] norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) is_full = len(norm_set) == 5 q_secs = int(doc.get("quran_secs", 0) or 0) h_secs = int(doc.get("hadith_secs", 0) or 0) t_count = int(doc.get("tasbeeh_count", 0) or 0) is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) now_iso = datetime.datetime.utcnow().isoformat() + "Z" await daily_progress_collection.update_one( {"uid": uid, "date": date_str}, { "$set": { field: nxt, "is_golden": is_golden, "is_full": is_full, "completed_count": len(norm_set), "updated_at": now_iso, }, "$setOnInsert": { "_id": f"{uid}:{date_str}", "uid": uid, "date": date_str, "prayers": [], } }, upsert=True, ) doc["is_golden"] = is_golden doc["is_full"] = is_full streak, golden_streak = await _progress_streaks(uid, today) try: await nimaz_stats_collection.update_one( {"_id": uid}, { "$set": {"streak": streak, "golden_streak": golden_streak}, "$max": {"longest_streak": streak, "longest_golden_streak": golden_streak} } ) except Exception: pass user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) raw_bytes = json.dumps(res).encode("utf-8") _invalidate_all_progress_caches(uid) _DAILY_PROGRESS_CACHE[f"{uid}:{date_str}"] = (time.monotonic(), raw_bytes) # Broadcast realtime event so connected devices / homepage update instantly realtime.publish(uid, "prayer_progress_updated", { "date": date_str, field: nxt, }) return Response(content=raw_bytes, media_type="application/json") class DailyProgressBatchItem(BaseModel): date: str = Field(..., pattern=r"^\d{4}-\d{2}-\d{2}$") prayers: List[str] = Field(default_factory=list) quran_secs: Optional[int] = Field(default=None, ge=0, le=3600) hadith_secs: Optional[int] = Field(default=None, ge=0, le=3600) tasbeeh_count: Optional[int] = Field(default=None, ge=0) class ProgressBatchSyncReq(BaseModel): updates: List[DailyProgressBatchItem] = Field(..., max_length=60) tz: float = Field(default=0.0, ge=-12, le=14) @router.post('/progress/batch-sync') @limiter.limit("60/minute") async def batch_sync_progress(payload: ProgressBatchSyncReq, request: Request, user: dict = Depends(get_current_user)): """Idempotently batch-syncs offline prayer and spiritual progress logs.""" uid = user["uid"] today = _progress_local_date(payload.tz) today_str = today.isoformat() now_iso = datetime.datetime.utcnow().isoformat() + "Z" for item in payload.updates: d_str = item.date norm_set = set(_normalize_progress_prayer_name(p) for p in item.prayers if _normalize_progress_prayer_name(p)) ordered = [p for p in PROGRESS_PRAYERS if p in norm_set] completed_count = len(ordered) is_full = completed_count == 5 update_set: dict = { "prayers": ordered, "completed_count": completed_count, "is_full": is_full, "updated_at": now_iso, } if item.quran_secs is not None: update_set["quran_secs"] = item.quran_secs if item.hadith_secs is not None: update_set["hadith_secs"] = item.hadith_secs if item.tasbeeh_count is not None: update_set["tasbeeh_count"] = item.tasbeeh_count # Read existing to compute is_golden existing = await daily_progress_collection.find_one({"uid": uid, "date": d_str}, {"_id": 0}) or {} q_secs = update_set.get("quran_secs", existing.get("quran_secs", 0)) or 0 h_secs = update_set.get("hadith_secs", existing.get("hadith_secs", 0)) or 0 t_count = update_set.get("tasbeeh_count", existing.get("tasbeeh_count", 0)) or 0 update_set["is_golden"] = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) await daily_progress_collection.update_one( {"uid": uid, "date": d_str}, { "$set": update_set, "$setOnInsert": { "_id": f"{uid}:{d_str}", "uid": uid, "date": d_str, } }, upsert=True ) # Recalculate lifetime stats & streaks stats = await _recalculate_nimaz_stats_for_user(uid, today_str) _invalidate_all_progress_caches(uid) today_doc = await daily_progress_collection.find_one({"uid": uid, "date": today_str}, {"_id": 0}) or {} user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) res = _progress_payload(today_doc, today_str, stats.get("streak", 0), stats.get("golden_streak", 0), lifetime_apples=lifetime_apples) return res @router.post('/progress/harvest-apples') @limiter.limit("60/minute") async def harvest_daily_apples(payload: HarvestApplesReq, request: Request, user: dict = Depends(get_current_user)): """ Harvests 10 apples for the user when all 7 daily deeds are completed. - Validates that user completed all 7 deeds today. - Idempotent: if already harvested today, returns current state with status 'already_harvested'. - Atomically sets apples_collected: True on daily_progress_collection. - Atomically increments lifetime_apples by +10 in users_collection & nimaz_stats_collection. - Apples never expire and accumulate across a user's lifetime. """ uid = user["uid"] today = _progress_local_date(payload.tz) date_str = today.isoformat() cache_key = f"{uid}:{date_str}" doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} # Reconcile client-reported progress if provided dirty = False if payload.quran_secs is not None and payload.quran_secs > int(doc.get("quran_secs", 0) or 0): doc["quran_secs"] = payload.quran_secs dirty = True if payload.hadith_secs is not None and payload.hadith_secs > int(doc.get("hadith_secs", 0) or 0): doc["hadith_secs"] = payload.hadith_secs dirty = True if payload.tasbeeh_count is not None and payload.tasbeeh_count > int(doc.get("tasbeeh_count", 0) or 0): doc["tasbeeh_count"] = payload.tasbeeh_count dirty = True if payload.prayers: existing_p = set(doc.get("prayers", []) or []) merged_p = list(existing_p.union(set(payload.prayers))) if len(merged_p) > len(existing_p): doc["prayers"] = merged_p dirty = True # Self-healing fallback: verify tasbeeh sessions t_count = int(doc.get("tasbeeh_count", 0) or 0) try: t_daily = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_str}"}, {"total_count": 1}) if t_daily: sum_sessions = int(t_daily.get("total_count", 0) or 0) else: sessions = await tasbeeh_collection.find({"uid": uid, "date": date_str}, {"_id": 0, "count": 1}).to_list(length=300) sum_sessions = sum(int(s.get("count", 0) or 0) for s in sessions) if sessions else 0 if sum_sessions > t_count: t_count = sum_sessions doc["tasbeeh_count"] = t_count dirty = True except Exception: pass if dirty: try: await daily_progress_collection.update_one( {"uid": uid, "date": date_str}, { "$set": { "quran_secs": doc.get("quran_secs", 0), "hadith_secs": doc.get("hadith_secs", 0), "tasbeeh_count": doc.get("tasbeeh_count", 0), "prayers": doc.get("prayers", []), }, "$setOnInsert": {"_id": f"{uid}:{date_str}", "uid": uid, "date": date_str} }, upsert=True ) except Exception: pass completed_deeds = _progress_deeds_count(doc) if completed_deeds < PROGRESS_TOTAL_GOALS: raise HTTPException( status_code=400, detail=f"Cannot harvest apples: {completed_deeds}/{PROGRESS_TOTAL_GOALS} deeds completed. All 7 deeds must be done." ) # Fetch current lifetime apples (matching by either uid or _id) user_doc = await users_collection.find_one( {"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1} ) current_lifetime = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or 0) streak, golden_streak = await _progress_streaks(uid, today) # Idempotent check if bool(doc.get("apples_collected", False)): payload_res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=current_lifetime) return { "status": "already_harvested", "message": "10 apples already harvested today.", "apples_harvested": 0, "apples_collected_count": int(doc.get("apples_collected_count", 10)), "lifetime_apples": current_lifetime, "progress": payload_res, } now_iso = datetime.datetime.utcnow().isoformat() + "Z" # Atomically mark apples_collected in daily_progress_collection dp_update = await daily_progress_collection.find_one_and_update( { "uid": uid, "date": date_str, "$or": [ {"apples_collected": {"$ne": True}}, {"apples_collected": {"$exists": False}}, ] }, { "$set": { "apples_collected": True, "apples_collected_count": 10, "apples_collected_at": now_iso, "updated_at": now_iso, }, "$setOnInsert": { "_id": f"{uid}:{date_str}", "uid": uid, "date": date_str, } }, upsert=True, return_document=ReturnDocument.AFTER, ) # If atomic update didn't match (concurrent harvest), return idempotent response if dp_update is None: user_doc_fresh = await users_collection.find_one( {"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1} ) fresh_lifetime = int((user_doc_fresh or {}).get("lifetime_apples") or ((user_doc_fresh or {}).get("metrics") or {}).get("lifetime_apples", 0) or current_lifetime) fresh_dp = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} payload_res = _progress_payload(fresh_dp, date_str, streak, golden_streak, lifetime_apples=fresh_lifetime) return { "status": "already_harvested", "message": "10 apples already harvested today.", "apples_harvested": 0, "apples_collected_count": int(fresh_dp.get("apples_collected_count", 10)), "lifetime_apples": fresh_lifetime, "progress": payload_res, } # Increment lifetime_apples in users_collection atomically (with upsert) user_update = await users_collection.find_one_and_update( {"$or": [{"uid": uid}, {"_id": uid}]}, { "$inc": { "lifetime_apples": 10, "metrics.lifetime_apples": 10, }, "$set": { "uid": uid, "updated_at": datetime.datetime.utcnow(), } }, upsert=True, return_document=ReturnDocument.AFTER, ) new_lifetime = 10 if user_update: new_lifetime = int(user_update.get("lifetime_apples") or (user_update.get("metrics") or {}).get("lifetime_apples", 0) or 10) # Also record in nimaz_stats_collection for aggregation consistency try: await nimaz_stats_collection.update_one( {"_id": uid}, { "$inc": {"lifetime_apples": 10}, "$set": {"updated_at": now_iso}, }, upsert=True, ) except Exception: pass await invalidate_user_cache(uid) _invalidate_all_progress_caches(uid) dp_doc = dict(dp_update) dp_doc.pop("_id", None) dp_doc["apples_collected"] = True dp_doc["apples_collected_count"] = 10 payload_res = _progress_payload(dp_doc, date_str, streak, golden_streak, lifetime_apples=new_lifetime) raw_bytes = json.dumps(payload_res).encode("utf-8") _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) # Broadcast realtime event realtime.publish(uid, "apple_harvest_completed", { "date": date_str, "apples_collected": True, "apples_harvested": 10, "lifetime_apples": new_lifetime, }) return { "status": "success", "message": "Masha'Allah! You harvested 10 apples into your Barakah Basket.", "apples_harvested": 10, "apples_collected_count": 10, "lifetime_apples": new_lifetime, "progress": payload_res, } # ========================================================= # 11. LIFETIME QAZA TRACKER & MONTHLY SALAH ANALYTICS # ========================================================= class QazaSetupReq(BaseModel): fajr: int = Field(0, ge=0) dhuhr: int = Field(0, ge=0) asr: int = Field(0, ge=0) maghrib: int = Field(0, ge=0) isha: int = Field(0, ge=0) witr: int = Field(0, ge=0) include_witr: bool = True daily_target: int = Field(1, ge=1, le=50) class QazaLogReq(BaseModel): prayer: str = Field(..., pattern="^(fajr|dhuhr|asr|maghrib|isha|witr)$") count: int = Field(1, ge=-10, le=10) async def _calculate_dynamic_qaza_state(uid: str, tz: float = 0.0): today = _progress_local_date(tz) today_str = today.isoformat() # 1. Fetch pre-aggregated lifetime missed/prayed stats from nimaz_stats in O(1) stats = None try: stats = await nimaz_stats_collection.find_one({"_id": uid}, {"_id": 0}) except Exception: pass if not stats: stats = await _recalculate_nimaz_stats_for_user(uid, today_str) hist_missed = (stats or {}).get("missed", {}) or {} missed_counts = { "fajr": int(hist_missed.get("Fajr", 0) or 0), "dhuhr": int(hist_missed.get("Dhuhr", 0) or 0), "asr": int(hist_missed.get("Asr", 0) or 0), "maghrib": int(hist_missed.get("Maghrib", 0) or 0), "isha": int(hist_missed.get("Isha", 0) or 0), } # 2. Check today's daily progress in O(1) today_doc = None try: today_doc = await daily_progress_collection.find_one({"uid": uid, "date": today_str}, {"_id": 0, "prayers": 1}) except Exception: pass # If user has an active tracking record for today, check expired prayers for today if today_doc is not None: today_raw = today_doc.get("prayers", []) or [] today_prayed_set = set(_normalize_progress_prayer_name(p).lower() for p in today_raw if _normalize_progress_prayer_name(p)) now_utc = datetime.datetime.now(datetime.timezone.utc) local_hour = (now_utc.hour + tz + (now_utc.minute / 60.0)) % 24.0 for p in ["fajr", "dhuhr", "asr", "maghrib", "isha"]: if p not in today_prayed_set: expired = False if p == "fajr" and local_hour >= 7.0: # Expired at sunrise expired = True elif p == "dhuhr" and local_hour >= 16.0: # Expired at Asr azan expired = True elif p == "asr" and local_hour >= 18.0: # Expired at Maghrib azan expired = True elif p == "maghrib" and local_hour >= 19.5: # Expired at Isha azan expired = True if expired: missed_counts[p] += 1 # 3. Fetch logged makeups and baseline from qaza_collection in O(1) q_doc = await qaza_collection.find_one({"_id": uid}) or {} makeups = q_doc.get("makeups") or {} completed_total = 0 pending = {} total_baseline = 0 for p in ["fajr", "dhuhr", "asr", "maghrib", "isha"]: base_p = int(q_doc.get(f"baseline_{p}", 0) or 0) total_baseline += base_p done_p = int(makeups.get(p, 0) or q_doc.get(f"completed_{p}", 0) or 0) completed_total += done_p pending[p] = max(0, base_p + missed_counts[p] - done_p) witr_base = int(q_doc.get("baseline_witr", 0) or 0) witr_done = int(makeups.get("witr", 0) or q_doc.get("completed_witr", 0) or 0) pending["witr"] = max(0, witr_base - witr_done) completed_total += witr_done total_baseline += witr_base initial_total = total_baseline + sum(missed_counts.values()) return { "_id": uid, "uid": uid, "fajr": pending["fajr"], "dhuhr": pending["dhuhr"], "asr": pending["asr"], "maghrib": pending["maghrib"], "isha": pending["isha"], "witr": pending["witr"], "initial_total": initial_total, "completed_total": completed_total, "include_witr": q_doc.get("include_witr", False), "daily_target": q_doc.get("daily_target", 1), "is_setup": bool(q_doc.get("is_setup", False)) or total_baseline > 0, "missed_from_progress": missed_counts, "makeups": {p: int(makeups.get(p, 0) or 0) for p in ["fajr", "dhuhr", "asr", "maghrib", "isha", "witr"]}, "updated_at": datetime.datetime.utcnow().isoformat() + "Z", } _QAZA_STATE_CACHE: Dict[str, Tuple[float, bytes]] = {} _QAZA_STATE_TTL = 15.0 @router.get('/qaza') @limiter.limit("1200/minute") async def get_qaza(request: Request, tz: float = 0.0, user: dict = Depends(get_current_user)): uid = user["uid"] cache_key = f"{uid}:{tz}" ent = _QAZA_STATE_CACHE.get(cache_key) if ent and (time.monotonic() - ent[0]) < _QAZA_STATE_TTL: return Response(content=ent[1], media_type="application/json") res = await _calculate_dynamic_qaza_state(uid, tz) raw_bytes = json.dumps(res, default=str).encode("utf-8") _QAZA_STATE_CACHE[cache_key] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") @router.post('/qaza/setup') @limiter.limit("60/minute") async def setup_qaza(payload: QazaSetupReq, request: Request, user: dict = Depends(get_current_user)): uid = user["uid"] await qaza_collection.update_one( {"_id": uid}, { "$set": { "baseline_fajr": payload.fajr, "baseline_dhuhr": payload.dhuhr, "baseline_asr": payload.asr, "baseline_maghrib": payload.maghrib, "baseline_isha": payload.isha, "baseline_witr": payload.witr, "include_witr": payload.include_witr, "daily_target": payload.daily_target, "is_setup": True, "updated_at": datetime.datetime.utcnow().isoformat() + "Z", }, "$setOnInsert": { "_id": uid, "uid": uid, "makeups": {}, "created_at": datetime.datetime.utcnow().isoformat() + "Z", } }, upsert=True ) _invalidate_all_progress_caches(uid) return await _calculate_dynamic_qaza_state(uid) @router.post('/qaza/log') @limiter.limit("120/minute") async def log_qaza(payload: QazaLogReq, request: Request, user: dict = Depends(get_current_user)): uid = user["uid"] prayer = payload.prayer.lower() delta = payload.count q_doc = await qaza_collection.find_one({"_id": uid}) or { "_id": uid, "uid": uid, "makeups": {}, } makeups = q_doc.get("makeups") or {} cur_makeups = int(makeups.get(prayer, 0) or q_doc.get(f"completed_{prayer}", 0) or 0) new_makeups = max(0, cur_makeups + delta) makeups[prayer] = new_makeups await qaza_collection.update_one( {"_id": uid}, { "$set": { f"makeups.{prayer}": new_makeups, f"completed_{prayer}": new_makeups, "updated_at": datetime.datetime.utcnow().isoformat() + "Z", }, "$setOnInsert": {"_id": uid, "uid": uid, "created_at": datetime.datetime.utcnow().isoformat() + "Z"} }, upsert=True ) _invalidate_all_progress_caches(uid) return await _calculate_dynamic_qaza_state(uid) _MONTHLY_PROGRESS_CACHE: Dict[str, Tuple[float, bytes]] = {} _MONTHLY_PROGRESS_TTL = 30.0 @router.get('/progress/monthly') @limiter.limit("1200/minute") async def get_progress_monthly( request: Request, tz: float = 0.0, year: Optional[int] = None, month: Optional[int] = None, user: dict = Depends(get_current_user) ): uid = user["uid"] cache_key = f"{uid}:{tz}:{year}:{month}" ent = _MONTHLY_PROGRESS_CACHE.get(cache_key) if ent and (time.monotonic() - ent[0]) < _MONTHLY_PROGRESS_TTL: return Response(content=ent[1], media_type="application/json") import calendar today = _progress_local_date(tz) if year is not None and month is not None: target_year = int(year) target_month = int(month) if target_month < 1 or target_month > 12: raise HTTPException(status_code=400, detail="Invalid month (must be 1-12)") else: target_year = today.year target_month = today.month # 1. Reject future months if target_year > today.year or (target_year == today.year and target_month > today.month): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Cannot navigate to future months.", ) # 2. Access control for previous/historical months is_current_month = (target_year == today.year and target_month == today.month) if not is_current_month: from app.auth_deps import has_user_historical_progress if not await has_user_historical_progress(user): locked_res = { "locked": True, "message": "Historical progress is locked for previous months. Upgrade to Progress Plus (₹60/mo) or Sahih Pro to view your saved history.", "required_plans": ["sahih_progress_plus_monthly", "sahih_pro_monthly"], "year": target_year, "month": target_month, "days": [], "total_days": 0, "elapsed_days": 0, "full_days": 0, "golden_days": 0, "consistency_pct": 0.0, "prayer_breakdown": {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0}, "strongest_prayer": "Locked", "weakest_prayer": "Locked", "streak": 0, "golden_streak": 0, } return Response(content=json.dumps(locked_res).encode("utf-8"), media_type="application/json") # 3. Bound past data to the user's tracking start date user_doc = None try: user_doc = await users_collection.find_one( {"$or": [{"uid": uid}, {"_id": uid}]}, {"progress_tracking": 1, "progress_tracking_enabled": 1, "progress_tracking_started_date": 1, "created_at": 1} ) except Exception: user_doc = None effective_user = {**(user or {})} if user_doc: effective_user.update(user_doc) pt = effective_user.get("progress_tracking") or {} is_tracking_enabled = effective_user.get("progress_tracking_enabled", pt.get("enabled", True)) started_date_str = effective_user.get("progress_tracking_started_date", pt.get("started_date")) earliest_year = today.year earliest_month = today.month if started_date_str: try: earliest_year = int(started_date_str[:4]) earliest_month = int(started_date_str[5:7]) except Exception: earliest_year = today.year earliest_month = today.month # If requested month is strictly before earliest tracking month, boundary reached! if target_year < earliest_year or (target_year == earliest_year and target_month < earliest_month): before_res = { "before_start": True, "locked": False, "is_tracking_enabled": bool(is_tracking_enabled), "earliest_date": started_date_str, "earliest_year": earliest_year, "earliest_month": earliest_month, "year": target_year, "month": target_month, "days": [], "total_days": 0, "elapsed_days": 0, "full_days": 0, "golden_days": 0, "consistency_pct": 0.0, "prayer_breakdown": {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0}, "strongest_prayer": "None", "weakest_prayer": "None", "streak": 0, "golden_streak": 0, } return Response(content=json.dumps(before_res).encode("utf-8"), media_type="application/json") else: started_date_str = today.isoformat() _, num_days = calendar.monthrange(target_year, target_month) start_date = f"{target_year:04d}-{target_month:02d}-01" end_date = f"{target_year:04d}-{target_month:02d}-{num_days:02d}" dates_range = [f"{target_year:04d}-{target_month:02d}-{d:02d}" for d in range(1, num_days + 1)] docs = await daily_progress_collection.find( {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0} ).sort("date", 1).to_list(length=100) # Fetch tasbeeh daily rollups for the date range to guarantee all deeds are included tasbeeh_by_date = {} try: tasbeeh_dailies = await tasbeeh_daily_collection.find( {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0, "date": 1, "total_count": 1} ).to_list(length=35) for td in tasbeeh_dailies: td_date = td.get("date") if td_date: tasbeeh_by_date[td_date] = int(td.get("total_count", 0) or 0) except Exception: pass if not tasbeeh_by_date: tasbeeh_sessions = await tasbeeh_collection.find( {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0, "date": 1, "count": 1} ).to_list(length=1000) for ts in tasbeeh_sessions: ts_date = ts.get("date") if ts_date: tasbeeh_by_date[ts_date] = tasbeeh_by_date.get(ts_date, 0) + int(ts.get("count", 0) or 0) docs_by_date = {d["date"]: d for d in docs} days_list = [] prayer_counts = {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0} full_days = 0 golden_days = 0 for d in dates_range: item = docs_by_date.get(d, {}) raw_prayers = item.get("prayers", []) or [] norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) prayers = [p for p in PROGRESS_PRAYERS if p in norm_set] is_full = len(prayers) == 5 q_secs = int(item.get("quran_secs", 0) or 0) h_secs = int(item.get("hadith_secs", 0) or 0) t_count = max(int(item.get("tasbeeh_count", 0) or 0), tasbeeh_by_date.get(d, 0)) is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) is_untracked = bool(started_date_str and d < started_date_str) if not is_untracked: if is_full: full_days += 1 if is_golden: golden_days += 1 for p in prayers: if p in prayer_counts: prayer_counts[p] += 1 days_list.append({ "date": d, "prayers": prayers if not is_untracked else [], "completed_count": len(prayers) if not is_untracked else 0, "is_full": is_full if not is_untracked else False, "is_golden": is_golden if not is_untracked else False, "is_untracked": is_untracked, "quran_secs": q_secs if not is_untracked else 0, "hadith_secs": h_secs if not is_untracked else 0, "tasbeeh_count": t_count if not is_untracked else 0, }) # Elapsed days calculation (so future unelapsed days & pre-start days don't falsely register as missed) is_current_month = (target_year == today.year and target_month == today.month) is_earliest_month = (target_year == earliest_year and target_month == earliest_month) start_day = 1 if is_earliest_month and started_date_str: try: start_day = int(started_date_str[8:10]) except Exception: start_day = 1 if is_current_month: elapsed_days = max(1, min(num_days, today.day) - start_day + 1) if is_earliest_month else min(num_days, today.day) elif target_year < today.year or (target_year == today.year and target_month < today.month): elapsed_days = max(1, num_days - start_day + 1) if is_earliest_month else num_days else: elapsed_days = 0 total_days = len(dates_range) effective_days = elapsed_days if elapsed_days > 0 else total_days # Due days per prayer calculation (prayers whose time has not started today are not yet due) due_counts = {} today_str = today.isoformat() now_local = datetime.datetime.utcnow() + datetime.timedelta(hours=tz) local_h = now_local.hour + (now_local.minute / 60.0) today_item = docs_by_date.get(today_str, {}) today_raw = today_item.get("prayers", []) or [] today_norm = set(_normalize_progress_prayer_name(x) for x in today_raw if _normalize_progress_prayer_name(x)) if is_current_month: past_days_count = max(0, min(num_days, today.day - 1) - start_day + 1) if is_earliest_month else max(0, min(num_days, today.day - 1)) else: past_days_count = elapsed_days missed_today_count = 0 for p in PROGRESS_PRAYERS: if is_current_month: p_started = False if p == "Fajr": p_started = local_h >= 5.0 elif p == "Dhuhr": p_started = local_h >= 12.0 elif p == "Asr": p_started = local_h >= 16.0 elif p == "Maghrib": p_started = local_h >= 17.75 elif p == "Isha": p_started = local_h >= 19.25 if p in today_norm: p_started = True due_counts[p] = past_days_count + (1 if p_started else 0) if p_started and p not in today_norm: missed_today_count += 1 else: due_counts[p] = elapsed_days effective_full_days = full_days if is_current_month and missed_today_count == 0 and elapsed_days > 0: if not today_item.get("is_full", False) and len(today_norm) < 5: effective_full_days = min(elapsed_days, full_days + 1) consistency_pct = round((effective_full_days / float(effective_days)) * 100, 1) if effective_days > 0 else 0.0 # Determine strongest and weakest prayer with tie handling counts_vals = list(prayer_counts.values()) max_c = max(counts_vals) if counts_vals else 0 min_c = min(counts_vals) if counts_vals else 0 if max_c == 0: strongest = "None" weakest = "None" elif max_c == min_c: strongest = "Equal Consistency" weakest = "Equal Consistency" else: max_prayers = [p for p, c in prayer_counts.items() if c == max_c] min_prayers = [p for p, c in prayer_counts.items() if c == min_c] strongest = ", ".join(max_prayers) if len(max_prayers) < 5 else "Equal Consistency" weakest = ", ".join(min_prayers) if len(min_prayers) < 5 else "Equal Consistency" current_streak, golden_streak = await _progress_streaks(uid, today) res = { "locked": False, "days": days_list, "total_days": total_days, "elapsed_days": elapsed_days, "full_days": full_days, "golden_days": golden_days, "consistency_pct": consistency_pct, "prayer_breakdown": prayer_counts, "due_days": due_counts, "strongest_prayer": strongest, "weakest_prayer": weakest, "streak": current_streak, "golden_streak": golden_streak, "year": target_year, "month": target_month, "earliest_date": started_date_str, "earliest_year": earliest_year, "earliest_month": earliest_month, "is_tracking_enabled": bool(is_tracking_enabled), } raw_bytes = json.dumps(res).encode("utf-8") _MONTHLY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") _RECORDED_MISSED_CACHE: Dict[str, Tuple[float, bytes]] = {} _RECORDED_MISSED_TTL = 30.0 @router.get('/progress/recorded-missed') @limiter.limit("1200/minute") async def get_recorded_missed_prayers(request: Request, tz: float = 0.0, user: dict = Depends(get_current_user)): """Calculates all-time completed and missed prayers across all recorded daily progress data in O(1).""" uid = user["uid"] today = _progress_local_date(tz) today_str = today.isoformat() cache_key = f"{uid}:{tz}:{today_str}" ent = _RECORDED_MISSED_CACHE.get(cache_key) if ent and (time.monotonic() - ent[0]) < _RECORDED_MISSED_TTL: return Response(content=ent[1], media_type="application/json") stats = None try: stats = await nimaz_stats_collection.find_one({"_id": uid}, {"_id": 0}) except Exception: pass if not stats: stats = await _recalculate_nimaz_stats_for_user(uid, today_str) payload = { "total_days": int((stats or {}).get("total_days", 0) or 0), "missed": (stats or {}).get("missed", {p: 0 for p in PROGRESS_PRAYERS}), "prayed": (stats or {}).get("prayed", {p: 0 for p in PROGRESS_PRAYERS}), "total_missed": int((stats or {}).get("total_missed", 0) or 0), "total_prayed": int((stats or {}).get("total_prayed", 0) or 0), "total_quran_days": int((stats or {}).get("total_quran_days", 0) or 0), "total_golden_days": int((stats or {}).get("total_golden_days", 0) or 0), } raw_bytes = json.dumps(payload).encode("utf-8") _RECORDED_MISSED_CACHE[cache_key] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") class TrackingStatusReq(BaseModel): action: str = Field(..., description="'enable' or 'later'") tz: float = 0.0 @router.get('/progress/tracking-status') @limiter.limit("1200/minute") async def get_progress_tracking_status(request: Request, user: dict = Depends(get_current_user)): """Retrieves whether the user has enabled progress tracking, consent response state, and recording start date.""" uid = user["uid"] u = await users_collection.find_one( {"$or": [{"uid": uid}, {"_id": uid}]}, { "progress_tracking": 1, "progress_tracking_enabled": 1, "progress_tracking_prompt_responded": 1, "progress_tracking_started_at": 1, "progress_tracking_started_date": 1, "lifetime_apples": 1, "metrics.lifetime_apples": 1, "created_at": 1, } ) if not u: raise HTTPException(status_code=404, detail="User not found") pt = u.get("progress_tracking") or {} responded = u.get("progress_tracking_prompt_responded", pt.get("prompt_responded")) enabled = u.get("progress_tracking_enabled", pt.get("enabled")) started_at = u.get("progress_tracking_started_at", pt.get("started_at")) started_date = u.get("progress_tracking_started_date", pt.get("started_date")) lifetime_apples = int(u.get("lifetime_apples", 0) or (u.get("metrics") or {}).get("lifetime_apples", 0) or 0) # Auto-heal: If user is already tracking (enabled is True, started_date set, apples collected, or has records) is_already_tracking = bool(enabled) or bool(started_date) or (lifetime_apples > 0) if not is_already_tracking: first_dp = await daily_progress_collection.find({"$or": [{"uid": uid}, {"_id": uid}]}).sort("date", 1).limit(1).to_list(1) if first_dp: is_already_tracking = True started_date = started_date or first_dp[0].get("date") if is_already_tracking: enabled = True responded = True if not started_date: started_date = datetime.datetime.utcnow().date().isoformat() await users_collection.update_one( {"$or": [{"uid": uid}, {"_id": uid}]}, { "$set": { "progress_tracking_enabled": True, "progress_tracking_prompt_responded": True, "progress_tracking_started_date": started_date, "progress_tracking.enabled": True, "progress_tracking.prompt_responded": True, "progress_tracking.started_date": started_date, } } ) elif responded is None: enabled = False responded = False return { "enabled": bool(enabled), "prompt_responded": bool(responded), "started_at": str(started_at) if started_at else None, "started_date": str(started_date) if started_date else None, } @router.post('/progress/tracking-status') @limiter.limit("120/minute") async def update_progress_tracking_status(payload: TrackingStatusReq, request: Request, user: dict = Depends(get_current_user)): """Allows user to enable tracking or choose 'later'.""" uid = user["uid"] act = payload.action.strip().lower() now = datetime.datetime.utcnow() now_iso = now.isoformat() + "Z" today_str = _progress_local_date(payload.tz).isoformat() if act in ("enable", "yes", "true"): u = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"progress_tracking_started_date": 1, "progress_tracking.started_date": 1}) existing_start = (u or {}).get("progress_tracking_started_date") or ((u or {}).get("progress_tracking") or {}).get("started_date") start_date = existing_start or today_str await users_collection.update_one( {"$or": [{"uid": uid}, {"_id": uid}]}, { "$set": { "progress_tracking_enabled": True, "progress_tracking_prompt_responded": True, "progress_tracking_started_at": now_iso, "progress_tracking_started_date": start_date, "progress_tracking.enabled": True, "progress_tracking.prompt_responded": True, "progress_tracking.started_at": now_iso, "progress_tracking.started_date": start_date, "updated_at": now, } } ) return { "status": "success", "enabled": True, "prompt_responded": True, "started_at": now_iso, "started_date": start_date, } elif act in ("later", "false"): await users_collection.update_one( {"$or": [{"uid": uid}, {"_id": uid}]}, { "$set": { "progress_tracking_enabled": False, "progress_tracking_prompt_responded": True, "progress_tracking.enabled": False, "progress_tracking.prompt_responded": True, "updated_at": now, } } ) return { "status": "success", "enabled": False, "prompt_responded": True, "started_at": None, "started_date": None, } else: raise HTTPException(status_code=400, detail="Invalid action. Use 'enable' or 'later'.") # ========================================================= # 11b. TASBEEH & DHIKR LIFETIME ARCHITECTURE & CONSISTENCY # ========================================================= def _sanitize_dhikr_key(name: str) -> str: if not name: return "General_Dhikr" cleaned = re.sub(r'[\.\$\0]', '_', name.strip()) return cleaned if cleaned else "General_Dhikr" class TasbeehLogReq(BaseModel): count: int = Field(..., ge=1, le=100000) dhikr: str = Field("Subhan Allah", max_length=150) arabic: Optional[str] = Field("", max_length=150) laps: int = Field(0, ge=0) target: int = Field(33, ge=0) duration_seconds: int = Field(0, ge=0) date: Optional[str] = None # YYYY-MM-DD local date tz: float = Field(default=0.0, ge=-12, le=14) event_id: Optional[str] = Field(None, max_length=100) class TasbeehBatchSyncReq(BaseModel): sessions: List[TasbeehLogReq] = Field(..., min_length=1, max_length=500) tz: float = Field(default=0.0, ge=-12, le=14) async def _apply_tasbeeh_rollup( uid: str, date_str: str, count: int, dhikr: str, arabic: str, laps: int, duration_seconds: int, event_id: str, now_utc: str ) -> int: """Atomically increments daily progress, daily rollup and lifetime stats. Guarantees O(1) lifetime write scaling and zero unbounded document bloat. """ safe_key = _sanitize_dhikr_key(dhikr) # 1. Update daily_progress_collection new_daily_progress_count = count try: dp_res = await daily_progress_collection.find_one_and_update( { "uid": uid, "date": date_str, "applied_tasbeeh_events": {"$ne": event_id} }, { "$inc": {"tasbeeh_count": count}, "$addToSet": {"applied_tasbeeh_events": event_id}, "$setOnInsert": { "uid": uid, "date": date_str, "prayers": [], "quran_secs": 0, "hadith_secs": 0 } }, upsert=True, return_document=ReturnDocument.AFTER, projection={"_id": 0, "tasbeeh_count": 1} ) if dp_res: new_daily_progress_count = int(dp_res.get("tasbeeh_count", 0) or count) except Exception as e: logger.warning(f"daily_progress update warning: {e}") # 2. Update tasbeeh_daily_collection new_day_total = new_daily_progress_count is_new_day = False try: daily_res = await tasbeeh_daily_collection.find_one_and_update( {"_id": f"{uid}:{date_str}"}, { "$inc": { "total_count": count, "total_seconds": duration_seconds, "total_laps": laps, "session_count": 1, f"dhikrs.{safe_key}.count": count, f"dhikrs.{safe_key}.seconds": duration_seconds, f"dhikrs.{safe_key}.laps": laps, }, "$set": { "uid": uid, "date": date_str, f"dhikrs.{safe_key}.dhikr": dhikr, f"dhikrs.{safe_key}.arabic": arabic or "", "updated_at": now_utc, }, "$setOnInsert": { "created_at": now_utc, } }, upsert=True, return_document=ReturnDocument.AFTER ) if daily_res: new_day_total = int(daily_res.get("total_count", 0) or new_day_total) is_new_day = (daily_res.get("session_count", 1) == 1) except Exception as e: logger.warning(f"tasbeeh_daily update warning: {e}") # 3. Update tasbeeh_stats_collection (Lifetime user stats) try: stats_inc = { "lifetime_count": count, "lifetime_seconds": duration_seconds, "lifetime_laps": laps, "lifetime_sessions": 1, f"dhikrs.{safe_key}.count": count, } if is_new_day: stats_inc["active_days_count"] = 1 stats_doc = await tasbeeh_stats_collection.find_one_and_update( {"_id": uid}, { "$inc": stats_inc, "$set": { "uid": uid, f"dhikrs.{safe_key}.dhikr": dhikr, f"dhikrs.{safe_key}.arabic": arabic or "", "updated_at": now_utc, }, "$setOnInsert": { "active_days_count": 1 if is_new_day else 0, "best_day": {"date": date_str, "count": new_day_total}, "streak": { "current": 1 if new_day_total >= PROGRESS_TASBEEH_GOAL else 0, "longest": 1 if new_day_total >= PROGRESS_TASBEEH_GOAL else 0, "last_active_date": date_str if new_day_total >= PROGRESS_TASBEEH_GOAL else "", }, "created_at": now_utc, } }, upsert=True, return_document=ReturnDocument.AFTER ) if stats_doc: current_best = stats_doc.get("best_day", {}).get("count", 0) streak_data = stats_doc.get("streak", {}) curr_streak = streak_data.get("current", 0) long_streak = streak_data.get("longest", 0) last_date_str = streak_data.get("last_active_date", "") updates = {} if new_day_total > current_best: updates["best_day"] = {"date": date_str, "count": new_day_total} # TDS-STREAK: Streak is strictly incremented ONLY when daily total reaches >= 297 beads if new_day_total >= PROGRESS_TASBEEH_GOAL and last_date_str != date_str: try: dt_curr = datetime.date.fromisoformat(date_str) if last_date_str: dt_last = datetime.date.fromisoformat(last_date_str) diff = (dt_curr - dt_last).days if diff == 1: curr_streak += 1 long_streak = max(long_streak, curr_streak) updates["streak"] = {"current": curr_streak, "longest": long_streak, "last_active_date": date_str} elif diff > 1: updates["streak"] = {"current": 1, "longest": max(long_streak, 1), "last_active_date": date_str} else: updates["streak"] = {"current": 1, "longest": max(long_streak, 1), "last_active_date": date_str} except Exception: pass if updates: await tasbeeh_stats_collection.update_one({"_id": uid}, {"$set": updates}) except Exception as e: logger.warning(f"tasbeeh_stats update warning: {e}") return max(new_day_total, new_daily_progress_count) async def _calculate_tasbeeh_streak(uid: str, today: datetime.date) -> tuple[int, int, str]: """Calculates current and longest consecutive day streak for Tasbeeh where daily count >= 297.""" streak_qualifying_dates = [] try: q_daily = await daily_progress_collection.distinct( "date", {"uid": uid, "tasbeeh_count": {"$gte": PROGRESS_TASBEEH_GOAL}} ) q_tasbeeh = await tasbeeh_daily_collection.distinct( "date", {"uid": uid, "total_count": {"$gte": PROGRESS_TASBEEH_GOAL}} ) streak_qualifying_dates = sorted(list(set(q_daily or []) | set(q_tasbeeh or []))) except Exception: pass current_streak = 0 longest_streak = 0 last_active_date = "" if streak_qualifying_dates: last_active_date = streak_qualifying_dates[-1] today_str = today.isoformat() yesterday_str = (today - datetime.timedelta(days=1)).isoformat() if today_str in streak_qualifying_dates: c = today while c.isoformat() in streak_qualifying_dates: current_streak += 1 c -= datetime.timedelta(days=1) elif yesterday_str in streak_qualifying_dates: c = today - datetime.timedelta(days=1) while c.isoformat() in streak_qualifying_dates: current_streak += 1 c -= datetime.timedelta(days=1) else: current_streak = 0 curr_run = 0 prev_d = None for d_str in streak_qualifying_dates: try: curr_d = datetime.date.fromisoformat(d_str) if prev_d is None or (curr_d - prev_d).days == 1: curr_run += 1 else: curr_run = 1 longest_streak = max(longest_streak, curr_run) prev_d = curr_d except Exception: pass longest_streak = max(longest_streak, current_streak) return current_streak, longest_streak, last_active_date async def _ensure_user_tasbeeh_stats(uid: str, today_str: Optional[str] = None) -> dict: """Guarantees lifetime stats exist for user with self-healing backfill.""" try: stats = await tasbeeh_stats_collection.find_one({"_id": uid}) if stats: return stats except Exception: pass now_utc = datetime.datetime.utcnow().isoformat() + "Z" # One-time aggregation across historical records sess_total = 0 sess_seconds = 0 sess_laps = 0 sess_count = 0 try: all_sessions_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid}}, {"$group": { "_id": None, "total_beads": {"$sum": "$count"}, "total_seconds": {"$sum": "$duration_seconds"}, "total_laps": {"$sum": "$laps"}, "total_sessions": {"$sum": 1} }} ]).to_list(length=1) if all_sessions_agg: sess_total = int(all_sessions_agg[0].get("total_beads", 0) or 0) sess_seconds = int(all_sessions_agg[0].get("total_seconds", 0) or 0) sess_laps = int(all_sessions_agg[0].get("total_laps", 0) or 0) sess_count = int(all_sessions_agg[0].get("total_sessions", 0) or 0) except Exception: pass daily_all_total = 0 try: daily_all_agg = await daily_progress_collection.aggregate([ {"$match": {"uid": uid, "tasbeeh_count": {"$gt": 0}}}, {"$group": {"_id": None, "total": {"$sum": "$tasbeeh_count"}}} ]).to_list(length=1) if daily_all_agg: daily_all_total = int(daily_all_agg[0]["total"]) except Exception: pass lifetime_count = max(sess_total, daily_all_total) active_dates = [] try: hist_tasbeeh_dates = await tasbeeh_collection.distinct("date", {"uid": uid}) hist_daily_dates = await daily_progress_collection.distinct("date", {"uid": uid, "tasbeeh_count": {"$gt": 0}}) active_dates = sorted(list(set(hist_tasbeeh_dates) | set(hist_daily_dates))) except Exception: pass active_days_count = len(active_dates) current_streak, longest_streak, last_active_date = await _calculate_tasbeeh_streak(uid, datetime.date.today()) best_day_count = 0 best_day_date = "" try: best_daily_doc = await daily_progress_collection.find_one( {"uid": uid}, {"_id": 0, "date": 1, "tasbeeh_count": 1}, sort=[("tasbeeh_count", -1)] ) if best_daily_doc: best_day_count = int(best_daily_doc.get("tasbeeh_count", 0) or 0) best_day_date = best_daily_doc.get("date", "") except Exception: pass dhikrs_map = {} try: dhikrs_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid}}, {"$group": {"_id": "$dhikr", "count": {"$sum": "$count"}}}, {"$sort": {"count": -1}} ]).to_list(length=20) for it in dhikrs_agg: d_name = it.get("_id") or "General Dhikr" d_cnt = int(it.get("count", 0) or 0) s_key = _sanitize_dhikr_key(d_name) dhikrs_map[s_key] = {"dhikr": d_name, "count": d_cnt} except Exception: pass stats_doc = { "_id": uid, "uid": uid, "lifetime_count": lifetime_count, "lifetime_seconds": sess_seconds, "lifetime_laps": sess_laps, "lifetime_sessions": sess_count, "active_days_count": active_days_count, "best_day": {"date": best_day_date, "count": best_day_count}, "streak": { "current": current_streak, "longest": longest_streak, "last_active_date": last_active_date, }, "dhikrs": dhikrs_map, "created_at": now_utc, "updated_at": now_utc, } try: await tasbeeh_stats_collection.update_one({"_id": uid}, {"$set": stats_doc}, upsert=True) except Exception as e: logger.warning(f"Error backfilling tasbeeh_stats for {uid}: {e}") return stats_doc @router.post('/tasbeeh/log') @limiter.limit("120/minute") async def log_tasbeeh_session( payload: TasbeehLogReq, request: Request, user: dict = Depends(get_current_user) ): uid = user["uid"] date_str = payload.date or _progress_local_date(payload.tz).isoformat() now_utc = datetime.datetime.utcnow().isoformat() + "Z" event_id = payload.event_id or str(uuid.uuid4()) doc_id = f"tasbeeh:{uid}:{event_id}" # TDS-001: Atomic event claim and resilient aggregate synchronization session_doc = { "_id": doc_id, "uid": uid, "event_id": event_id, "date": date_str, "timestamp": now_utc, "count": payload.count, "dhikr": payload.dhikr, "arabic": payload.arabic or "", "laps": payload.laps, "target": payload.target, "duration_seconds": payload.duration_seconds, "aggregate_applied": False, } is_duplicate = False try: await tasbeeh_collection.insert_one(session_doc) except DuplicateKeyError: is_duplicate = True existing = await tasbeeh_collection.find_one({"_id": doc_id}) # If aggregate was already applied, return current daily progress idempotently if existing and existing.get("aggregate_applied"): daily_doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0, "tasbeeh_count": 1}) or {} curr_c = int(daily_doc.get("tasbeeh_count", 0) or payload.count) return { "status": "success", "logged_count": payload.count, "tasbeeh_count": curr_c, "date": date_str, "idempotent": True, } # Atomically apply aggregate to daily_progress, tasbeeh_daily, and tasbeeh_stats new_tasbeeh_count = await _apply_tasbeeh_rollup( uid=uid, date_str=date_str, count=payload.count, dhikr=payload.dhikr, arabic=payload.arabic or "", laps=payload.laps, duration_seconds=payload.duration_seconds, event_id=event_id, now_utc=now_utc ) # Mark aggregate_applied True on the session document await tasbeeh_collection.update_one( {"_id": doc_id}, {"$set": {"aggregate_applied": True}} ) realtime.publish(uid, "prayer_progress_updated", { "date": date_str, "tasbeeh_count": new_tasbeeh_count, }) _invalidate_all_progress_caches(uid) return { "status": "success", "logged_count": payload.count, "tasbeeh_count": new_tasbeeh_count, "date": date_str, "idempotent": is_duplicate, } @router.post('/tasbeeh/batch-sync') @limiter.limit("60/minute") async def batch_sync_tasbeeh( payload: TasbeehBatchSyncReq, request: Request, user: dict = Depends(get_current_user) ): """Batched offline synchronization endpoint for digital Tasbeeh. Accepts up to 500 queued offline sessions in a single network round-trip. Deduplicates sessions via unique event IDs and updates lifetime rollups. """ uid = user["uid"] now_utc = datetime.datetime.utcnow().isoformat() + "Z" today_str = _progress_local_date(payload.tz).isoformat() synced_count = 0 duplicate_count = 0 today_tasbeeh_count = 0 for item in payload.sessions: date_str = item.date or today_str event_id = item.event_id or str(uuid.uuid4()) doc_id = f"tasbeeh:{uid}:{event_id}" session_doc = { "_id": doc_id, "uid": uid, "event_id": event_id, "date": date_str, "timestamp": now_utc, "count": item.count, "dhikr": item.dhikr, "arabic": item.arabic or "", "laps": item.laps, "target": item.target, "duration_seconds": item.duration_seconds, "aggregate_applied": False, } try: await tasbeeh_collection.insert_one(session_doc) synced_count += 1 new_c = await _apply_tasbeeh_rollup( uid=uid, date_str=date_str, count=item.count, dhikr=item.dhikr, arabic=item.arabic or "", laps=item.laps, duration_seconds=item.duration_seconds, event_id=event_id, now_utc=now_utc ) await tasbeeh_collection.update_one({"_id": doc_id}, {"$set": {"aggregate_applied": True}}) if date_str == today_str: today_tasbeeh_count = max(today_tasbeeh_count, new_c) except DuplicateKeyError: duplicate_count += 1 existing = await tasbeeh_collection.find_one({"_id": doc_id}) if existing and not existing.get("aggregate_applied"): new_c = await _apply_tasbeeh_rollup( uid=uid, date_str=date_str, count=item.count, dhikr=item.dhikr, arabic=item.arabic or "", laps=item.laps, duration_seconds=item.duration_seconds, event_id=event_id, now_utc=now_utc ) await tasbeeh_collection.update_one({"_id": doc_id}, {"$set": {"aggregate_applied": True}}) if date_str == today_str: today_tasbeeh_count = max(today_tasbeeh_count, new_c) if today_tasbeeh_count > 0: realtime.publish(uid, "prayer_progress_updated", { "date": today_str, "tasbeeh_count": today_tasbeeh_count, }) _invalidate_all_progress_caches(uid) return { "status": "success", "total_received": len(payload.sessions), "synced_count": synced_count, "duplicates_ignored": duplicate_count, "today_count": today_tasbeeh_count, "date": today_str, } _TASBEEH_SUMMARY_CACHE: Dict[str, Tuple[float, bytes]] = {} _TASBEEH_SUMMARY_TTL = 15.0 @router.get('/tasbeeh/summary') @limiter.limit("1200/minute") async def get_tasbeeh_summary( request: Request, tz: float = 0.0, month: Optional[str] = Query(None, description="Optional YYYY-MM target month"), user: dict = Depends(get_current_user) ): uid = user["uid"] today = _progress_local_date(tz) today_str = today.isoformat() month_start = today.replace(day=1).isoformat() month_elapsed_days = max(1, today.day) # Resolve target month range (TDS-002: historical month support) if month: try: parts = [int(p) for p in month.strip().split("-")] target_start = datetime.date(parts[0], parts[1], 1) if parts[1] == 12: next_m = datetime.date(parts[0] + 1, 1, 1) else: next_m = datetime.date(parts[0], parts[1] + 1, 1) target_end = next_m - datetime.timedelta(days=1) is_target_current = (target_start.year == today.year and target_start.month == today.month) eff_month_start = target_start.isoformat() eff_month_end = today_str if is_target_current else target_end.isoformat() eff_elapsed_days = today.day if is_target_current else target_end.day except Exception: eff_month_start = month_start eff_month_end = today_str eff_elapsed_days = month_elapsed_days else: eff_month_start = month_start eff_month_end = today_str eff_elapsed_days = month_elapsed_days cache_key = f"{uid}:{tz}:{today_str}:{eff_month_start}" ent = _TASBEEH_SUMMARY_CACHE.get(cache_key) if ent and (time.monotonic() - ent[0]) < _TASBEEH_SUMMARY_TTL: return Response(content=ent[1], media_type="application/json") # 1. Fetch or initialize lifetime stats (O(1)) stats = await _ensure_user_tasbeeh_stats(uid, today_str) # 2. Try fast-path via tasbeeh_daily_collection (O(31)) month_daily_sums = {} month_active_dates = set() month_seconds = 0 month_laps = 0 month_dhikr_totals = {} daily_docs = [] try: daily_docs = await tasbeeh_daily_collection.find( {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}} ).to_list(length=35) except Exception: daily_docs = [] if daily_docs: for d in daily_docs: dt = d.get("date") cnt = int(d.get("total_count", 0) or 0) if dt: month_daily_sums[dt] = cnt if cnt > 0: month_active_dates.add(dt) month_seconds += int(d.get("total_seconds", 0) or 0) month_laps += int(d.get("total_laps", 0) or 0) for dk, dv in (d.get("dhikrs") or {}).items(): d_name = dv.get("dhikr") or dk d_c = int(dv.get("count", 0) or 0) month_dhikr_totals[d_name] = month_dhikr_totals.get(d_name, 0) + d_c else: # Fallback to session aggregation (for historical users or test mocks) try: month_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}}, {"$group": { "_id": None, "total_count": {"$sum": "$count"}, "total_seconds": {"$sum": "$duration_seconds"}, "total_laps": {"$sum": "$laps"} }} ]).to_list(length=1) if month_agg: month_seconds = int(month_agg[0].get("total_seconds", 0) or 0) month_laps = int(month_agg[0].get("total_laps", 0) or 0) daily_session_sums = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}}, {"$group": {"_id": "$date", "count": {"$sum": "$count"}}} ]).to_list(length=35) for ds in daily_session_sums: dt = ds.get("_id") c = int(ds.get("count", 0) or 0) if dt: if c > 0: month_active_dates.add(dt) month_daily_sums[dt] = c month_dhikr_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}}, {"$group": {"_id": "$dhikr", "count": {"$sum": "$count"}}}, {"$sort": {"count": -1}}, {"$limit": 8} ]).to_list(length=8) for item in month_dhikr_agg: dn = item.get("_id") or "General Dhikr" month_dhikr_totals[dn] = int(item.get("count", 0) or 0) except Exception as e: logger.warning(f"Tasbeeh summary month fallback error: {e}") # Cross-check month days with daily_progress_collection try: daily_month_docs = await daily_progress_collection.find( {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}, {"_id": 0, "date": 1, "tasbeeh_count": 1} ).to_list(length=35) for dm in daily_month_docs: dt = dm.get("date") tc = int(dm.get("tasbeeh_count", 0) or 0) if tc > 0: month_active_dates.add(dt) month_daily_sums[dt] = max(month_daily_sums.get(dt, 0), tc) except Exception: pass month_total = sum(month_daily_sums.values()) if month_daily_sums else 0 today_count = month_daily_sums.get(today_str, 0) month_best_day = max(month_daily_sums.values()) if month_daily_sums else today_count month_avg_daily = round(month_total / float(eff_elapsed_days)) if month_total > 0 else 0 sorted_dhikrs = sorted(month_dhikr_totals.items(), key=lambda x: x[1], reverse=True)[:8] month_top_dhikrs = [] for dn, cnt in sorted_dhikrs: pct = round((cnt / float(month_total)) * 100, 1) if month_total > 0 else 0.0 month_top_dhikrs.append({"dhikr": dn, "count": cnt, "pct": pct}) # Streak calculation (TDS-STREAK: strictly requires >= 297 beads a day) streak_info = (stats or {}).get("streak", {}) last_act = streak_info.get("last_active_date", "") curr_streak = int(streak_info.get("current", 0) or 0) yesterday_str = (today - datetime.timedelta(days=1)).isoformat() if today_count >= PROGRESS_TASBEEH_GOAL: # Today qualifies (>= 297 beads) if last_act == today_str: streak = max(1, curr_streak) elif last_act == yesterday_str: streak = curr_streak + 1 else: streak = 1 elif last_act == yesterday_str: # Today not yet >= 297 beads, but yesterday qualified: streak is held until midnight streak = curr_streak elif last_act == today_str and curr_streak > 0: # If today_count < 297, today does not qualify; roll back today's premature increment streak = max(0, curr_streak - 1) else: # Neither today nor yesterday reached 297 beads streak = 0 # All-time statistics from tasbeeh_stats all_time_beads = max((stats or {}).get("lifetime_count", 0), month_total) all_time_seconds = max((stats or {}).get("lifetime_seconds", 0), month_seconds) all_time_laps = max((stats or {}).get("lifetime_laps", 0), month_laps) all_time_active_days = max((stats or {}).get("active_days_count", 0), len(month_active_dates)) all_time_avg_daily = round(all_time_beads / float(max(1, all_time_active_days))) if all_time_active_days > 0 else 0 all_time_best_day = max((stats or {}).get("best_day", {}).get("count", 0), month_best_day) # Top Dhikrs across lifetime all_dhikrs_dict = (stats or {}).get("dhikrs", {}) all_time_top_dhikrs = [] if all_dhikrs_dict: sorted_all_dhikrs = sorted( all_dhikrs_dict.values(), key=lambda x: x.get("count", 0), reverse=True )[:8] for it in sorted_all_dhikrs: dn = it.get("dhikr") or "General Dhikr" cnt = int(it.get("count", 0) or 0) pct = round((cnt / float(all_time_beads)) * 100, 1) if all_time_beads > 0 else 0.0 all_time_top_dhikrs.append({"dhikr": dn, "count": cnt, "pct": pct}) if not all_time_top_dhikrs: all_time_top_dhikrs = month_top_dhikrs # Recent 25 sessions (indexed lookup) recent_sessions = [] try: recent_sessions = await tasbeeh_collection.find( {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}, {"_id": 0} ).sort("timestamp", -1).to_list(length=25) if not recent_sessions: recent_sessions = await tasbeeh_collection.find({"uid": uid}, {"_id": 0}).sort("timestamp", -1).to_list(length=25) except Exception: recent_sessions = [] res = { "today_count": today_count, "month_total": month_total, "month_elapsed_days": eff_elapsed_days, "month_active_days": len(month_active_dates), "active_days": len(month_active_dates), "streak_days": streak, "best_day_count": month_best_day, "avg_daily": month_avg_daily, "total_seconds": month_seconds, "total_laps": month_laps, "top_dhikrs": month_top_dhikrs, "all_time_total": all_time_beads, "all_time_active_days": all_time_active_days, "all_time_avg_daily": all_time_avg_daily, "all_time_best_day": all_time_best_day, "all_time_total_seconds": all_time_seconds, "all_time_total_laps": all_time_laps, "all_time_top_dhikrs": all_time_top_dhikrs, "recent_sessions": recent_sessions, "date": today_str, } raw_bytes = json.dumps(res, default=str).encode("utf-8") _TASBEEH_SUMMARY_CACHE[cache_key] = (time.monotonic(), raw_bytes) return Response(content=raw_bytes, media_type="application/json") @router.get('/tasbeeh/day') @limiter.limit("600/minute") async def get_tasbeeh_day( request: Request, date: str, tz: float = 0.0, skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=500), user: dict = Depends(get_current_user) ): """Retrieves full Tasbeeh breakdown and sessions for a specific day. Protected under Progress Plus / Pro tier for previous historical months. Uses user-local timezone for boundary checks (TDS-013). Supports cursor pagination via skip and limit (TDS-004). """ uid = user["uid"] date_clean = (date or "").strip() try: parts = [int(p) for p in date_clean.split("-")] day_date = datetime.date(parts[0], parts[1], parts[2]) except Exception: raise HTTPException(status_code=400, detail="Invalid date format, expected YYYY-MM-DD") today = _progress_local_date(tz) is_current_month = (day_date.year == today.year and day_date.month == today.month) if not is_current_month: from app.auth_deps import has_user_historical_progress if not await has_user_historical_progress(user): return { "locked": True, "message": "Historical progress is locked for previous months. Upgrade to Progress Plus (₹60/mo) or Sahih Pro to view your saved history.", "date": date_clean, "total_count": 0, "total_seconds": 0, "sessions": [], "dhikr_breakdown": [], "total_sessions": 0, "skip": skip, "limit": limit, "has_more": False, } total_count = 0 total_seconds = 0 top_dhikrs = [] # 1. Try reading from tasbeeh_daily_collection day_doc = None try: day_doc = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_clean}"}) except Exception: day_doc = None if day_doc: total_count = int(day_doc.get("total_count", 0) or 0) total_seconds = int(day_doc.get("total_seconds", 0) or 0) dhikrs_map = day_doc.get("dhikrs", {}) or {} sorted_d = sorted(dhikrs_map.values(), key=lambda x: x.get("count", 0), reverse=True) for it in sorted_d: dn = it.get("dhikr") or "General Dhikr" ar = it.get("arabic") or "" c = int(it.get("count", 0) or 0) pct = round((c / float(total_count) * 100), 1) if total_count > 0 else 0.0 top_dhikrs.append({"dhikr": dn, "arabic": ar, "count": c, "pct": pct}) else: # Fallback to session aggregate (for unmigrated records or test mocks) try: day_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid, "date": date_clean}}, {"$group": { "_id": None, "total_count": {"$sum": "$count"}, "total_seconds": {"$sum": "$duration_seconds"} }} ]).to_list(length=1) total_count = int(day_agg[0]["total_count"]) if day_agg else 0 total_seconds = int(day_agg[0]["total_seconds"]) if day_agg else 0 day_dhikr_agg = await tasbeeh_collection.aggregate([ {"$match": {"uid": uid, "date": date_clean}}, {"$group": { "_id": "$dhikr", "arabic": {"$first": "$arabic"}, "count": {"$sum": "$count"} }}, {"$sort": {"count": -1}} ]).to_list(length=50) for item in day_dhikr_agg: d_name = item.get("_id") or "General Dhikr" d_arabic = item.get("arabic") or "" c = int(item.get("count", 0) or 0) pct = round((c / float(total_count) * 100), 1) if total_count > 0 else 0.0 top_dhikrs.append({"dhikr": d_name, "arabic": d_arabic, "count": c, "pct": pct}) except Exception as e: logger.warning(f"Error aggregating tasbeeh day: {e}") # Cross-check with daily_progress try: daily_doc = await daily_progress_collection.find_one({"uid": uid, "date": date_clean}, {"_id": 0, "tasbeeh_count": 1}) or {} daily_t = int(daily_doc.get("tasbeeh_count", 0) or 0) total_count = max(total_count, daily_t) except Exception: pass # Paged session retrieval with total count & has_more (TDS-004) try: skip_int = int(skip if isinstance(skip, (int, float, str)) else 0) except Exception: skip_int = 0 try: limit_int = int(limit if isinstance(limit, (int, float, str)) else 100) except Exception: limit_int = 100 limit_clamped = min(max(1, limit_int), 500) total_sessions_count = 0 sessions = [] try: total_sessions_count = await tasbeeh_collection.count_documents({"uid": uid, "date": date_clean}) sessions = await tasbeeh_collection.find( {"uid": uid, "date": date_clean}, {"_id": 0} ).sort("timestamp", -1).skip(skip_int).to_list(length=limit_clamped) except Exception as e: logger.warning(f"Error fetching tasbeeh sessions: {e}") has_more = (skip_int + len(sessions)) < total_sessions_count return { "locked": False, "date": date_clean, "total_count": total_count, "total_seconds": total_seconds, "sessions": sessions, "dhikr_breakdown": top_dhikrs, "total_sessions": total_sessions_count, "skip": skip_int, "limit": limit_clamped, "has_more": has_more, } class RequestEmailChangeReq(BaseModel): new_email: str = Field(..., min_length=5, max_length=150) class VerifyEmailChangeReq(BaseModel): new_email: str = Field(..., min_length=5, max_length=150) otp: str = Field(..., min_length=4, max_length=10) @router.post('/request-email-change') @limiter.limit("5/minute") async def request_email_change(payload: RequestEmailChangeReq, request: Request, user: dict = Depends(get_current_user)): import random from app.database import db uid = user["uid"] primary_email = user.get("email") or user.get("primary_email") if not primary_email: raise HTTPException(status_code=400, detail="Primary account email not found.") new_email_clean = payload.new_email.strip().lower() if not new_email_clean or "@" not in new_email_clean: raise HTTPException(status_code=400, detail="Invalid new email format.") if new_email_clean == primary_email.lower(): raise HTTPException(status_code=400, detail="New email is identical to current primary email.") # Prevent duplicate / conflicting email ownership in MongoDB conflict_user = await users_collection.find_one({ "$or": [{"email": new_email_clean}, {"primary_email": new_email_clean}], "uid": {"$ne": uid} }) if conflict_user: raise HTTPException(status_code=409, detail="An account with this email address already exists.") # Check collision in Firebase Auth if available try: fb_conflict = firebase_auth.get_user_by_email(new_email_clean) if fb_conflict and fb_conflict.uid != uid: raise HTTPException(status_code=409, detail="This email is already registered with another account in Firebase.") except HTTPException: raise except Exception: pass import secrets otp = f"{secrets.randbelow(900000) + 100000}" now = datetime.datetime.utcnow() expires_at = now + datetime.timedelta(minutes=15) from app.database import email_change_requests_collection await email_change_requests_collection.update_one( {"uid": uid}, {"$set": { "uid": uid, "primary_email": primary_email, "new_email": new_email_clean, "otp": otp, "attempts": 0, "max_attempts": 5, "created_at": now, "expires_at": expires_at, }}, upsert=True ) # Dispatch email to PRIMARY email via SMTP or Resend email_sent = False send_error = "" # 1. Check Resend API first if configured resend_api_key = os.getenv("RESEND_API_KEY") if resend_api_key: try: import urllib.request import json as py_json req_data = { "from": os.getenv("EMAIL_FROM", "Sahih Islam "), "to": [primary_email], "subject": "Sahih Islam - Confirm Email Change Authorization", "html": f"""

Sahih Islam

Email Change Authorization

Assalamu Alaikum,

A request was made to update your Sahih Islam login email from {primary_email} to {new_email_clean}.

6-Digit Authorization Code {otp}

• Enter this code into the Sahih Islam app to authorize the transfer.
• This code is valid for 15 minutes.
• If you did not make this request, simply ignore this email. Your primary login remains unchanged and secure.

""" } import httpx async with httpx.AsyncClient(timeout=8.0) as client: resp = await client.post( "https://api.resend.com/emails", json=req_data, headers={ "Authorization": f"Bearer {resend_api_key}", "Content-Type": "application/json" } ) if resp.status_code in (200, 201): email_sent = True except Exception as ex: logger.warning(f"Resend email dispatch failed: {ex}") send_error = str(ex) # 2. Check SMTP if not yet sent (executed in threadpool to prevent event loop freeze) if not email_sent: smtp_host = os.getenv("SMTP_HOST") smtp_port = int(os.getenv("SMTP_PORT", 587)) smtp_user = os.getenv("SMTP_USER") smtp_pass = os.getenv("SMTP_PASS") if smtp_host and smtp_user and smtp_pass: try: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText msg = MIMEMultipart("alternative") msg['Subject'] = "Sahih Islam - Confirm Email Change Authorization" msg['From'] = smtp_user msg['To'] = primary_email text_content = ( f"Assalamu Alaikum,\n\n" f"A request was made to update your Sahih Islam account email to: {new_email_clean}\n\n" f"Your 6-digit authorization code is: {otp}\n\n" f"This code will expire in 15 minutes.\n" f"Enter this code in the Sahih Islam app to authorize the update.\n\n" f"If you did NOT request this, ignore this email. Your primary email remains active and secure." ) html_content = f"""

Sahih Islam

Email Change Authorization

Assalamu Alaikum,

A request was made to update your Sahih Islam login email from {primary_email} to {new_email_clean}.

6-Digit Authorization Code {otp}

• Enter this code into the Sahih Islam app to authorize the transfer.
• This code is valid for 15 minutes.
• If you did not make this request, simply ignore this email. Your primary login remains unchanged and secure.

""" msg.attach(MIMEText(text_content, "plain")) msg.attach(MIMEText(html_content, "html")) def _send_smtp_sync(): if smtp_port == 465: with smtplib.SMTP_SSL(smtp_host, smtp_port, timeout=12) as s: s.login(smtp_user, smtp_pass) s.send_message(msg) else: with smtplib.SMTP(smtp_host, smtp_port, timeout=12) as s: s.starttls() s.login(smtp_user, smtp_pass) s.send_message(msg) await asyncio.to_thread(_send_smtp_sync) email_sent = True except Exception as ex: logger.warning(f"SMTP send failed: {ex}") send_error = str(ex) if not email_sent: logger.warning(f"Email could not be dispatched to {primary_email}. Error: {send_error}") raise HTTPException( status_code=503, detail=f"Unable to send authorization code to {primary_email}. SMTP server is not configured or encountered an error. Please check server SMTP configuration." ) realtime.publish(uid, "email_change_requested", { "primary_email": primary_email, "new_email": new_email_clean, }) return { "status": "ok", "message": f"Authorization code sent to your primary email ({primary_email}).", "primary_email": primary_email, "email_sent": True } @router.post('/verify-email-change') @limiter.limit("10/minute") async def verify_email_change(payload: VerifyEmailChangeReq, request: Request, user: dict = Depends(get_current_user)): from app.database import email_change_requests_collection uid = user["uid"] req_doc = await email_change_requests_collection.find_one({"uid": uid}) if not req_doc: raise HTTPException(status_code=400, detail="No pending email change request found.") import hmac expires_at = req_doc.get("expires_at") if isinstance(expires_at, datetime.datetime) and expires_at < datetime.datetime.utcnow(): await email_change_requests_collection.delete_one({"uid": uid}) raise HTTPException(status_code=400, detail="Verification code has expired. Please request a new one.") attempts = int(req_doc.get("attempts", 0)) + 1 max_attempts = int(req_doc.get("max_attempts", 5)) if attempts > max_attempts: await email_change_requests_collection.delete_one({"uid": uid}) raise HTTPException( status_code=429, detail="Too many failed verification attempts. Request invalidated. Please request a new code." ) try: upd = email_change_requests_collection.update_one({"uid": uid}, {"$set": {"attempts": attempts}}) if asyncio.iscoroutine(upd) or hasattr(upd, "__await__"): await upd except Exception as upd_err: logger.debug(f"Attempt counter update note: {upd_err}") expected_otp = str(req_doc.get("otp", "")).strip() provided_otp = str(payload.otp).strip() if not (expected_otp and hmac.compare_digest(expected_otp, provided_otp)): remaining = max(0, max_attempts - attempts) raise HTTPException( status_code=400, detail=f"Invalid verification code. Authorization denied. Primary email remains unchanged. ({remaining} attempts remaining)" ) new_email_clean = payload.new_email.strip().lower() if req_doc.get("new_email") != new_email_clean: raise HTTPException(status_code=400, detail="The email being verified does not match the requested address.") # Re-check collision in MongoDB before finalizing conflict_user = await users_collection.find_one({ "$or": [{"email": new_email_clean}, {"primary_email": new_email_clean}], "uid": {"$ne": uid} }) if conflict_user: raise HTTPException(status_code=409, detail="This email is already registered with another account.") # 1. Update Firebase Auth user try: firebase_auth.update_user(uid, email=new_email_clean) except Exception as fb_err: logger.warning(f"Firebase update_user error: {fb_err}") err_msg = str(fb_err).lower() if "already exists" in err_msg or "already in use" in err_msg: raise HTTPException(status_code=409, detail="This email is already registered with another account in Firebase.") if not ("not initialized" in err_msg or "default app has not been initialized" in err_msg or "no app" in err_msg): raise HTTPException(status_code=502, detail=f"Failed to synchronize email change with identity provider: {fb_err}") # 2. Update MongoDB document now = datetime.datetime.utcnow() await users_collection.update_one( {"uid": uid}, {"$set": { "email": new_email_clean, "primary_email": new_email_clean, "updated_at": now, "analytics.updated_at": now, }} ) # 3. Permanent Audit Log try: from app.database import email_change_logs_collection await email_change_logs_collection.insert_one({ "uid": uid, "old_email": req_doc.get("primary_email"), "new_email": new_email_clean, "changed_at": now, "status": "COMPLETED", "method": "VERIFIED_OTP_FLOW" }) except Exception as log_err: logger.warning(f"Failed to record email_change_logs: {log_err}") # 4. Clean up request and invalidate cache await email_change_requests_collection.delete_one({"uid": uid}) await invalidate_user_cache(uid) realtime.publish(uid, "email_changed", {"email": new_email_clean}) return { "status": "ok", "message": "Email verified and updated successfully. New email is now your primary login.", "email": new_email_clean, } class RecordEmailChangeReq(BaseModel): old_email: str = Field(..., max_length=150) new_email: str = Field(..., max_length=150) uid: Optional[str] = Field(None, max_length=128) @router.post('/record-email-change') async def record_email_change( payload: RecordEmailChangeReq, request: Request, current_user: dict = Depends(get_current_user), ): """ Records email change post-verification. NEW-005 / SI-SEC-015 Hardening: - Strictly bound to authenticated current_user. NEVER trusts caller-supplied payload.uid! - Fail-closed if Firebase identity verification cannot be confirmed. - Prevents takeover of arbitrary user accounts. """ uid = current_user["uid"] clean_new = payload.new_email.strip().lower() clean_old = payload.old_email.strip().lower() if not clean_new or "@" not in clean_new: raise HTTPException(status_code=400, detail="Invalid new email address.") # Require authoritative verification: identity provider MUST have new_email as verified email try: fb_user = firebase_auth.get_user(uid) if not (fb_user and fb_user.email and fb_user.email.lower() == clean_new): raise HTTPException( status_code=400, detail="Firebase email does not match requested new email. Verification in identity provider required." ) except HTTPException: raise except Exception as ex: logger.error(f"[record_email_change] Firebase verification error for {uid}: {ex}") raise HTTPException(status_code=503, detail=f"Cannot verify email with identity provider: {ex}") # Collision check in MongoDB conflict_user = await users_collection.find_one({ "$or": [{"email": clean_new}, {"primary_email": clean_new}], "uid": {"$ne": uid} }) if conflict_user: raise HTTPException(status_code=409, detail="An account with this email address already exists.") from app.database import email_change_logs_collection now = datetime.datetime.utcnow() # 1. Permanent Audit Log in MongoDB try: await email_change_logs_collection.insert_one({ "uid": uid, "old_email": clean_old, "new_email": clean_new, "changed_at": now, "status": "COMPLETED", "method": "FIREBASE_DUAL_LINK_VERIFICATION" }) except Exception as ex: logger.warning(f"Failed to record email_change_logs: {ex}") # 2. Update user document email fields in MongoDB # Only email and primary_email are changed. # All other user fields (favorites, masjids, subscriptions, pro status, etc.) remain 100% intact! await users_collection.update_one( {"uid": uid}, {"$set": { "email": clean_new, "primary_email": clean_new, "updated_at": now, "analytics.updated_at": now, }} ) realtime.publish(uid, "email_changed", {"email": clean_new}) return { "status": "ok", "message": f"Email successfully updated to {clean_new}. Change recorded in audit log.", "email": clean_new, } # ========================================================= # WORLDWIDE CLIENT RUNTIME ERROR TELEMETRY # ========================================================= import hashlib import uuid import json def _redact_sensitive_text(text: str) -> str: """Scrub sensitive tokens, secrets, credentials, and emails from client error reports.""" if not text: return "" # Redact Bearer tokens text = re.sub(r'Bearer\s+[A-Za-z0-9_\-\.]+', 'Bearer [REDACTED_TOKEN]', text, flags=re.IGNORECASE) # Redact JWTs (three base64 segments separated by dots) text = re.sub(r'\beyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\b', '[REDACTED_JWT]', text) # Redact API keys / secrets / password patterns text = re.sub(r'(api[_\-]?key|secret|password|passwd|auth[_\-]?token|private[_\-]?key)\s*[:=]\s*\S+', r'\1=[REDACTED]', text, flags=re.IGNORECASE) # Mask email addresses text = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', '[REDACTED_EMAIL]', text) return text def _mask_ip(ip: str) -> str: """Mask client IP address to preserve privacy while retaining geographic routing telemetry.""" if not ip or ip in ("unknown", "127.0.0.1", "localhost", "testclient"): return ip or "unknown" if "." in ip: parts = ip.split(".") if len(parts) == 4: return f"{parts[0]}.{parts[1]}.{parts[2]}.xxx" if ":" in ip: parts = ip.split(":") if len(parts) >= 4: return f"{':'.join(parts[:3])}::xxxx" return "masked" class ClientErrorReportReq(BaseModel): message: Optional[str] = Field(None, max_length=5000) error_type: Optional[str] = Field("UnhandledError", max_length=150) stack_trace: Optional[str] = Field("", max_length=20000) route: Optional[str] = Field("/unknown", max_length=250) context: Optional[str] = Field("", max_length=500) platform: Optional[str] = Field("unknown", max_length=50) device_model: Optional[str] = Field("unknown", max_length=150) os_version: Optional[str] = Field("unknown", max_length=100) app_version: Optional[str] = Field("1.0.0", max_length=50) event_id: Optional[str] = Field(None, max_length=64) # Legacy / client mismatch aliases accepted safely without 422 errors error_message: Optional[str] = Field(None, max_length=5000) screen_route: Optional[str] = Field(None, max_length=250) device_info: Optional[Union[Dict[str, Any], str]] = None context_data: Optional[Union[Dict[str, Any], str]] = None # Client-supplied identity fields are accepted in payload to avoid 422, but STRICTLY DISCARDED by auth logic user_id: Optional[str] = Field(None, max_length=128) user_email: Optional[str] = Field(None, max_length=150) user_role: Optional[str] = Field(None, max_length=50) is_pro: Optional[bool] = None metadata: Optional[Dict[str, Any]] = None @model_validator(mode='before') @classmethod def harmonize_payload(cls, data: Any) -> Any: if not isinstance(data, dict): return data # 1. Map error_message -> message if not data.get("message") and data.get("error_message"): data["message"] = data["error_message"] elif not data.get("message") and data.get("error"): data["message"] = str(data["error"]) elif not data.get("message"): data["message"] = "Unknown Application Error" # 2. Map screen_route -> route if not data.get("route") and data.get("screen_route"): data["route"] = data["screen_route"] # 3. Map context_data -> context if not data.get("context") and data.get("context_data"): ctx = data["context_data"] if isinstance(ctx, dict): data["context"] = json.dumps(ctx)[:500] else: data["context"] = str(ctx)[:500] # 4. Map device_info -> device_model / os_version dev = data.get("device_info") if isinstance(dev, dict): if not data.get("device_model") or data.get("device_model") == "unknown": data["device_model"] = str(dev.get("targetPlatform") or dev.get("model") or "unknown")[:150] if not data.get("os_version") or data.get("os_version") == "unknown": data["os_version"] = str(dev.get("osVersion") or dev.get("version") or "unknown")[:100] return data def _categorize_error(msg: str, err_type: str, stack: str) -> str: core_text = (f"{err_type} {msg}").lower() full_text = (f"{err_type} {msg} {stack}").lower() # Network / API if any(k in full_text for k in ["socketexception", "httpexception", "handshakeexception", "timeoutexception", "failed host lookup", "clientexception", "connection refused", "network", "dioerror"]): return "Network / API" # Auth & Security if any(k in full_text for k in ["firebaseauth", "unauthorized", "token", "forbidden", "invalid-credential", "jwt", "permission-denied", "401", "403"]): return "Auth & Security" # State & Logic if any(k in full_text for k in ["rangeerror", "formatstatus", "formatexception", "type 'null'", "nosuchmethoderror", "typeerror", "cast error", "null check operator"]): return "State & Logic" # Storage & Cache if any(k in full_text for k in ["database", "mongo", "hive", "sharedpreferences", "indexeddb", "sqlite", "cache"]): return "Storage & Cache" # UI / Rendering if any(k in core_text for k in ["renderflex", "overflowed", "pageview", "positions.isnotempty", "unbounded", "renderbox", "paint", "setstate", "widget", "viewport", "flex"]): return "UI / Rendering" if any(k in full_text for k in ["renderflex", "overflowed", "renderbox", "viewport was given unbounded"]): return "UI / Rendering" return "General / System" def _compute_fingerprint(msg: str, err_type: str, stack: str) -> tuple[str, str]: norm_msg = re.sub(r'0x[0-9a-fA-F]+', '0x...', msg or "") norm_msg = re.sub(r'\b\d{4,5}\b', 'PORT', norm_msg) norm_msg = re.sub(r'\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b', 'UUID', norm_msg) norm_msg = re.sub(r'\b[0-9a-fA-F]{24}\b', 'HEXID', norm_msg) norm_msg = norm_msg.strip() first_line = norm_msg.split('\n')[0].strip() if len(first_line) > 160: first_line = first_line[:157] + '...' title = f"{err_type}: {first_line}" if err_type and err_type not in first_line else first_line if not title: title = "Unknown Application Error" stack_sig = "" if stack: frames = [] for line in stack.split('\n'): sline = line.strip() if not sline or sline.startswith('package:flutter/') or sline.startswith('dart-sdk/'): continue frames.append(re.sub(r':\d+:\d+', ':#:#', sline)) if not frames: frames = [re.sub(r':\d+:\d+', ':#:#', l.strip()) for l in stack.split('\n') if l.strip()] stack_sig = " | ".join(frames[:2]) raw_sig = f"{(err_type or '').strip().lower()}::{norm_msg[:120].lower()}::{stack_sig.lower()}" fp = hashlib.sha256(raw_sig.encode('utf-8')).hexdigest()[:16] return fp, title ALLOWED_TELEMETRY_PLATFORMS = {"Android", "iOS", "Web", "Windows", "macOS", "Linux", "Other"} @router.post('/report-error') @limiter.limit("20/minute") async def report_client_error(payload: ClientErrorReportReq, request: Request): """ Public telemetry endpoint for worldwide client runtime errors. Automatically categorizes and groups identical errors by fingerprint. Stores up to 50 recent occurrences per error category with bounded schema. Eliminates forged identities: verifies Firebase token when present; treats unauthenticated as anonymous. Automatically reopens resolved signatures upon recurrence. """ try: from app.database import client_errors_collection now_dt = datetime.datetime.utcnow() now = now_dt.isoformat() # 1. Input sanitization & platform whitelisting raw_platform = (payload.platform or "Other").strip().title() clean_platform = raw_platform if raw_platform in ALLOWED_TELEMETRY_PLATFORMS else "Other" raw_ver = (payload.app_version or "1.0.0").strip()[:30] clean_ver = re.sub(r'[^a-zA-Z0-9_\-\.]', '', raw_ver) or "1.0.0" raw_message = (payload.message or "")[:2000] raw_stack = (payload.stack_trace or "")[:5000] safe_message = _redact_sensitive_text(raw_message) safe_stack = _redact_sensitive_text(raw_stack) safe_route = (payload.route or "/unknown")[:250] safe_context = _redact_sensitive_text((payload.context or "")[:500]) safe_device = (payload.device_model or "unknown")[:100] safe_os = (payload.os_version or "unknown")[:100] fingerprint, title = _compute_fingerprint(safe_message, payload.error_type or "UnknownError", safe_stack) category = _categorize_error(safe_message, payload.error_type or "", safe_stack) # ELOG-03: Eliminate Identity Forging. Never trust payload user_id/email/role/is_pro! uid = None email = None role = "ANONYMOUS" is_pro = False auth_header = request.headers.get("Authorization", "") if auth_header.startswith("Bearer "): try: token = auth_header.split(" ")[1].strip() decoded = verify_firebase_token(token) if decoded and isinstance(decoded, dict): uid = decoded.get("uid") email = decoded.get("email") role = decoded.get("role") or "USER" is_pro = bool(decoded.get("is_pro", False)) except Exception: uid = None email = None role = "ANONYMOUS" is_pro = False occurrence_id = f"err_{uuid.uuid4().hex[:12]}" client_ip = _mask_ip(request.client.host if request.client else "unknown") user_agent = (request.headers.get("user-agent") or "unknown")[:250] # Bound metadata to small primitive dict (max 10 keys) safe_metadata = {} if isinstance(payload.metadata, dict): for k, v in list(payload.metadata.items())[:10]: safe_metadata[str(k)[:50]] = _redact_sensitive_text(str(v)[:200]) occurrence = { "occurrence_id": occurrence_id, "event_id": (payload.event_id or occurrence_id)[:64], "timestamp": now, "user_id": (uid or "anonymous")[:128], "user_email": (email or "anonymous")[:150], "user_role": (role or "ANONYMOUS")[:50], "is_pro": bool(is_pro), "platform": clean_platform, "device_model": safe_device, "os_version": safe_os, "app_version": clean_ver, "route": safe_route, "context": safe_context, "message": safe_message, "stack_trace": safe_stack, "ip": client_ip, "user_agent": user_agent, "metadata": safe_metadata, "is_test": False, } # 2. Check for duplicate or resolved recurrence existing_status = None is_duplicate = False try: if hasattr(client_errors_collection, "find_one"): existing_doc = await client_errors_collection.find_one( {"fingerprint": fingerprint}, {"status": 1, "recent_occurrences.event_id": 1, "reopened_count": 1} ) if existing_doc: existing_status = existing_doc.get("status") if payload.event_id: recent = existing_doc.get("recent_occurrences") or [] if any(isinstance(occ, dict) and occ.get("event_id") == payload.event_id for occ in recent): is_duplicate = True except Exception as ex: logger.debug(f"Telemetry existing doc probe skipped: {ex}") if is_duplicate: return { "status": "ok", "fingerprint": fingerprint, "category": category, "occurrence_id": payload.event_id or occurrence_id, "duplicate": True, "message": "Occurrence already recorded" } set_fields = { "last_seen": now, "last_seen_date": now_dt, "latest_route": safe_route, "latest_app_version": clean_ver, "latest_stack_trace": safe_stack, "updated_at": now, } inc_fields = { "total_occurrences": 1, f"platforms.{clean_platform}": 1, } # ELOG-07: Automatically reopen resolved errors upon recurrence if existing_status == "RESOLVED": set_fields["status"] = "UNRESOLVED" set_fields["reopened_at"] = now set_fields["reopened_reason"] = "New client occurrence observed after resolution" inc_fields["reopened_count"] = 1 update_query = { "$setOnInsert": { "fingerprint": fingerprint, "title": title[:200], "error_type": (payload.error_type or "UnknownError")[:100], "category": category, "first_seen": now, "first_seen_date": now_dt, "status": "UNRESOLVED", "created_at": now, "is_test": False, }, "$set": set_fields, "$inc": inc_fields, "$push": { "recent_occurrences": { "$each": [occurrence], "$slice": -50 } } } # ELOG-04: Only populate affected_user_ids if authenticated via verified Firebase token if uid: add_to_set = {"affected_user_ids": uid} if email: add_to_set["affected_user_emails"] = email update_query["$addToSet"] = add_to_set # 3. Bounded document update (prevents 16MB BSON DoS) await client_errors_collection.update_one( {"fingerprint": fingerprint}, update_query, upsert=True ) return { "status": "ok", "fingerprint": fingerprint, "category": category, "occurrence_id": occurrence_id, "reopened": existing_status == "RESOLVED" } except Exception as e: logger.error(f"Failed to record client error telemetry: {e}") raise HTTPException(status_code=500, detail="Failed to ingest error telemetry") # ========================================================= # ACCOUNT DELETION LIFECYCLE (7-DAY GRACE PERIOD & REAUTH) # ========================================================= async def verify_user_credentials(user: dict, password: Optional[str], reauth_token: Optional[str]) -> bool: """ Verifies that the caller has re-authenticated before initiating account deletion: 1. If user provided a fresh reauth_token (Firebase ID Token), verify that token's auth_time is within 10 minutes. 2. If user provided a password, verify against Firebase Identity Toolkit signInWithPassword API. 3. In non-prod / test / synthetic environment, allows synthetic passwords gracefully. """ uid = user.get("uid", "") if getattr(settings, "ALLOW_MOCK_AUTH", False) and (any(uid.lower().startswith(p) for p in ("mock", "test", "locust", "dev", "sim")) or password in ("test_password_pass", "password123")): return True # 1. Fresh Firebase ID Token verification if reauth_token and reauth_token.strip(): try: from app.infrastructure.firebase import verify_firebase_token decoded = verify_firebase_token(reauth_token.strip()) if decoded.get("uid") == uid: auth_time = decoded.get("auth_time", 0) now_ts = time.time() # Must be authenticated within last 10 minutes if (now_ts - auth_time) <= 600: return True else: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Re-authentication token has expired. Please sign in again." ) else: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Re-authentication token does not match the active account." ) except HTTPException: raise except Exception as e: logger.warning(f"Reauth token verification failed: {e}") raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid re-authentication token." ) # 2. Email & Password verification via Google Identity Toolkit if password and password.strip(): email = user.get("email") or user.get("primary_email") or (user.get("profile", {}) or {}).get("email") if not email: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="User email not found for password verification." ) web_api_key = settings.firebase.web_api_key or os.getenv("FIREBASE_WEB_API_KEY") or "AIzaSyB7Y9vge87FRzBY1cHOIBU-iaosqdRsImI" url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={web_api_key}" try: async with httpx.AsyncClient(timeout=10.0) as http_client: res = await http_client.post(url, json={"email": email, "password": password, "returnSecureToken": True}) if res.status_code == 200: data = res.json() if data.get("localId") == uid or data.get("email", "").lower() == email.lower(): return True else: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Password verified for a different account ID." ) else: err_json = res.json().get("error", {}) err_msg = err_json.get("message", "INVALID_PASSWORD") if "INVALID_PASSWORD" in err_msg or "INVALID_LOGIN_CREDENTIALS" in err_msg: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Incorrect password. Account deletion aborted." ) elif "EMAIL_NOT_FOUND" in err_msg: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="No password configured for this Google account. Please authenticate via Google." ) else: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Authentication verification failed: {err_msg}" ) except HTTPException: raise except Exception as e: logger.error(f"Error during Google Identity Toolkit verification: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Error verifying password with authentication service." ) raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Password or re-authentication credential is required to delete your account." ) @router.post("/account/delete-request") async def request_account_deletion( payload: AccountDeleteRequest, current_user: dict = Depends(get_current_user), ): """ Initiates account deletion with a mandatory 7-day grace period. Requires server-side verification of account password or Google OAuth re-authentication. Immediately disables active sessions and marks status to 'deletion_pending'. """ uid = current_user.get("uid") if not uid: raise HTTPException(status_code=400, detail="Invalid user identifier") if current_user.get("account_status") == "deletion_pending": sched_at = current_user.get("deletion_scheduled_at") return { "status": "pending", "message": f"Account deletion is already scheduled for {sched_at}.", "account_status": "deletion_pending", "deletion_scheduled_at": sched_at, } # Verify credentials await verify_user_credentials(current_user, payload.password, payload.reauth_token) now = datetime.datetime.utcnow() scheduled_at = now + datetime.timedelta(days=7) now_iso = now.isoformat() scheduled_iso = scheduled_at.isoformat() # Update database await users_collection.update_one( {"uid": uid}, { "$set": { "account_status": "deletion_pending", "deletion_requested_at": now_iso, "deletion_scheduled_at": scheduled_iso, "deletion_reason": payload.reason, "deletion_feedback": payload.feedback, "updated_at": now_iso, } } ) # Invalidate cached user profile immediately await invalidate_user_cache(uid) # Revoke Firebase tokens try: firebase_auth.revoke_refresh_tokens(uid) except Exception as e: logger.warning(f"Could not revoke Firebase refresh tokens for {uid}: {e}") # Audit logging try: from app.database import audit_logs_collection await audit_logs_collection.insert_one({ "action": "ACCOUNT_DELETION_SCHEDULED", "user_id": uid, "email": current_user.get("email"), "timestamp": now_iso, "created_at": now, # Native BSON Date for 90-day MongoDB TTL Index Auto-Purge "scheduled_deletion_date": scheduled_iso, "reason": payload.reason, "feedback": payload.feedback, }) except Exception as e: logger.warning(f"Failed to record deletion audit log: {e}") return { "status": "success", "message": "Account deletion scheduled. Your account and all personal data will be permanently deleted after 7 days.", "account_status": "deletion_pending", "deletion_requested_at": now_iso, "deletion_scheduled_at": scheduled_iso, "grace_period_days": 7, } @router.get("/account/deletion-status") async def get_account_deletion_status( current_user: dict = Depends(get_current_user), ): """ Returns the account deletion status and remaining grace period. Exempt from 403 suspension so the user can inspect their status. """ uid = current_user.get("uid") db_user = await users_collection.find_one({"uid": uid}) or current_user status_str = db_user.get("account_status", "active") is_pending = status_str == "deletion_pending" sched_at = db_user.get("deletion_scheduled_at") req_at = db_user.get("deletion_requested_at") days_rem = None hours_rem = None if is_pending and sched_at: try: sched_dt = datetime.datetime.fromisoformat(str(sched_at).replace("Z", "+00:00")).replace(tzinfo=None) diff = sched_dt - datetime.datetime.utcnow() total_sec = max(0, int(diff.total_seconds())) days_rem = total_sec // 86400 hours_rem = (total_sec % 86400) // 3600 except Exception: pass return { "uid": uid, "account_status": status_str, "is_deletion_pending": is_pending, "deletion_requested_at": req_at, "deletion_scheduled_at": sched_at, "days_remaining": days_rem, "hours_remaining": hours_rem, } @router.post("/account/cancel-deletion") async def cancel_account_deletion( payload: Optional[AccountCancelDeleteRequest] = None, current_user: dict = Depends(get_current_user), ): """ Cancels a pending account deletion during the 7-day grace period. Restores the account to 'active' status immediately. """ uid = current_user.get("uid") if not uid: raise HTTPException(status_code=400, detail="Invalid user identifier") db_user = await users_collection.find_one({"uid": uid}) or current_user if db_user.get("account_status") != "deletion_pending": return { "status": "success", "message": "Account is not scheduled for deletion.", "account_status": db_user.get("account_status", "active"), } now_iso = datetime.datetime.utcnow().isoformat() await users_collection.update_one( {"uid": uid}, { "$set": { "account_status": "active", "deletion_cancelled_at": now_iso, "updated_at": now_iso, }, "$unset": { "deletion_requested_at": "", "deletion_scheduled_at": "", "deletion_reason": "", "deletion_feedback": "", }, } ) await invalidate_user_cache(uid) try: from app.database import audit_logs_collection await audit_logs_collection.insert_one({ "action": "ACCOUNT_DELETION_CANCELLED", "user_id": uid, "email": current_user.get("email"), "timestamp": now_iso, "created_at": datetime.datetime.utcnow(), "cancel_reason": payload.reason if payload else None, }) except Exception as e: logger.warning(f"Failed to record cancel deletion audit log: {e}") return { "status": "success", "message": "Account deletion successfully cancelled. Your account has been restored to active status.", "account_status": "active", } @router.post('/rag-disclaimer/ack') @router.post('/api/rag-disclaimer/ack') async def acknowledge_rag_disclaimer(user: dict = Depends(get_current_user)): """Permanently records that the user has completed the 60-second theological RAG disclaimer.""" uid = user.get("uid") or user.get("user_id") if not uid: raise HTTPException(status_code=401, detail="Unauthorized") now_iso = datetime.datetime.utcnow().isoformat() await users_collection.update_one( {"uid": uid}, { "$set": { "has_acknowledged_rag_theology_disclaimer": True, "rag_theology_acknowledged_at": now_iso, "updated_at": now_iso, } } ) await invalidate_user_cache(uid) return {"status": "success", "has_acknowledged_rag": True} @router.get('/rag-disclaimer/status') @router.get('/api/rag-disclaimer/status') async def get_rag_disclaimer_status(user: dict = Depends(get_current_user)): """Retrieves lifetime acknowledgment status of the 60-second theological RAG disclaimer.""" uid = user.get("uid") or user.get("user_id") if not uid: raise HTTPException(status_code=401, detail="Unauthorized") db_user = await users_collection.find_one( {"uid": uid}, {"has_acknowledged_rag_theology_disclaimer": 1, "_id": 0} ) is_ack = bool(db_user and db_user.get("has_acknowledged_rag_theology_disclaimer")) return {"status": "success", "has_acknowledged_rag": is_ack}