[mob] Switch to person_v2 where data is gzipped

This commit is contained in:
Neeraj Gupta
2024-08-14 12:27:36 +05:30
parent d7ffb3c7e0
commit 9f96ef8d83
3 changed files with 52 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ import "package:flutter/foundation.dart";
enum EntityType {
location,
person,
personV2,
unknown,
}
@@ -12,18 +13,29 @@ EntityType typeFromString(String type) {
return EntityType.location;
case "person":
return EntityType.location;
case "person_v2":
return EntityType.personV2;
}
debugPrint("unexpected collection type $type");
return EntityType.unknown;
}
extension EntityTypeExtn on EntityType {
bool isZipped() {
if (this == EntityType.location || this == EntityType.person) {
return false;
}
return true;
}
String typeToString() {
switch (this) {
case EntityType.location:
return "location";
case EntityType.person:
return "person";
case EntityType.personV2:
return "person_v2";
case EntityType.unknown:
return "unknown";
}

View File

@@ -14,6 +14,7 @@ import "package:photos/models/api/entity/key.dart";
import "package:photos/models/api/entity/type.dart";
import "package:photos/models/local_entity_data.dart";
import "package:photos/utils/crypto_util.dart";
import "package:photos/utils/gzip.dart";
import 'package:shared_preferences/shared_preferences.dart';
class EntityService {
@@ -61,11 +62,18 @@ class EntityService {
}) async {
final String plainText = jsonEncode(jsonMap);
final key = await getOrCreateEntityKey(type);
final encryptedKeyData =
await CryptoUtil.encryptChaCha(utf8.encode(plainText), key);
final String encryptedData =
CryptoUtil.bin2base64(encryptedKeyData.encryptedData!);
final String header = CryptoUtil.bin2base64(encryptedKeyData.header!);
late String encryptedData, header;
if (type.isZipped()) {
final ChaChaEncryptionResult result =
await gzipAndEncryptJson(jsonMap, key);
encryptedData = result.encData;
header = result.header;
} else {
final encryptedKeyData =
await CryptoUtil.encryptChaCha(utf8.encode(plainText), key);
encryptedData = CryptoUtil.bin2base64(encryptedKeyData.encryptedData!);
header = CryptoUtil.bin2base64(encryptedKeyData.header!);
}
debugPrint(
" ${id == null ? 'Adding' : 'Updating'} entity of type: " +
type.typeToString(),
@@ -93,7 +101,7 @@ class EntityService {
Future<void> syncEntities() async {
try {
await _remoteToLocalSync(EntityType.location);
await _remoteToLocalSync(EntityType.person);
await _remoteToLocalSync(EntityType.personV2);
} catch (e) {
_logger.severe("Failed to sync entities", e);
}
@@ -126,12 +134,22 @@ class EntityService {
final List<LocalEntityData> entities = [];
for (EntityData e in result) {
try {
final decryptedValue = await CryptoUtil.decryptChaCha(
CryptoUtil.base642bin(e.encryptedData!),
entityKey,
CryptoUtil.base642bin(e.header!),
);
final String plainText = utf8.decode(decryptedValue);
late String plainText;
if (type.isZipped()) {
final jsonMap = await decryptAndUnzipJson(
entityKey,
encryptedData: e.encryptedData!,
header: e.header!,
);
plainText = jsonEncode(jsonMap);
} else {
final Uint8List decryptedValue = await CryptoUtil.decryptChaCha(
CryptoUtil.base642bin(e.encryptedData!),
entityKey,
CryptoUtil.base642bin(e.header!),
);
plainText = utf8.decode(decryptedValue);
}
entities.add(
LocalEntityData(
id: e.id,

View File

@@ -37,7 +37,7 @@ class PersonService {
}
Future<List<PersonEntity>> getPersons() async {
final entities = await entityService.getEntities(EntityType.person);
final entities = await entityService.getEntities(EntityType.personV2);
return entities
.map(
(e) => PersonEntity(e.id, PersonData.fromJson(json.decode(e.data))),
@@ -46,7 +46,7 @@ class PersonService {
}
Future<PersonEntity?> getPerson(String id) {
return entityService.getEntity(EntityType.person, id).then((e) {
return entityService.getEntity(EntityType.personV2, id).then((e) {
if (e == null) {
return null;
}
@@ -55,7 +55,7 @@ class PersonService {
}
Future<Map<String, PersonEntity>> getPersonsMap() async {
final entities = await entityService.getEntities(EntityType.person);
final entities = await entityService.getEntities(EntityType.personV2);
final Map<String, PersonEntity> map = {};
for (var e in entities) {
final person =
@@ -95,7 +95,7 @@ class PersonService {
)
.toList();
entityService
.addOrUpdate(EntityType.person, personData.toJson(), id: personID)
.addOrUpdate(EntityType.personV2, personData.toJson(), id: personID)
.ignore();
personData.logStats();
}
@@ -163,7 +163,7 @@ class PersonService {
isHidden: isHidden,
);
final result = await entityService.addOrUpdate(
EntityType.person,
EntityType.personV2,
data.toJson(),
);
await faceMLDataDB.assignClusterToPerson(
@@ -181,7 +181,7 @@ class PersonService {
final personData = person.data;
personData.assigned!.removeWhere((element) => element.id != clusterID);
await entityService.addOrUpdate(
EntityType.person,
EntityType.personV2,
personData.toJson(),
id: personID,
);
@@ -216,7 +216,7 @@ class PersonService {
}
await entityService.addOrUpdate(
EntityType.person,
EntityType.personV2,
personData.toJson(),
id: person.remoteID,
);
@@ -232,7 +232,7 @@ class PersonService {
final PersonEntity justName =
PersonEntity(personID, PersonData(name: entity.data.name));
await entityService.addOrUpdate(
EntityType.person,
EntityType.personV2,
justName.data.toJson(),
id: personID,
);
@@ -249,7 +249,7 @@ class PersonService {
Future<void> fetchRemoteClusterFeedback() async {
await entityService.syncEntities();
final entities = await entityService.getEntities(EntityType.person);
final entities = await entityService.getEntities(EntityType.personV2);
entities.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
final Map<String, String> faceIdToClusterID = {};
final Map<String, String> clusterToPersonID = {};
@@ -307,7 +307,7 @@ class PersonService {
Future<void> _updatePerson(PersonEntity updatePerson) async {
await entityService.addOrUpdate(
EntityType.person,
EntityType.personV2,
updatePerson.data.toJson(),
id: updatePerson.remoteID,
);