From f97dd092169b8bffc1eeddefd68d499c96b6dba7 Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Fri, 19 Sep 2025 14:34:50 +0200 Subject: [PATCH] GramJS: Fix media repair in private chats (#6241) --- src/api/gramjs/methods/client.ts | 36 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 81ad2d382..bbb6e8fae 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -339,10 +339,15 @@ export async function downloadMedia( onProgress?: ApiOnProgress, ) { try { - return (await downloadMediaWithClient(args, client, onProgress)); + const result = await downloadMediaWithClient(args, client, onProgress); + return result; } catch (err: unknown) { if (err instanceof RPCError) { if (err.errorMessage.startsWith('FILE_REFERENCE')) { + if (DEBUG) { + // eslint-disable-next-line no-console + console.warn('Trying to repair file reference', args.url); + } const isFileReferenceRepaired = await repairFileReference({ url: args.url }); if (isFileReferenceRepaired) { return downloadMediaWithClient(args, client, onProgress); @@ -525,20 +530,25 @@ export async function repairFileReference({ async function repairMessageMedia(peerId: string, messageId: number) { const type = getEntityTypeById(peerId); const inputChannel = buildInputChannelFromLocalDb(peerId); - if (!inputChannel) return false; - const result = await invokeRequest( - type === 'channel' - ? new GramJs.channels.GetMessages({ - channel: inputChannel, - id: [new GramJs.InputMessageID({ id: messageId })], - }) - : new GramJs.messages.GetMessages({ + let result; + + if (type === 'channel' && inputChannel) { + result = await invokeRequest(new GramJs.channels.GetMessages({ + channel: inputChannel, + id: [new GramJs.InputMessageID({ id: messageId })], + }), { + shouldIgnoreErrors: true, + }); + } else { + result = await invokeRequest( + new GramJs.messages.GetMessages({ id: [new GramJs.InputMessageID({ id: messageId })], }), - { - shouldIgnoreErrors: true, - }, - ); + { + shouldIgnoreErrors: true, + }, + ); + } if (!result || result instanceof GramJs.messages.MessagesNotModified) return false;