From 9fe5ca5c453568f2cbc20baffc9aec68047818fe Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 12 Feb 2022 21:13:49 +0300 Subject: [PATCH] Sync: Fix support for `tgaddr` parameter --- src/global/cache.ts | 3 +-- src/modules/actions/api/sync.ts | 7 ++++--- src/util/routing.ts | 25 +++++++++++++++---------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/global/cache.ts b/src/global/cache.ts index 223e8ed16..bd755b61b 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -30,7 +30,6 @@ import { import { hasStoredSession } from '../util/sessions'; import { INITIAL_STATE } from './initial'; import { parseLocationHash } from '../util/routing'; -import { LOCATION_HASH } from '../hooks/useHistoryBack'; import { isUserId } from '../modules/helpers'; import { getOrderedIds } from '../util/folderManager'; @@ -120,7 +119,7 @@ function readCache(initialState: GlobalState): GlobalState { ...cached, }; - const parsedMessageList = !IS_SINGLE_COLUMN_LAYOUT ? parseLocationHash(LOCATION_HASH) : undefined; + const parsedMessageList = !IS_SINGLE_COLUMN_LAYOUT ? parseLocationHash() : undefined; return { ...newState, diff --git a/src/modules/actions/api/sync.ts b/src/modules/actions/api/sync.ts index 2ef81e5e0..fb32e4835 100644 --- a/src/modules/actions/api/sync.ts +++ b/src/modules/actions/api/sync.ts @@ -81,7 +81,6 @@ async function loadAndReplaceMessages() { let areMessagesLoaded = false; let global = getGlobal(); - const { chatId: currentChatId, threadId: currentThreadId } = selectCurrentMessageList(global) || {}; // Memoize drafts const draftChatIds = Object.keys(global.messages.byChatId); @@ -94,8 +93,10 @@ async function loadAndReplaceMessages() { return acc; }, {}); - if (currentChatId) { - const result = await loadTopMessages(global.chats.byId[currentChatId]); + const { chatId: currentChatId, threadId: currentThreadId } = selectCurrentMessageList(global) || {}; + const currentChat = currentChatId ? global.chats.byId[currentChatId] : undefined; + if (currentChatId && currentChat) { + const result = await loadTopMessages(currentChat); global = getGlobal(); const { chatId: newCurrentChatId } = selectCurrentMessageList(global) || {}; const threadInfo = currentThreadId && selectThreadInfo(global, currentChatId, currentThreadId); diff --git a/src/util/routing.ts b/src/util/routing.ts index d6a6764ed..b3a493116 100644 --- a/src/util/routing.ts +++ b/src/util/routing.ts @@ -1,18 +1,23 @@ import { MessageList, MessageListType } from '../global/types'; import { MAIN_THREAD_ID } from '../api/types'; -export const createMessageHash = (messageList: MessageList): string => ( - messageList.chatId.toString() - + (messageList.type !== 'thread' ? `_${messageList.type}` - : (messageList.threadId !== -1 ? `_${messageList.threadId}` : '')) -); +import { LOCATION_HASH } from '../hooks/useHistoryBack'; -export const parseLocationHash = (value: string): MessageList | undefined => { - if (!value) return undefined; +export function createMessageHash(messageList: MessageList) { + const typeOrThreadId = messageList.type !== 'thread' ? ( + `_${messageList.type}` + ) : messageList.threadId !== -1 ? ( + `_${messageList.threadId}` + ) : ''; - const [chatId, typeOrThreadId] = value.replace(/^#/, '').split('_'); + return `${messageList.chatId}${typeOrThreadId}`; +} - if (!chatId) return undefined; +export function parseLocationHash() { + if (!LOCATION_HASH) return undefined; + + const [chatId, typeOrThreadId] = LOCATION_HASH.replace(/^#/, '').split('_'); + if (!chatId?.match(/^-?\d+$/)) return undefined; const isType = ['thread', 'pinned', 'scheduled'].includes(typeOrThreadId); @@ -21,4 +26,4 @@ export const parseLocationHash = (value: string): MessageList | undefined => { type: Boolean(typeOrThreadId) && isType ? (typeOrThreadId as MessageListType) : 'thread', threadId: Boolean(typeOrThreadId) && !isType ? Number(typeOrThreadId) : MAIN_THREAD_ID, }; -}; +}