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
+1 -1
View File
@@ -10,7 +10,7 @@ part 'app_database.g.dart';
@DataClassName('DbCategory')
class Categories extends Table {
TextColumn get id => text()();
TextColumn get encryptedName => text().named('encrypted_name')();
TextColumn get name => text().named('name')();
IntColumn get serverVersion =>
integer().named('server_version').withDefault(const Constant(0))();
BoolColumn get isDeleted =>
+148 -105
View File
@@ -629,12 +629,10 @@ class $CategoriesTable extends Categories
type: DriftSqlType.string,
requiredDuringInsert: true,
);
static const VerificationMeta _encryptedNameMeta = const VerificationMeta(
'encryptedName',
);
static const VerificationMeta _nameMeta = const VerificationMeta('name');
@override
late final GeneratedColumn<String> encryptedName = GeneratedColumn<String>(
'encrypted_name',
late final GeneratedColumn<String> name = GeneratedColumn<String>(
'name',
aliasedName,
false,
type: DriftSqlType.string,
@@ -667,21 +665,6 @@ class $CategoriesTable extends Categories
),
defaultValue: const Constant(false),
);
static const VerificationMeta _isDirtyMeta = const VerificationMeta(
'isDirty',
);
@override
late final GeneratedColumn<bool> isDirty = GeneratedColumn<bool>(
'is_dirty',
aliasedName,
false,
type: DriftSqlType.bool,
requiredDuringInsert: false,
defaultConstraints: GeneratedColumn.constraintIsAlways(
'CHECK ("is_dirty" IN (0, 1))',
),
defaultValue: const Constant(true),
);
static const VerificationMeta _colorValueMeta = const VerificationMeta(
'colorValue',
);
@@ -704,6 +687,21 @@ class $CategoriesTable extends Categories
type: DriftSqlType.int,
requiredDuringInsert: false,
);
static const VerificationMeta _isDirtyMeta = const VerificationMeta(
'isDirty',
);
@override
late final GeneratedColumn<bool> isDirty = GeneratedColumn<bool>(
'is_dirty',
aliasedName,
false,
type: DriftSqlType.bool,
requiredDuringInsert: false,
defaultConstraints: GeneratedColumn.constraintIsAlways(
'CHECK ("is_dirty" IN (0, 1))',
),
defaultValue: const Constant(true),
);
static const VerificationMeta _updatedAtMeta = const VerificationMeta(
'updatedAt',
);
@@ -718,12 +716,12 @@ class $CategoriesTable extends Categories
@override
List<GeneratedColumn> get $columns => [
id,
encryptedName,
name,
serverVersion,
isDeleted,
isDirty,
colorValue,
iconCodePoint,
isDirty,
updatedAt,
];
@override
@@ -743,16 +741,13 @@ class $CategoriesTable extends Categories
} else if (isInserting) {
context.missing(_idMeta);
}
if (data.containsKey('encrypted_name')) {
if (data.containsKey('name')) {
context.handle(
_encryptedNameMeta,
encryptedName.isAcceptableOrUnknown(
data['encrypted_name']!,
_encryptedNameMeta,
),
_nameMeta,
name.isAcceptableOrUnknown(data['name']!, _nameMeta),
);
} else if (isInserting) {
context.missing(_encryptedNameMeta);
context.missing(_nameMeta);
}
if (data.containsKey('server_version')) {
context.handle(
@@ -769,12 +764,6 @@ class $CategoriesTable extends Categories
isDeleted.isAcceptableOrUnknown(data['is_deleted']!, _isDeletedMeta),
);
}
if (data.containsKey('is_dirty')) {
context.handle(
_isDirtyMeta,
isDirty.isAcceptableOrUnknown(data['is_dirty']!, _isDirtyMeta),
);
}
if (data.containsKey('color_value')) {
context.handle(
_colorValueMeta,
@@ -790,6 +779,12 @@ class $CategoriesTable extends Categories
),
);
}
if (data.containsKey('is_dirty')) {
context.handle(
_isDirtyMeta,
isDirty.isAcceptableOrUnknown(data['is_dirty']!, _isDirtyMeta),
);
}
if (data.containsKey('updated_at')) {
context.handle(
_updatedAtMeta,
@@ -811,9 +806,9 @@ class $CategoriesTable extends Categories
DriftSqlType.string,
data['${effectivePrefix}id'],
)!,
encryptedName: attachedDatabase.typeMapping.read(
name: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}encrypted_name'],
data['${effectivePrefix}name'],
)!,
serverVersion: attachedDatabase.typeMapping.read(
DriftSqlType.int,
@@ -823,10 +818,6 @@ class $CategoriesTable extends Categories
DriftSqlType.bool,
data['${effectivePrefix}is_deleted'],
)!,
isDirty: attachedDatabase.typeMapping.read(
DriftSqlType.bool,
data['${effectivePrefix}is_dirty'],
)!,
colorValue: attachedDatabase.typeMapping.read(
DriftSqlType.int,
data['${effectivePrefix}color_value'],
@@ -835,6 +826,10 @@ class $CategoriesTable extends Categories
DriftSqlType.int,
data['${effectivePrefix}icon_code_point'],
),
isDirty: attachedDatabase.typeMapping.read(
DriftSqlType.bool,
data['${effectivePrefix}is_dirty'],
)!,
updatedAt: attachedDatabase.typeMapping.read(
DriftSqlType.dateTime,
data['${effectivePrefix}updated_at'],
@@ -850,37 +845,37 @@ class $CategoriesTable extends Categories
class DbCategory extends DataClass implements Insertable<DbCategory> {
final String id;
final String encryptedName;
final String name;
final int serverVersion;
final bool isDeleted;
final bool isDirty;
final int? colorValue;
final int? iconCodePoint;
final bool isDirty;
final DateTime updatedAt;
const DbCategory({
required this.id,
required this.encryptedName,
required this.name,
required this.serverVersion,
required this.isDeleted,
required this.isDirty,
this.colorValue,
this.iconCodePoint,
required this.isDirty,
required this.updatedAt,
});
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
map['id'] = Variable<String>(id);
map['encrypted_name'] = Variable<String>(encryptedName);
map['name'] = Variable<String>(name);
map['server_version'] = Variable<int>(serverVersion);
map['is_deleted'] = Variable<bool>(isDeleted);
map['is_dirty'] = Variable<bool>(isDirty);
if (!nullToAbsent || colorValue != null) {
map['color_value'] = Variable<int>(colorValue);
}
if (!nullToAbsent || iconCodePoint != null) {
map['icon_code_point'] = Variable<int>(iconCodePoint);
}
map['is_dirty'] = Variable<bool>(isDirty);
map['updated_at'] = Variable<DateTime>(updatedAt);
return map;
}
@@ -888,16 +883,16 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
CategoriesCompanion toCompanion(bool nullToAbsent) {
return CategoriesCompanion(
id: Value(id),
encryptedName: Value(encryptedName),
name: Value(name),
serverVersion: Value(serverVersion),
isDeleted: Value(isDeleted),
isDirty: Value(isDirty),
colorValue: colorValue == null && nullToAbsent
? const Value.absent()
: Value<int?>(colorValue),
: Value(colorValue),
iconCodePoint: iconCodePoint == null && nullToAbsent
? const Value.absent()
: Value<int?>(iconCodePoint),
: Value(iconCodePoint),
isDirty: Value(isDirty),
updatedAt: Value(updatedAt),
);
}
@@ -909,12 +904,12 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
serializer ??= driftRuntimeOptions.defaultSerializer;
return DbCategory(
id: serializer.fromJson<String>(json['id']),
encryptedName: serializer.fromJson<String>(json['encryptedName']),
name: serializer.fromJson<String>(json['name']),
serverVersion: serializer.fromJson<int>(json['serverVersion']),
isDeleted: serializer.fromJson<bool>(json['isDeleted']),
isDirty: serializer.fromJson<bool>(json['isDirty']),
colorValue: serializer.fromJson<int?>(json['colorValue']),
iconCodePoint: serializer.fromJson<int?>(json['iconCodePoint']),
isDirty: serializer.fromJson<bool>(json['isDirty']),
updatedAt: serializer.fromJson<DateTime>(json['updatedAt']),
);
}
@@ -923,52 +918,52 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<String>(id),
'encryptedName': serializer.toJson<String>(encryptedName),
'name': serializer.toJson<String>(name),
'serverVersion': serializer.toJson<int>(serverVersion),
'isDeleted': serializer.toJson<bool>(isDeleted),
'isDirty': serializer.toJson<bool>(isDirty),
'colorValue': serializer.toJson<int?>(colorValue),
'iconCodePoint': serializer.toJson<int?>(iconCodePoint),
'isDirty': serializer.toJson<bool>(isDirty),
'updatedAt': serializer.toJson<DateTime>(updatedAt),
};
}
DbCategory copyWith({
String? id,
String? encryptedName,
String? name,
int? serverVersion,
bool? isDeleted,
Value<int?> colorValue = const Value.absent(),
Value<int?> iconCodePoint = const Value.absent(),
bool? isDirty,
int? colorValue,
int? iconCodePoint,
DateTime? updatedAt,
}) => DbCategory(
id: id ?? this.id,
encryptedName: encryptedName ?? this.encryptedName,
name: name ?? this.name,
serverVersion: serverVersion ?? this.serverVersion,
isDeleted: isDeleted ?? this.isDeleted,
colorValue: colorValue.present ? colorValue.value : this.colorValue,
iconCodePoint: iconCodePoint.present
? iconCodePoint.value
: this.iconCodePoint,
isDirty: isDirty ?? this.isDirty,
colorValue: colorValue ?? this.colorValue,
iconCodePoint: iconCodePoint ?? this.iconCodePoint,
updatedAt: updatedAt ?? this.updatedAt,
);
DbCategory copyWithCompanion(CategoriesCompanion data) {
return DbCategory(
id: data.id.present ? data.id.value : this.id,
encryptedName: data.encryptedName.present
? data.encryptedName.value
: this.encryptedName,
name: data.name.present ? data.name.value : this.name,
serverVersion: data.serverVersion.present
? data.serverVersion.value
: this.serverVersion,
isDeleted: data.isDeleted.present ? data.isDeleted.value : this.isDeleted,
isDirty: data.isDirty.present ? data.isDirty.value : this.isDirty,
colorValue: data.colorValue.present
? data.colorValue.value
: this.colorValue,
iconCodePoint: data.iconCodePoint.present
? data.iconCodePoint.value
: this.iconCodePoint,
isDirty: data.isDirty.present ? data.isDirty.value : this.isDirty,
updatedAt: data.updatedAt.present ? data.updatedAt.value : this.updatedAt,
);
}
@@ -977,9 +972,11 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
String toString() {
return (StringBuffer('DbCategory(')
..write('id: $id, ')
..write('encryptedName: $encryptedName, ')
..write('name: $name, ')
..write('serverVersion: $serverVersion, ')
..write('isDeleted: $isDeleted, ')
..write('colorValue: $colorValue, ')
..write('iconCodePoint: $iconCodePoint, ')
..write('isDirty: $isDirty, ')
..write('updatedAt: $updatedAt')
..write(')'))
@@ -989,9 +986,11 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
@override
int get hashCode => Object.hash(
id,
encryptedName,
name,
serverVersion,
isDeleted,
colorValue,
iconCodePoint,
isDirty,
updatedAt,
);
@@ -1000,66 +999,68 @@ class DbCategory extends DataClass implements Insertable<DbCategory> {
identical(this, other) ||
(other is DbCategory &&
other.id == this.id &&
other.encryptedName == this.encryptedName &&
other.name == this.name &&
other.serverVersion == this.serverVersion &&
other.isDeleted == this.isDeleted &&
other.colorValue == this.colorValue &&
other.iconCodePoint == this.iconCodePoint &&
other.isDirty == this.isDirty &&
other.updatedAt == this.updatedAt);
}
class CategoriesCompanion extends UpdateCompanion<DbCategory> {
final Value<String> id;
final Value<String> encryptedName;
final Value<String> name;
final Value<int> serverVersion;
final Value<bool> isDeleted;
final Value<int?> colorValue;
final Value<int?> iconCodePoint;
final Value<bool> isDirty;
final Value<DateTime> updatedAt;
final Value<int> rowid;
final Value<int?> colorValue;
final Value<int?> iconCodePoint;
const CategoriesCompanion({
this.id = const Value.absent(),
this.encryptedName = const Value.absent(),
this.name = const Value.absent(),
this.serverVersion = const Value.absent(),
this.isDeleted = const Value.absent(),
this.isDirty = const Value.absent(),
this.colorValue = const Value.absent(),
this.iconCodePoint = const Value.absent(),
this.isDirty = const Value.absent(),
this.updatedAt = const Value.absent(),
this.rowid = const Value.absent(),
});
CategoriesCompanion.insert({
required String id,
required String encryptedName,
required String name,
this.serverVersion = const Value.absent(),
this.isDeleted = const Value.absent(),
this.isDirty = const Value.absent(),
this.colorValue = const Value.absent(),
this.iconCodePoint = const Value.absent(),
this.isDirty = const Value.absent(),
required DateTime updatedAt,
this.rowid = const Value.absent(),
}) : id = Value(id),
encryptedName = Value(encryptedName),
name = Value(name),
updatedAt = Value(updatedAt);
static Insertable<DbCategory> custom({
Expression<String>? id,
Expression<String>? encryptedName,
Expression<String>? name,
Expression<int>? serverVersion,
Expression<bool>? isDeleted,
Expression<bool>? isDirty,
Expression<int>? colorValue,
Expression<int>? iconCodePoint,
Expression<bool>? isDirty,
Expression<DateTime>? updatedAt,
Expression<int>? rowid,
}) {
return RawValuesInsertable({
if (id != null) 'id': id,
if (encryptedName != null) 'encrypted_name': encryptedName,
if (name != null) 'name': name,
if (serverVersion != null) 'server_version': serverVersion,
if (isDeleted != null) 'is_deleted': isDeleted,
if (isDirty != null) 'is_dirty': isDirty,
if (colorValue != null) 'color_value': colorValue,
if (iconCodePoint != null) 'icon_code_point': iconCodePoint,
if (isDirty != null) 'is_dirty': isDirty,
if (updatedAt != null) 'updated_at': updatedAt,
if (rowid != null) 'rowid': rowid,
});
@@ -1067,23 +1068,23 @@ class CategoriesCompanion extends UpdateCompanion<DbCategory> {
CategoriesCompanion copyWith({
Value<String>? id,
Value<String>? encryptedName,
Value<String>? name,
Value<int>? serverVersion,
Value<bool>? isDeleted,
Value<int?>? colorValue,
Value<int?>? iconCodePoint,
Value<bool>? isDirty,
Value<int>? colorValue,
Value<int>? iconCodePoint,
Value<DateTime>? updatedAt,
Value<int>? rowid,
}) {
return CategoriesCompanion(
id: id ?? this.id,
encryptedName: encryptedName ?? this.encryptedName,
name: name ?? this.name,
serverVersion: serverVersion ?? this.serverVersion,
isDeleted: isDeleted ?? this.isDeleted,
isDirty: isDirty ?? this.isDirty,
colorValue: colorValue ?? this.colorValue,
iconCodePoint: iconCodePoint ?? this.iconCodePoint,
isDirty: isDirty ?? this.isDirty,
updatedAt: updatedAt ?? this.updatedAt,
rowid: rowid ?? this.rowid,
);
@@ -1095,8 +1096,8 @@ class CategoriesCompanion extends UpdateCompanion<DbCategory> {
if (id.present) {
map['id'] = Variable<String>(id.value);
}
if (encryptedName.present) {
map['encrypted_name'] = Variable<String>(encryptedName.value);
if (name.present) {
map['name'] = Variable<String>(name.value);
}
if (serverVersion.present) {
map['server_version'] = Variable<int>(serverVersion.value);
@@ -1104,15 +1105,15 @@ class CategoriesCompanion extends UpdateCompanion<DbCategory> {
if (isDeleted.present) {
map['is_deleted'] = Variable<bool>(isDeleted.value);
}
if (isDirty.present) {
map['is_dirty'] = Variable<bool>(isDirty.value);
}
if (colorValue.present) {
map['color_value'] = Variable<int>(colorValue.value);
}
if (iconCodePoint.present) {
map['icon_code_point'] = Variable<int>(iconCodePoint.value);
}
if (isDirty.present) {
map['is_dirty'] = Variable<bool>(isDirty.value);
}
if (updatedAt.present) {
map['updated_at'] = Variable<DateTime>(updatedAt.value);
}
@@ -1126,9 +1127,11 @@ class CategoriesCompanion extends UpdateCompanion<DbCategory> {
String toString() {
return (StringBuffer('CategoriesCompanion(')
..write('id: $id, ')
..write('encryptedName: $encryptedName, ')
..write('name: $name, ')
..write('serverVersion: $serverVersion, ')
..write('isDeleted: $isDeleted, ')
..write('colorValue: $colorValue, ')
..write('iconCodePoint: $iconCodePoint, ')
..write('isDirty: $isDirty, ')
..write('updatedAt: $updatedAt, ')
..write('rowid: $rowid')
@@ -1444,9 +1447,11 @@ typedef $$NotesTableProcessedTableManager =
typedef $$CategoriesTableCreateCompanionBuilder =
CategoriesCompanion Function({
required String id,
required String encryptedName,
required String name,
Value<int> serverVersion,
Value<bool> isDeleted,
Value<int?> colorValue,
Value<int?> iconCodePoint,
Value<bool> isDirty,
required DateTime updatedAt,
Value<int> rowid,
@@ -1454,9 +1459,11 @@ typedef $$CategoriesTableCreateCompanionBuilder =
typedef $$CategoriesTableUpdateCompanionBuilder =
CategoriesCompanion Function({
Value<String> id,
Value<String> encryptedName,
Value<String> name,
Value<int> serverVersion,
Value<bool> isDeleted,
Value<int?> colorValue,
Value<int?> iconCodePoint,
Value<bool> isDirty,
Value<DateTime> updatedAt,
Value<int> rowid,
@@ -1476,8 +1483,8 @@ class $$CategoriesTableFilterComposer
builder: (column) => ColumnFilters(column),
);
ColumnFilters<String> get encryptedName => $composableBuilder(
column: $table.encryptedName,
ColumnFilters<String> get name => $composableBuilder(
column: $table.name,
builder: (column) => ColumnFilters(column),
);
@@ -1491,6 +1498,16 @@ class $$CategoriesTableFilterComposer
builder: (column) => ColumnFilters(column),
);
ColumnFilters<int> get colorValue => $composableBuilder(
column: $table.colorValue,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<int> get iconCodePoint => $composableBuilder(
column: $table.iconCodePoint,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<bool> get isDirty => $composableBuilder(
column: $table.isDirty,
builder: (column) => ColumnFilters(column),
@@ -1516,8 +1533,8 @@ class $$CategoriesTableOrderingComposer
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<String> get encryptedName => $composableBuilder(
column: $table.encryptedName,
ColumnOrderings<String> get name => $composableBuilder(
column: $table.name,
builder: (column) => ColumnOrderings(column),
);
@@ -1531,6 +1548,16 @@ class $$CategoriesTableOrderingComposer
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<int> get colorValue => $composableBuilder(
column: $table.colorValue,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<int> get iconCodePoint => $composableBuilder(
column: $table.iconCodePoint,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<bool> get isDirty => $composableBuilder(
column: $table.isDirty,
builder: (column) => ColumnOrderings(column),
@@ -1554,10 +1581,8 @@ class $$CategoriesTableAnnotationComposer
GeneratedColumn<String> get id =>
$composableBuilder(column: $table.id, builder: (column) => column);
GeneratedColumn<String> get encryptedName => $composableBuilder(
column: $table.encryptedName,
builder: (column) => column,
);
GeneratedColumn<String> get name =>
$composableBuilder(column: $table.name, builder: (column) => column);
GeneratedColumn<int> get serverVersion => $composableBuilder(
column: $table.serverVersion,
@@ -1567,6 +1592,16 @@ class $$CategoriesTableAnnotationComposer
GeneratedColumn<bool> get isDeleted =>
$composableBuilder(column: $table.isDeleted, builder: (column) => column);
GeneratedColumn<int> get colorValue => $composableBuilder(
column: $table.colorValue,
builder: (column) => column,
);
GeneratedColumn<int> get iconCodePoint => $composableBuilder(
column: $table.iconCodePoint,
builder: (column) => column,
);
GeneratedColumn<bool> get isDirty =>
$composableBuilder(column: $table.isDirty, builder: (column) => column);
@@ -1606,17 +1641,21 @@ class $$CategoriesTableTableManager
updateCompanionCallback:
({
Value<String> id = const Value.absent(),
Value<String> encryptedName = const Value.absent(),
Value<String> name = const Value.absent(),
Value<int> serverVersion = const Value.absent(),
Value<bool> isDeleted = const Value.absent(),
Value<int?> colorValue = const Value.absent(),
Value<int?> iconCodePoint = const Value.absent(),
Value<bool> isDirty = const Value.absent(),
Value<DateTime> updatedAt = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => CategoriesCompanion(
id: id,
encryptedName: encryptedName,
name: name,
serverVersion: serverVersion,
isDeleted: isDeleted,
colorValue: colorValue,
iconCodePoint: iconCodePoint,
isDirty: isDirty,
updatedAt: updatedAt,
rowid: rowid,
@@ -1624,17 +1663,21 @@ class $$CategoriesTableTableManager
createCompanionCallback:
({
required String id,
required String encryptedName,
required String name,
Value<int> serverVersion = const Value.absent(),
Value<bool> isDeleted = const Value.absent(),
Value<int?> colorValue = const Value.absent(),
Value<int?> iconCodePoint = const Value.absent(),
Value<bool> isDirty = const Value.absent(),
required DateTime updatedAt,
Value<int> rowid = const Value.absent(),
}) => CategoriesCompanion.insert(
id: id,
encryptedName: encryptedName,
name: name,
serverVersion: serverVersion,
isDeleted: isDeleted,
colorValue: colorValue,
iconCodePoint: iconCodePoint,
isDirty: isDirty,
updatedAt: updatedAt,
rowid: rowid,
+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;
}
}