From 1162804e9d902724196a975538301a64610dff8a Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Mon, 1 Jun 2026 01:16:09 +0200 Subject: [PATCH] Story: Fix handling forwards from deleted channels (#6972) --- src/api/gramjs/methods/stories.ts | 35 +++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/api/gramjs/methods/stories.ts b/src/api/gramjs/methods/stories.ts index dac6c44cb..c62c03ab7 100644 --- a/src/api/gramjs/methods/stories.ts +++ b/src/api/gramjs/methods/stories.ts @@ -1,4 +1,5 @@ import { Api as GramJs } from '../../../lib/gramjs'; +import { RPCError } from '../../../lib/gramjs/errors'; import type { ApiError, @@ -32,7 +33,7 @@ import { } from '../gramjsBuilders'; import { addStoryToLocalDb } from '../helpers/localDb'; import { deserializeBytes } from '../helpers/misc'; -import { invokeRequest } from './client'; +import { dispatchErrorUpdate, invokeRequest } from './client'; export async function fetchAllStories({ stateHash, @@ -181,16 +182,30 @@ export function fetchStoriesArchive({ } export async function fetchPeerStoriesByIds({ peer, ids }: { peer: ApiPeer; ids: number[] }) { - const result = await invokeRequest(new GramJs.stories.GetStoriesByID({ + const request = new GramJs.stories.GetStoriesByID({ peer: buildInputPeer(peer.id, peer.accessHash), id: ids, - })); + }); + let result; + + try { + result = await invokeRequest(request, { shouldThrow: true }); + } catch (err) { + if (err instanceof RPCError && err.errorMessage === 'CHANNEL_PRIVATE') { + return { + stories: buildDeletedStories(peer.id, ids), + }; + } + + dispatchErrorUpdate(err as Error, request); + return undefined; + } if (!result) { return undefined; } - const stories = ids.reduce>((acc, id) => { + const stories = ids.reduce>((acc, id) => { const story = result.stories.find(({ id: currentId }) => currentId === id); if (story) { acc[id] = buildApiStory(peer.id, story); @@ -214,6 +229,18 @@ export async function fetchPeerStoriesByIds({ peer, ids }: { peer: ApiPeer; ids: }; } +function buildDeletedStories(peerId: string, ids: number[]): Record { + return ids.reduce>((acc, id) => { + acc[id] = { + id, + peerId, + isDeleted: true, + }; + + return acc; + }, {}); +} + export function viewStory({ peer, storyId }: { peer: ApiPeer; storyId: number }) { return invokeRequest(new GramJs.stories.IncrementStoryViews({ peer: buildInputPeer(peer.id, peer.accessHash),