Files
speaking-practice/backend/db.py
T
TTlaji 64e7cc5d8b
Deploy Speaking Practice / deploy (push) Successful in 44s
feat: sync learning progress to backend
2026-06-15 12:52:20 +08:00

831 lines
27 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pathlib import Path
import json
import sqlite3
BASE_DIR = Path(__file__).resolve().parent
DATABASE_PATH = BASE_DIR / "speaking_lab.sqlite"
MOJIBAKE_MARKERS = ("Ã", "Â", "â", "ä", "å", "æ", "ç", "è", "é")
def get_connection() -> sqlite3.Connection:
connection = sqlite3.connect(DATABASE_PATH)
connection.row_factory = sqlite3.Row
connection.execute("PRAGMA foreign_keys = ON")
return connection
def initialize_database() -> None:
BASE_DIR.mkdir(parents=True, exist_ok=True)
with get_connection() as connection:
connection.executescript(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
display_name TEXT,
role TEXT NOT NULL DEFAULT 'user',
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS videos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
speaker TEXT,
description TEXT,
tags TEXT,
video_url TEXT,
cover_url TEXT,
duration_seconds INTEGER,
status TEXT NOT NULL DEFAULT 'draft',
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS subtitles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
video_id INTEGER NOT NULL,
language_code TEXT NOT NULL,
language_name TEXT,
format TEXT NOT NULL,
file_path TEXT,
content TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (video_id) REFERENCES videos (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS user_interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
video_id INTEGER NOT NULL,
interaction_type TEXT NOT NULL,
current_time_ms INTEGER,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (user_id, video_id, interaction_type),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (video_id) REFERENCES videos (id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_videos_title ON videos (title);
CREATE INDEX IF NOT EXISTS idx_subtitles_video_id ON subtitles (video_id);
CREATE INDEX IF NOT EXISTS idx_user_interactions_user_id
ON user_interactions (user_id);
"""
)
video_columns = {
row["name"]
for row in connection.execute("PRAGMA table_info(videos)").fetchall()
}
if "cover_url" not in video_columns:
connection.execute("ALTER TABLE videos ADD COLUMN cover_url TEXT")
user_columns = {
row["name"]
for row in connection.execute("PRAGMA table_info(users)").fetchall()
}
if "is_active" not in user_columns:
connection.execute("ALTER TABLE users ADD COLUMN is_active INTEGER NOT NULL DEFAULT 1")
if "last_login_at" not in user_columns:
connection.execute("ALTER TABLE users ADD COLUMN last_login_at TEXT")
interaction_columns = {
row["name"]
for row in connection.execute("PRAGMA table_info(user_interactions)").fetchall()
}
if "payload_json" not in interaction_columns:
connection.execute("ALTER TABLE user_interactions ADD COLUMN payload_json TEXT")
connection.executescript(
"""
CREATE TABLE IF NOT EXISTS activation_codes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL UNIQUE,
created_by TEXT NOT NULL,
used_by_user_id INTEGER,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
used_at TEXT,
status TEXT NOT NULL DEFAULT 'unused',
FOREIGN KEY (used_by_user_id) REFERENCES users (id) ON DELETE SET NULL
);
CREATE INDEX IF NOT EXISTS idx_activation_codes_status
ON activation_codes (status);
"""
)
connection.execute(
"""
DELETE FROM subtitles
WHERE id NOT IN (
SELECT MAX(id)
FROM subtitles
GROUP BY video_id, language_code
)
"""
)
connection.execute(
"""
CREATE UNIQUE INDEX IF NOT EXISTS idx_subtitles_video_language
ON subtitles (video_id, language_code)
"""
)
def row_to_user(row: sqlite3.Row) -> dict[str, object]:
return {
"id": row["id"],
"username": repair_mojibake_text(row["email"]),
"displayName": repair_mojibake_text(row["display_name"] or row["email"]),
"role": row["role"],
"isActive": bool(row["is_active"]),
"createdAt": row["created_at"],
"updatedAt": row["updated_at"],
"lastLoginAt": row["last_login_at"] or "",
}
def row_to_activation_code(row: sqlite3.Row) -> dict[str, object]:
return {
"id": row["id"],
"code": row["code"],
"createdBy": row["created_by"],
"status": row["status"],
"createdAt": row["created_at"],
"usedAt": row["used_at"] or "",
"usedByUserId": row["used_by_user_id"],
"usedByUsername": repair_mojibake_text(row["used_by_username"] or ""),
}
def get_user_by_username(username: str) -> dict[str, object] | None:
with get_connection() as connection:
row = connection.execute(
"""
SELECT *
FROM users
WHERE email = ?
""",
(repair_mojibake_text(username),),
).fetchone()
return row_to_user(row) if row else None
def get_user_auth_record(username: str) -> sqlite3.Row | None:
with get_connection() as connection:
return connection.execute(
"""
SELECT *
FROM users
WHERE email = ?
""",
(repair_mojibake_text(username),),
).fetchone()
def get_user_by_id(user_id: int) -> dict[str, object] | None:
with get_connection() as connection:
row = connection.execute(
"""
SELECT *
FROM users
WHERE id = ?
""",
(user_id,),
).fetchone()
return row_to_user(row) if row else None
def list_users() -> list[dict[str, object]]:
with get_connection() as connection:
rows = connection.execute(
"""
SELECT *
FROM users
ORDER BY created_at DESC, id DESC
"""
).fetchall()
return [row_to_user(row) for row in rows]
def update_user_active(user_id: int, is_active: bool) -> dict[str, object] | None:
with get_connection() as connection:
cursor = connection.execute(
"""
UPDATE users
SET is_active = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
AND role != 'admin'
""",
(1 if is_active else 0, user_id),
)
if cursor.rowcount == 0:
return None
return get_user_by_id(user_id)
def normalize_interaction_indexes(value: object) -> list[int]:
if not isinstance(value, list):
return []
indexes = {
int(item)
for item in value
if isinstance(item, int) or (isinstance(item, str) and item.isdigit())
}
return sorted(index for index in indexes if index >= 0)
def normalize_progress_payload(payload: dict[str, object] | None, current_time_ms: int | None = None) -> dict[str, object]:
payload = payload if isinstance(payload, dict) else {}
current_time = payload.get("currentTime")
if not isinstance(current_time, (int, float)):
current_time = (current_time_ms or 0) / 1000
active_line_index = payload.get("activeLineIndex")
if not isinstance(active_line_index, int) or active_line_index < 0:
active_line_index = 0
return {
"currentTime": float(current_time or 0),
"activeLineIndex": active_line_index,
"practicedLineIndexes": normalize_interaction_indexes(payload.get("practicedLineIndexes")),
"difficultLineIndexes": normalize_interaction_indexes(payload.get("difficultLineIndexes")),
"updatedAt": str(payload.get("updatedAt") or ""),
}
def get_existing_video_ids(connection: sqlite3.Connection) -> set[int]:
rows = connection.execute("SELECT id FROM videos").fetchall()
return {int(row["id"]) for row in rows}
def parse_interaction_video_id(video_id: str | int) -> int | None:
try:
parsed = int(video_id)
except (TypeError, ValueError):
return None
return parsed if parsed > 0 else None
def get_user_learning_state(user_id: int) -> dict[str, object]:
with get_connection() as connection:
rows = connection.execute(
"""
SELECT video_id, interaction_type, current_time_ms, payload_json, updated_at
FROM user_interactions
WHERE user_id = ?
AND interaction_type IN ('favorite', 'progress')
ORDER BY video_id ASC
""",
(user_id,),
).fetchall()
favorites: list[str] = []
progress: dict[str, dict[str, object]] = {}
for row in rows:
video_id = str(row["video_id"])
if row["interaction_type"] == "favorite":
favorites.append(video_id)
continue
try:
payload = json.loads(row["payload_json"] or "{}")
except json.JSONDecodeError:
payload = {}
normalized = normalize_progress_payload(payload, row["current_time_ms"])
if not normalized["updatedAt"]:
normalized["updatedAt"] = row["updated_at"] or ""
progress[video_id] = normalized
return {
"favorites": sorted(set(favorites), key=lambda item: int(item)),
"progress": progress,
}
def save_user_learning_state(
user_id: int,
*,
favorites: list[str],
progress: dict[str, dict[str, object]],
) -> dict[str, object]:
with get_connection() as connection:
existing_video_ids = get_existing_video_ids(connection)
favorite_ids = [
parsed
for item in favorites
if (parsed := parse_interaction_video_id(item)) in existing_video_ids
]
progress_items = {
parsed: normalize_progress_payload(payload)
for item, payload in progress.items()
if (parsed := parse_interaction_video_id(item)) in existing_video_ids
}
connection.execute(
"""
DELETE FROM user_interactions
WHERE user_id = ?
AND interaction_type = 'favorite'
""",
(user_id,),
)
for video_id in sorted(set(favorite_ids)):
connection.execute(
"""
INSERT INTO user_interactions (user_id, video_id, interaction_type)
VALUES (?, ?, 'favorite')
ON CONFLICT(user_id, video_id, interaction_type) DO UPDATE SET
updated_at = CURRENT_TIMESTAMP
""",
(user_id, video_id),
)
connection.execute(
"""
DELETE FROM user_interactions
WHERE user_id = ?
AND interaction_type = 'progress'
""",
(user_id,),
)
for video_id, payload in progress_items.items():
current_time_ms = int(round(float(payload["currentTime"]) * 1000))
connection.execute(
"""
INSERT INTO user_interactions (
user_id, video_id, interaction_type, current_time_ms, payload_json
)
VALUES (?, ?, 'progress', ?, ?)
ON CONFLICT(user_id, video_id, interaction_type) DO UPDATE SET
current_time_ms = excluded.current_time_ms,
payload_json = excluded.payload_json,
updated_at = CURRENT_TIMESTAMP
""",
(
user_id,
video_id,
current_time_ms,
json.dumps(payload, ensure_ascii=False),
),
)
return get_user_learning_state(user_id)
def mark_user_login(user_id: int) -> None:
with get_connection() as connection:
connection.execute(
"""
UPDATE users
SET last_login_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(user_id,),
)
def register_user_with_activation(*, username: str, password_hash: str, activation_code: str) -> dict[str, object]:
clean_username = repair_mojibake_text(username)
clean_code = activation_code.strip().upper()
with get_connection() as connection:
code_row = connection.execute(
"""
SELECT *
FROM activation_codes
WHERE code = ?
AND status = 'unused'
""",
(clean_code,),
).fetchone()
if code_row is None:
raise ValueError("activation_code_unavailable")
try:
cursor = connection.execute(
"""
INSERT INTO users (
email, password_hash, display_name, role, is_active
)
VALUES (?, ?, ?, 'user', 1)
""",
(clean_username, password_hash, clean_username),
)
except sqlite3.IntegrityError as exc:
raise ValueError("username_exists") from exc
user_id = int(cursor.lastrowid)
connection.execute(
"""
DELETE FROM activation_codes
WHERE id = ?
""",
(code_row["id"],),
)
user = get_user_by_id(user_id)
if user is None:
raise RuntimeError("Registered user could not be loaded")
return user
def create_activation_codes(codes: list[str], created_by: str) -> list[dict[str, object]]:
created_ids: list[int] = []
with get_connection() as connection:
for code in codes:
cursor = connection.execute(
"""
INSERT INTO activation_codes (code, created_by)
VALUES (?, ?)
""",
(code, created_by),
)
created_ids.append(int(cursor.lastrowid))
with get_connection() as connection:
placeholders = ",".join("?" for _ in created_ids)
rows = connection.execute(
f"""
SELECT activation_codes.*, users.email AS used_by_username
FROM activation_codes
LEFT JOIN users ON users.id = activation_codes.used_by_user_id
WHERE activation_codes.id IN ({placeholders})
ORDER BY activation_codes.id DESC
""",
created_ids,
).fetchall()
return [row_to_activation_code(row) for row in rows]
def list_activation_codes() -> list[dict[str, object]]:
with get_connection() as connection:
rows = connection.execute(
"""
SELECT activation_codes.*, users.email AS used_by_username
FROM activation_codes
LEFT JOIN users ON users.id = activation_codes.used_by_user_id
ORDER BY activation_codes.created_at DESC, activation_codes.id DESC
"""
).fetchall()
return [row_to_activation_code(row) for row in rows]
def delete_unused_activation_code(code_id: int) -> bool:
with get_connection() as connection:
cursor = connection.execute(
"""
DELETE FROM activation_codes
WHERE id = ?
AND status = 'unused'
""",
(code_id,),
)
return cursor.rowcount > 0
def repair_mojibake_text(value: str | None) -> str:
if not value:
return ""
repaired = value
for _ in range(2):
if not any(marker in repaired for marker in MOJIBAKE_MARKERS):
break
try:
candidate = repaired.encode("latin1").decode("utf-8")
except UnicodeError:
break
if candidate == repaired:
break
repaired = candidate
return repaired
def parse_tags(tags_text: str | None) -> list[str]:
tags = [
tag.strip()
for tag in repair_mojibake_text(tags_text).replace(",", ",").split(",")
if tag.strip()
]
return tags or ["未分类"]
def row_to_video(row: sqlite3.Row, subtitle_rows: list[sqlite3.Row]) -> dict[str, object]:
subtitles: dict[str, dict[str, str]] = {}
for subtitle in subtitle_rows:
subtitles[subtitle["language_code"]] = {
"languageName": repair_mojibake_text(subtitle["language_name"] or subtitle["language_code"]),
"format": subtitle["format"],
"filePath": subtitle["file_path"],
"content": repair_mojibake_text(subtitle["content"] or ""),
}
try:
tags = json.loads(row["tags"] or "[]")
except json.JSONDecodeError:
tags = parse_tags(row["tags"])
tags = [repair_mojibake_text(str(tag)) for tag in tags] if isinstance(tags, list) else parse_tags(str(tags))
return {
"id": row["id"],
"title": repair_mojibake_text(row["title"]),
"speaker": repair_mojibake_text(row["speaker"] or "Unknown"),
"description": repair_mojibake_text(row["description"] or ""),
"tags": tags if isinstance(tags, list) and tags else ["未分类"],
"duration": row["duration_seconds"] or 0,
"videoUrl": row["video_url"] or "",
"coverUrl": row["cover_url"] or "",
"sourceType": "url" if str(row["video_url"] or "").startswith(("http://", "https://")) else "local-file",
"subtitles": subtitles,
}
def create_video(
*,
title: str,
speaker: str | None,
description: str | None,
tags: list[str],
video_url: str,
cover_url: str = "",
subtitles: list[dict[str, str]],
) -> dict[str, object]:
with get_connection() as connection:
cursor = connection.execute(
"""
INSERT INTO videos (
title, speaker, description, tags, video_url, cover_url, status
)
VALUES (?, ?, ?, ?, ?, ?, 'published')
""",
(
title,
repair_mojibake_text(speaker or "Unknown"),
repair_mojibake_text(description or ""),
json.dumps([repair_mojibake_text(tag) for tag in tags], ensure_ascii=False),
repair_mojibake_text(video_url),
repair_mojibake_text(cover_url),
),
)
video_id = int(cursor.lastrowid)
for subtitle in subtitles:
connection.execute(
"""
INSERT INTO subtitles (
video_id, language_code, language_name, format, file_path, content
)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
video_id,
subtitle["language_code"],
repair_mojibake_text(subtitle["language_name"]),
subtitle["format"],
subtitle["file_path"],
repair_mojibake_text(subtitle["content"]),
),
)
video = get_video(video_id)
if video is None:
raise RuntimeError("Created video could not be loaded")
return video
def list_videos() -> list[dict[str, object]]:
with get_connection() as connection:
video_rows = connection.execute(
"""
SELECT *
FROM videos
WHERE status = 'published'
ORDER BY created_at DESC, id DESC
"""
).fetchall()
subtitle_rows = connection.execute(
"""
SELECT *
FROM subtitles
ORDER BY id ASC
"""
).fetchall()
subtitles_by_video: dict[int, list[sqlite3.Row]] = {}
for subtitle in subtitle_rows:
subtitles_by_video.setdefault(int(subtitle["video_id"]), []).append(subtitle)
return [
row_to_video(row, subtitles_by_video.get(int(row["id"]), []))
for row in video_rows
]
def get_video(video_id: int) -> dict[str, object] | None:
with get_connection() as connection:
row = connection.execute(
"""
SELECT *
FROM videos
WHERE id = ? AND status = 'published'
""",
(video_id,),
).fetchone()
if row is None:
return None
subtitle_rows = connection.execute(
"""
SELECT *
FROM subtitles
WHERE video_id = ?
ORDER BY id ASC
""",
(video_id,),
).fetchall()
return row_to_video(row, subtitle_rows)
def delete_video(video_id: int) -> dict[str, object] | None:
with get_connection() as connection:
video_row = connection.execute(
"""
SELECT id, video_url, cover_url
FROM videos
WHERE id = ?
""",
(video_id,),
).fetchone()
if video_row is None:
return None
subtitle_rows = connection.execute(
"""
SELECT file_path
FROM subtitles
WHERE video_id = ?
""",
(video_id,),
).fetchall()
upload_paths = []
if video_row["video_url"]:
upload_paths.append(video_row["video_url"])
if video_row["cover_url"]:
upload_paths.append(video_row["cover_url"])
upload_paths.extend(row["file_path"] for row in subtitle_rows if row["file_path"])
connection.execute("DELETE FROM videos WHERE id = ?", (video_id,))
return {
"deletedVideoId": video_id,
"uploadPaths": upload_paths,
}
def update_video(
video_id: int,
*,
title: str,
speaker: str | None,
description: str | None,
tags: list[str],
video_url: str | None,
cover_url: str | None = None,
subtitles: list[dict[str, str]],
) -> dict[str, object] | None:
with get_connection() as connection:
existing = connection.execute(
"""
SELECT id, video_url, cover_url
FROM videos
WHERE id = ?
""",
(video_id,),
).fetchone()
if existing is None:
return None
old_upload_paths = []
final_video_url = video_url if video_url is not None else existing["video_url"]
if video_url is not None and existing["video_url"] and existing["video_url"] != video_url:
old_upload_paths.append(existing["video_url"])
final_cover_url = cover_url if cover_url is not None else existing["cover_url"]
if cover_url is not None and existing["cover_url"] and existing["cover_url"] != cover_url:
old_upload_paths.append(existing["cover_url"])
connection.execute(
"""
UPDATE videos
SET title = ?,
speaker = ?,
description = ?,
tags = ?,
video_url = ?,
cover_url = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(
title,
repair_mojibake_text(speaker or "Unknown"),
repair_mojibake_text(description or ""),
json.dumps([repair_mojibake_text(tag) for tag in tags], ensure_ascii=False),
repair_mojibake_text(final_video_url),
repair_mojibake_text(final_cover_url or ""),
video_id,
),
)
for subtitle in subtitles:
existing_subtitle = connection.execute(
"""
SELECT file_path
FROM subtitles
WHERE video_id = ? AND language_code = ?
""",
(video_id, subtitle["language_code"]),
).fetchone()
if existing_subtitle and existing_subtitle["file_path"] != subtitle["file_path"]:
old_upload_paths.append(existing_subtitle["file_path"])
connection.execute(
"""
INSERT INTO subtitles (
video_id, language_code, language_name, format, file_path, content
)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(video_id, language_code) DO UPDATE SET
language_name = excluded.language_name,
format = excluded.format,
file_path = excluded.file_path,
content = excluded.content,
updated_at = CURRENT_TIMESTAMP
""",
(
video_id,
subtitle["language_code"],
repair_mojibake_text(subtitle["language_name"]),
subtitle["format"],
subtitle["file_path"],
repair_mojibake_text(subtitle["content"]),
),
)
video = get_video(video_id)
if video is None:
return None
return {
"video": video,
"oldUploadPaths": old_upload_paths,
}
def upsert_video_subtitle(
video_id: int,
*,
language_code: str,
language_name: str,
format: str,
file_path: str,
content: str,
) -> None:
with get_connection() as connection:
connection.execute(
"""
INSERT INTO subtitles (
video_id, language_code, language_name, format, file_path, content
)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(video_id, language_code) DO UPDATE SET
language_name = excluded.language_name,
format = excluded.format,
file_path = excluded.file_path,
content = excluded.content,
updated_at = CURRENT_TIMESTAMP
""",
(
video_id,
language_code,
repair_mojibake_text(language_name),
format,
file_path,
repair_mojibake_text(content),
),
)
def table_names() -> list[str]:
with get_connection() as connection:
rows = connection.execute(
"""
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name NOT LIKE 'sqlite_%'
ORDER BY name
"""
).fetchall()
return [row["name"] for row in rows]