feat: Initialize ObsiGate application with core frontend and backend components.

This commit is contained in:
Bruno Charest 2026-03-27 14:37:23 -04:00
parent c72f3369dd
commit 960a06f189
5 changed files with 303 additions and 34 deletions

View File

@ -15,31 +15,33 @@ def _get_history_file(username: str) -> Path:
HISTORY_DIR.mkdir(parents=True, exist_ok=True) HISTORY_DIR.mkdir(parents=True, exist_ok=True)
return HISTORY_DIR / f"{username}.json" return HISTORY_DIR / f"{username}.json"
def _read_history(username: str) -> List[Dict[str, Any]]: def _get_bookmarks_file(username: str) -> Path:
h_file = _get_history_file(username) HISTORY_DIR.mkdir(parents=True, exist_ok=True)
if not h_file.exists(): return HISTORY_DIR / f"{username}_bookmarks.json"
def _read_data(file: Path) -> List[Dict[str, Any]]:
if not file.exists():
return [] return []
try: try:
return json.loads(h_file.read_text(encoding="utf-8")) return json.loads(file.read_text(encoding="utf-8"))
except Exception as e: except Exception as e:
logger.error(f"Failed to read history for {username}: {e}") logger.error(f"Failed to read data from {file.name}: {e}")
return [] return []
def _write_history(username: str, history: List[Dict[str, Any]]): def _write_data(file: Path, data: List[Dict[str, Any]]):
h_file = _get_history_file(username)
try: try:
tmp = h_file.with_suffix(".tmp") tmp = file.with_suffix(".tmp")
tmp.write_text(json.dumps(history, indent=2, ensure_ascii=False), encoding="utf-8") tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
shutil.move(str(tmp), str(h_file)) shutil.move(str(tmp), str(file))
except Exception as e: except Exception as e:
logger.error(f"Failed to write history for {username}: {e}") logger.error(f"Failed to write data to {file.name}: {e}")
def record_open(username: str, vault: str, path: str, title: str = ""): def record_open(username: str, vault: str, path: str, title: str = ""):
"""Record that a file was opened by a user.""" """Record that a file was opened by a user."""
if not username: if not username:
return return
history = _read_history(username) history = _read_data(_get_history_file(username))
# Remove existing entry for the same file if any # Remove existing entry for the same file if any
history = [item for item in history if not (item["vault"] == vault and item["path"] == path)] history = [item for item in history if not (item["vault"] == vault and item["path"] == path)]
@ -55,16 +57,62 @@ def record_open(username: str, vault: str, path: str, title: str = ""):
# Limit history size (e.g., 100 entries) # Limit history size (e.g., 100 entries)
history = history[:100] history = history[:100]
_write_history(username, history) _write_data(_get_history_file(username), history)
def get_recent_opened(username: str, vault_filter: Optional[str] = None, limit: int = 20) -> List[Dict[str, Any]]: 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.""" """Get the most recently opened files for a user."""
if not username: if not username:
return [] return []
history = _read_history(username) history = _read_data(_get_history_file(username))
if vault_filter: if vault_filter:
history = [item for item in history if item["vault"] == vault_filter] history = [item for item in history if item["vault"] == vault_filter]
return history[:limit] return history[:limit]
def toggle_bookmark(username: str, vault: str, path: str, title: str = ""):
"""Toggle a file as bookmark for a user. Returns True if bookmarked, False if removed."""
if not username:
return False
b_file = _get_bookmarks_file(username)
bookmarks = _read_data(b_file)
# Check if already bookmarked
existing = [b for b in bookmarks if b["vault"] == vault and b["path"] == path]
if existing:
# Remove
bookmarks = [b for b in bookmarks if not (b["vault"] == vault and b["path"] == path)]
_write_data(b_file, bookmarks)
return False
else:
# Add
bookmarks.insert(0, {
"vault": vault,
"path": path,
"title": title or path.split("/")[-1],
"bookmarked_at": time.time()
})
_write_data(b_file, bookmarks)
return True
def get_bookmarks(username: str, vault_filter: Optional[str] = None) -> List[Dict[str, Any]]:
"""Get the bookmarks for a user."""
if not username:
return []
bookmarks = _read_data(_get_bookmarks_file(username))
if vault_filter:
bookmarks = [b for b in bookmarks if b["vault"] == vault_filter]
return bookmarks
def is_bookmarked(username: str, vault: str, path: str) -> bool:
"""Fast check if a file is bookmarked by a user."""
if not username:
return False
bookmarks = _read_data(_get_bookmarks_file(username))
return any(b["vault"] == vault and b["path"] == path for b in bookmarks)

View File

@ -50,7 +50,7 @@ from backend.vault_settings import (
get_all_vault_settings, get_all_vault_settings,
delete_vault_setting, delete_vault_setting,
) )
from backend.history import record_open, get_recent_opened from backend.history import record_open, get_recent_opened, toggle_bookmark, get_bookmarks, is_bookmarked
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
@ -669,7 +669,8 @@ async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] =
"mtime_human": humanize_mtime(item["opened_at"]), "mtime_human": humanize_mtime(item["opened_at"]),
"size_bytes": f_idx.get("size", 0), "size_bytes": f_idx.get("size", 0),
"tags": [f"#{t}" for t in f_idx.get("tags", [])][:5], "tags": [f"#{t}" for t in f_idx.get("tags", [])][:5],
"preview": f_idx.get("content_preview", "")[:120] "preview": f_idx.get("content_preview", "")[:120],
"bookmarked": is_bookmarked(username, v_name, f_idx["path"])
}) })
else: else:
# File might have been renamed/deleted since last open # File might have been renamed/deleted since last open
@ -680,7 +681,8 @@ async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] =
"mtime": item["opened_at"], "mtime": item["opened_at"],
"mtime_human": humanize_mtime(item["opened_at"]), "mtime_human": humanize_mtime(item["opened_at"]),
"tags": [], "tags": [],
"preview": "" "preview": "",
"bookmarked": is_bookmarked(username, v_name, item["path"])
}) })
return { return {
"files": files_resp, "files": files_resp,
@ -721,7 +723,8 @@ async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] =
"mtime_iso": iso_modified, "mtime_iso": iso_modified,
"size_bytes": f.get("size", 0), "size_bytes": f.get("size", 0),
"tags": [f"#{t}" for t in f.get("tags", [])][:5], "tags": [f"#{t}" for t in f.get("tags", [])][:5],
"preview": f.get("content_preview", "")[:120] "preview": f.get("content_preview", "")[:120],
"bookmarked": is_bookmarked(username, v_name, f["path"])
}) })
return { return {
@ -732,6 +735,68 @@ async def api_recent(limit: Optional[int] = Query(None), vault: Optional[str] =
} }
@app.get("/api/bookmarks")
async def api_bookmarks(vault: Optional[str] = Query(None), current_user=Depends(require_auth)):
username = current_user.get("username")
user_vaults = current_user.get("_token_vaults") or current_user.get("vaults", [])
if not username:
return {"files": []}
history = get_bookmarks(username, vault_filter=vault)
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
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["bookmarked_at"],
"mtime_human": humanize_mtime(item["bookmarked_at"]),
"size_bytes": f_idx.get("size", 0),
"tags": [f"#{t}" for t in f_idx.get("tags", [])][:5],
"bookmarked": True
})
else:
files_resp.append({
"path": item["path"],
"title": item.get("title") or item["path"].split("/")[-1],
"vault": v_name,
"mtime": item["bookmarked_at"],
"mtime_human": humanize_mtime(item["bookmarked_at"]),
"tags": [],
"bookmarked": True
})
return {
"files": files_resp,
"total": len(files_resp)
}
class BookmarkToggleRequest(BaseModel):
vault: str
path: str
title: Optional[str] = None
@app.post("/api/bookmarks/toggle")
async def api_toggle_bookmark(req: BookmarkToggleRequest, current_user=Depends(require_auth)):
username = current_user.get("username")
if not username:
raise HTTPException(status_code=401, detail="Not authenticated")
# Check vault access
if not check_vault_access(req.vault, current_user):
raise HTTPException(status_code=403, detail="Access denied to vault")
is_now_bookmarked = toggle_bookmark(username, req.vault, req.path, req.title)
return {"bookmarked": is_now_bookmarked}
@app.get("/api/browse/{vault_name}", response_model=BrowseResponse) @app.get("/api/browse/{vault_name}", response_model=BrowseResponse)
async def api_browse(vault_name: str, path: str = "", current_user=Depends(require_auth)): async def api_browse(vault_name: str, path: str = "", current_user=Depends(require_auth)):
"""Browse directories and files in a vault at a given path level. """Browse directories and files in a vault at a given path level.

View File

@ -2885,6 +2885,38 @@
} }
}, },
async toggleBookmark(vault, path, title, card) {
try {
const data = await api("/api/bookmarks/toggle", {
method: "POST",
body: JSON.stringify({ vault, path, title }),
});
// Refresh both widgets to keep sync
DashboardBookmarkWidget.load();
// Update current card icon if it exists
if (card) {
const btn = card.querySelector(".dashboard-card-bookmark-btn");
if (btn) {
btn.classList.toggle("active", data.bookmarked);
const icon = btn.querySelector("i");
if (icon) icon.setAttribute("data-lucide", data.bookmarked ? "bookmark" : "bookmark-plus");
safeCreateIcons();
}
}
// Check if we need to refresh the current list to reflect bookmark status across all cards
// To avoid flickering, just update the cache and re-render if needed or do a silent refresh
this._cache.forEach(f => {
if (f.vault === vault && f.path === path) f.bookmarked = data.bookmarked;
});
} catch (err) {
console.error("Failed to toggle bookmark:", err);
showToast("Erreur lors de l'épinglage", "error");
}
},
showLoading() { showLoading() {
const grid = document.getElementById("dashboard-recent-grid"); const grid = document.getElementById("dashboard-recent-grid");
const loading = document.getElementById("dashboard-loading"); const loading = document.getElementById("dashboard-loading");
@ -2944,8 +2976,19 @@
badge.className = "dashboard-vault-badge"; badge.className = "dashboard-vault-badge";
badge.textContent = file.vault; badge.textContent = file.vault;
const bookmarkBtn = document.createElement("button");
bookmarkBtn.className = `dashboard-card-bookmark-btn ${file.bookmarked ? "active" : ""}`;
bookmarkBtn.title = file.bookmarked ? "Retirer des bookmarks" : "Ajouter aux bookmarks";
bookmarkBtn.innerHTML = `<i data-lucide="${file.bookmarked ? "bookmark" : "bookmark-plus"}" style="width:14px;height:14px"></i>`;
bookmarkBtn.addEventListener("click", (e) => {
e.stopPropagation();
this.toggleBookmark(file.vault, file.path, file.title, card);
});
header.appendChild(icon); header.appendChild(icon);
header.appendChild(badge); header.appendChild(badge);
header.appendChild(bookmarkBtn);
card.appendChild(header); card.appendChild(header);
// Title // Title
@ -3055,10 +3098,74 @@
} }
this.populateVaultFilter(); this.populateVaultFilter();
this.load(selectedContextVault);
}, },
}; };
// ---------------------------------------------------------------------------
// Dashboard Bookmarks Widget
// ---------------------------------------------------------------------------
const DashboardBookmarkWidget = {
_cache: [],
_currentFilter: "",
async load(vaultFilter = "") {
const v = vaultFilter || selectedContextVault || "all";
this._currentFilter = v;
this.showLoading();
let url = "/api/bookmarks";
if (v !== "all") url += `?vault=${encodeURIComponent(v)}`;
try {
const data = await api(url);
this._cache = data.files || [];
this.render();
} catch (err) {
console.error("Dashboard: Failed to load bookmarks:", err);
this.showEmpty();
}
},
showLoading() {
const grid = document.getElementById("dashboard-bookmarks-grid");
const empty = document.getElementById("dashboard-bookmarks-empty");
const section = document.getElementById("dashboard-bookmarks-section");
if (grid) grid.innerHTML = "";
if (empty) empty.classList.add("hidden");
},
render() {
const grid = document.getElementById("dashboard-bookmarks-grid");
const empty = document.getElementById("dashboard-bookmarks-empty");
const section = document.getElementById("dashboard-bookmarks-section");
if (!this._cache || this._cache.length === 0) {
if (grid) grid.innerHTML = "";
if (empty) empty.classList.remove("hidden");
return;
}
if (empty) empty.classList.add("hidden");
if (!grid) return;
grid.innerHTML = "";
this._cache.forEach((f, idx) => {
const card = DashboardRecentWidget._createCard(f, idx);
grid.appendChild(card);
});
safeCreateIcons();
},
showEmpty() {
const grid = document.getElementById("dashboard-bookmarks-grid");
const empty = document.getElementById("dashboard-bookmarks-empty");
if (grid) grid.innerHTML = "";
if (empty) empty.classList.remove("hidden");
}
};
async function loadRecentFiles(vaultFilter) { async function loadRecentFiles(vaultFilter) {
const listEl = document.getElementById("recent-list"); const listEl = document.getElementById("recent-list");
const emptyEl = document.getElementById("recent-empty"); const emptyEl = document.getElementById("recent-empty");
@ -4671,19 +4778,22 @@
<div class="skeleton-card"></div> <div class="skeleton-card"></div>
<div class="skeleton-card"></div> <div class="skeleton-card"></div>
</div> </div>
<div id="dashboard-recent-empty" class="dashboard-recent-empty hidden"> <div id="dashboard-bookmarks-empty" class="dashboard-recent-empty hidden">
<i data-lucide="layout"></i> <i data-lucide="pin"></i>
<span>Aucun fichier récent</span> <span>Aucun bookmark</span>
<p>Les fichiers que vous ouvrirez s'afficheront ici.</p> <p>Épinglez des fichiers pour les retrouver ici.</p>
</div> </div>
</div>`; </div>`;
safeCreateIcons(); safeCreateIcons();
} }
// Show the dashboard widget // Show the dashboard widgets
if (typeof DashboardRecentWidget !== "undefined") { if (typeof DashboardRecentWidget !== "undefined") {
DashboardRecentWidget.load(selectedContextVault); DashboardRecentWidget.load(selectedContextVault);
} }
if (typeof DashboardBookmarkWidget !== "undefined") {
DashboardBookmarkWidget.load(selectedContextVault);
}
} }
function showLoading() { function showLoading() {

View File

@ -350,14 +350,31 @@
<!-- Content --> <!-- Content -->
<main class="content-area" id="content-area" aria-label="Contenu principal"> <main class="content-area" id="content-area" aria-label="Contenu principal">
<div id="dashboard-home" class="dashboard-home" role="region" aria-label="Derniers fichiers ouverts"> <div id="dashboard-home" class="dashboard-home" role="region" aria-label="Tableau de bord">
<!-- Bookmarks Section -->
<div id="dashboard-bookmarks-section" class="dashboard-section">
<div class="dashboard-header">
<div class="dashboard-title-row">
<i data-lucide="bookmark" class="dashboard-icon" style="color:var(--accent-green)"></i>
<h2>Bookmarks</h2>
</div>
</div>
<div id="dashboard-bookmarks-grid" class="dashboard-recent-grid"></div>
<div id="dashboard-bookmarks-empty" class="dashboard-recent-empty">
<i data-lucide="pin"></i>
<span>Aucun bookmark</span>
<p>Épinglez des fichiers pour les retrouver ici.</p>
</div>
</div>
<!-- Recently Opened Section -->
<div id="dashboard-recent-section" class="dashboard-section">
<div class="dashboard-header"> <div class="dashboard-header">
<div class="dashboard-title-row"> <div class="dashboard-title-row">
<i data-lucide="clock" class="dashboard-icon"></i> <i data-lucide="clock" class="dashboard-icon"></i>
<h2>Derniers fichiers ouverts</h2> <h2>Derniers fichiers ouverts</h2>
</div> </div>
</div> </div>
</div>
<div id="dashboard-recent-grid" class="dashboard-recent-grid"></div> <div id="dashboard-recent-grid" class="dashboard-recent-grid"></div>
<div id="dashboard-loading" class="dashboard-loading"> <div id="dashboard-loading" class="dashboard-loading">
<div class="skeleton-card"></div> <div class="skeleton-card"></div>

View File

@ -4653,6 +4653,12 @@ body.popup-mode .content-area {
width: 100%; width: 100%;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 32px;
}
.dashboard-section {
display: flex;
flex-direction: column;
} }
.dashboard-header { .dashboard-header {
@ -4833,9 +4839,32 @@ body.popup-mode .content-area {
font-weight: 600; font-weight: 600;
padding: 3px 8px; padding: 3px 8px;
border-radius: 10px; border-radius: 10px;
background: var(--accent); background: var(--bg-primary);
color: white; color: var(--text-secondary);
opacity: 0.9; border: 1px solid var(--border);
margin-right: auto;
}
.dashboard-card-bookmark-btn {
background: transparent;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: 4px;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
}
.dashboard-card-bookmark-btn:hover {
background: var(--bg-primary);
color: var(--accent);
}
.dashboard-card-bookmark-btn.active {
color: var(--accent-green);
} }
/* Card title */ /* Card title */