Pinned Chats: Update pinned chats after sleep (#5985)

This commit is contained in:
Alexander Zinchuk 2025-06-04 20:42:11 +02:00
parent 4281b219a0
commit 39ca8ce15f
6 changed files with 64 additions and 6 deletions

View File

@ -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());

View File

@ -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',

View File

@ -165,7 +165,7 @@ export type ApiUpdateChatMembers = {
export type ApiUpdatePinnedChatIds = {
'@type': 'updatePinnedChatIds';
ids: string[];
ids?: string[];
folderId?: number;
};

View File

@ -542,6 +542,35 @@ addActionHandler('loadAllChats', async (global, actions, payload): Promise<void>
}
});
addActionHandler('loadPinnedDialogs', async (global, actions, payload): Promise<void> => {
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,

View File

@ -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,

View File

@ -345,6 +345,9 @@ export interface ActionPayloads {
listType: ChatListType;
whenFirstBatchDone?: () => Promise<void>;
};
loadPinnedDialogs: {
listType: ChatListType;
};
openChatWithInfo: ActionPayloads['openChat'] & {
profileTab?: ProfileTabType;
forceScrollProfileTab?: boolean;