From eb07c8d02f9956dff6a174bcd3f0a4191ab2a86e Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Sat, 9 Nov 2024 15:40:15 +0400 Subject: [PATCH] Profile: Fix stuck shared media loader after search (#5154) --- src/api/gramjs/methods/messages.ts | 12 +++++----- src/api/gramjs/methods/users.ts | 2 +- src/global/actions/api/globalSearch.ts | 28 ++++++++++++----------- src/global/actions/api/middleSearch.ts | 31 +++++++++++++------------- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index a2849342e..04593a082 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -1168,9 +1168,9 @@ export async function fetchDiscussionMessage({ } export async function searchMessagesInChat({ - chat, isSavedDialog, savedTag, type, query = '', threadId, minDate, maxDate, ...pagination + peer, isSavedDialog, savedTag, type, query = '', threadId, minDate, maxDate, ...pagination }: { - chat: ApiChat; + peer: ApiPeer; isSavedDialog?: boolean; savedTag?: ApiReaction; type?: ApiMessageSearchType | ApiGlobalMessageSearchType; @@ -1208,11 +1208,11 @@ export async function searchMessagesInChat({ } } - const peer = buildInputPeer(chat.id, chat.accessHash); + const inputPeer = buildInputPeer(peer.id, peer.accessHash); const result = await invokeRequest(new GramJs.messages.Search({ - peer: isSavedDialog ? new GramJs.InputPeerSelf() : peer, - savedPeerId: isSavedDialog ? peer : undefined, + peer: isSavedDialog ? new GramJs.InputPeerSelf() : inputPeer, + savedPeerId: isSavedDialog ? inputPeer : undefined, savedReaction: savedTag && [buildInputReaction(savedTag)], topMsgId: threadId !== MAIN_THREAD_ID && !isSavedDialog ? Number(threadId) : undefined, filter, @@ -1221,7 +1221,7 @@ export async function searchMessagesInChat({ maxDate, ...pagination, }), { - abortControllerChatId: chat.id, + abortControllerChatId: peer.id, abortControllerThreadId: threadId, }); diff --git a/src/api/gramjs/methods/users.ts b/src/api/gramjs/methods/users.ts index 3c50969db..3be68551f 100644 --- a/src/api/gramjs/methods/users.ts +++ b/src/api/gramjs/methods/users.ts @@ -274,7 +274,7 @@ export async function fetchProfilePhotos({ if (chat?.isRestricted) return undefined; const result = await searchMessagesInChat({ - chat: chat!, + peer, type: 'profilePhoto', limit, }); diff --git a/src/global/actions/api/globalSearch.ts b/src/global/actions/api/globalSearch.ts index 4bc501c46..b4dde6149 100644 --- a/src/global/actions/api/globalSearch.ts +++ b/src/global/actions/api/globalSearch.ts @@ -1,5 +1,5 @@ import type { - ApiChat, ApiGlobalMessageSearchType, ApiMessage, ApiTopic, + ApiChat, ApiGlobalMessageSearchType, ApiMessage, ApiPeer, ApiTopic, ApiUserStatus, } from '../../../api/types'; import type { ActionReturnType, GlobalState, TabArgs } from '../../types'; @@ -11,6 +11,7 @@ import { getCurrentTabId } from '../../../util/establishMultitabRole'; import { throttle } from '../../../util/schedulers'; import { callApi } from '../../../api/gramjs'; import { isChatChannel, isChatGroup, toChannelId } from '../../helpers/chats'; +import { isApiPeerChat } from '../../helpers/peers'; import { addActionHandler, getGlobal, setGlobal } from '../../index'; import { addMessages, @@ -21,7 +22,7 @@ import { updateTopics, } from '../../reducers'; import { - selectChat, selectChatByUsername, selectChatMessage, selectCurrentGlobalSearchQuery, selectTabState, + selectChat, selectChatByUsername, selectChatMessage, selectCurrentGlobalSearchQuery, selectPeer, selectTabState, } from '../../selectors'; const searchThrottled = throttle((cb) => cb(), 500, false); @@ -98,8 +99,8 @@ addActionHandler('searchMessagesGlobal', (global, actions, payload): ActionRetur return; } - const chat = chatId ? selectChat(global, chatId) : undefined; - const offsetPeer = nextOffsetPeerId ? selectChat(global, nextOffsetPeerId) : undefined; + const chat = chatId ? selectPeer(global, chatId) : undefined; + const offsetPeer = nextOffsetPeerId ? selectPeer(global, nextOffsetPeerId) : undefined; searchMessagesGlobal(global, { query, @@ -107,7 +108,7 @@ addActionHandler('searchMessagesGlobal', (global, actions, payload): ActionRetur offsetRate: nextOffsetRate, offsetId: nextOffsetId, offsetPeer, - chat, + peer: chat, tabId, }); }); @@ -146,14 +147,14 @@ async function searchMessagesGlobal(global: T, params: { type: ApiGlobalMessageSearchType; offsetRate?: number; offsetId?: number; - offsetPeer?: ApiChat; - chat?: ApiChat; + offsetPeer?: ApiPeer; + peer?: ApiPeer; maxDate?: number; minDate?: number; tabId: TabArgs[0]; }) { const { - query = '', type, offsetRate, offsetId, offsetPeer, chat, maxDate, minDate, tabId = getCurrentTabId(), + query = '', type, offsetRate, offsetId, offsetPeer, peer, maxDate, minDate, tabId = getCurrentTabId(), } = params; let result: { messages: ApiMessage[]; @@ -168,9 +169,9 @@ async function searchMessagesGlobal(global: T, params: { let messageLink: ApiMessage | undefined; - if (chat) { + if (peer) { const inChatResultRequest = callApi('searchMessagesInChat', { - chat, + peer, query, type, limit: GLOBAL_SEARCH_SLICE, @@ -178,8 +179,9 @@ async function searchMessagesGlobal(global: T, params: { minDate, maxDate, }); - const topicsRequest = chat.isForum ? callApi('fetchTopics', { - chat, + const isChat = isApiPeerChat(peer); + const topicsRequest = isChat && peer.isForum ? callApi('fetchTopics', { + chat: peer, query, limit: GLOBAL_TOPIC_SEARCH_SLICE, }) : undefined; @@ -258,7 +260,7 @@ async function searchMessagesGlobal(global: T, params: { ); if (result.topics) { - global = updateTopics(global, chat!.id, result.totalTopicsCount!, result.topics); + global = updateTopics(global, peer!.id, result.totalTopicsCount!, result.topics); } const sortedTopics = result.topics?.map(({ id }) => id).sort((a, b) => b - a); diff --git a/src/global/actions/api/middleSearch.ts b/src/global/actions/api/middleSearch.ts index 3e6daa991..6b308d7e2 100644 --- a/src/global/actions/api/middleSearch.ts +++ b/src/global/actions/api/middleSearch.ts @@ -2,7 +2,7 @@ import type { ChatMediaSearchParams, ChatMediaSearchSegment, LoadingState, SharedMediaType, ThreadId, } from '../../../types'; import type { ActionReturnType, GlobalState, TabArgs } from '../../types'; -import { type ApiChat, MAIN_THREAD_ID } from '../../../api/types'; +import { type ApiPeer, MAIN_THREAD_ID } from '../../../api/types'; import { LoadMoreDirection } from '../../../types'; import { @@ -34,6 +34,7 @@ import { selectCurrentMessageList, selectCurrentMiddleSearch, selectCurrentSharedMediaSearch, + selectPeer, } from '../../selectors'; const MEDIA_PRELOAD_OFFSET = 9; @@ -49,9 +50,9 @@ addActionHandler('performMiddleSearch', async (global, actions, payload): Promis const isSavedDialog = getIsSavedDialog(chatId, threadId, currentUserId); const realChatId = isSavedDialog ? String(threadId) : chatId; - const chat = realChatId ? selectChat(global, realChatId) : undefined; + const peer = realChatId ? selectPeer(global, realChatId) : undefined; let currentSearch = selectCurrentMiddleSearch(global, tabId); - if (!chat) { + if (!peer) { return; } @@ -87,7 +88,7 @@ addActionHandler('performMiddleSearch', async (global, actions, payload): Promis let result; if (type === 'chat') { result = await callApi('searchMessagesInChat', { - chat, + peer, type: 'text', query: isHashtag ? `#${query}` : query, threadId, @@ -138,7 +139,7 @@ addActionHandler('performMiddleSearch', async (global, actions, payload): Promis return; } - const resultChatId = isSavedDialog ? currentUserId : chat.id; + const resultChatId = isSavedDialog ? currentUserId : peer.id; global = addUserStatuses(global, userStatusesById); global = addMessages(global, messages); @@ -187,10 +188,10 @@ addActionHandler('searchSharedMediaMessages', (global, actions, payload): Action const isSavedDialog = getIsSavedDialog(chatId, threadId, global.currentUserId); const realChatId = isSavedDialog ? String(threadId) : chatId; - const chat = selectChat(global, realChatId); + const peer = selectPeer(global, realChatId); const currentSearch = selectCurrentSharedMediaSearch(global, tabId); - if (!chat || !currentSearch) { + if (!peer || !currentSearch) { return; } @@ -202,7 +203,7 @@ addActionHandler('searchSharedMediaMessages', (global, actions, payload): Action return; } - void searchSharedMedia(global, chat, threadId, type, offsetId, undefined, isSavedDialog, tabId); + void searchSharedMedia(global, peer, threadId, type, offsetId, undefined, isSavedDialog, tabId); }); addActionHandler('searchChatMediaMessages', (global, actions, payload): ActionReturnType => { const { @@ -273,7 +274,7 @@ addActionHandler('searchMessagesByDate', async (global, actions, payload): Promi async function searchSharedMedia( global: T, - chat: ApiChat, + peer: ApiPeer, threadId: ThreadId, type: SharedMediaType, offsetId?: number, @@ -281,10 +282,10 @@ async function searchSharedMedia( isSavedDialog?: boolean, ...[tabId = getCurrentTabId()]: TabArgs ) { - const resultChatId = isSavedDialog ? global.currentUserId! : chat.id; + const resultChatId = isSavedDialog ? global.currentUserId! : peer.id; const result = await callApi('searchMessagesInChat', { - chat, + peer, type, limit: SHARED_MEDIA_SLICE * 2, threadId, @@ -318,7 +319,7 @@ async function searchSharedMedia( setGlobal(global); if (!isBudgetPreload) { - void searchSharedMedia(global, chat, threadId, type, nextOffsetId, true, isSavedDialog, tabId); + void searchSharedMedia(global, peer, threadId, type, nextOffsetId, true, isSavedDialog, tabId); } } @@ -415,7 +416,7 @@ function calcLoadingState( async function searchChatMedia( global: T, - chat: ApiChat, + peer: ApiPeer, threadId: ThreadId, currentMediaMessageId: number, chatMediaSearchParams: ChatMediaSearchParams, @@ -441,13 +442,13 @@ async function searchChatMedia( const offsetId = calcChatMediaSearchOffsetId(direction, currentMediaMessageId, currentSegment); const addOffset = calcChatMediaSearchAddOffset(direction, limit); - const resultChatId = isSavedDialog ? global.currentUserId! : chat.id; + const resultChatId = isSavedDialog ? global.currentUserId! : peer.id; global = setChatMediaSearchLoading(global, resultChatId, threadId, true, tabId); setGlobal(global); const result = await callApi('searchMessagesInChat', { - chat, + peer, type: 'media', limit, threadId,