π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Refine wrapped media selection
laurenspriem8cc2ab192802
diff --git a/mobile/apps/photos/lib/services/wrapped/builders/aesthetics_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/aesthetics_candidate_builder.dartindex a1cf7670ac..0250b77183 100644--- a/mobile/apps/photos/lib/services/wrapped/builders/aesthetics_candidate_builder.dart+++ b/mobile/apps/photos/lib/services/wrapped/builders/aesthetics_candidate_builder.dart@@ -10,7 +10,6 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {static const double _kTopWowPerQueryThreshold = 0.175;static const double _kTopWowHighlightThreshold = 0.225;static const double _kBlurryFaceBlurThreshold = 50;- static const int _kMaxMatchesPerQuery = 250;static const int _kMaxMediaPerCard = 6;static const int _kMinBlurryFiles = 3;static const int _kMinColorBuckets = 2;@@ -125,16 +124,23 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {return null;}+ final List<int> candidateIds = limitSelectorCandidates(+ candidates.map((candidate) => candidate.uploadedFileID),+ );+ if (candidateIds.length < _kMinBlurryFiles) {+ return null;+ }+final Map<int, double> scoreHints = <int, double>{for (final _BlurryCandidate candidate in candidates)- candidate.uploadedFileID: candidate.faceScore,+ if (candidateIds.contains(candidate.uploadedFileID))+ candidate.uploadedFileID: candidate.faceScore,};final List<MediaRef> mediaRefs = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs:- candidates.map((c) => c.uploadedFileID).toList(growable: false),- maxCount: math.min(_kMaxMediaPerCard, candidates.length),+ candidateUploadedFileIDs: candidateIds,+ maxCount: math.min(_kMaxMediaPerCard, candidateIds.length),scoreHints: scoreHints,preferNamedPeople: true,minimumSpacing: const Duration(days: 21),@@ -224,45 +230,47 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {? tiedBuckets.first: tiedBuckets[randomizer.nextInt(tiedBuckets.length)];- List<_ClipMatch> heroMatches = chosenBucket.matches- .where((match) => match.score >= _kDisplayThreshold)- .take(_kMaxMatchesPerQuery)- .toList();- if (heroMatches.length < _kMaxMediaPerCard) {- final Set<int> seen =- heroMatches.map((match) => match.uploadedFileID).toSet();- for (final _ClipMatch match in chosenBucket.matches) {- if (seen.length >= _kMaxMediaPerCard) {- break;- }- if (seen.add(match.uploadedFileID)) {- heroMatches.add(match);- }- }- }- if (heroMatches.isEmpty) {- heroMatches = chosenBucket.matches- .take(_kMaxMatchesPerQuery)- .toList(growable: false);+ final List<_ClipMatch> prioritizedMatches = _limitMatches(+ <_ClipMatch>[+ ...chosenBucket.matches+ .where((match) => match.score >= _kDisplayThreshold),+ ...chosenBucket.matches,+ ],+ );+ if (prioritizedMatches.length < _kMaxMediaPerCard) {+ return null;}+ final List<int> candidateIds = prioritizedMatches+ .map((match) => match.uploadedFileID)+ .toList(growable: false);final Map<int, double> scoreHints = <int, double>{- for (final _ClipMatch match in heroMatches)+ for (final _ClipMatch match in prioritizedMatches)match.uploadedFileID: match.score,};final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs:- heroMatches.map((match) => match.uploadedFileID),- maxCount: math.min(_kMaxMediaPerCard, heroMatches.length),+ candidateUploadedFileIDs: candidateIds,+ maxCount: _kMaxMediaPerCard,scoreHints: scoreHints,minimumSpacing: const Duration(days: 30),enforceDistinctness: false,);if (media.length < _kMaxMediaPerCard) {- return null;+ final Set<int> seen = media.map((ref) => ref.uploadedFileID).toSet();+ for (final int id in candidateIds) {+ if (seen.add(id)) {+ media.add(MediaRef(id));+ }+ if (media.length >= _kMaxMediaPerCard) {+ break;+ }+ }+ if (media.length < _kMaxMediaPerCard) {+ return null;+ }}final Map<String, Object?> meta = <String, Object?>{@@ -290,32 +298,32 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {return null;}- final List<_ClipMatch> heroes = matches- .where((match) => match.score >= _kMonochromeDisplayThreshold)- .toList();+ final List<_ClipMatch> prioritizedMatches = _limitMatches(+ <_ClipMatch>[+ ...matches+ .where((match) => match.score >= _kMonochromeDisplayThreshold),+ ...matches,+ ],+ );- if (heroes.length < _kMaxMediaPerCard) {- final Set<int> seen = heroes.map((match) => match.uploadedFileID).toSet();- for (final _ClipMatch match in matches) {- if (seen.length >= _kMaxMediaPerCard) {- break;- }- if (seen.add(match.uploadedFileID)) {- heroes.add(match);- }- }+ if (prioritizedMatches.length < _kMaxMediaPerCard) {+ return null;}+ final List<int> candidateIds = prioritizedMatches+ .map((match) => match.uploadedFileID)+ .toList(growable: false);+final Map<int, double> scoreHints = <int, double>{- for (final _ClipMatch match in heroes) match.uploadedFileID: match.score,+ for (final _ClipMatch match in prioritizedMatches)+ match.uploadedFileID: match.score,};final List<MediaRef> primarySelection =WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs:- heroes.map((match) => match.uploadedFileID).toList(growable: false),- maxCount: math.min(_kMaxMediaPerCard, heroes.length),+ candidateUploadedFileIDs: candidateIds,+ maxCount: _kMaxMediaPerCard,scoreHints: scoreHints,minimumSpacing: const Duration(days: 21),enforceDistinctness: false,@@ -327,34 +335,14 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {final Set<int> seen =media.map((MediaRef ref) => ref.uploadedFileID).toSet();- final List<MediaRef> relaxedSelection =- WrappedMediaSelector.selectMediaRefs(- context: context,- candidateUploadedFileIDs:- heroes.map((match) => match.uploadedFileID).toList(growable: false),- maxCount: math.min(_kMaxMediaPerCard, heroes.length),- scoreHints: scoreHints,- enforceDistinctness: false,- );- for (final MediaRef ref in relaxedSelection) {- if (seen.add(ref.uploadedFileID)) {- media.add(ref);+ for (final int id in candidateIds) {+ if (seen.add(id)) {+ media.add(MediaRef(id));}if (media.length >= _kMaxMediaPerCard) {break;}}-- if (media.length < _kMaxMediaPerCard) {- for (final _ClipMatch match in matches) {- if (seen.add(match.uploadedFileID)) {- media.add(MediaRef(match.uploadedFileID));- }- if (media.length >= _kMaxMediaPerCard) {- break;- }- }- }}if (media.length < _kMaxMediaPerCard) {@@ -421,17 +409,25 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {final Set<int> intersectionIds =intersection.map((match) => match.uploadedFileID).toSet();++ final List<_ClipMatch> limitedCandidates = _limitMatches(candidates);+ if (limitedCandidates.length < desiredCount) {+ return null;+ }++ final List<int> candidateIds = limitedCandidates+ .map((match) => match.uploadedFileID)+ .toList(growable: false);+final Map<int, double> scoreHints = <int, double>{- for (final _ClipMatch match in candidates)+ for (final _ClipMatch match in limitedCandidates)match.uploadedFileID: match.score *(intersectionIds.contains(match.uploadedFileID) ? 1.5 : 1.0),};final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs: candidates- .map((match) => match.uploadedFileID)- .toList(growable: false),+ candidateUploadedFileIDs: candidateIds,maxCount: desiredCount,scoreHints: scoreHints,preferNamedPeople: true,@@ -439,7 +435,18 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {);if (media.length < desiredCount) {- return null;+ final Set<int> seen = media.map((ref) => ref.uploadedFileID).toSet();+ for (final int id in candidateIds) {+ if (seen.add(id)) {+ media.add(MediaRef(id));+ }+ if (media.length >= desiredCount) {+ break;+ }+ }+ if (media.length < desiredCount) {+ return null;+ }}final Map<String, Object?> meta = <String, Object?>{@@ -459,6 +466,22 @@ class AestheticsCandidateBuilder extends WrappedCandidateBuilder {);}+ static List<_ClipMatch> _limitMatches(Iterable<_ClipMatch> matches) {+ final List<_ClipMatch> limited = <_ClipMatch>[];+ final Set<int> seen = <int>{};+ for (final _ClipMatch match in matches) {+ final int id = match.uploadedFileID;+ if (id <= 0 || !seen.add(id)) {+ continue;+ }+ limited.add(match);+ if (limited.length >= kWrappedSelectorCandidateCap) {+ break;+ }+ }+ return limited;+ }+String? _buildBlurrySubtitle(Set<String> names) {if (names.isEmpty) {return "Soft-focus favorites that still made the cut.";@@ -771,8 +794,8 @@ class _AestheticsSnapshot {}}matches.sort((a, b) => b.score.compareTo(a.score));- if (matches.length > AestheticsCandidateBuilder._kMaxMatchesPerQuery) {- matches.length = AestheticsCandidateBuilder._kMaxMatchesPerQuery;+ if (matches.length > kWrappedSelectorCandidateCap) {+ matches.length = kWrappedSelectorCandidateCap;}return matches;});diff --git a/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dartindex 6709adeda0..869b8956cb 100644--- a/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart+++ b/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart@@ -65,10 +65,9 @@ class CurationCandidateBuilder extends WrappedCandidateBuilder {final Set<int> seenHeroIds = <int>{};final List<int> heroCandidates = <int>[];- const int baseLimit = _kGalleryGroupSize * _kMaxGalleryGroups;final int extendedLimit = math.min(uploadedIds.length,- baseLimit * 4,+ kWrappedSelectorCandidateCap,);for (final List<int> group in galleryGroups) {diff --git a/mobile/apps/photos/lib/services/wrapped/builders/people_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/people_candidate_builder.dartindex 4caeb1bb9f..3ca08aa7cf 100644--- a/mobile/apps/photos/lib/services/wrapped/builders/people_candidate_builder.dart+++ b/mobile/apps/photos/lib/services/wrapped/builders/people_candidate_builder.dart@@ -78,14 +78,20 @@ class PeopleCandidateBuilder extends WrappedCandidateBuilder {: endsWithS? "$displayName spotlight": "$displayName's spotlight";- final String? heroName =- displayName.isNotEmpty ? displayName : null;+ final String? heroName = displayName.isNotEmpty ? displayName : null;final String subtitle = heroName != null? "$heroName was the star of your year!": "Someone special was the star of your year!";- final List<int> candidateIds = topPerson.topMediaFileIDs(6);- final Map<int, double> scoreHints = topPerson.mediaScoreHints();+ final List<int> candidateIds = limitSelectorCandidates(+ topPerson.topMediaFileIDs(kWrappedSelectorCandidateCap),+ );+ final Set<int> candidateSet = candidateIds.toSet();+ final Map<int, double> scoreHints = <int, double>{+ for (final MapEntry<int, double> entry+ in topPerson.mediaScoreHints().entries)+ if (candidateSet.contains(entry.key)) entry.key: entry.value,+ };final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,candidateUploadedFileIDs: candidateIds,@@ -143,15 +149,23 @@ class PeopleCandidateBuilder extends WrappedCandidateBuilder {],);- final List<int> candidateIds = topThree- .map((_PersonStats stats) => stats.topMediaFileIDs(3))- .expand((List<int> ids) => ids)- .toList(growable: false);+ final List<int> candidateIds = limitSelectorCandidates(+ topThree+ .map(+ (_PersonStats stats) =>+ stats.topMediaFileIDs(kWrappedSelectorCandidateCap),+ )+ .expand((List<int> ids) => ids),+ );+ final Set<int> candidateSet = candidateIds.toSet();final Map<int, double> scoreHints = <int, double>{};for (final _PersonStats stats in topThree) {final Map<int, double> personHints = stats.mediaScoreHints();for (final MapEntry<int, double> entry in personHints.entries) {+ if (!candidateSet.contains(entry.key)) {+ continue;+ }scoreHints.update(entry.key,(double value) => value + entry.value,@@ -248,10 +262,12 @@ class PeopleCandidateBuilder extends WrappedCandidateBuilder {"Solo shots: ${numberFormat.format(dataset.soloMoments)}",]);- final List<int> candidateIds = <int>[- ...dataset.groupSampleFileIDs(4),- ...dataset.soloSampleFileIDs(2),- ]..removeWhere((int id) => id <= 0);+ final List<int> candidateIds = limitSelectorCandidates(+ <int>[+ ...dataset.groupSampleFileIDs(kWrappedSelectorCandidateCap),+ ...dataset.soloSampleFileIDs(kWrappedSelectorCandidateCap),+ ],+ );final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,@@ -327,9 +343,7 @@ class PeopleCandidateBuilder extends WrappedCandidateBuilder {: "${_formatNameList(highlightNames)} joined your story.";final List<String> chips = _cleanChips(<String>[- for (int index = 0;- index < highlights.length && index < 3;- index += 1)+ for (int index = 0; index < highlights.length && index < 3; index += 1)() {final _PersonStats stats = highlights[index];final String name = stats.displayNameForTitle.trim();@@ -342,15 +356,23 @@ class PeopleCandidateBuilder extends WrappedCandidateBuilder {}(),]);- final List<int> candidateIds = highlights- .map((_PersonStats stats) => stats.topMediaFileIDs(3))- .expand((List<int> ids) => ids)- .toList(growable: false);+ final List<int> candidateIds = limitSelectorCandidates(+ highlights+ .map(+ (_PersonStats stats) =>+ stats.topMediaFileIDs(kWrappedSelectorCandidateCap),+ )+ .expand((List<int> ids) => ids),+ );+ final Set<int> candidateSet = candidateIds.toSet();final Map<int, double> scoreHints = <int, double>{};for (final _PersonStats stats in highlights) {final Map<int, double> personHints = stats.mediaScoreHints();for (final MapEntry<int, double> entry in personHints.entries) {+ if (!candidateSet.contains(entry.key)) {+ continue;+ }scoreHints.update(entry.key,(double value) => value + entry.value,diff --git a/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dartindex 827f5414e4..d3d5c5562f 100644--- a/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart+++ b/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart@@ -9,10 +9,8 @@ class PlacesCandidateBuilder extends WrappedCandidateBuilder {static const int _kMinSpotCaptures = 6;static const int _kMinSpotDistinctDays = 2;static const double _kMaxCityLabelDistanceKm = 120.0;- static const int _kTopCityMediaCount = 4;+ static const int _kTopCityMediaCount = 3;static const int _kSpotMediaCount = 6;- static const int _kSelectionPoolMultiplier = 5;- static const int _kSelectionPoolMinimum = 18;@overrideString get debugLabel => "places";@@ -247,17 +245,11 @@ class PlacesCandidateBuilder extends WrappedCandidateBuilder {return collected;}- final int candidateLimit = math.max(- maxCount,- math.max(maxCount * _kSelectionPoolMultiplier, _kSelectionPoolMinimum),- );+ const int candidateLimit = kWrappedSelectorCandidateCap;for (final _PlaceClusterSummary cluster in clusters) {final int perClusterLimit = math.min(- cluster.totalCount,- math.max(- maxCount,- (candidateLimit / clusters.length).ceil(),- ),+ candidateLimit,+ math.max(cluster.totalCount, maxCount),);final List<int> clusterIds = cluster.sampleMediaIds(perClusterLimit,diff --git a/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dartindex e56b3859ae..64a04747f0 100644--- a/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart+++ b/mobile/apps/photos/lib/services/wrapped/builders/stats_candidate_builder.dart@@ -67,10 +67,12 @@ class StatsCandidateBuilder extends WrappedCandidateBuilder {selectionTarget: 6,);+ final List<int> heroCandidates = limitSelectorCandidates(heroIds);+final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs: heroIds,- maxCount: 6,+ candidateUploadedFileIDs: heroCandidates,+ maxCount: 3,preferNamedPeople: true,minimumSpacing: const Duration(days: 45),);@@ -155,10 +157,12 @@ class StatsCandidateBuilder extends WrappedCandidateBuilder {selectionTarget: 6,);+ final List<int> rhythmCandidates = limitSelectorCandidates(rhythmIds);+final List<MediaRef> media = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs: rhythmIds,- maxCount: 6,+ candidateUploadedFileIDs: rhythmCandidates,+ maxCount: 3,preferNamedPeople: true,minimumSpacing: const Duration(days: 21),);@@ -222,9 +226,12 @@ class StatsCandidateBuilder extends WrappedCandidateBuilder {final NumberFormat numberFormat = NumberFormat.decimalPattern();final DateTime day = snapshot.busiestDay!;+ final List<int> candidateIds =+ limitSelectorCandidates(snapshot.busiestDayMediaUploadedIDs);+final List<MediaRef> mediaRefs = WrappedMediaSelector.selectMediaRefs(context: context,- candidateUploadedFileIDs: snapshot.busiestDayMediaUploadedIDs,+ candidateUploadedFileIDs: candidateIds,maxCount: 6,preferNamedPeople: true,);@@ -447,7 +454,7 @@ class _StatsSnapshot {dayAggregates.putIfAbsent(dayKey, () => _DayAggregate(day));aggregate.count += 1;final int? uploadedId = file.uploadedFileID;- if (uploadedId != null && aggregate.uploadedFileIDs.length < 9) {+ if (uploadedId != null) {aggregate.uploadedFileIDs.add(uploadedId);}}diff --git a/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart b/mobile/apps/photos/lib/services/wrapped/candidate_builders.dartindex a4062a3167..93e0e5810d 100644--- a/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart+++ b/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart@@ -22,6 +22,23 @@ part "builders/narrative_candidate_builder.dart";part "builders/badge_selector.dart";part "builders/media_selector.dart";+const int kWrappedSelectorCandidateCap = 400;++List<int> limitSelectorCandidates(Iterable<int> ids) {+ final List<int> limited = <int>[];+ final Set<int> seen = <int>{};+ for (final int id in ids) {+ if (id <= 0 || !seen.add(id)) {+ continue;+ }+ limited.add(id);+ if (limited.length >= kWrappedSelectorCandidateCap) {+ break;+ }+ }+ return limited;+}+/// Provides basic context details for candidate builders.@immutableclass WrappedEngineContext {