Kontakte mit dem Handy abgleichen über Nextcloud-Adressbuch, .vcf einlesen (0.44.0)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
@@ -12,6 +12,7 @@ import { uebersicht } from "./uebersicht.js";
|
||||
import { aufgabenKalender, baustellenKalender } from "./kalender.js";
|
||||
import { nummernAufraeumen, projektNummerVorbereiten } from "./nummern.js";
|
||||
import { Nextcloud, STATUSORDNER, ordnerAbgleichen, webdavBasis } from "./nextcloud.js";
|
||||
import { CardDav, ergebnisText, kontakteAbgleichen, vcfEinlesen } from "./kontaktabgleich.js";
|
||||
import { NUR_SICHERUNG, TABELLEN } from "./tabellen.js";
|
||||
import { EinstellungenSchema, type Einstellungen } from "./einstellungen.js";
|
||||
import { liquiditaet } from "../domain/liquiditaet.js";
|
||||
@@ -49,6 +50,8 @@ export interface Optionen {
|
||||
anmeldung?: Anmeldeart;
|
||||
/** Nur auf dem Server: Code, mit dem der erste Benutzer (Inhaber) angelegt wird */
|
||||
einrichtungsCode?: string;
|
||||
/** Nur auf dem Server: Kontakte so oft (ms) mit dem Handy-Adressbuch abgleichen, wenn eingeschaltet */
|
||||
kontaktTakt?: number;
|
||||
}
|
||||
|
||||
/** Wer angemeldet ist, steht im Änderungsprotokoll */
|
||||
@@ -189,6 +192,80 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
return { ok, fehler };
|
||||
});
|
||||
|
||||
// Kontakte mit dem Handy abgleichen (Henning, 04.11.2026): Adressbuch in derselben Nextcloud wie die Projektordner
|
||||
async function cardDav(): Promise<CardDav | undefined> {
|
||||
const nc = await nextcloud();
|
||||
return nc ? new CardDav(nc.z, nc.abruf) : undefined;
|
||||
}
|
||||
let laeuft: Promise<unknown> | null = null;
|
||||
async function kontakteLauf(): Promise<{ ok: boolean; meldung: string }> {
|
||||
const e = (await ladeEinstellungen()).kontakteAbgleich;
|
||||
const cd = await cardDav();
|
||||
if (!cd) return { ok: false, meldung: "Für den Abgleich fehlt der Nextcloud-Zugang." };
|
||||
if (!e.adressbuch) return { ok: false, meldung: "Bitte zuerst das Adressbuch wählen." };
|
||||
const stand = (meldung: string | null, fehler: string | null) => pool.query(`insert into abgleich_stand (art, zuletzt, meldung, fehler) values ('kontakte', now(), $1, $2)
|
||||
on conflict (art) do update set zuletzt = now(), meldung = coalesce(excluded.meldung, abgleich_stand.meldung), fehler = excluded.fehler`, [meldung, fehler]);
|
||||
try {
|
||||
const meldung = ergebnisText(await kontakteAbgleichen(pool, cd, e.adressbuch));
|
||||
await stand(meldung, null);
|
||||
return { ok: true, meldung };
|
||||
} catch (err) {
|
||||
await stand(null, (err as Error).message);
|
||||
return { ok: false, meldung: (err as Error).message };
|
||||
}
|
||||
}
|
||||
/** nie zwei Läufe gleichzeitig */
|
||||
const kontakteEinmal = async () => {
|
||||
while (laeuft) await laeuft.catch(() => {});
|
||||
const p = kontakteLauf();
|
||||
laeuft = p;
|
||||
try { return await p; } finally { laeuft = null; }
|
||||
};
|
||||
if (o.kontaktTakt) {
|
||||
const takt = setInterval(() => {
|
||||
void (async () => { if ((await ladeEinstellungen()).kontakteAbgleich.an) await kontakteEinmal(); })().catch((err) => app.log.error(err));
|
||||
}, o.kontaktTakt);
|
||||
app.addHook("onClose", async () => clearInterval(takt));
|
||||
}
|
||||
app.get("/api/kontakte-abgleich", async () => {
|
||||
const e = (await ladeEinstellungen()).kontakteAbgleich;
|
||||
const stand = (await pool.query("select zuletzt, meldung, fehler from abgleich_stand where art = 'kontakte'")).rows[0] ?? null;
|
||||
const n = (await pool.query("select count(*) filter (where not geloescht) as verknuepft, count(*) filter (where geloescht) as geloescht from kontakt_abgleich")).rows[0];
|
||||
return { ...e, moeglich: !!(await cardDav()), stand, verknuepft: Number(n.verknuepft), geloescht: Number(n.geloescht) };
|
||||
});
|
||||
app.get("/api/kontakte-abgleich/adressbuecher", async (_req, reply) => {
|
||||
const cd = await cardDav();
|
||||
if (!cd) return reply.status(409).send({ fehler: einrichtbar || o.nextcloud ? "Bitte zuerst den Nextcloud-Zugang eintragen." : "Der Abgleich mit dem Handy geht erst auf dem Server." });
|
||||
try { return await cd.adressbuecher(); } catch (err) { return reply.status(502).send({ fehler: (err as Error).message }); }
|
||||
});
|
||||
app.put("/api/kontakte-abgleich", async (req) => {
|
||||
const d = z.object({ adressbuch: z.string().trim().max(500), an: z.boolean() }).parse(req.body);
|
||||
const e = await ladeEinstellungen();
|
||||
const neu = EinstellungenSchema.parse({ ...e, kontakteAbgleich: d });
|
||||
await transaktion(pool, benutzer(req), async (c) => {
|
||||
await c.query("update einstellungen set daten = $1", [neu]);
|
||||
// anderes Adressbuch: Zuordnung neu aufbauen (der erste Lauf führt gleiche Kontakte zusammen)
|
||||
if (d.adressbuch !== e.kontakteAbgleich.adressbuch) {
|
||||
await c.query("delete from kontakt_abgleich");
|
||||
await c.query("delete from abgleich_stand where art = 'kontakte'");
|
||||
}
|
||||
});
|
||||
return { ok: true };
|
||||
});
|
||||
app.post("/api/kontakte-abgleich/jetzt", async (_req, reply) => {
|
||||
const r = await kontakteEinmal();
|
||||
return r.ok ? r : reply.status(409).send({ fehler: r.meldung });
|
||||
});
|
||||
/** welche Kontakte im Handy stehen, für die Markierung in der Kontaktliste */
|
||||
app.get("/api/kontakte-abgleich/kontakte", async () =>
|
||||
(await pool.query("select kontakt_id, geloescht from kontakt_abgleich where kontakt_id is not null")).rows);
|
||||
// .vcf-Datei einlesen (Apple: Kontakte auswählen → Exportieren); geht auch in der Testversion
|
||||
app.post("/api/kontakte/vcf", async (req, reply) => {
|
||||
const d = z.object({ text: z.string().max(20 * 1024 * 1024) }).parse(req.body);
|
||||
if (!/BEGIN:VCARD/i.test(d.text)) return reply.status(400).send({ fehler: "Das ist keine Kontaktdatei (vCard, .vcf)." });
|
||||
return transaktion(pool, benutzer(req), (c) => vcfEinlesen(c, d.text));
|
||||
});
|
||||
|
||||
// Ablage auf der Projektseite (Henning, 20.10.2026): Inhalt des Nextcloud-Projektordners anzeigen und Dateien öffnen
|
||||
const AblageAnfrage = z.object({ pfad: z.string().max(500).default("") })
|
||||
.refine((q) => !q.pfad.split("/").some((t) => t === ".." || t === "."), { message: "Pfad ungültig" });
|
||||
|
||||
Reference in New Issue
Block a user