Compare commits

..

No commits in common. "0229c77abd98a7e4d0bae92ae040bbe7391f9983" and "4684144093d27a5ad54022c51bd404f2c94503cb" have entirely different histories.

5 changed files with 42 additions and 34 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "real-debrid-downloader", "name": "real-debrid-downloader",
"version": "1.7.192", "version": "1.7.191",
"description": "Desktop downloader", "description": "Desktop downloader",
"main": "build/main/main/main.js", "main": "build/main/main/main.js",
"author": "Sucukdeluxe", "author": "Sucukdeluxe",

View File

@ -10496,8 +10496,10 @@ export class DownloadManager extends EventEmitter {
} }
this.notifiedPackages.add(pkg.id); this.notifiedPackages.add(pkg.id);
void sendNotification(url, { void sendNotification(url, {
title: kind === "completed" ? "✅ Paket fertig" : "❌ Paket fehlgeschlagen", title: kind === "completed" ? "Paket fertig" : "Paket fehlgeschlagen",
message: `${pkg.name}\n${detail}` message: `${pkg.name}\n${detail}`,
priority: kind === "failed" ? "high" : "default",
tags: kind === "completed" ? "white_check_mark" : "x"
}); });
} }
@ -12120,8 +12122,10 @@ export class DownloadManager extends EventEmitter {
this.session.summaryText = `Summary: Dauer ${duration}s, Ø Speed ${humanSize(avgSpeed)}/s, Erfolg ${success}/${total}`; this.session.summaryText = `Summary: Dauer ${duration}s, Ø Speed ${humanSize(avgSpeed)}/s, Erfolg ${success}/${total}`;
if (this.settings.notifyOnRunFinished && total > 0) { if (this.settings.notifyOnRunFinished && total > 0) {
void sendNotification(this.settings.notifyUrl, { void sendNotification(this.settings.notifyUrl, {
title: failed > 0 ? "⚠️ Durchlauf beendet" : "🏁 Durchlauf beendet", title: "Durchlauf beendet",
message: `${success}/${total} erfolgreich, ${failed} fehlgeschlagen, ${cancelled} abgebrochen\nDauer ${duration}s, Durchschnitt ${humanSize(avgSpeed)}/s` 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.runItemIds.clear();

View File

@ -3,24 +3,30 @@ import { logger } from "./logger";
export interface NotifyPayload { export interface NotifyPayload {
title: string; title: string;
message: string; message: string;
priority?: "default" | "high";
tags?: string;
} }
const NOTIFY_TIMEOUT_MS = 5000; const NOTIFY_TIMEOUT_MS = 5000;
const WEBHOOK_USERNAME = "Real-Debrid Downloader";
export function isNotifyUrlValid(url: string): boolean { export function isNotifyUrlValid(url: string): boolean {
return /^https?:\/\/\S+$/i.test(String(url || "").trim()); return /^https?:\/\/\S+$/i.test(String(url || "").trim());
} }
export function buildNotifyRequest(url: string, payload: NotifyPayload): { url: string; init: RequestInit } { export function buildNotifyRequest(url: string, payload: NotifyPayload): { url: string; init: RequestInit } {
const content = `**${payload.title}**\n${payload.message}`.slice(0, 2000); const headers: Record<string, string> = {
"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 { return {
url: String(url || "").trim(), url: String(url || "").trim(),
init: { init: { method: "POST", headers, body: payload.message }
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: WEBHOOK_USERNAME, content })
}
}; };
} }

View File

@ -4957,9 +4957,9 @@ export function App(): ReactElement {
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.minimizeToTray} onChange={(e) => setBool("minimizeToTray", e.target.checked)} /> In System Tray minimieren</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.minimizeToTray} onChange={(e) => setBool("minimizeToTray", e.target.checked)} /> In System Tray minimieren</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.confirmDeleteSelection} onChange={(e) => setBool("confirmDeleteSelection", e.target.checked)} /> Vor dem Löschen bestätigen</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.confirmDeleteSelection} onChange={(e) => setBool("confirmDeleteSelection", e.target.checked)} /> Vor dem Löschen bestätigen</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.backupIncludeDownloads} onChange={(e) => setBool("backupIncludeDownloads", e.target.checked)} /> Download-Liste in Sicherung mitsichern (Standard: nur Einstellungen)</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.backupIncludeDownloads} onChange={(e) => setBool("backupIncludeDownloads", e.target.checked)} /> Download-Liste in Sicherung mitsichern (Standard: nur Einstellungen)</label>
<label>Webhook-URL (Discord)</label> <label>Benachrichtigungs-URL (ntfy/Webhook)</label>
<input value={settingsDraft.notifyUrl} placeholder="https://discord.com/api/webhooks/..." onChange={(e) => setText("notifyUrl", e.target.value)} /> <input value={settingsDraft.notifyUrl} placeholder="https://ntfy.sh/mein-topic" onChange={(e) => setText("notifyUrl", e.target.value)} />
<div className="hint">In Discord: Servereinstellungen Integrationen Webhooks Neuer Webhook URL kopieren und hier eintragen. Die gewählten Ereignisse landen als Nachricht im Kanal.</div> <div className="hint">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.</div>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageCompleted} onChange={(e) => setBool("notifyOnPackageCompleted", e.target.checked)} /> Benachrichtigen wenn ein Paket fertig ist</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageCompleted} onChange={(e) => setBool("notifyOnPackageCompleted", e.target.checked)} /> Benachrichtigen wenn ein Paket fertig ist</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageFailed} onChange={(e) => setBool("notifyOnPackageFailed", e.target.checked)} /> Benachrichtigen wenn ein Paket fehlschlägt</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnPackageFailed} onChange={(e) => setBool("notifyOnPackageFailed", e.target.checked)} /> Benachrichtigen wenn ein Paket fehlschlägt</label>
<label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnRunFinished} onChange={(e) => setBool("notifyOnRunFinished", e.target.checked)} /> Benachrichtigen wenn der Durchlauf beendet ist</label> <label className="toggle-line"><input type="checkbox" checked={settingsDraft.notifyOnRunFinished} onChange={(e) => setBool("notifyOnRunFinished", e.target.checked)} /> Benachrichtigen wenn der Durchlauf beendet ist</label>

View File

@ -3,48 +3,46 @@ import { buildNotifyRequest, isNotifyUrlValid, sendNotification } from "../src/m
describe("isNotifyUrlValid", () => { describe("isNotifyUrlValid", () => {
it("accepts http/https URLs", () => { it("accepts http/https URLs", () => {
expect(isNotifyUrlValid("https://discord.com/api/webhooks/123/abc")).toBe(true); expect(isNotifyUrlValid("https://ntfy.sh/mein-topic")).toBe(true);
expect(isNotifyUrlValid("http://192.168.1.10:8080/hook")).toBe(true); expect(isNotifyUrlValid("http://192.168.1.10:8080/hook")).toBe(true);
expect(isNotifyUrlValid(" https://discord.com/api/webhooks/123/abc ")).toBe(true); expect(isNotifyUrlValid(" https://ntfy.sh/topic ")).toBe(true);
}); });
it("rejects empty and non-http values", () => { it("rejects empty and non-http values", () => {
expect(isNotifyUrlValid("")).toBe(false); expect(isNotifyUrlValid("")).toBe(false);
expect(isNotifyUrlValid("discord.com/api/webhooks/123/abc")).toBe(false); expect(isNotifyUrlValid("ntfy.sh/topic")).toBe(false);
expect(isNotifyUrlValid("ftp://x")).toBe(false); expect(isNotifyUrlValid("ftp://x")).toBe(false);
expect(isNotifyUrlValid("https:// mit leerzeichen")).toBe(false); expect(isNotifyUrlValid("https:// mit leerzeichen")).toBe(false);
}); });
}); });
describe("buildNotifyRequest", () => { describe("buildNotifyRequest", () => {
it("builds a Discord-compatible JSON webhook POST (bold title + message as content)", () => { it("builds an ntfy-style POST with title/priority/tags headers and message body", () => {
const req = buildNotifyRequest(" https://discord.com/api/webhooks/123/abc ", { title: "Paket fertig", message: "Show.S01\n5 Datei(en)" }); const req = buildNotifyRequest(" https://ntfy.sh/topic ", { title: "Paket fertig", message: "Show.S01\n5 Datei(en)", priority: "high", tags: "x" });
expect(req.url).toBe("https://discord.com/api/webhooks/123/abc"); expect(req.url).toBe("https://ntfy.sh/topic");
expect(req.init.method).toBe("POST"); expect(req.init.method).toBe("POST");
expect(req.init.headers).toMatchObject({ "Content-Type": "application/json" }); expect(req.init.body).toBe("Show.S01\n5 Datei(en)");
const body = JSON.parse(String(req.init.body)); expect(req.init.headers).toMatchObject({ Title: "Paket fertig", Priority: "high", Tags: "x" });
expect(body.content).toBe("**✅ Paket fertig**\nShow.S01\n5 Datei(en)");
expect(body.username).toBe("Real-Debrid Downloader");
}); });
it("caps the content at Discord's 2000-char limit", () => { it("omits default priority and empty tags", () => {
const req = buildNotifyRequest("https://discord.com/api/webhooks/123/abc", { title: "T", message: "x".repeat(3000) }); const req = buildNotifyRequest("https://ntfy.sh/topic", { title: "T", message: "M", priority: "default" });
const body = JSON.parse(String(req.init.body)); expect(req.init.headers).not.toHaveProperty("Priority");
expect(body.content.length).toBe(2000); expect(req.init.headers).not.toHaveProperty("Tags");
}); });
}); });
describe("sendNotification", () => { describe("sendNotification", () => {
it("returns true on HTTP ok (Discord answers 204 No Content)", async () => { it("returns true on HTTP ok", async () => {
const fetchFn = vi.fn().mockResolvedValue(new Response(null, { status: 204 })); const fetchFn = vi.fn().mockResolvedValue(new Response("", { status: 200 }));
await expect(sendNotification("https://discord.com/api/webhooks/123/abc", { title: "T", message: "M" }, fetchFn)).resolves.toBe(true); await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(true);
expect(fetchFn).toHaveBeenCalledTimes(1); expect(fetchFn).toHaveBeenCalledTimes(1);
}); });
it("returns false on HTTP error without throwing", async () => { it("returns false on HTTP error without throwing", async () => {
const fetchFn = vi.fn().mockResolvedValue(new Response("", { status: 500 })); const fetchFn = vi.fn().mockResolvedValue(new Response("", { status: 500 }));
await expect(sendNotification("https://discord.com/api/webhooks/123/abc", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false); await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
}); });
it("returns false on network error without throwing", async () => { it("returns false on network error without throwing", async () => {
const fetchFn = vi.fn().mockRejectedValue(new Error("offline")); const fetchFn = vi.fn().mockRejectedValue(new Error("offline"));
await expect(sendNotification("https://discord.com/api/webhooks/123/abc", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false); await expect(sendNotification("https://ntfy.sh/topic", { title: "T", message: "M" }, fetchFn)).resolves.toBe(false);
}); });
it("does not call fetch for an invalid URL", async () => { it("does not call fetch for an invalid URL", async () => {
const fetchFn = vi.fn(); const fetchFn = vi.fn();