Files
tsumugu/frontend/src/api.js
T
LarsandClaude Sonnet 5.5 21c2d05fb8
Deploy Development / deploy (push) Successful in 40s
Test Suite / pytest-backend (push) Successful in 4m51s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 21s
Mache Datenräume zu Teams, damit Akten und Vorgänge mit beliebigen Räumen geteilt werden können.
Die Raumfreigabe hängt nicht mehr am Heimatraum. Der Owner teilt eine Akte, Unterakte oder einen Vorgang mit jedem Raum, in dem er schreiben darf; die Mitglieder erhalten Rechte nach ihrer Rolle, begrenzbar auf nur lesen, mit Personenausnahme. Ein einzelner Vorgang lässt sich teilen, ohne die Akte oder die anderen Vorgänge offenzulegen. Wer ein Team verlässt oder Leser wird, nimmt seine Freigaben mit; wer entfernt wird, arbeitet danach im persönlichen Raum und sieht weiter alles, was ihm gehört. Der Heimatraum ist nur noch Ablage.

Die Migration übernimmt die bisherigen Raumfreigaben in die neue Tabelle room_shares und wurde mit Bestandsdaten geprüft. Die Sitzung meldet einen Raumwechsel jetzt mit eigenem Fehlercode, die Oberfläche lädt sie dann neu.

Die lokale Suite läuft mit 132 Tests grün, der Frontend-Build ebenfalls. Die Oberfläche wurde noch nicht im Browser bedient.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 21:51:49 +02:00

214 lines
6.4 KiB
JavaScript

let csrf = "";
let scope = null;
let epoch = 0;
let dirty = false;
export function sessionState() {
return { csrf, scope, epoch };
}
export function applySession(data) {
csrf = data.csrf_token || "";
scope = data.scope || null;
epoch = data.epoch || 0;
dirty = false;
return data;
}
export function clearSession() {
csrf = "";
scope = null;
epoch = 0;
dirty = false;
}
export function markDirty(value) {
dirty = value;
}
export function isDirty() {
return dirty;
}
function scopeHeaders() {
if (!scope) return {};
return { "X-Scope-Kind": scope.kind, "X-Scope-Id": scope.id };
}
export async function api(path, { method = "GET", body, form } = {}) {
const started = epoch;
const headers = { ...scopeHeaders() };
if (csrf && method !== "GET" && method !== "HEAD") {
headers["X-CSRF-Token"] = csrf;
}
let payload;
if (form) {
payload = form;
} else if (body !== undefined) {
headers["Content-Type"] = "application/json";
payload = JSON.stringify(body);
}
const response = await fetch(path, { method, headers, body: payload, credentials: "include" });
if (started !== epoch) {
const error = new Error("Der Datenraum wurde gewechselt.");
error.status = 0;
throw error;
}
if (response.status === 204) return null;
const text = await response.text();
const data = text ? JSON.parse(text) : null;
if (!response.ok) {
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
error.status = response.status;
error.code = data?.code;
if (response.status === 401) clearSession();
if (data?.code === "scope_changed" && typeof window !== "undefined") {
window.dispatchEvent(new Event("tsumugu:scope-changed"));
}
throw error;
}
return data;
}
function mailQuery(context) {
const params = new URLSearchParams();
if (context?.kind && context?.id) {
params.set("context_kind", context.kind);
params.set("context_id", context.id);
}
return params;
}
export async function fetchMailPart(elementId, context, suffix) {
const response = await fetch(`/api/v1/external-refs/${elementId}/mail/${suffix}?${mailQuery(context)}`, {
credentials: "include",
headers: scopeHeaders(),
});
if (!response.ok) {
const data = await response.json().catch(() => null);
const fallback = suffix === "original" ? "Die vollständige E-Mail ist nicht verfügbar." : "Die Anlage ist nicht verfügbar.";
throw new Error(data?.detail || fallback);
}
return response.blob();
}
export async function downloadMailPart(elementId, context, suffix, filename) {
const blob = await fetchMailPart(elementId, context, suffix);
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename || "nachricht.eml";
document.body.appendChild(link);
link.click();
link.remove();
setTimeout(() => URL.revokeObjectURL(url), 60000);
}
export async function fetchExternal(elementId, context) {
const params = new URLSearchParams();
if (context?.kind && context?.id) {
params.set("context_kind", context.kind);
params.set("context_id", context.id);
}
const response = await fetch(`/api/v1/external-refs/${elementId}/content?${params}`, {
credentials: "include",
headers: scopeHeaders(),
});
if (!response.ok) {
const data = await response.json().catch(() => null);
throw new Error(data?.detail || "Die Quelle stellt die Datei nicht bereit.");
}
return response.blob();
}
export async function fetchPaperless(elementId, context) {
return fetchExternal(elementId, context);
}
export async function fetchFile(elementId, context) {
const params = new URLSearchParams();
if (context?.kind && context?.id) {
params.set("context_kind", context.kind);
params.set("context_id", context.id);
}
const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, {
credentials: "include",
headers: scopeHeaders(),
});
if (!response.ok) throw new Error("Die Datei ist nicht verfügbar.");
return response.blob();
}
export async function downloadFile(elementId, context, filename) {
const params = new URLSearchParams();
if (context?.kind && context?.id) {
params.set("context_kind", context.kind);
params.set("context_id", context.id);
}
const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, {
credentials: "include",
headers: scopeHeaders(),
});
if (!response.ok) {
throw new Error("Die Datei ist nicht verfügbar.");
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename || "datei";
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}
export async function fetchBlob(path) {
const response = await fetch(path, { credentials: "include", headers: scopeHeaders() });
if (!response.ok) {
const data = await response.json().catch(() => null);
const error = new Error(data?.detail || "Die Vorschau ist nicht verfügbar.");
error.status = response.status;
throw error;
}
return response.blob();
}
export async function downloadPost(path, body, fallbackName) {
const headers = { ...scopeHeaders(), "Content-Type": "application/json" };
if (csrf) headers["X-CSRF-Token"] = csrf;
const response = await fetch(path, {
method: "POST",
headers,
body: JSON.stringify(body),
credentials: "include",
});
if (!response.ok) {
const data = await response.json().catch(() => null);
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
error.status = response.status;
throw error;
}
const blob = await response.blob();
const disposition = response.headers.get("Content-Disposition") || "";
const extended = disposition.match(/filename\*=UTF-8''([^;]+)/i);
const plain = disposition.match(/filename="([^"]+)"/);
const filename = extended ? decodeURIComponent(extended[1]) : (plain && plain[1]) || fallbackName || "uebergabe.zip";
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}
export function textToDocument(text) {
return { version: 1, blocks: [{ type: "markdown", text: text || "" }] };
}
export function documentToText(document) {
return (document?.blocks || []).map((block) => block.text || "").join("\n");
}