Global State: Remove legacy migrations (#3730)

This commit is contained in:
Alexander Zinchuk 2023-08-14 11:17:56 +02:00
parent ac7e8f2ec6
commit c7db27482d

View File

@ -4,9 +4,6 @@ import { addCallback, removeCallback } from '../lib/teact/teactn';
import { addActionHandler, getGlobal } from './index';
import type { ActionReturnType, GlobalState, MessageList } from './types';
import type {
ApiChat, ApiChatFullInfo, ApiUser, ApiUserFullInfo,
} from '../api/types';
import { MAIN_THREAD_ID } from '../api/types';
import { onBeforeUnload, onIdle, throttle } from '../util/schedulers';
@ -20,7 +17,6 @@ import {
GLOBAL_STATE_CACHE_CUSTOM_EMOJI_LIMIT,
ALL_FOLDER_ID,
ARCHIVED_FOLDER_ID,
DEFAULT_PATTERN_COLOR,
DEFAULT_LIMITS,
ANIMATION_LEVEL_MIN,
ANIMATION_LEVEL_MED,
@ -173,186 +169,19 @@ function unsafeMigrateCache(cached: GlobalState, initialState: GlobalState) {
}
}
if ('canAutoPlayVideos' in cached.settings.byKey) {
cached.settings.performance.autoplayVideos = cached.settings.byKey.canAutoPlayVideos;
delete cached.settings.byKey.canAutoPlayVideos;
}
if ('canAutoPlayGifs' in cached.settings.byKey) {
cached.settings.performance.autoplayGifs = cached.settings.byKey.canAutoPlayGifs;
delete cached.settings.byKey.canAutoPlayGifs;
}
cached.settings.performance = {
...initialState.settings.performance,
...cached.settings.performance,
};
if (!cached.stickers.premium) {
cached.stickers.premium = initialState.stickers.premium;
}
if (!cached.attachMenu) {
cached.attachMenu = {
bots: {},
};
}
if (!cached.trustedBotIds) {
cached.trustedBotIds = [];
}
if (!cached.passcode) {
cached.passcode = {};
}
if (cached.activeSessions?.byHash === undefined) {
cached.activeSessions = {
byHash: {},
orderedHashes: [],
};
}
if (!cached.activeWebSessions) {
cached.activeWebSessions = {
byHash: {},
orderedHashes: [],
};
}
if (!cached.transcriptions) {
cached.transcriptions = {};
}
if (cached.appConfig && !cached.appConfig.limits) {
cached.appConfig.limits = DEFAULT_LIMITS;
}
if (!cached.customEmojis) {
cached.customEmojis = {
added: {},
byId: {},
lastRendered: [],
forEmoji: {},
statusRecent: {},
};
}
if (!cached.customEmojis.statusRecent) {
cached.customEmojis.statusRecent = {};
}
if (!cached.recentCustomEmojis) {
cached.recentCustomEmojis = [];
}
if (!cached.stickers.premiumSet) {
cached.stickers.premiumSet = {
stickers: [],
};
}
if (!cached.customEmojis.forEmoji) {
cached.customEmojis.forEmoji = {};
}
if (!cached.users.fullInfoById) {
const result = Object.entries(cached.users.byId).reduce((acc, [id, user]) => {
if ('fullInfo' in user) {
if (user.fullInfo !== undefined) {
acc.fullInfo[id] = user.fullInfo as ApiUserFullInfo;
}
delete user.fullInfo;
}
acc.users[id] = user;
return acc;
}, {
users: {} as Record<string, ApiUser>,
fullInfo: {} as Record<string, ApiUserFullInfo>,
});
cached.users.fullInfoById = result.fullInfo;
cached.users.byId = result.users;
}
if (!cached.chats.fullInfoById) {
const result = Object.entries(cached.chats.byId).reduce((acc, [id, chat]) => {
if ('fullInfo' in chat) {
if (chat.fullInfo !== undefined) {
acc.fullInfo[id] = chat.fullInfo as ApiChatFullInfo;
}
delete chat.fullInfo;
}
acc.chats[id] = chat;
return acc;
}, {
chats: {} as Record<string, ApiChat>,
fullInfo: {} as Record<string, ApiChatFullInfo>,
});
cached.chats.fullInfoById = result.fullInfo;
cached.chats.byId = result.chats;
}
// TODO Remove in Jan 2023 (this was re-designed but can be hardcoded in cache)
const { light: lightTheme } = cached.settings.themes;
if (lightTheme?.patternColor === 'rgba(90, 110, 70, 0.6)' || !lightTheme?.patternColor) {
cached.settings.themes.light = {
...lightTheme,
patternColor: DEFAULT_PATTERN_COLOR,
};
}
cached.serviceNotifications.forEach((notification) => {
const { isHidden } = notification as any;
if (isHidden) {
notification.isDeleted = isHidden;
}
});
// TODO Remove in Mar 2023 (this was re-designed but can be hardcoded in cache)
if (cached.users.byId && Object.values(cached.users.byId).some((u) => 'username' in u)) {
cached.users.byId = Object.entries(cached.users.byId).reduce((acc, [id, user]) => {
if ('username' in user) {
delete user.username;
}
acc[id] = user;
return acc;
}, {} as Record<string, ApiUser>);
}
// TODO Remove in Mar 2023 (this was re-designed but can be hardcoded in cache)
if (cached.chats.byId && Object.values(cached.chats.byId).some((c) => 'username' in c)) {
cached.chats.byId = Object.entries(cached.chats.byId).reduce((acc, [id, user]) => {
if ('username' in user) {
delete user.username;
}
acc[id] = user;
return acc;
}, {} as Record<string, ApiChat>);
}
// TODO Remove in Apr 2023 (this was re-designed but can be hardcoded in cache)
if (cached.messages.byChatId) {
const wasUpdated = Object.values(cached.messages.byChatId)
.some((messages) => Object.values(messages.byId).some(({ reactions }) => {
return reactions?.results[0]?.reaction && typeof reactions.results[0].reaction !== 'string';
}));
if (!wasUpdated) {
for (const messages of Object.values(cached.messages.byChatId)) {
for (const message of Object.values(messages.byId)) {
delete message.reactions;
}
}
}
}
if (typeof cached.config?.defaultReaction === 'string') {
cached.config.defaultReaction = { emoticon: cached.config.defaultReaction };
}
if (typeof cached.availableReactions?.[0].reaction === 'string') {
cached.availableReactions = cached.availableReactions
.map((r) => ({ ...r, reaction: { emoticon: r.reaction as unknown as string } }));
@ -361,6 +190,10 @@ function unsafeMigrateCache(cached: GlobalState, initialState: GlobalState) {
if (!cached.archiveSettings) {
cached.archiveSettings = initialState.archiveSettings;
}
if (!cached.stories) {
cached.stories = initialState.stories;
}
}
function updateCache() {