Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): 14983 Images upload to shared album with common name #15127

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions mobile/lib/interfaces/album.interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract interface class IAlbumRepository implements IDatabaseRepository {
String name, {
bool? shared,
bool? remote,
bool? owner,
});

Future<List<Album>> getAll({
Expand Down
16 changes: 13 additions & 3 deletions mobile/lib/providers/album/album.provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,26 @@ class AlbumNotifier extends StateNotifier<List<Album>> {
) =>
_albumService.createAlbum(albumTitle, assets, []);

Future<Album?> getAlbumByName(String albumName, {bool remoteOnly = false}) =>
_albumService.getAlbumByName(albumName, remoteOnly);
Future<Album?> getAlbumByName(
String albumName, {
bool? remote,
bool? shared,
bool? owner,
}) =>
_albumService.getAlbumByName(
albumName,
remote: remote,
shared: shared,
owner: owner,
);

/// Create an album on the server with the same name as the selected album for backup
/// First this will check if the album already exists on the server with name
/// If it does not exist, it will create the album on the server
Future<void> createSyncAlbum(
String albumName,
) async {
final album = await getAlbumByName(albumName, remoteOnly: true);
final album = await getAlbumByName(albumName, remote: true, owner: true);
if (album != null) {
return;
}
Expand Down
16 changes: 15 additions & 1 deletion mobile/lib/repositories/album.repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,25 @@ class AlbumRepository extends DatabaseRepository implements IAlbumRepository {
Future<Album> create(Album album) => txn(() => db.albums.store(album));

@override
Future<Album?> getByName(String name, {bool? shared, bool? remote}) {
Future<Album?> getByName(
String name, {
bool? shared,
bool? remote,
bool? owner,
}) {
var query = db.albums.filter().nameEqualTo(name);
if (shared != null) {
query = query.sharedEqualTo(shared);
}
if (owner == true) {
query = query.owner(
(q) => q.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
);
} else if (owner == false) {
query = query.owner(
(q) => q.not().isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
);
}
if (remote == true) {
query = query.localIdIsNull();
} else if (remote == false) {
Expand Down
28 changes: 17 additions & 11 deletions mobile/lib/services/album.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class AlbumService {
return result;
}

/// Checks remote albums (owned if `isShared` is false) for changes,
/// Checks remote albums for changes,
/// updates the local database and returns `true` if there were any changes
Future<bool> refreshRemoteAlbums() async {
if (!_remoteCompleter.isCompleted) {
Expand All @@ -169,18 +169,14 @@ class AlbumService {
bool changes = false;
try {
await _userService.refreshUsers();
final (sharedAlbum, ownedAlbum) = await (
_albumApiRepository.getAll(shared: true),
_albumApiRepository.getAll(shared: null)
).wait;
final allAlbums = await _albumApiRepository.getAll();

final albums = HashSet<Album>(
equals: (a, b) => a.remoteId == b.remoteId,
hashCode: (a) => a.remoteId.hashCode,
);

albums.addAll(sharedAlbum);
albums.addAll(ownedAlbum);
albums.addAll(allAlbums);

changes = await _syncService.syncRemoteAlbumsToDb(albums.toList());
} finally {
Expand Down Expand Up @@ -212,7 +208,7 @@ class AlbumService {
for (int round = 0;; round++) {
final proposedName = "$baseName${round == 0 ? "" : " ($round)"}";

if (null == await _albumRepository.getByName(proposedName)) {
if (null == await _albumRepository.getByName(proposedName, owner: true)) {
return proposedName;
}
}
Expand Down Expand Up @@ -408,8 +404,18 @@ class AlbumService {
}
}

Future<Album?> getAlbumByName(String name, bool remoteOnly) =>
_albumRepository.getByName(name, remote: remoteOnly ? true : null);
Future<Album?> getAlbumByName(
String name, {
bool? remote,
bool? shared,
bool? owner,
}) =>
_albumRepository.getByName(
name,
remote: remote,
shared: shared,
owner: owner,
);

///
/// Add the uploaded asset to the selected albums
Expand All @@ -419,7 +425,7 @@ class AlbumService {
List<String> assetIds,
) async {
for (final albumName in albumNames) {
Album? album = await getAlbumByName(albumName, true);
Album? album = await getAlbumByName(albumName, remote: true, owner: true);
album ??= await createAlbum(albumName, []);
if (album != null && album.remoteId != null) {
await _albumApiRepository.addAssets(album.remoteId!, assetIds);
Expand Down
8 changes: 6 additions & 2 deletions mobile/test/fixtures/album.stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ final class AlbumStub {
shared: true,
activityEnabled: false,
endDate: DateTime(2020),
)..sharedUsers.addAll([UserStub.admin]);
)
..sharedUsers.addAll([UserStub.admin])
..owner.value = UserStub.user2;

static final oneAsset = Album(
name: "album-with-single-asset",
Expand All @@ -38,7 +40,9 @@ final class AlbumStub {
activityEnabled: false,
startDate: DateTime(2020),
endDate: DateTime(2023),
)..assets.addAll([AssetStub.image1]);
)
..assets.addAll([AssetStub.image1])
..owner.value = UserStub.user1;

static final twoAsset = Album(
name: "album-with-two-assets",
Expand Down
10 changes: 3 additions & 7 deletions mobile/test/services/album.service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ void main() {
group('refreshRemoteAlbums', () {
test('is working', () async {
when(() => userService.refreshUsers()).thenAnswer((_) async => true);
when(() => albumApiRepository.getAll(shared: true))
.thenAnswer((_) async => [AlbumStub.sharedWithUser]);

when(() => albumApiRepository.getAll(shared: null))
.thenAnswer((_) async => [AlbumStub.oneAsset, AlbumStub.twoAsset]);
when(() => albumApiRepository.getAll())
.thenAnswer((_) async => [AlbumStub.oneAsset, AlbumStub.twoAsset, AlbumStub.sharedWithUser]);

when(
() => syncService.syncRemoteAlbumsToDb([
Expand All @@ -100,8 +97,7 @@ void main() {
final result = await sut.refreshRemoteAlbums();
expect(result, true);
verify(() => userService.refreshUsers()).called(1);
verify(() => albumApiRepository.getAll(shared: true)).called(1);
verify(() => albumApiRepository.getAll(shared: null)).called(1);
verify(() => albumApiRepository.getAll()).called(1);
verify(
() => syncService.syncRemoteAlbumsToDb(
[
Expand Down
Loading