feat: Refactor note decryption method and update category handling for improved clarity
This commit is contained in:
@@ -44,7 +44,7 @@ class NoteEncryption {
|
||||
}
|
||||
|
||||
/// Desencripta el contenido de una nota usando el master key
|
||||
static Future<String> decryptNote(
|
||||
static Future<String> decrypt(
|
||||
String encodedBox,
|
||||
String masterKey,
|
||||
) async {
|
||||
|
||||
@@ -305,20 +305,19 @@ class NoteRepository {
|
||||
// Apply categories from server
|
||||
for (final SyncCategoryResponse catResponse
|
||||
in response.changes.categories) {
|
||||
final String plainName = await _plainCategoryName(
|
||||
catResponse.encryptedName,
|
||||
_masterKey,
|
||||
final Category category = await catResponse.toCategory(
|
||||
masterKey: _masterKey,
|
||||
);
|
||||
|
||||
await _database.upsertCategory(
|
||||
CategoriesCompanion(
|
||||
id: Value(catResponse.id),
|
||||
name: Value(plainName),
|
||||
serverVersion: Value(catResponse.serverVersion),
|
||||
isDeleted: Value(catResponse.isDeleted),
|
||||
colorValue: Value<int?>(catResponse.colorValue),
|
||||
iconCodePoint: Value<int?>(catResponse.iconCodePoint),
|
||||
updatedAt: Value(catResponse.updatedAt),
|
||||
id: Value(category.id),
|
||||
name: Value(category.name),
|
||||
serverVersion: Value(category.serverVersion),
|
||||
isDeleted: Value(category.isDeleted),
|
||||
colorValue: Value<int?>(category.colorValue),
|
||||
iconCodePoint: Value<int?>(category.iconCodePoint),
|
||||
updatedAt: Value(category.updatedAt),
|
||||
isDirty: const Value(false),
|
||||
),
|
||||
);
|
||||
@@ -479,16 +478,11 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
|
||||
final List<SyncCategoryPayload> payloads = [];
|
||||
|
||||
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(
|
||||
SyncCategoryPayload.fromCategory(
|
||||
await SyncCategoryPayload.fromCategory(
|
||||
Category(
|
||||
id: row.id,
|
||||
name: plainName,
|
||||
name: row.name,
|
||||
serverVersion: row.serverVersion,
|
||||
isDeleted: row.isDeleted,
|
||||
updatedAt: row.updatedAt,
|
||||
@@ -496,7 +490,7 @@ Future<List<SyncCategoryPayload>> _encryptCategories(
|
||||
colorValue: row.colorValue,
|
||||
iconCodePoint: row.iconCodePoint,
|
||||
),
|
||||
encryptedName: await NoteEncryption.encryptNote(plainName, masterKey),
|
||||
masterKey: masterKey,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -657,11 +651,11 @@ Future<List<Map<String, Object?>>> _decryptNoteBatch(
|
||||
String decryptedBody = 'Encrypted';
|
||||
if (!isPermanentlyDeleted) {
|
||||
try {
|
||||
decryptedTitle = await NoteEncryption.decryptNote(
|
||||
decryptedTitle = await NoteEncryption.decrypt(
|
||||
note['encryptedTitle']! as String,
|
||||
masterKey,
|
||||
);
|
||||
decryptedBody = await NoteEncryption.decryptNote(
|
||||
decryptedBody = await NoteEncryption.decrypt(
|
||||
note['encryptedBody']! as String,
|
||||
masterKey,
|
||||
);
|
||||
@@ -698,11 +692,3 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:notas/data/note_encryption.dart';
|
||||
import 'package:notas/models/note.dart';
|
||||
import 'package:notas/models/category.dart';
|
||||
import 'dart:convert';
|
||||
@@ -108,13 +109,13 @@ class SyncCategoryPayload {
|
||||
final int? iconCodePoint;
|
||||
final DateTime updatedAt;
|
||||
|
||||
factory SyncCategoryPayload.fromCategory(
|
||||
static Future<SyncCategoryPayload> fromCategory(
|
||||
Category category, {
|
||||
required String encryptedName,
|
||||
}) {
|
||||
required String masterKey,
|
||||
}) async {
|
||||
return SyncCategoryPayload(
|
||||
id: category.id,
|
||||
encryptedName: encryptedName,
|
||||
encryptedName: await NoteEncryption.encryptNote(category.name, masterKey),
|
||||
serverVersion: category.serverVersion,
|
||||
isDeleted: category.isDeleted,
|
||||
colorValue: category.colorValue,
|
||||
@@ -249,10 +250,10 @@ class SyncCategoryResponse {
|
||||
);
|
||||
}
|
||||
|
||||
Category toCategory({required String name}) {
|
||||
Future<Category> toCategory({required String masterKey}) async {
|
||||
return Category(
|
||||
id: id,
|
||||
name: name,
|
||||
name: await NoteEncryption.decrypt(encryptedName, masterKey),
|
||||
serverVersion: serverVersion,
|
||||
isDeleted: isDeleted,
|
||||
colorValue: colorValue,
|
||||
|
||||
@@ -15,7 +15,7 @@ class CategoryStyle {
|
||||
];
|
||||
|
||||
static const List<IconData> icons = <IconData>[
|
||||
Icons.folder,
|
||||
Icons.label_outline_rounded,
|
||||
Icons.work,
|
||||
Icons.star,
|
||||
Icons.home,
|
||||
|
||||
Reference in New Issue
Block a user