From 39ca8ce15f251442b58ada240ff4256f710fdfc5 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 4 Jun 2025 20:42:11 +0200 Subject: [PATCH] Pinned Chats: Update pinned chats after sleep (#5985) --- src/api/gramjs/methods/chats.ts | 24 ++++++++++++++++++ src/api/gramjs/updates/mtpUpdateHandler.ts | 8 +++--- src/api/types/updates.ts | 2 +- src/global/actions/api/chats.ts | 29 ++++++++++++++++++++++ src/global/actions/apiUpdaters/chats.ts | 4 +++ src/global/types/actions.ts | 3 +++ 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index 88f949eaa..6ee05d36b 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -2,6 +2,7 @@ import BigInt from 'big-integer'; import { Api as GramJs } from '../../../lib/gramjs'; import { RPCError } from '../../../lib/gramjs/errors'; +import type { ChatListType } from '../../../types'; import type { ApiChat, ApiChatAdminRights, @@ -1081,6 +1082,29 @@ export async function fetchChatFolders() { }; } +export async function fetchPinnedDialogs({ + listType, +}: { + listType: ChatListType; +}) { + const result = await invokeRequest(new GramJs.messages.GetPinnedDialogs({ + folderId: listType === 'archived' ? ARCHIVED_FOLDER_ID : undefined, + })); + + if (!result) { + return undefined; + } + + const { dialogs, messages, chats, users } = result; + + return { + dialogIds: dialogs.map((dialog) => getApiChatIdFromMtpPeer(dialog.peer)), + messages: messages.map((message) => buildApiMessage(message)).filter(Boolean), + chats: chats.map((chat) => buildApiChatFromPreview(chat)).filter(Boolean), + users: users.map((user) => buildApiUser(user)).filter(Boolean), + }; +} + export async function fetchRecommendedChatFolders() { const results = await invokeRequest(new GramJs.messages.GetSuggestedDialogFilters()); diff --git a/src/api/gramjs/updates/mtpUpdateHandler.ts b/src/api/gramjs/updates/mtpUpdateHandler.ts index 052898958..0f691f3cb 100644 --- a/src/api/gramjs/updates/mtpUpdateHandler.ts +++ b/src/api/gramjs/updates/mtpUpdateHandler.ts @@ -531,11 +531,9 @@ export function updater(update: Update) { isPinned: update.pinned || false, }); } else if (update instanceof GramJs.UpdatePinnedDialogs) { - const ids = update.order - ? update.order - .filter((dp): dp is GramJs.DialogPeer => dp instanceof GramJs.DialogPeer) - .map((dp) => getApiChatIdFromMtpPeer(dp.peer)) - : []; + const ids = update.order?.filter( + (dp): dp is GramJs.DialogPeer => dp instanceof GramJs.DialogPeer) + .map((dp) => getApiChatIdFromMtpPeer(dp.peer)); sendApiUpdate({ '@type': 'updatePinnedChatIds', diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index 1eef49642..540cdc91c 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -165,7 +165,7 @@ export type ApiUpdateChatMembers = { export type ApiUpdatePinnedChatIds = { '@type': 'updatePinnedChatIds'; - ids: string[]; + ids?: string[]; folderId?: number; }; diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index 87b2092c5..d05104787 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -542,6 +542,35 @@ addActionHandler('loadAllChats', async (global, actions, payload): Promise } }); +addActionHandler('loadPinnedDialogs', async (global, actions, payload): Promise => { + const { + listType, + } = payload; + + const result = await callApi('fetchPinnedDialogs', { listType }); + if (!result) return; + + const { dialogIds, messages, chats, users } = result; + + global = getGlobal(); + global = updateChats(global, buildCollectionByKey(chats, 'id')); + global = updateUsers(global, buildCollectionByKey(users, 'id')); + global = replaceMessages(global, messages); + + global = { + ...global, + chats: { + ...global.chats, + orderedPinnedIds: { + ...global.chats.orderedPinnedIds, + [listType]: dialogIds.length ? dialogIds : undefined, + }, + }, + }; + + setGlobal(global); +}); + addActionHandler('loadFullChat', (global, actions, payload): ActionReturnType => { const { chatId, force, withPhotos, diff --git a/src/global/actions/apiUpdaters/chats.ts b/src/global/actions/apiUpdaters/chats.ts index c7252c4fc..46e24b3fd 100644 --- a/src/global/actions/apiUpdaters/chats.ts +++ b/src/global/actions/apiUpdaters/chats.ts @@ -242,6 +242,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { case 'updatePinnedChatIds': { const { ids, folderId } = update; const listType = folderId === ARCHIVED_FOLDER_ID ? 'archived' : 'active'; + if (!ids) { + actions.loadPinnedDialogs({ listType }); + return global; + } return { ...global, diff --git a/src/global/types/actions.ts b/src/global/types/actions.ts index b1ff26f2b..9ba4b6fde 100644 --- a/src/global/types/actions.ts +++ b/src/global/types/actions.ts @@ -345,6 +345,9 @@ export interface ActionPayloads { listType: ChatListType; whenFirstBatchDone?: () => Promise; }; + loadPinnedDialogs: { + listType: ChatListType; + }; openChatWithInfo: ActionPayloads['openChat'] & { profileTab?: ProfileTabType; forceScrollProfileTab?: boolean;