feat: Rename encryptedName to name in Categories table and update related logic

This commit is contained in:
2026-05-22 09:27:20 +02:00
parent f595f33f4a
commit 27e1199178
4 changed files with 245 additions and 150 deletions
+31 -34
View File
@@ -39,41 +39,29 @@ class NoteRepository {
final List<DbCategory> dbCategories = await _database.getAllCategories();
final List<Category> categories = [];
for (final DbCategory row in dbCategories) {
try {
final String decryptedName = await NoteEncryption.decryptNote(
row.encryptedName,
_masterKey,
);
categories.add(
Category(
id: row.id,
name: decryptedName,
serverVersion: row.serverVersion,
isDeleted: row.isDeleted,
updatedAt: row.updatedAt,
isDirty: row.isDirty,
colorValue: row.colorValue,
iconCodePoint: row.iconCodePoint,
),
);
} catch (e) {
debugPrint('Error al desencriptar categoría: $e');
}
categories.add(
Category(
id: row.id,
name: row.name,
serverVersion: row.serverVersion,
isDeleted: row.isDeleted,
updatedAt: row.updatedAt,
isDirty: row.isDirty,
colorValue: row.colorValue,
iconCodePoint: row.iconCodePoint,
),
);
}
return categories;
}
Future<void> createCategory(Category category) async {
debugPrint('createCategory called with: ${category.name}');
final String encryptedName = await NoteEncryption.encryptNote(
category.name,
_masterKey,
);
debugPrint('Category name encrypted');
await _database.upsertCategory(
CategoriesCompanion.insert(
id: category.id,
encryptedName: encryptedName,
name: category.name,
updatedAt: category.updatedAt,
serverVersion: const Value(0),
isDeleted: const Value(false),
@@ -317,14 +305,15 @@ class NoteRepository {
// Apply categories from server
for (final SyncCategoryResponse catResponse
in response.changes.categories) {
// Store the encrypted blob received from the server directly in the DB.
// Decryption is only performed when loading categories for display.
final String encryptedBlob = catResponse.encryptedName;
final String plainName = await _plainCategoryName(
catResponse.encryptedName,
_masterKey,
);
await _database.upsertCategory(
CategoriesCompanion(
id: Value(catResponse.id),
encryptedName: Value(encryptedBlob),
name: Value(plainName),
serverVersion: Value(catResponse.serverVersion),
isDeleted: Value(catResponse.isDeleted),
colorValue: Value<int?>(catResponse.colorValue),
@@ -490,16 +479,16 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
final List<SyncCategoryPayload> payloads = [];
for (final DbCategory row in categories) {
// The DB already stores the encrypted name blob in `encryptedName`.
// The DB stores the plain category name locally.
// Use it directly when building the sync payload and preserve
// color/icon values from the DB row so they are sent to the server.
final String encryptedName = row.encryptedName;
final String plainName = row.name;
payloads.add(
SyncCategoryPayload.fromCategory(
Category(
id: row.id,
name: row.encryptedName,
name: plainName,
serverVersion: row.serverVersion,
isDeleted: row.isDeleted,
updatedAt: row.updatedAt,
@@ -507,7 +496,7 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
colorValue: row.colorValue,
iconCodePoint: row.iconCodePoint,
),
encryptedName: encryptedName,
encryptedName: await NoteEncryption.encryptNote(plainName, masterKey),
),
);
}
@@ -709,3 +698,11 @@ int _parallelWorkerCount(int itemCount) {
);
return math.max(1, math.min(itemCount, cappedByCpu));
}
Future<String> _plainCategoryName(String storedName, String masterKey) async {
try {
return await NoteEncryption.decryptNote(storedName, masterKey);
} catch (_) {
return storedName;
}
}