Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] add support for creating timelines from lone clusters in memory lane service

Priyanshu Dangare3728a1ef27e9
diff --git a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
index 9c5a78d246..ebef978883 100644
--- a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
+++ b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
@@ -20,24 +20,6 @@ import "package:photos/services/memory_lane/memory_lane_cache_service.dart";
import "package:photos/services/search_service.dart";
import "package:photos/utils/face/face_thumbnail_cache.dart";
-@visibleForTesting
-int? eligibleCreationTimeCutoffMicros(String? birthDateString) {
- if (birthDateString == null || birthDateString.isEmpty) {
- return null;
- }
- final birthDate = DateTime.tryParse(birthDateString);
- if (birthDate == null) {
- return null;
- }
- final year = birthDate.year + 3;
- final lastDay = DateTime(year, birthDate.month + 1, 0).day;
- return DateTime(
- year,
- birthDate.month,
- birthDate.day.clamp(1, lastDay),
- ).microsecondsSinceEpoch;
-}
-
class MemoryLaneService {
MemoryLaneService._internal() {
__isFeatureEnabled = hasGrantedMLConsent;
@@ -121,14 +103,18 @@ class MemoryLaneService {
final persons = await PersonService.instance.getPersons();
for (final person in persons) {
if (person.data.isIgnored) {
- await _handleIgnoredPerson(person.remoteID);
+ await _removeTimeline(person.remoteID);
continue;
}
schedulePersonRecompute(person.remoteID, force: force);
}
}
- void schedulePersonRecompute(String personId, {bool force = false}) {
+ void schedulePersonRecompute(
+ String personId, {
+ bool isCluster = false,
+ bool force = false,
+ }) {
if (personId.isEmpty) {
return;
}
@@ -136,7 +122,11 @@ class MemoryLaneService {
_precomputeQueue
.addTask(personId, () async {
final requestForce = _pendingRequests.remove(personId) ?? force;
- await _recomputeTimelineForPerson(personId, force: requestForce);
+ await _recomputeTimelineForPerson(
+ personId,
+ isCluster: isCluster,
+ force: requestForce,
+ );
})
.catchError((e, s) {
_pendingRequests.remove(personId);
@@ -144,7 +134,10 @@ class MemoryLaneService {
});
}
- Future<void> ensureTimelineReachability(String personId) async {
+ Future<void> ensureTimelineReachability(
+ String personId, {
+ bool isCluster = false,
+ }) async {
if (!isFeatureEnabled) {
return;
}
@@ -154,7 +147,7 @@ class MemoryLaneService {
final timeline = await _cacheService.getTimeline(personId);
if (timeline == null || !timeline.isEligible || timeline.entries.isEmpty) {
await _refreshReadyPersonIds();
- schedulePersonRecompute(personId);
+ schedulePersonRecompute(personId, isCluster: isCluster);
return;
}
if (await _areTimelineFaceCropsCached(timeline)) {
@@ -162,10 +155,13 @@ class MemoryLaneService {
return;
}
await _refreshReadyPersonIds();
- _queueTimelineCropReadiness(personId);
+ _queueTimelineCropReadiness(personId, isCluster: isCluster);
}
- Future<MemoryLanePersonTimeline?> getTimeline(String personId) async {
+ Future<MemoryLanePersonTimeline?> getTimeline(
+ String personId, {
+ bool isCluster = false,
+ }) async {
if (!isFeatureEnabled) {
return null;
}
@@ -185,7 +181,7 @@ class MemoryLaneService {
if (!containsHiddenEntry) {
if (timeline.isEligible && !await _areTimelineFaceCropsCached(timeline)) {
_logger.info("Missing face crops for $personId");
- _queueTimelineCropReadiness(personId);
+ _queueTimelineCropReadiness(personId, isCluster: isCluster);
await _refreshReadyPersonIds();
return null;
}
@@ -193,13 +189,12 @@ class MemoryLaneService {
}
_logger.info("Removing timeline with hidden files for $personId");
- await _cacheService.removeTimeline(personId);
- await _refreshReadyPersonIds();
- schedulePersonRecompute(personId, force: true);
+ await _removeTimeline(personId);
+ schedulePersonRecompute(personId, isCluster: isCluster, force: true);
return null;
}
- bool hasReadyTimelineSync(String personId) {
+ bool hasReadyTimelineSync(String personId, {bool isCluster = false}) {
if (!isFeatureEnabled) {
return false;
}
@@ -218,7 +213,7 @@ class MemoryLaneService {
);
}
- void _queueTimelineCropReadiness(String personId) {
+ void _queueTimelineCropReadiness(String personId, {required bool isCluster}) {
if (_cropReadinessInFlight.contains(personId)) {
return;
}
@@ -226,7 +221,7 @@ class MemoryLaneService {
_cropReadinessQueue
.addTask(personId, () async {
try {
- await _repairTimelineCropReadiness(personId);
+ await _repairTimelineCropReadiness(personId, isCluster: isCluster);
} finally {
_cropReadinessInFlight.remove(personId);
}
@@ -237,11 +232,14 @@ class MemoryLaneService {
});
}
- Future<void> _repairTimelineCropReadiness(String personId) async {
+ Future<void> _repairTimelineCropReadiness(
+ String personId, {
+ required bool isCluster,
+ }) async {
if (!isFeatureEnabled) {
return;
}
- if (!PersonService.isInitialized) {
+ if (!isCluster && !PersonService.isInitialized) {
_logger.warning(
"Crop repair skipped for $personId: PersonService unavailable",
);
@@ -256,15 +254,18 @@ class MemoryLaneService {
await _refreshReadyPersonIds();
return;
}
- final person = await PersonService.instance.getPerson(personId);
- if (person == null) {
- await _cacheService.removeTimeline(personId);
- await _refreshReadyPersonIds();
- return;
- }
- if (person.data.isIgnored) {
- await _handleIgnoredPerson(personId);
- return;
+ if (isCluster) {
+ final faceIds = await _mlDataDB.getFaceIDsForCluster(personId);
+ if (faceIds.isEmpty) {
+ await _removeTimeline(personId);
+ return;
+ }
+ } else {
+ final person = await PersonService.instance.getPerson(personId);
+ if (person == null || person.data.isIgnored) {
+ await _removeTimeline(personId);
+ return;
+ }
}
final fileIds = timeline.entries.map((entry) => entry.fileId).toSet();
@@ -273,6 +274,7 @@ class MemoryLaneService {
personId,
timeline.entries,
filesById,
+ isCluster: isCluster,
);
await _refreshReadyPersonIds();
if (!cropsReady) {
@@ -280,9 +282,9 @@ class MemoryLaneService {
}
}
- Future<void> _handleIgnoredPerson(String personId) async {
- _pendingRequests.remove(personId);
- await _cacheService.removeTimeline(personId);
+ Future<void> _removeTimeline(String id) async {
+ _pendingRequests.remove(id);
+ await _cacheService.removeTimeline(id);
await _refreshReadyPersonIds();
}
@@ -304,7 +306,7 @@ class MemoryLaneService {
return;
}
if (person.data.isIgnored) {
- await _handleIgnoredPerson(person.remoteID);
+ await _removeTimeline(person.remoteID);
return;
}
final logEntry = await _cacheService.getComputeLogEntry(person.remoteID);
@@ -344,7 +346,7 @@ class MemoryLaneService {
final facesPerYear = await _countEligibleFacesByYear(
faceIds,
- eligibleCreationTimeCutoffMicros(person.data.birthDate),
+ _eligibleCreationTimeCutoffMicros(person.data.birthDate),
);
if (_hasNewYearWithTenFaces(timeline, facesPerYear)) {
schedulePersonRecompute(person.remoteID);
@@ -470,27 +472,27 @@ class MemoryLaneService {
Future<void> _recomputeTimelineForPerson(
String personId, {
+ required bool isCluster,
required bool force,
}) async {
if (!isFeatureEnabled) {
return;
}
- if (!PersonService.isInitialized) {
+ if (!isCluster && !PersonService.isInitialized) {
_logger.warning(
"Recompute skipped for $personId: PersonService unavailable",
);
return;
}
- final person = await PersonService.instance.getPerson(personId);
- if (person == null) {
- await _cacheService.removeTimeline(personId);
- await _refreshReadyPersonIds();
- return;
- }
- if (person.data.isIgnored) {
- await _handleIgnoredPerson(personId);
- return;
+ final person = isCluster
+ ? null
+ : await PersonService.instance.getPerson(personId);
+ if (!isCluster) {
+ if (person == null || person.data.isIgnored) {
+ await _removeTimeline(personId);
+ return;
+ }
}
final nowMicros = DateTime.now().microsecondsSinceEpoch;
@@ -514,22 +516,26 @@ class MemoryLaneService {
_lastForcedComputeMicros[personId] = nowMicros;
}
- final faceIds = await _mlDataDB.getFaceIDsForPerson(personId);
+ final faceIds = isCluster
+ ? await _mlDataDB.getFaceIDsForCluster(personId)
+ : await _mlDataDB.getFaceIDsForPerson(personId);
final (timeline, filesById) = await _computeTimeline(
faceIds,
personId,
nowMicros,
- eligibleCreationTimeCutoffMicros(person.data.birthDate),
+ _eligibleCreationTimeCutoffMicros(person?.data.birthDate),
+ isCluster: isCluster,
);
await _cacheService.upsertTimeline(timeline);
await _cacheService.upsertComputeLogEntry(
MemoryLaneComputeLogEntry(
personId: personId,
- name: person.data.name,
- birthDate: person.data.birthDate,
+ name: person?.data.name,
+ birthDate: person?.data.birthDate,
faceCount: faceIds.length,
lastComputedMicros: nowMicros,
logicVersion: _timelineLogicVersion,
+ isCluster: isCluster,
),
);
if (!timeline.isEligible) {
@@ -540,20 +546,22 @@ class MemoryLaneService {
personId,
timeline.entries,
filesById,
+ isCluster: isCluster,
);
await _refreshReadyPersonIds();
if (!cropsReady) {
_logger.warning("Crop repair failed for $personId");
- _queueTimelineCropReadiness(personId);
+ _queueTimelineCropReadiness(personId, isCluster: isCluster);
}
}
Future<(MemoryLanePersonTimeline, Map<int, EnteFile>)> _computeTimeline(
- Set<String> faceIds,
+ Iterable<String> faceIds,
String personId,
int nowMicros,
- int? minCreationTimeMicros,
- ) async {
+ int? minCreationTimeMicros, {
+ required bool isCluster,
+ }) async {
if (faceIds.isEmpty) {
return (
MemoryLanePersonTimeline(
@@ -561,6 +569,7 @@ class MemoryLaneService {
isEligible: false,
updatedAtMicros: nowMicros,
entries: const [],
+ isCluster: isCluster,
),
const <int, EnteFile>{},
);
@@ -631,6 +640,7 @@ class MemoryLaneService {
isEligible: false,
updatedAtMicros: nowMicros,
entries: const [],
+ isCluster: isCluster,
),
fileMap,
);
@@ -654,6 +664,7 @@ class MemoryLaneService {
isEligible: false,
updatedAtMicros: nowMicros,
entries: const [],
+ isCluster: isCluster,
),
fileMap,
);
@@ -678,6 +689,7 @@ class MemoryLaneService {
isEligible: true,
updatedAtMicros: nowMicros,
entries: entries,
+ isCluster: isCluster,
),
fileMap,
);
@@ -686,8 +698,9 @@ class MemoryLaneService {
Future<bool> _ensureFaceCrops(
String personId,
List<MemoryLaneEntry> entries,
- Map<int, EnteFile> fileMap,
- ) async {
+ Map<int, EnteFile> fileMap, {
+ required bool isCluster,
+ }) async {
if (entries.isEmpty) {
return false;
}
@@ -742,6 +755,7 @@ class MemoryLaneService {
Future<void> prewarmTimelineFrames(
String personId, {
+ bool isCluster = false,
int frameCount = 6,
}) async {
if (!isFeatureEnabled) {
@@ -930,3 +944,17 @@ List<_TimelineFaceData> _pickFacesForYear(List<_TimelineFaceData> faces) {
return picks;
}
+
+int? _eligibleCreationTimeCutoffMicros(String? birthDateString) {
+ final birthDate = DateTime.tryParse(birthDateString ?? "");
+ if (birthDate == null) {
+ return null;
+ }
+ final year = birthDate.year + 3;
+ final lastDay = DateTime(year, birthDate.month + 1, 0).day;
+ return DateTime(
+ year,
+ birthDate.month,
+ birthDate.day.clamp(1, lastDay),
+ ).microsecondsSinceEpoch;
+}