diff --git a/src/main/constants.ts b/src/main/constants.ts index 615ae0f..197522b 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -107,6 +107,10 @@ export function defaultSettings(): AppSettings { hideExtractedItems: true, confirmDeleteSelection: true, backupIncludeDownloads: false, + notifyUrl: "", + notifyOnPackageCompleted: false, + notifyOnPackageFailed: false, + notifyOnRunFinished: false, totalDownloadedAllTime: 0, totalCompletedFilesAllTime: 0, totalRuntimeAllTimeMs: 0, diff --git a/src/main/download-manager.ts b/src/main/download-manager.ts index 09ab8e1..34fbb77 100644 --- a/src/main/download-manager.ts +++ b/src/main/download-manager.ts @@ -56,6 +56,7 @@ import { cleanupArchives, clearExtractResumeState, collectArchiveCleanupTargets, import { validateFileAgainstManifest } from "./integrity"; import { classifyDiskError } from "./fs-error"; import { processVideoFile, resolveVideoTooling, stripDualLangMarker, hasDualLangMarker, isRemuxableVideoFile, type GermanAudioMode, type VideoProcessResult } from "./video-processor"; +import { sendNotification } from "./notify"; import { logger } from "./logger"; import { getRecentRotationEvents, runWithRotationItemSink, setRotationEventListener } from "./account-rotation-log"; import type { RotationEvent } from "../shared/types"; @@ -1750,6 +1751,8 @@ export class DownloadManager extends EventEmitter { private historyRecordedPackages = new Set(); + private notifiedPackages = new Set(); + private itemCount = 0; private lastSchedulerHeartbeatAt = 0; @@ -2788,6 +2791,7 @@ export class DownloadManager extends EventEmitter { this.runOutcomes.clear(); this.runCompletedPackages.clear(); this.historyRecordedPackages.clear(); + this.notifiedPackages.clear(); this.retryAfterByItem.clear(); this.providerStartReservations.clear(); this.pacedStartReservationByItem.clear(); @@ -5109,6 +5113,7 @@ export class DownloadManager extends EventEmitter { pkg.enabled = true; pkg.updatedAt = nowMs(); this.historyRecordedPackages.delete(packageId); + this.notifiedPackages.delete(packageId); if (this.session.running) { for (const itemId of itemIds) { @@ -5176,6 +5181,7 @@ export class DownloadManager extends EventEmitter { this.abortPackagePostProcessing(pkgId, "reset"); this.runCompletedPackages.delete(pkgId); this.historyRecordedPackages.delete(pkgId); + this.notifiedPackages.delete(pkgId); const pkg = this.session.packages[pkgId]; if (pkg && (pkg.status === "completed" || pkg.status === "failed" || pkg.status === "cancelled")) { @@ -7608,6 +7614,7 @@ export class DownloadManager extends EventEmitter { } } this.historyRecordedPackages.delete(packageId); + this.notifiedPackages.delete(packageId); this.abortPackagePostProcessing(packageId, "package_removed"); for (const itemId of itemIds) { this.retryAfterByItem.delete(itemId); @@ -10468,6 +10475,34 @@ export class DownloadManager extends EventEmitter { return /\b0\s*B\b/i.test(item.fullStatus || ""); } + // Once per package and run; trailing post-processing after run-end still + // notifies (runPackageIds keeps the id), startup recovery does not (set empty). + private notifyPackageOutcome(pkg: PackageEntry, kind: "completed" | "failed", detail: string): void { + const url = String(this.settings.notifyUrl || "").trim(); + if (!url) { + return; + } + if (kind === "completed" && !this.settings.notifyOnPackageCompleted) { + return; + } + if (kind === "failed" && !this.settings.notifyOnPackageFailed) { + return; + } + if (!this.session.running && !this.runPackageIds.has(pkg.id)) { + return; + } + if (this.notifiedPackages.has(pkg.id)) { + return; + } + this.notifiedPackages.add(pkg.id); + void sendNotification(url, { + title: kind === "completed" ? "Paket fertig" : "Paket fehlgeschlagen", + message: `${pkg.name}\n${detail}`, + priority: kind === "failed" ? "high" : "default", + tags: kind === "completed" ? "white_check_mark" : "x" + }); + } + private refreshPackageStatus(pkg: PackageEntry): void { let pending = 0; let success = 0; @@ -10501,6 +10536,7 @@ export class DownloadManager extends EventEmitter { return; } + const prevStatus = pkg.status; if (failed > 0) { pkg.status = "failed"; } else if (cancelled > 0) { @@ -10509,6 +10545,11 @@ export class DownloadManager extends EventEmitter { pkg.status = "completed"; } pkg.updatedAt = nowMs(); + // A package whose items ALL failed never enters post-processing, so the + // post-process notify hook can't fire for it — cover that case here. + if (pkg.status === "failed" && prevStatus !== "failed" && success === 0) { + this.notifyPackageOutcome(pkg, "failed", `${failed} von ${total} Datei(en) fehlgeschlagen`); + } } private cachedSpeedLimitKbps = 0; @@ -11731,6 +11772,12 @@ export class DownloadManager extends EventEmitter { pkg.status = "completed"; } + if (pkg.status === "completed") { + this.notifyPackageOutcome(pkg, "completed", `${success} Datei(en)${extractedCount > 0 ? `, ${extractedCount} entpackt` : ""}`); + } else if (pkg.status === "failed") { + this.notifyPackageOutcome(pkg, "failed", `${failed} von ${success + failed + cancelled} Datei(en) fehlgeschlagen`); + } + this.emitState(); if (pkg.status === "completed" || (pkg.status === "failed" && success > 0)) { @@ -12073,6 +12120,14 @@ export class DownloadManager extends EventEmitter { averageSpeedBps: avgSpeed }; this.session.summaryText = `Summary: Dauer ${duration}s, Ø Speed ${humanSize(avgSpeed)}/s, Erfolg ${success}/${total}`; + if (this.settings.notifyOnRunFinished && total > 0) { + void sendNotification(this.settings.notifyUrl, { + title: "Durchlauf beendet", + message: `${success}/${total} erfolgreich, ${failed} fehlgeschlagen, ${cancelled} abgebrochen\nDauer ${duration}s, Durchschnitt ${humanSize(avgSpeed)}/s`, + priority: failed > 0 ? "high" : "default", + tags: failed > 0 ? "warning" : "checkered_flag" + }); + } this.runItemIds.clear(); this.runOutcomes.clear(); if (this.packagePostProcessTasks.size === 0 && !this.hasAnyDeferredPostProcessPending()) { diff --git a/src/main/notify.ts b/src/main/notify.ts new file mode 100644 index 0000000..44b9bd2 --- /dev/null +++ b/src/main/notify.ts @@ -0,0 +1,49 @@ +import { logger } from "./logger"; + +export interface NotifyPayload { + title: string; + message: string; + priority?: "default" | "high"; + tags?: string; +} + +const NOTIFY_TIMEOUT_MS = 5000; + +export function isNotifyUrlValid(url: string): boolean { + return /^https?:\/\/\S+$/i.test(String(url || "").trim()); +} + +export function buildNotifyRequest(url: string, payload: NotifyPayload): { url: string; init: RequestInit } { + const headers: Record = { + "Title": payload.title, + "Content-Type": "text/plain; charset=utf-8" + }; + if (payload.priority && payload.priority !== "default") { + headers["Priority"] = payload.priority; + } + if (payload.tags) { + headers["Tags"] = payload.tags; + } + return { + url: String(url || "").trim(), + init: { method: "POST", headers, body: payload.message } + }; +} + +export async function sendNotification(url: string, payload: NotifyPayload, fetchFn: typeof fetch = fetch): Promise { + if (!isNotifyUrlValid(url)) { + return false; + } + try { + const request = buildNotifyRequest(url, payload); + const response = await fetchFn(request.url, { ...request.init, signal: AbortSignal.timeout(NOTIFY_TIMEOUT_MS) }); + if (!response.ok) { + logger.warn(`Benachrichtigung fehlgeschlagen (HTTP ${response.status}): ${payload.title}`); + return false; + } + return true; + } catch (error) { + logger.warn(`Benachrichtigung fehlgeschlagen: ${String(error)}`); + return false; + } +} diff --git a/src/main/storage.ts b/src/main/storage.ts index 647ef54..aa1811f 100644 --- a/src/main/storage.ts +++ b/src/main/storage.ts @@ -459,6 +459,10 @@ export function normalizeSettings(settings: AppSettings): AppSettings { hideExtractedItems: settings.hideExtractedItems !== undefined ? Boolean(settings.hideExtractedItems) : defaults.hideExtractedItems, confirmDeleteSelection: settings.confirmDeleteSelection !== undefined ? Boolean(settings.confirmDeleteSelection) : defaults.confirmDeleteSelection, backupIncludeDownloads: settings.backupIncludeDownloads !== undefined ? Boolean(settings.backupIncludeDownloads) : defaults.backupIncludeDownloads, + notifyUrl: asText(settings.notifyUrl) || defaults.notifyUrl, + notifyOnPackageCompleted: settings.notifyOnPackageCompleted !== undefined ? Boolean(settings.notifyOnPackageCompleted) : defaults.notifyOnPackageCompleted, + notifyOnPackageFailed: settings.notifyOnPackageFailed !== undefined ? Boolean(settings.notifyOnPackageFailed) : defaults.notifyOnPackageFailed, + notifyOnRunFinished: settings.notifyOnRunFinished !== undefined ? Boolean(settings.notifyOnRunFinished) : defaults.notifyOnRunFinished, totalDownloadedAllTime: typeof settings.totalDownloadedAllTime === "number" && settings.totalDownloadedAllTime >= 0 ? settings.totalDownloadedAllTime : defaults.totalDownloadedAllTime, totalCompletedFilesAllTime: typeof settings.totalCompletedFilesAllTime === "number" && settings.totalCompletedFilesAllTime >= 0 ? settings.totalCompletedFilesAllTime : defaults.totalCompletedFilesAllTime, totalRuntimeAllTimeMs: typeof settings.totalRuntimeAllTimeMs === "number" && settings.totalRuntimeAllTimeMs >= 0 ? settings.totalRuntimeAllTimeMs : defaults.totalRuntimeAllTimeMs, diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index c38b5f5..b0a22b1 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -853,6 +853,7 @@ const emptySnapshot = (): UiSnapshot => ({ maxParallel: 4, maxParallelExtract: 2, extractCpuPriority: "high", retryLimit: 0, speedLimitEnabled: false, speedLimitKbps: 0, speedLimitMode: "global", updateRepo: "", autoUpdateCheck: true, clipboardWatch: false, minimizeToTray: false, theme: "dark", collapseNewPackages: true, historyRetentionMode: "permanent", autoSortPackagesByProgress: true, autoSkipExtracted: false, hideExtractedItems: true, confirmDeleteSelection: true, backupIncludeDownloads: false, + notifyUrl: "", notifyOnPackageCompleted: false, notifyOnPackageFailed: false, notifyOnRunFinished: false, accountListShowDetailedDebridLinkKeys: false, bandwidthSchedules: [], totalDownloadedAllTime: 0, totalCompletedFilesAllTime: 0, totalRuntimeAllTimeMs: 0, columnOrder: ["name", "size", "progress", "hoster", "account", "prio", "status", "speed"], @@ -4956,6 +4957,12 @@ export function App(): ReactElement { + + setText("notifyUrl", e.target.value)} /> +
POST an diese URL bei den unten gewählten Ereignissen. Mit der ntfy-App aufs Handy: Topic-URL eintragen, Topic in der App abonnieren — kein Account nötig.
+ + +