Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Log old memories properly

laurenspriemc32974cf952f
diff --git a/mobile/lib/models/people_memory.dart b/mobile/lib/models/people_memory.dart
index ecb639137d..3a59cc155d 100644
--- a/mobile/lib/models/people_memory.dart
+++ b/mobile/lib/models/people_memory.dart
@@ -66,6 +66,8 @@ class PeopleMemory extends SmartMemory {
super.name,
super.firstCreationTime,
super.lastCreationTime,
+ super.firstDateToShow,
+ super.lastDateToShow,
}) : super(memories, MemoryType.people);
@override
@@ -76,6 +78,8 @@ class PeopleMemory extends SmartMemory {
String? name,
int? firstCreationTime,
int? lastCreationTime,
+ int? firstDateToShow,
+ int? lastDateToShow,
}) {
return PeopleMemory(
memories ?? super.memories,
@@ -84,6 +88,8 @@ class PeopleMemory extends SmartMemory {
name: name ?? super.name,
firstCreationTime: firstCreationTime ?? super.firstCreationTime,
lastCreationTime: lastCreationTime ?? super.lastCreationTime,
+ firstDateToShow: firstDateToShow ?? super.firstDateToShow,
+ lastDateToShow: lastDateToShow ?? super.lastDateToShow,
);
}
}
diff --git a/mobile/lib/models/smart_memory.dart b/mobile/lib/models/smart_memory.dart
index f85ea9be22..a9e99d4608 100644
--- a/mobile/lib/models/smart_memory.dart
+++ b/mobile/lib/models/smart_memory.dart
@@ -25,12 +25,13 @@ MemoryType memoryTypeFromString(String type) {
class SmartMemory {
final List<Memory> memories;
final MemoryType type;
- String? name;
+ String name;
int? firstCreationTime;
int? lastCreationTime;
int? firstDateToShow;
int? lastDateToShow;
+ // TODO: lau: make the above two non-nullable!!!
// TODO: lau: actually use this in calculated filters
SmartMemory(
@@ -44,6 +45,26 @@ class SmartMemory {
}) : name = name != null ? name + "(I)" : null;
// TODO: lau: remove (I) from name when opening up the feature flag
+ bool isOld() {
+ if (firstDateToShow == null || lastDateToShow == null) {
+ return false;
+ }
+ final now = DateTime.now().microsecondsSinceEpoch;
+ return lastDateToShow! < now;
+ }
+
+ bool hasShowTime() {
+ return firstDateToShow != null && lastDateToShow != null;
+ }
+
+ bool shouldShowNow() {
+ if (!hasShowTime()) {
+ return false;
+ }
+ final int now = DateTime.now().microsecondsSinceEpoch;
+ return now >= firstDateToShow! && now <= lastDateToShow!;
+ }
+
int averageCreationTime() {
if (firstCreationTime != null && lastCreationTime != null) {
return (firstCreationTime! + lastCreationTime!) ~/ 2;
diff --git a/mobile/lib/models/time_memory.dart b/mobile/lib/models/time_memory.dart
index e90dabe187..761c84a55e 100644
--- a/mobile/lib/models/time_memory.dart
+++ b/mobile/lib/models/time_memory.dart
@@ -7,12 +7,16 @@ class TimeMemory extends SmartMemory {
String? name,
int? firstCreationTime,
int? lastCreationTime,
+ int? firstDateToShow,
+ int? lastDateToShow,
}) : super(
memories,
MemoryType.time,
name: name,
firstCreationTime: firstCreationTime,
lastCreationTime: lastCreationTime,
+ firstDateToShow: firstDateToShow,
+ lastDateToShow: lastDateToShow,
);
@override
@@ -21,12 +25,16 @@ class TimeMemory extends SmartMemory {
String? name,
int? firstCreationTime,
int? lastCreationTime,
+ int? firstDateToShow,
+ int? lastDateToShow,
}) {
return TimeMemory(
memories ?? this.memories,
name: name ?? this.name,
firstCreationTime: firstCreationTime ?? super.firstCreationTime,
lastCreationTime: lastCreationTime ?? super.lastCreationTime,
+ firstDateToShow: firstDateToShow ?? super.firstDateToShow,
+ lastDateToShow: lastDateToShow ?? super.lastDateToShow,
);
}
}
diff --git a/mobile/lib/models/trip_memory.dart b/mobile/lib/models/trip_memory.dart
index 2ff4e339e0..244e77e86d 100644
--- a/mobile/lib/models/trip_memory.dart
+++ b/mobile/lib/models/trip_memory.dart
@@ -11,6 +11,8 @@ class TripMemory extends SmartMemory {
super.name,
super.firstCreationTime,
super.lastCreationTime,
+ super.firstDateToShow,
+ super.lastDateToShow,
}) : super(memories, MemoryType.trips);
@override
@@ -20,6 +22,8 @@ class TripMemory extends SmartMemory {
String? name,
int? firstCreationTime,
int? lastCreationTime,
+ int? firstDateToShow,
+ int? lastDateToShow,
}) {
return TripMemory(
memories ?? super.memories,
@@ -27,6 +31,8 @@ class TripMemory extends SmartMemory {
name: name ?? super.name,
firstCreationTime: firstCreationTime ?? super.firstCreationTime,
lastCreationTime: lastCreationTime ?? super.lastCreationTime,
+ firstDateToShow: firstDateToShow ?? super.firstDateToShow,
+ lastDateToShow: lastDateToShow ?? super.lastDateToShow,
);
}
}
diff --git a/mobile/lib/services/memories_cache_service.dart b/mobile/lib/services/memories_cache_service.dart
index 9a78725489..3acc3c0611 100644
--- a/mobile/lib/services/memories_cache_service.dart
+++ b/mobile/lib/services/memories_cache_service.dart
@@ -65,18 +65,60 @@ class ToShowMemory {
final int firstTimeToShow;
final int lastTimeToShow;
+ final String? personID;
+ final PeopleMemoryType? peopleMemoryType;
+ final Location? location;
+
bool get shouldShowNow {
final now = DateTime.now().microsecondsSinceEpoch;
return now >= firstTimeToShow && now <= lastTimeToShow;
}
+ bool get isOld {
+ final now = DateTime.now().microsecondsSinceEpoch;
+ return now > lastTimeToShow;
+ }
+
ToShowMemory(
this.title,
this.fileUploadedIDs,
this.type,
this.firstTimeToShow,
- this.lastTimeToShow,
- );
+ this.lastTimeToShow, {
+ this.personID,
+ this.peopleMemoryType,
+ this.location,
+ }) : assert(
+ (type == MemoryType.people &&
+ personID != null &&
+ peopleMemoryType != null) ||
+ (type == MemoryType.trips && location != null) ||
+ (type != MemoryType.people && type != MemoryType.trips),
+ "PersonID and peopleMemoryType must be provided for people memory type, and location must be provided for trips memory type",
+ );
+
+ factory ToShowMemory.fromSmartMemory(SmartMemory memory) {
+ assert(memory.firstDateToShow != null && memory.lastDateToShow != null);
+ String? personID;
+ PeopleMemoryType? peopleMemoryType;
+ Location? location;
+ if (memory is PeopleMemory) {
+ personID = memory.personID;
+ peopleMemoryType = memory.peopleMemoryType;
+ } else if (memory is TripMemory) {
+ location = memory.location;
+ }
+ return ToShowMemory(
+ memory.name,
+ memory.memories.map((m) => m.file.uploadedFileID!).toList(),
+ memory.type,
+ memory.firstDateToShow!,
+ memory.lastDateToShow!,
+ personID: personID,
+ peopleMemoryType: peopleMemoryType,
+ location: location,
+ );
+ }
factory ToShowMemory.fromJson(Map<String, dynamic> json) {
return ToShowMemory(
@@ -85,6 +127,14 @@ class ToShowMemory {
memoryTypeFromString(json['type']),
json['firstTimeToShow'],
json['lastTimeToShow'],
+ personID: json['personID'],
+ peopleMemoryType: peopleMemoryTypeFromString(json['peopleMemoryType']),
+ location: json['location'] != null
+ ? Location(
+ latitude: json['location']['latitude'],
+ longitude: json['location']['longitude'],
+ )
+ : null,
);
}
@@ -95,6 +145,14 @@ class ToShowMemory {
'type': type.toString().split('.').last,
'firstTimeToShow': firstTimeToShow,
'lastTimeToShow': lastTimeToShow,
+ 'personID': personID,
+ 'peopleMemoryType': peopleMemoryType.toString().split('.').last,
+ 'location': location != null
+ ? {
+ 'latitude': location!.latitude!,
+ 'longitude': location!.longitude!,
+ }
+ : null,
};
}
@@ -120,6 +178,19 @@ class PeopleShownLogs {
this.lastTimeShown,
);
+ factory PeopleShownLogs.fromOldCacheMemory(ToShowMemory memory) {
+ assert(
+ memory.type == MemoryType.people &&
+ memory.personID != null &&
+ memory.peopleMemoryType != null,
+ );
+ return PeopleShownLogs(
+ memory.personID!,
+ memory.peopleMemoryType!,
+ memory.lastTimeToShow,
+ );
+ }
+
factory PeopleShownLogs.fromJson(Map<String, dynamic> json) {
return PeopleShownLogs(
json['personID'],
@@ -156,6 +227,14 @@ class TripsShownLogs {
this.lastTimeShown,
);
+ factory TripsShownLogs.fromOldCacheMemory(ToShowMemory memory) {
+ assert(memory.type == MemoryType.trips && memory.location != null);
+ return TripsShownLogs(
+ memory.location!,
+ memory.lastTimeToShow,
+ );
+ }
+
factory TripsShownLogs.fromJson(Map<String, dynamic> json) {
return TripsShownLogs(
Location(
@@ -332,9 +411,8 @@ class MemoriesCacheService {
final List<ToShowMemory> toShowMemories = [];
final List<PeopleShownLogs> peopleShownLogs = [];
final List<TripsShownLogs> tripsShownLogs = [];
- final now = DateTime.now().microsecondsSinceEpoch;
for (final memory in memories) {
- if (memory.lastDateToShow != null && memory.lastDateToShow! < now) {
+ if (memory.isOld()) {
if (memory is PeopleMemory) {
peopleShownLogs.add(
PeopleShownLogs(
@@ -352,16 +430,8 @@ class MemoriesCacheService {
);
}
} else {
- if (memory.firstDateToShow != null && memory.lastDateToShow != null) {
- toShowMemories.add(
- ToShowMemory(
- memory.name!,
- memory.memories.map((m) => m.file.uploadedFileID!).toList(),
- memory.type,
- memory.firstDateToShow!,
- memory.lastDateToShow!,
- ),
- );
+ if (memory.hasShowTime()) {
+ toShowMemories.add(ToShowMemory.fromSmartMemory(memory));
} else {
_logger.severe('Memory has no first or last date to show');
}
@@ -390,6 +460,30 @@ class MemoriesCacheService {
}
tripsShownLogs.add(shownTrip);
}
+ for (final oldMemory in oldCache.toShowMemories) {
+ if (oldMemory.isOld) {
+ if (oldMemory.type == MemoryType.people) {
+ if (!peopleShownLogs.any(
+ (person) =>
+ (person.personID == oldMemory.personID) &&
+ (person.peopleMemoryType == oldMemory.peopleMemoryType),
+ )) {
+ peopleShownLogs
+ .add(PeopleShownLogs.fromOldCacheMemory(oldMemory));
+ }
+ } else if (oldMemory.type == MemoryType.trips) {
+ if (!tripsShownLogs.any(
+ (trip) => isFileInsideLocationTag(
+ oldMemory.location!,
+ trip.location,
+ 10.0,
+ ),
+ )) {
+ tripsShownLogs.add(TripsShownLogs.fromOldCacheMemory(oldMemory));
+ }
+ }
+ }
+ }
}
return MemoriesCache(
toShowMemories: toShowMemories,