Skip to content

Commit 4973b9b

Browse files
Fix missing all chat folder on startup without new manifest
1 parent bf217a8 commit 4973b9b

File tree

9 files changed

+191
-91
lines changed

9 files changed

+191
-91
lines changed

ts/background.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,17 @@ export async function startApp(): Promise<void> {
10141014
) {
10151015
await window.storage.put('needProfileMovedModal', true);
10161016
}
1017+
1018+
if (window.isBeforeVersion(lastVersion, 'v7.75.0-beta.1')) {
1019+
const hasAllChatsChatFolder = await DataReader.hasAllChatsChatFolder();
1020+
if (!hasAllChatsChatFolder) {
1021+
log.info('Creating "all chats" chat folder');
1022+
await DataWriter.createAllChatsChatFolder();
1023+
StorageService.storageServiceUploadJobAfterEnabled({
1024+
reason: 'createAllChatsChatFolder',
1025+
});
1026+
}
1027+
}
10171028
}
10181029

10191030
setAppLoadingScreenMessage(

ts/services/storage.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ import { isDone as isRegistrationDone } from '../util/registration.js';
8888
import { callLinkRefreshJobQueue } from '../jobs/callLinkRefreshJobQueue.js';
8989
import { isMockEnvironment } from '../environment.js';
9090
import { validateConversation } from '../util/validateConversation.js';
91-
import {
92-
ChatFolderType,
93-
toCurrentChatFolders,
94-
type ChatFolder,
95-
} from '../types/ChatFolder.js';
91+
import { hasAllChatsChatFolder, type ChatFolder } from '../types/ChatFolder.js';
9692

9793
const { debounce, isNumber, chunk } = lodash;
9894

@@ -1663,20 +1659,9 @@ async function processManifest(
16631659
});
16641660
});
16651661

1666-
const chatFoldersHasAllChatsFolder = chatFolders.some(chatFolder => {
1667-
return (
1668-
chatFolder.folderType === ChatFolderType.ALL &&
1669-
chatFolder.deletedAtTimestampMs === 0
1670-
);
1671-
});
1672-
1673-
if (!chatFoldersHasAllChatsFolder) {
1662+
if (!hasAllChatsChatFolder(chatFolders)) {
16741663
log.info(`process(${version}): creating all chats chat folder`);
1675-
await DataWriter.createAllChatsChatFolder();
1676-
const currentChatFolders = await DataReader.getCurrentChatFolders();
1677-
window.reduxActions.chatFolders.replaceAllChatFolderRecords(
1678-
toCurrentChatFolders(currentChatFolders)
1679-
);
1664+
window.reduxActions.chatFolders.createAllChatsChatFolder();
16801665
}
16811666
}
16821667

@@ -2235,6 +2220,7 @@ async function upload({
22352220
}
22362221

22372222
let storageServiceEnabled = false;
2223+
let storageServiceNeedsUploadAfterEnabled = false;
22382224

22392225
export function enableStorageService(): void {
22402226
if (storageServiceEnabled) {
@@ -2243,6 +2229,12 @@ export function enableStorageService(): void {
22432229

22442230
storageServiceEnabled = true;
22452231
log.info('enableStorageService');
2232+
2233+
if (storageServiceNeedsUploadAfterEnabled) {
2234+
storageServiceUploadJob({
2235+
reason: 'storageServiceNeedsUploadAfterEnabled',
2236+
});
2237+
}
22462238
}
22472239

22482240
export function disableStorageService(reason: string): void {
@@ -2351,6 +2343,18 @@ export async function reprocessUnknownFields(): Promise<void> {
23512343
);
23522344
}
23532345

2346+
export function storageServiceUploadJobAfterEnabled({
2347+
reason,
2348+
}: {
2349+
reason: string;
2350+
}): void {
2351+
if (storageServiceEnabled) {
2352+
return storageServiceUploadJob({ reason });
2353+
}
2354+
log.info(`storageServiceNeedsUploadAfterEnabled: ${reason}`);
2355+
storageServiceNeedsUploadAfterEnabled = true;
2356+
}
2357+
23542358
export const storageServiceUploadJob = debounce(
23552359
({ reason }: { reason: string }) => {
23562360
if (!storageServiceEnabled) {

ts/sql/Interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ type ReadableInterface = {
923923
getAllChatFolders: () => ReadonlyArray<ChatFolder>;
924924
getCurrentChatFolders: () => ReadonlyArray<ChatFolder>;
925925
getChatFolder: (id: ChatFolderId) => ChatFolder | null;
926+
hasAllChatsChatFolder: () => boolean;
926927
getOldestDeletedChatFolder: () => ChatFolder | null;
927928

928929
getMessagesNeedingUpgrade: (

ts/sql/Server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ import {
240240
getCurrentChatFolders,
241241
getChatFolder,
242242
createChatFolder,
243+
hasAllChatsChatFolder,
243244
createAllChatsChatFolder,
244245
updateChatFolder,
245246
markChatFolderDeleted,
@@ -459,6 +460,7 @@ export const DataReader: ServerReadableInterface = {
459460
getAllChatFolders,
460461
getCurrentChatFolders,
461462
getChatFolder,
463+
hasAllChatsChatFolder,
462464
getOldestDeletedChatFolder,
463465

464466
callLinkExists,

ts/sql/server/chatFolders.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ export function createChatFolder(db: WritableDB, chatFolder: ChatFolder): void {
146146
})();
147147
}
148148

149+
export function hasAllChatsChatFolder(db: ReadableDB): boolean {
150+
const [query, params] = sql`
151+
SELECT EXISTS (
152+
SELECT 1 FROM chatFolders
153+
WHERE folderType IS ${ChatFolderType.ALL}
154+
AND deletedAtTimestampMs IS 0
155+
LIMIT 1
156+
)
157+
`;
158+
const result = db.prepare(query, { pluck: true }).get<number>(params);
159+
return result === 1;
160+
}
161+
149162
export function createAllChatsChatFolder(db: WritableDB): ChatFolder {
150163
return db.transaction(() => {
151164
const allChatsChatFolder: ChatFolder = {

ts/state/ducks/chatFolders.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
type CurrentChatFolders,
1818
} from '../../types/ChatFolder.js';
1919
import { getCurrentChatFolders } from '../selectors/chatFolders.js';
20-
import { DataWriter } from '../../sql/Client.js';
20+
import { DataReader, DataWriter } from '../../sql/Client.js';
2121
import { storageServiceUploadJob } from '../../services/storage.js';
2222
import { parseStrict } from '../../util/schemas.js';
2323
import { chatFolderCleanupService } from '../../services/expiring/chatFolderCleanupService.js';
@@ -142,6 +142,20 @@ function createChatFolder(
142142
};
143143
}
144144

145+
function createAllChatsChatFolder(): ThunkAction<
146+
void,
147+
RootStateType,
148+
unknown,
149+
ChatFolderRecordReplaceAll
150+
> {
151+
return async dispatch => {
152+
await DataWriter.createAllChatsChatFolder();
153+
storageServiceUploadJob({ reason: 'createAllChatsChatFolder' });
154+
const chatFolders = await DataReader.getCurrentChatFolders();
155+
dispatch(replaceAllChatFolderRecords(toCurrentChatFolders(chatFolders)));
156+
};
157+
}
158+
145159
function updateChatFolder(
146160
chatFolderId: ChatFolderId,
147161
chatFolderParams: ChatFolderParams
@@ -210,6 +224,7 @@ export const actions = {
210224
replaceChatFolderRecord,
211225
removeChatFolderRecord,
212226
createChatFolder,
227+
createAllChatsChatFolder,
213228
updateChatFolder,
214229
deleteChatFolder,
215230
updateChatFoldersPositions,

0 commit comments

Comments
 (0)