fix(settings): refuse enabling a channel the operator switched off

With the switches now enforced at dispatch, a user could still flip a
channel on, see the toggle stick, and get nothing. Refuse the enable
instead, with 403 and a message that names the reason: the ntfy and
Telegram config endpoints on both accepted body shapes, and the Web Push
subscribe endpoint, which is the enable step for that channel because it
creates the channel row.

Telegram is checked before the webhook registration, so a refused enable
makes no outbound call and stores nothing. Disabling a channel keeps
working while the switch is off — a user must always be able to turn a
channel off, whatever the operator has done.

The three refusals are published in the contract; `/api/settings/global-services`
already exposes the switches so a client can hide the control instead of
provoking the refusal.
This commit is contained in:
Marc-André Bombeck committed 2026-09-03 02:02:15 +02:00
1 parent 8d2753f942
commit de0876b215
8 files changed
+368 -11

No files matched your search

+25
View File
@@ -7,8 +7,17 @@ import { notificationChannelEnabledSchema } from "@/lib/validations/notification
import { NextRequest } from "next/server";
import { z } from "zod/v4";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { isChannelGloballyEnabled } from "@/lib/app-settings";
import { annotate } from "@/lib/logging/context";
/**
* Refusal for an enable attempt on a channel the operator switched off
* instance-wide (`AppSettings.telegramGlobal`). A toggle that appeared to
* work and then delivered nothing is worse than a refusal that says why.
*/
const TELEGRAM_GLOBALLY_DISABLED =
"Telegram is disabled on this instance by the operator";
/**
* Get Telegram notification settings for the current user.
* Never returns the bot token in plaintext.
@@ -117,6 +126,14 @@ export const PUT = apiHandler(async (request: NextRequest) => {
const enabledOnly = notificationChannelEnabledSchema.safeParse(body);
if (enabledOnly.success) {
const { enabled } = enabledOnly.data;
if (enabled && !(await isChannelGloballyEnabled("TELEGRAM"))) {
annotate({
action: { name: "settings.telegram.update" },
meta: { refused: "globally_disabled" },
});
return apiError(TELEGRAM_GLOBALLY_DISABLED, 403);
}
if (enabled && (!current.telegramBotToken || !current.telegramChatId)) {
return apiError(
"Bot token and chat ID are required when Telegram is enabled",
@@ -166,6 +183,14 @@ export const PUT = apiHandler(async (request: NextRequest) => {
const hasChatIdAfter =
chatId !== undefined ? !!trimmedChatId : !!current.telegramChatId;
if (enabled && !(await isChannelGloballyEnabled("TELEGRAM"))) {
annotate({
action: { name: "settings.telegram.update" },
meta: { refused: "globally_disabled" },
});
return apiError(TELEGRAM_GLOBALLY_DISABLED, 403);
}
if (enabled && (!hasTokenAfter || !hasChatIdAfter)) {
return apiError(
"Bot token and chat ID are required when Telegram is enabled",