From 443b4ab715b1c6ef21f6bba89c867c1691e3efd2 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Mon, 16 Jan 2023 13:22:57 +0500 Subject: [PATCH] sync: improve perf of user account reset & deletion this adds a new UserId index in all the notesnook collections which helps to speed up the deletion time during account reset & delete. This fixes the request timeout issues during both of these processes. --- Notesnook.API/Controllers/UsersController.cs | 13 +++++++------ Notesnook.API/Repositories/SyncItemsRepository.cs | 5 +++-- Notesnook.API/Services/UserService.cs | 5 +++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Notesnook.API/Controllers/UsersController.cs b/Notesnook.API/Controllers/UsersController.cs index 0b8b0b7..85f5e44 100644 --- a/Notesnook.API/Controllers/UsersController.cs +++ b/Notesnook.API/Controllers/UsersController.cs @@ -100,17 +100,18 @@ namespace Notesnook.API.Controllers { var userId = this.User.FindFirstValue("sub"); - Response response = await this.httpClient.ForwardAsync(this.HttpContextAccessor, $"{Servers.IdentityServer.ToString()}/account/unregister", HttpMethod.Post); - if (!response.Success) return BadRequest(); - if (await UserService.DeleteUserAsync(userId, User.FindFirstValue("jti"))) - return Ok(); + { + Response response = await this.httpClient.ForwardAsync(this.HttpContextAccessor, $"{Servers.IdentityServer.ToString()}/account/unregister", HttpMethod.Post); + if (!response.Success) return BadRequest(); + return Ok(); + } return BadRequest(); } - catch + catch (Exception ex) { - return BadRequest(); + return BadRequest(ex.Message); } } } diff --git a/Notesnook.API/Repositories/SyncItemsRepository.cs b/Notesnook.API/Repositories/SyncItemsRepository.cs index 1c2fc1a..9bae38b 100644 --- a/Notesnook.API/Repositories/SyncItemsRepository.cs +++ b/Notesnook.API/Repositories/SyncItemsRepository.cs @@ -39,8 +39,9 @@ namespace Notesnook.API.Repositories { public SyncItemsRepository(IDbContext dbContext) : base(dbContext) { - Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Descending(i => i.DateSynced).Ascending(i => i.UserId))); - Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending((i) => i.ItemId).Ascending(i => i.UserId))); + Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced))); + Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId))); + Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId))); } private readonly List ALGORITHMS = new List { Algorithms.Default }; diff --git a/Notesnook.API/Services/UserService.cs b/Notesnook.API/Services/UserService.cs index 8604a97..a1bce61 100644 --- a/Notesnook.API/Services/UserService.cs +++ b/Notesnook.API/Services/UserService.cs @@ -173,7 +173,10 @@ namespace Notesnook.API.Services Repositories.Contents.DeleteByUserId(userId); Repositories.Settings.DeleteByUserId(userId); Repositories.Attachments.DeleteByUserId(userId); + Repositories.Reminders.DeleteByUserId(userId); + Repositories.Relations.DeleteByUserId(userId); Repositories.UsersSettings.Delete((u) => u.UserId == userId); + Repositories.Monographs.DeleteMany((m) => m.UserId == userId); if (!Constants.IS_SELF_HOSTED) { @@ -211,6 +214,8 @@ namespace Notesnook.API.Services Repositories.Contents.DeleteByUserId(userId); Repositories.Settings.DeleteByUserId(userId); Repositories.Attachments.DeleteByUserId(userId); + Repositories.Reminders.DeleteByUserId(userId); + Repositories.Relations.DeleteByUserId(userId); Repositories.Monographs.DeleteMany((m) => m.UserId == userId); if (!await unit.Commit()) return false;