Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] fix remaining contacts review issues

Neeraj Gupta5b6b28d3a0bf
diff --git a/mobile/apps/photos/lib/services/photos_contacts_service.dart b/mobile/apps/photos/lib/services/photos_contacts_service.dart
index b14edea16a..91d12d225b 100644
--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart
+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart
@@ -4,13 +4,16 @@ import "package:logging/logging.dart";
import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/events/contacts_changed_event.dart";
+import "package:photos/events/user_logged_out_event.dart";
import "package:photos/service_locator.dart";
class PhotosContactsService {
PhotosContactsService._privateConstructor()
: _contactsServiceFactory = (() => contacts.ContactsService(
preferences: ServiceLocator.instance.prefs,
- ));
+ )) {
+ _attachSessionResetListeners();
+ }
@visibleForTesting
PhotosContactsService.forTesting({
@@ -18,7 +21,9 @@ class PhotosContactsService {
contacts.ContactsService Function()? contactsServiceFactory,
}) : _contacts = contactsService,
_contactsServiceFactory = contactsServiceFactory ??
- (contactsService != null ? () => contactsService : null);
+ (contactsService != null ? () => contactsService : null) {
+ _attachSessionResetListeners();
+ }
static final PhotosContactsService instance =
PhotosContactsService._privateConstructor();
@@ -115,7 +120,7 @@ class PhotosContactsService {
}
contacts.ContactRecord? getCachedContactByUserId(int? contactUserId) {
- if (contactUserId == null) {
+ if (contactUserId == null || _sessionKey == null) {
return null;
}
final contact = _contactsByUserId[contactUserId];
@@ -135,6 +140,7 @@ class PhotosContactsService {
Uint8List? getCachedProfilePictureBytesByUserId(int? contactUserId) {
if (contactUserId == null ||
+ _sessionKey == null ||
!_resolvedProfilePictureUserIds.contains(contactUserId)) {
return null;
}
@@ -143,6 +149,7 @@ class PhotosContactsService {
bool hasResolvedProfilePictureByUserId(int? contactUserId) {
return contactUserId != null &&
+ _sessionKey != null &&
_resolvedProfilePictureUserIds.contains(contactUserId);
}
@@ -150,10 +157,10 @@ class PhotosContactsService {
if (contactUserId == null) {
return null;
}
- if (!flagService.enableContact) {
+ if (!flagService.enableContact || _sessionKey == null) {
return null;
}
- if (_resolvedProfilePictureUserIds.contains(contactUserId)) {
+ if (hasResolvedProfilePictureByUserId(contactUserId)) {
return _profilePictureBytesByUserId[contactUserId];
}
final inflightLoad = _profilePictureLoadsByUserId[contactUserId];
@@ -171,10 +178,6 @@ class PhotosContactsService {
final contact = await getContactByUserId(contactUserId);
final attachmentId = contact?.profilePictureAttachmentId;
if (contact == null) {
- if (_hasHydratedCache && _sessionKey != null) {
- _profilePictureBytesByUserId.remove(contactUserId);
- _resolvedProfilePictureUserIds.add(contactUserId);
- }
return null;
}
if (attachmentId == null) {
@@ -452,6 +455,13 @@ class PhotosContactsService {
_profilePictureLoadsByUserId.remove(contactUserId);
}
+ void _attachSessionResetListeners() {
+ Bus.instance.on<UserLoggedOutEvent>().listen((_) {
+ _sessionGeneration += 1;
+ _resetSessionState(notify: true);
+ });
+ }
+
contacts.ContactsService _newContactsService() {
final factory = _contactsServiceFactory;
if (factory != null) {
diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart b/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
index f8dd2e2d36..f1bf8d4795 100644
--- a/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/result/contact_photo_picker_sheet.dart
@@ -70,9 +70,10 @@ class _ContactPhotoPickerSheet extends StatelessWidget {
asc,
}) async {
final files =
- initialFiles ??
- await SearchService.instance
- .getAllFilesForGenericGallery();
+ initialFiles == null || initialFiles!.isEmpty
+ ? await SearchService.instance
+ .getAllFilesForGenericGallery()
+ : initialFiles!;
return FileLoadResult(files, false);
},
tagPrefix: "pick_contact_photo_gallery",
diff --git a/mobile/apps/photos/test/services/photos_contacts_service_test.dart b/mobile/apps/photos/test/services/photos_contacts_service_test.dart
index 42f283c4c8..34e6d77d1b 100644
--- a/mobile/apps/photos/test/services/photos_contacts_service_test.dart
+++ b/mobile/apps/photos/test/services/photos_contacts_service_test.dart
@@ -6,6 +6,8 @@ import 'package:dio/dio.dart';
import 'package:ente_contacts/contacts.dart' as contacts;
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
+import 'package:photos/core/event_bus.dart';
+import 'package:photos/events/user_logged_out_event.dart';
import 'package:photos/service_locator.dart';
import 'package:photos/services/photos_contacts_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -227,6 +229,20 @@ void main() {
expect(contactsService.getProfilePictureCalls, 1);
},
);
+
+ test('logout event clears hydrated contact cache immediately', () async {
+ await service.debugOpenAndSync(session);
+
+ expect(service.getCachedSavedNameByUserId(7), 'Alice');
+
+ Bus.instance.fire(UserLoggedOutEvent());
+ await Future<void>.delayed(Duration.zero);
+
+ expect(service.getCachedSavedNameByUserId(7), isNull);
+ expect(service.getCachedResolvedEmailByUserId(7), isNull);
+ expect(service.getCachedProfilePictureBytesByUserId(7), isNull);
+ expect(service.hasHydratedCache, isFalse);
+ });
}
class FakeContactsService extends Fake implements contacts.ContactsService {