feat: Implement core application structure with frontend styling, JavaScript, and Python backend services.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
# backend/history.py
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
logger = logging.getLogger("obsigate.history")
|
||||
|
||||
HISTORY_DIR = Path("data/history")
|
||||
|
||||
def _get_history_file(username: str) -> Path:
|
||||
HISTORY_DIR.mkdir(parents=True, exist_ok=True)
|
||||
return HISTORY_DIR / f"{username}.json"
|
||||
|
||||
def _read_history(username: str) -> List[Dict[str, Any]]:
|
||||
h_file = _get_history_file(username)
|
||||
if not h_file.exists():
|
||||
return []
|
||||
try:
|
||||
return json.loads(h_file.read_text(encoding="utf-8"))
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read history for {username}: {e}")
|
||||
return []
|
||||
|
||||
def _write_history(username: str, history: List[Dict[str, Any]]):
|
||||
h_file = _get_history_file(username)
|
||||
try:
|
||||
tmp = h_file.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
shutil.move(str(tmp), str(h_file))
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to write history for {username}: {e}")
|
||||
|
||||
def record_open(username: str, vault: str, path: str, title: str = ""):
|
||||
"""Record that a file was opened by a user."""
|
||||
if not username:
|
||||
return
|
||||
|
||||
history = _read_history(username)
|
||||
|
||||
# Remove existing entry for the same file if any
|
||||
history = [item for item in history if not (item["vault"] == vault and item["path"] == path)]
|
||||
|
||||
# Add new entry at the beginning
|
||||
history.insert(0, {
|
||||
"vault": vault,
|
||||
"path": path,
|
||||
"title": title,
|
||||
"opened_at": time.time()
|
||||
})
|
||||
|
||||
# Limit history size (e.g., 100 entries)
|
||||
history = history[:100]
|
||||
|
||||
_write_history(username, history)
|
||||
|
||||
def get_recent_opened(username: str, vault_filter: Optional[str] = None, limit: int = 20) -> List[Dict[str, Any]]:
|
||||
"""Get the most recently opened files for a user."""
|
||||
if not username:
|
||||
return []
|
||||
|
||||
history = _read_history(username)
|
||||
|
||||
if vault_filter:
|
||||
history = [item for item in history if item["vault"] == vault_filter]
|
||||
|
||||
return history[:limit]
|
||||
+51
-2
@@ -50,6 +50,7 @@ from backend.vault_settings import (
|
||||
get_all_vault_settings,
|
||||
delete_vault_setting,
|
||||
)
|
||||
from backend.history import record_open, get_recent_opened
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -641,12 +642,54 @@ def humanize_mtime(mtime: float) -> str:
|
||||
|
||||
|
||||
@app.get("/api/recent")
|
||||
async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] = Query(None), current_user=Depends(require_auth)):
|
||||
async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] = Query(None), mode: Optional[str] = Query("opened"), current_user=Depends(require_auth)):
|
||||
config = _load_config()
|
||||
actual_limit = limit if limit is not None else config.get("recent_files_limit", 20)
|
||||
|
||||
username = current_user.get("username")
|
||||
user_vaults = current_user.get("_token_vaults") or current_user.get("vaults", [])
|
||||
|
||||
if mode == "opened" and username:
|
||||
# Use history file for "last opened"
|
||||
history = get_recent_opened(username, vault_filter=vault, limit=actual_limit)
|
||||
files_resp = []
|
||||
for item in history:
|
||||
v_name = item["vault"]
|
||||
if "*" not in user_vaults and v_name not in user_vaults:
|
||||
continue
|
||||
|
||||
# Find in index to get metadata/preview
|
||||
f_idx = find_file_in_index(item["path"], v_name)
|
||||
if f_idx:
|
||||
files_resp.append({
|
||||
"path": f_idx["path"],
|
||||
"title": f_idx.get("title") or item["path"].split("/")[-1],
|
||||
"vault": v_name,
|
||||
"mtime": item["opened_at"],
|
||||
"mtime_human": humanize_mtime(item["opened_at"]),
|
||||
"size_bytes": f_idx.get("size", 0),
|
||||
"tags": [f"#{t}" for t in f_idx.get("tags", [])][:5],
|
||||
"preview": f_idx.get("content_preview", "")[:120]
|
||||
})
|
||||
else:
|
||||
# File might have been renamed/deleted since last open
|
||||
files_resp.append({
|
||||
"path": item["path"],
|
||||
"title": item.get("title") or item["path"].split("/")[-1],
|
||||
"vault": v_name,
|
||||
"mtime": item["opened_at"],
|
||||
"mtime_human": humanize_mtime(item["opened_at"]),
|
||||
"tags": [],
|
||||
"preview": ""
|
||||
})
|
||||
return {
|
||||
"files": files_resp,
|
||||
"total": len(files_resp),
|
||||
"limit": actual_limit,
|
||||
"mode": "opened"
|
||||
}
|
||||
|
||||
# Fallback to "last modified" (original logic)
|
||||
all_files = []
|
||||
for v_name, v_data in index.items():
|
||||
if vault and v_name != vault:
|
||||
@@ -685,7 +728,7 @@ async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] =
|
||||
"files": files_resp,
|
||||
"total": len(all_files),
|
||||
"limit": actual_limit,
|
||||
"generated_at": time.time()
|
||||
"mode": "modified"
|
||||
}
|
||||
|
||||
|
||||
@@ -840,6 +883,9 @@ async def api_file_download(vault_name: str, path: str = Query(..., description=
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise HTTPException(status_code=404, detail=f"File not found: {path}")
|
||||
|
||||
# Record history
|
||||
record_open(current_user.get("username"), vault_name, path)
|
||||
|
||||
return FileResponse(
|
||||
path=str(file_path),
|
||||
filename=file_path.name,
|
||||
@@ -955,6 +1001,9 @@ async def api_file(vault_name: str, path: str = Query(..., description="Relative
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise HTTPException(status_code=404, detail=f"File not found: {path}")
|
||||
|
||||
# Record history
|
||||
record_open(current_user.get("username"), vault_name, path, title=file_path.name)
|
||||
|
||||
try:
|
||||
raw = file_path.read_text(encoding="utf-8", errors="replace")
|
||||
except PermissionError as e:
|
||||
|
||||
Reference in New Issue
Block a user