Pinned Chats: Update pinned chats after sleep (#5985)
This commit is contained in:
parent
4281b219a0
commit
39ca8ce15f
@ -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());
|
||||
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -165,7 +165,7 @@ export type ApiUpdateChatMembers = {
|
||||
|
||||
export type ApiUpdatePinnedChatIds = {
|
||||
'@type': 'updatePinnedChatIds';
|
||||
ids: string[];
|
||||
ids?: string[];
|
||||
folderId?: number;
|
||||
};
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -345,6 +345,9 @@ export interface ActionPayloads {
|
||||
listType: ChatListType;
|
||||
whenFirstBatchDone?: () => Promise<void>;
|
||||
};
|
||||
loadPinnedDialogs: {
|
||||
listType: ChatListType;
|
||||
};
|
||||
openChatWithInfo: ActionPayloads['openChat'] & {
|
||||
profileTab?: ProfileTabType;
|
||||
forceScrollProfileTab?: boolean;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user