feat: Refactor note decryption method and update category handling for improved clarity

This commit is contained in:
2026-05-22 10:11:41 +02:00
parent 27e1199178
commit e0f226d3bc
4 changed files with 23 additions and 36 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ class NoteEncryption {
} }
/// Desencripta el contenido de una nota usando el master key /// Desencripta el contenido de una nota usando el master key
static Future<String> decryptNote( static Future<String> decrypt(
String encodedBox, String encodedBox,
String masterKey, String masterKey,
) async { ) async {
+14 -28
View File
@@ -305,20 +305,19 @@ class NoteRepository {
// Apply categories from server // Apply categories from server
for (final SyncCategoryResponse catResponse for (final SyncCategoryResponse catResponse
in response.changes.categories) { in response.changes.categories) {
final String plainName = await _plainCategoryName( final Category category = await catResponse.toCategory(
catResponse.encryptedName, masterKey: _masterKey,
_masterKey,
); );
await _database.upsertCategory( await _database.upsertCategory(
CategoriesCompanion( CategoriesCompanion(
id: Value(catResponse.id), id: Value(category.id),
name: Value(plainName), name: Value(category.name),
serverVersion: Value(catResponse.serverVersion), serverVersion: Value(category.serverVersion),
isDeleted: Value(catResponse.isDeleted), isDeleted: Value(category.isDeleted),
colorValue: Value<int?>(catResponse.colorValue), colorValue: Value<int?>(category.colorValue),
iconCodePoint: Value<int?>(catResponse.iconCodePoint), iconCodePoint: Value<int?>(category.iconCodePoint),
updatedAt: Value(catResponse.updatedAt), updatedAt: Value(category.updatedAt),
isDirty: const Value(false), isDirty: const Value(false),
), ),
); );
@@ -479,16 +478,11 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
final List<SyncCategoryPayload> payloads = []; final List<SyncCategoryPayload> payloads = [];
for (final DbCategory row in categories) { for (final DbCategory row in categories) {
// 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 plainName = row.name;
payloads.add( payloads.add(
SyncCategoryPayload.fromCategory( await SyncCategoryPayload.fromCategory(
Category( Category(
id: row.id, id: row.id,
name: plainName, name: row.name,
serverVersion: row.serverVersion, serverVersion: row.serverVersion,
isDeleted: row.isDeleted, isDeleted: row.isDeleted,
updatedAt: row.updatedAt, updatedAt: row.updatedAt,
@@ -496,7 +490,7 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
colorValue: row.colorValue, colorValue: row.colorValue,
iconCodePoint: row.iconCodePoint, iconCodePoint: row.iconCodePoint,
), ),
encryptedName: await NoteEncryption.encryptNote(plainName, masterKey), masterKey: masterKey,
), ),
); );
} }
@@ -657,11 +651,11 @@ Future<List<Map<String, Object?>>> _decryptNoteBatch(
String decryptedBody = 'Encrypted'; String decryptedBody = 'Encrypted';
if (!isPermanentlyDeleted) { if (!isPermanentlyDeleted) {
try { try {
decryptedTitle = await NoteEncryption.decryptNote( decryptedTitle = await NoteEncryption.decrypt(
note['encryptedTitle']! as String, note['encryptedTitle']! as String,
masterKey, masterKey,
); );
decryptedBody = await NoteEncryption.decryptNote( decryptedBody = await NoteEncryption.decrypt(
note['encryptedBody']! as String, note['encryptedBody']! as String,
masterKey, masterKey,
); );
@@ -698,11 +692,3 @@ int _parallelWorkerCount(int itemCount) {
); );
return math.max(1, math.min(itemCount, cappedByCpu)); 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;
}
}
+7 -6
View File
@@ -1,3 +1,4 @@
import 'package:notas/data/note_encryption.dart';
import 'package:notas/models/note.dart'; import 'package:notas/models/note.dart';
import 'package:notas/models/category.dart'; import 'package:notas/models/category.dart';
import 'dart:convert'; import 'dart:convert';
@@ -108,13 +109,13 @@ class SyncCategoryPayload {
final int? iconCodePoint; final int? iconCodePoint;
final DateTime updatedAt; final DateTime updatedAt;
factory SyncCategoryPayload.fromCategory( static Future<SyncCategoryPayload> fromCategory(
Category category, { Category category, {
required String encryptedName, required String masterKey,
}) { }) async {
return SyncCategoryPayload( return SyncCategoryPayload(
id: category.id, id: category.id,
encryptedName: encryptedName, encryptedName: await NoteEncryption.encryptNote(category.name, masterKey),
serverVersion: category.serverVersion, serverVersion: category.serverVersion,
isDeleted: category.isDeleted, isDeleted: category.isDeleted,
colorValue: category.colorValue, colorValue: category.colorValue,
@@ -249,10 +250,10 @@ class SyncCategoryResponse {
); );
} }
Category toCategory({required String name}) { Future<Category> toCategory({required String masterKey}) async {
return Category( return Category(
id: id, id: id,
name: name, name: await NoteEncryption.decrypt(encryptedName, masterKey),
serverVersion: serverVersion, serverVersion: serverVersion,
isDeleted: isDeleted, isDeleted: isDeleted,
colorValue: colorValue, colorValue: colorValue,
+1 -1
View File
@@ -15,7 +15,7 @@ class CategoryStyle {
]; ];
static const List<IconData> icons = <IconData>[ static const List<IconData> icons = <IconData>[
Icons.folder, Icons.label_outline_rounded,
Icons.work, Icons.work,
Icons.star, Icons.star,
Icons.home, Icons.home,