From eda882dc906e0df42d6a1694013d1c8e3bd4b690 Mon Sep 17 00:00:00 2001 From: xCyanGrizzly Date: Fri, 31 Jul 2026 04:40:15 +0200 Subject: [PATCH] feat(worker): detect + persist per-channel forwarding permission --- worker/src/db/queries.ts | 7 +++++++ worker/src/worker.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/worker/src/db/queries.ts b/worker/src/db/queries.ts index 3f7f63f..d48b3f2 100644 --- a/worker/src/db/queries.ts +++ b/worker/src/db/queries.ts @@ -639,6 +639,13 @@ export async function setChannelForum(channelId: string, isForum: boolean) { }); } +export async function setChannelAllowsForwarding(channelId: string, allowsForwarding: boolean) { + return db.telegramChannel.update({ + where: { id: channelId }, + data: { allowsForwarding }, + }); +} + export async function getTopicProgress(mappingId: string) { return db.topicProgress.findMany({ where: { accountChannelMapId: mappingId }, diff --git a/worker/src/worker.ts b/worker/src/worker.ts index 0017de0..e1f4d4a 100644 --- a/worker/src/worker.ts +++ b/worker/src/worker.ts @@ -16,6 +16,7 @@ import { updateLastProcessedMessage, updateRunActivity, setChannelForum, + setChannelAllowsForwarding, getTopicProgress, upsertTopicProgress, upsertChannel, @@ -493,9 +494,13 @@ export async function runWorkerForAccount( try { // ── Ensure TDLib knows about this chat ── // getChats may not have loaded all channels (pagination, archive folder, etc.) - // so we explicitly load each channel before scanning. + // so we explicitly load each channel before scanning. The response is + // also where we read has_protected_content (below) to decide whether + // this channel is eligible for the forward-priority ingestion path. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let chatInfo: any; try { - await client.invoke({ + chatInfo = await client.invoke({ _: "getChat", chat_id: Number(channel.telegramId), }); @@ -517,6 +522,28 @@ export async function runWorkerForAccount( ); } + // ── Check if channel allows forwarding ── + // TDLib's chat.has_protected_content is documented on the general + // Chat object (core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1chat.html), + // but PENDING LIVE VERIFICATION here: confirm on first deploy that a + // real chatTypeSupergroup/channel response actually populates this + // field (some TDLib doc pages describe it in the context of basic + // groups only). If it's ever `undefined` in practice, this block is a + // no-op and allowsForwarding stays at its last-known/null value — + // which safely keeps the channel on the download path. + const hasProtectedContent: boolean | undefined = chatInfo?.has_protected_content; + if (typeof hasProtectedContent === "boolean") { + const allowsForwarding = !hasProtectedContent; + if (allowsForwarding !== channel.allowsForwarding) { + await setChannelAllowsForwarding(channel.id, allowsForwarding); + accountLog.info( + { channelId: channel.id, title: channel.title, allowsForwarding }, + "Updated channel forwarding permission" + ); + } + channel.allowsForwarding = allowsForwarding; + } + const pipelineCtx: PipelineContext = { client, runId: activeRunId,