Skip to content

Commit

Permalink
perf: Optimize detecting deleted conversation after a backup restore …
Browse files Browse the repository at this point in the history
…[WPB-6166] (#16678) (#16680)

Co-authored-by: Thomas Belin <thomasbelin4@gmail.com>
  • Loading branch information
otto-the-bot and atomrc authored Jan 29, 2024
1 parent 48fcf2c commit f06a04e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/script/backup/BackupRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async function buildBackupRepository() {
.mockImplementation(conversations => conversations.map(c => generateConversation({type: c.type, overwites: c})));
jest.spyOn(conversationRepository, 'updateConversationStates');
jest.spyOn(conversationRepository, 'updateConversations');
jest.spyOn(conversationRepository, 'syncDeletedConversations').mockResolvedValue(undefined);
return [
new BackupRepository(backupService, conversationRepository),
{backupService, conversationRepository, storageService},
Expand Down Expand Up @@ -279,7 +280,7 @@ describe('BackupRepository', () => {
expect(mockGenerateChaCha20Key).toHaveBeenCalledWith(decodedHeader);
});

test('compressHistoryFiles does not call the encryption function if no password is provided', async () => {
it('compressHistoryFiles does not call the encryption function if no password is provided', async () => {
// Mocked values
const password = '';
const clientId = 'ClientId';
Expand All @@ -298,7 +299,8 @@ describe('BackupRepository', () => {
expect(mockEncodeHeader).not.toHaveBeenCalled();
expect(mockGenerateChaCha20Key).not.toHaveBeenCalled();
});
test('compressHistoryFiles returns a Blob object with the correct type', async () => {

it('compressHistoryFiles returns a Blob object with the correct type', async () => {
// Mocked values...
const password = 'Password';
const clientId = 'ClientId';
Expand Down
2 changes: 1 addition & 1 deletion src/script/backup/BackupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export class BackupRepository {
await this.conversationRepository.updateConversations(readableConversations);
await this.conversationRepository.initAllLocal1To1Conversations();
// doesn't need to be awaited
void this.conversationRepository.checkForDeletedConversations();
void this.conversationRepository.syncDeletedConversations();
}

private async importConversations(
Expand Down
12 changes: 3 additions & 9 deletions src/script/conversation/ConversationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1947,22 +1947,16 @@ describe('ConversationRepository', () => {

describe('checkForDeletedConversations', () => {
it('removes conversations that have been deleted on the backend', async () => {
const existingGroup = _generateConversation();
const deletedGroup = _generateConversation();
const conversationRepository = testFactory.conversation_repository!;

spyOn(testFactory.conversation_service, 'getConversationById').and.callFake(({id}) => {
if (id === deletedGroup.id) {
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject({code: HTTP_STATUS.NOT_FOUND});
}
return Promise.resolve();
jest.spyOn(testFactory.conversation_service!, 'getConversationByIds').mockResolvedValue({
not_found: [deletedGroup],
});
await conversationRepository['saveConversation'](existingGroup);
await conversationRepository['saveConversation'](deletedGroup);

const currentNbConversations = conversationRepository['conversationState'].conversations().length;
await testFactory.conversation_repository.checkForDeletedConversations();
await testFactory.conversation_repository!.syncDeletedConversations();

expect(conversationRepository['conversationState'].conversations()).toHaveLength(currentNbConversations - 1);
});
Expand Down
20 changes: 6 additions & 14 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1892,20 +1892,12 @@ export class ConversationRepository {
};

/**
* @returns resolves when deleted conversations are locally deleted, too.
*/
checkForDeletedConversations() {
return Promise.all(
this.conversationState.conversations().map(async conversation => {
try {
await this.conversationService.getConversationById(conversation);
} catch ({code}) {
if (code === HTTP_STATUS.NOT_FOUND) {
this.deleteConversationLocally(conversation, true);
}
}
}),
);
* will locally delete conversations that no longer exist on backend side
*/
async syncDeletedConversations() {
const conversationIds = this.conversationState.conversations().map(conversation => conversation.qualifiedId);
const {not_found = []} = await this.conversationService.getConversationByIds(conversationIds);
not_found.forEach(deletedConversationId => this.deleteConversationLocally(deletedConversationId, true));
}

private readonly onUserSupportedProtocolsUpdated = async ({user}: {user: User}) => {
Expand Down

0 comments on commit f06a04e

Please sign in to comment.