Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor (#186)

Leendert de Borst90471c362f23
diff --git a/src/AliasVault.Client/Main/Components/Attachments/AttachmentUploader.razor b/src/AliasVault.Client/Main/Components/Attachments/AttachmentUploader.razor
index 31785a175..3b24c54f6 100644
--- a/src/AliasVault.Client/Main/Components/Attachments/AttachmentUploader.razor
+++ b/src/AliasVault.Client/Main/Components/Attachments/AttachmentUploader.razor
@@ -49,10 +49,19 @@
/// Original attachments that were passed in. This is used to determine if a deleted attachment was part of the original set and
/// can be hard deleted (did not exist in the original set) or should be soft deleted (was part of the original set).
/// </summary>
- private List<Attachment> _originalAttachments = [];
+ private List<Guid> _originalAttachmentsIds = [];
private string statusMessage = string.Empty;
+ /// <inheritdoc />
+ protected override void OnInitialized()
+ {
+ base.OnInitialized();
+
+ // Keep track of the original attachments.
+ _originalAttachmentsIds = Attachments.Select(a => a.Id).ToList();
+ }
+
private async Task HandleFileSelection(InputFileChangeEventArgs e)
{
statusMessage = "Uploading...";
@@ -91,7 +100,7 @@
try
{
// Check if the attachment was part of the original set.
- if (_originalAttachments.Contains(attachment))
+ if (_originalAttachmentsIds.Contains(attachment.Id))
{
// If it was part of the original set, we soft delete it.
attachment.IsDeleted = true;
diff --git a/src/AliasVault.Client/Services/Database/DbMergeUtility.cs b/src/AliasVault.Client/Services/Database/DbMergeUtility.cs
index 3d8d4f9b1..42266ce17 100644
--- a/src/AliasVault.Client/Services/Database/DbMergeUtility.cs
+++ b/src/AliasVault.Client/Services/Database/DbMergeUtility.cs
@@ -7,7 +7,7 @@
namespace AliasVault.Client.Services.Database;
-using System.Data;
+using System.Globalization;
using Microsoft.Data.Sqlite;
/// <summary>
@@ -49,33 +49,20 @@ public static class DbMergeUtility
baseCommand.CommandText = $"PRAGMA table_info({tableName})";
var columns = new List<string>();
- bool hasId = false;
- bool hasUpdatedAt = false;
- bool hasIsDeleted = false;
+ // Get column names from the base table.
using (var reader = await baseCommand.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
string columnName = reader.GetString(1);
columns.Add(columnName);
- if (columnName == "Id")
- {
- hasId = true;
- }
- else if (columnName == "UpdatedAt")
- {
- hasUpdatedAt = true;
- }
- else if (columnName == "IsDeleted")
- {
- hasIsDeleted = true;
- }
}
}
- // Check if the table has Id, UpdatedAt and IsDeleted columns.
- if (!hasId || !hasUpdatedAt || !hasIsDeleted)
+ // Check if the table has Id, UpdatedAt and IsDeleted columns which are required in order to merge.
+ // If columns are missing, skip the table.
+ if (!columns.Contains("Id") || !columns.Contains("UpdatedAt") || !columns.Contains("IsDeleted"))
{
return;
}
@@ -90,7 +77,6 @@ public static class DbMergeUtility
{
var id = sourceReader.GetValue(0);
var updatedAt = sourceReader.GetDateTime(columns.IndexOf("UpdatedAt"));
- var isDeleted = sourceReader.GetBoolean(columns.IndexOf("IsDeleted"));
// Check if the record exists in the base table.
baseCommand.CommandText = $"SELECT UpdatedAt FROM {tableName} WHERE Id = @Id";
@@ -105,25 +91,17 @@ public static class DbMergeUtility
Console.WriteLine($"Record exists in {tableName}.");
// Record exists, compare UpdatedAt if it exists.
- if (hasUpdatedAt)
+ Console.WriteLine($"Comparing UpdatedAt in {tableName}.");
+ Console.WriteLine($"UpdatedAt: {existingRecord}");
+ var baseUpdatedAt = DateTime.Parse((string)existingRecord, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
+ if (updatedAt > baseUpdatedAt)
{
- Console.WriteLine($"Comparing UpdatedAt in {tableName}.");
- Console.WriteLine($"UpdatedAt: {existingRecord}");
- var baseUpdatedAt = DateTime.Parse((string)existingRecord);
- if (updatedAt > baseUpdatedAt)
- {
- // Source record is newer, update the base record.
- await UpdateRecord(baseConnection, tableName, sourceReader, columns);
- }
- else
- {
- Console.WriteLine($"Base record is newer, skipping {tableName}.");
- }
+ // Source record is newer, update the base record.
+ await UpdateRecord(baseConnection, tableName, sourceReader, columns);
}
else
{
- // If UpdatedAt doesn't exist, always update.
- await UpdateRecord(baseConnection, tableName, sourceReader, columns);
+ Console.WriteLine($"Base record is newer, skipping {tableName}.");
}
}
else
diff --git a/src/AliasVault.Client/Services/Database/DbService.cs b/src/AliasVault.Client/Services/Database/DbService.cs
index c08e14c00..edb99e730 100644
--- a/src/AliasVault.Client/Services/Database/DbService.cs
+++ b/src/AliasVault.Client/Services/Database/DbService.cs
@@ -104,7 +104,7 @@ public sealed class DbService : IDisposable
try
{
var vaultsToMerge = await _httpClient.GetFromJsonAsync<VaultMergeResponse>($"api/v1/Vault/merge?currentRevisionNumber={_vaultRevisionNumber}");
- if (vaultsToMerge == null || !vaultsToMerge.Vaults.Any())
+ if (vaultsToMerge == null || vaultsToMerge.Vaults.Count == 0)
{
// No vaults to merge found, set error state.
_state.UpdateState(DbServiceState.DatabaseStatus.MergeFailed, "No vaults to merge found.");
@@ -121,7 +121,7 @@ public sealed class DbService : IDisposable
var decryptedBase64String = await _jsInteropService.SymmetricDecrypt(vault.Blob, _authService.GetEncryptionKeyAsBase64Async());
Console.WriteLine($"Decrypted vault {vault.UpdatedAt}.");
var connection = new SqliteConnection("Data Source=:memory:");
- connection.Open();
+ await connection.OpenAsync();
await ImportDbContextFromBase64Async(decryptedBase64String, connection);
sqlConnections.Add(connection);
}
@@ -171,8 +171,8 @@ public sealed class DbService : IDisposable
// Clean up other connections.
foreach (var connection in sqlConnections)
{
- connection.Close();
- connection.Dispose();
+ await connection.CloseAsync();
+ await connection.DisposeAsync();
}
await _dbContext.Database.MigrateAsync();
@@ -470,7 +470,7 @@ public sealed class DbService : IDisposable
/// </summary>
/// <param name="base64String">Base64 string representation of a .sqlite file.</param>
/// <param name="connection">The connection to the database that should be used for the import.</param>
- private async Task ImportDbContextFromBase64Async(string base64String, SqliteConnection connection)
+ private static async Task ImportDbContextFromBase64Async(string base64String, SqliteConnection connection)
{
var bytes = Convert.FromBase64String(base64String);
var tempFileName = Path.GetRandomFileName();