Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] generate memory lane schedule on startup

Priyanshu Dangared915c2ffd2b6
diff --git a/mobile/apps/photos/lib/services/memory_lane/memory_lane_cache_service.dart b/mobile/apps/photos/lib/services/memory_lane/memory_lane_cache_service.dart
index 3e4b52caf3..1b088e6ee0 100644
--- a/mobile/apps/photos/lib/services/memory_lane/memory_lane_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memory_lane/memory_lane_cache_service.dart
@@ -70,6 +70,30 @@ class MemoryLaneCacheService {
return Map<String, MemoryLaneComputeLogEntry>.from(cache.computeLog);
}
+ Future<void> updateMemoriesStripSchedule(
+ Set<String> invalid,
+ MemoryLaneSchedule? schedule,
+ ) async {
+ if (invalid.isEmpty && schedule == null) {
+ return;
+ }
+ await _ensureInitialized();
+ await _lock.synchronized(() async {
+ final currentCache = await _loadCacheUnsafe();
+ var updatedCache = currentCache.copyWithoutMemoriesStripScheduleEntries(
+ invalid,
+ );
+ if (schedule != null) {
+ updatedCache = updatedCache.copyWithMemoriesStripScheduleEntry(
+ schedule.personID,
+ schedule,
+ );
+ }
+ _cache = updatedCache;
+ await _writeCacheUnsafe();
+ });
+ }
+
Future<void> upsertTimelineAndLog(
MemoryLanePersonTimeline timeline,
MemoryLaneComputeLogEntry log,
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 67673d5d20..30dc42e44e 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
@@ -82,6 +82,9 @@ class MemoryLaneService {
Bus.instance.on<MLConsentChangedEvent>().listen(_handleMlConsentChange);
_scheduleStartupBackfill();
_initialized = true;
+ if (flagService.internalUser) {
+ await _scheduleTimelinesForMemoriesStrip();
+ }
await _queueFullRecompute();
} catch (e, s) {
_logger.severe("Initialization failed", e, s);
@@ -898,6 +901,69 @@ class MemoryLaneService {
files.map((file) => MapEntry(localIdToId[file.localID]!, file)),
);
}
+
+ Future<void> _scheduleTimelinesForMemoriesStrip() async {
+ final scheduleWindowMs = MemoryLaneSchedule.displayDuration.inMicroseconds;
+ final cooldownMs = const Duration(days: 30).inMicroseconds;
+ final cache = await _cacheService.getCache();
+ final nowMicros = DateTime.now().microsecondsSinceEpoch;
+
+ final invalid = <String>{};
+ for (final entry in cache.memoriesStripSchedule.entries) {
+ final timeline = cache.timelines[entry.key];
+ final isInCooldown = nowMicros - entry.value.beginShowingAt < cooldownMs;
+ if (timeline == null ||
+ !timeline.isEligible ||
+ timeline.entries.isEmpty ||
+ !isInCooldown) {
+ invalid.add(entry.key);
+ }
+ }
+
+ final valid = cache.memoriesStripSchedule.entries
+ .where((entry) => !invalid.contains(entry.key))
+ .toList();
+ final alreadyScheduledAhead = valid.any(
+ (s) => s.value.beginShowingAt >= nowMicros + scheduleWindowMs,
+ );
+ if (alreadyScheduledAhead) {
+ await _cacheService.updateMemoriesStripSchedule(invalid, null);
+ return;
+ }
+
+ final timelines = cache.allTimelines.toList()..shuffle();
+ final timeline = timelines.firstWhereOrNull(
+ (t) =>
+ t.isEligible &&
+ t.entries.isNotEmpty &&
+ (!cache.memoriesStripSchedule.containsKey(t.personId) ||
+ invalid.contains(t.personId)),
+ );
+ if (timeline == null) {
+ await _cacheService.updateMemoriesStripSchedule(invalid, null);
+ return;
+ }
+
+ final newBeginShowingAt = valid
+ .map((entry) => entry.value.beginShowingAt + scheduleWindowMs)
+ .followedBy([nowMicros])
+ .max;
+ await _cacheService.updateMemoriesStripSchedule(
+ invalid,
+ MemoryLaneSchedule(
+ personID: timeline.personId,
+ isCluster: timeline.isCluster,
+ beginShowingAt: newBeginShowingAt,
+ ),
+ );
+
+ unawaited(
+ ensureTimelineReachability(
+ timeline.personId,
+ isCluster: timeline.isCluster,
+ ),
+ );
+ }
}
class _TimelineRequest {