Skip to content

Commit efbc0cb

Browse files
Tyristomgquestalextran1502
authored
fix(mobile): 14983 Images upload to shared album with common name (#15127)
* Initial look at fixing issue where images are uploaded to the wrong album if a shared album conflicts with a local users album. * Use owner instead of shared flag when fetching albums. * Fix issue with refreshRemoteAlbums getting shared items twice and removed incorrect isShared comment. Using `getAll(shared: true)` gets all shared albums the user can access (regardless of owner, despite the previous comment). Using `getAll(shared: null)` gets all albums (incuding shared = true and shared = false). I presume the intent here was to get albums that were shared (and not mine), and not shared (ie: mine), but the logic is way off. It also just then combines them - so makes more sense to just get them in a single call. * Fix formatting. * Fixed tests. * Revert "Fixed tests." This reverts commit c38f5af. * Revert "Fix issue with refreshRemoteAlbums getting shared items twice and removed incorrect isShared comment." This reverts commit 979ce90. * Added comments to explain why filters behave the way they do for getAll() albums. --------- Co-authored-by: Tom graham <[email protected]> Co-authored-by: Alex <[email protected]>
1 parent fd99bd0 commit efbc0cb

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

mobile/lib/interfaces/album.interface.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract interface class IAlbumRepository implements IDatabaseRepository {
1313
String name, {
1414
bool? shared,
1515
bool? remote,
16+
bool? owner,
1617
});
1718

1819
Future<List<Album>> getAll({

mobile/lib/providers/album/album.provider.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,26 @@ class AlbumNotifier extends StateNotifier<List<Album>> {
4646
) =>
4747
_albumService.createAlbum(albumTitle, assets, []);
4848

49-
Future<Album?> getAlbumByName(String albumName, {bool remoteOnly = false}) =>
50-
_albumService.getAlbumByName(albumName, remoteOnly);
49+
Future<Album?> getAlbumByName(
50+
String albumName, {
51+
bool? remote,
52+
bool? shared,
53+
bool? owner,
54+
}) =>
55+
_albumService.getAlbumByName(
56+
albumName,
57+
remote: remote,
58+
shared: shared,
59+
owner: owner,
60+
);
5161

5262
/// Create an album on the server with the same name as the selected album for backup
5363
/// First this will check if the album already exists on the server with name
5464
/// If it does not exist, it will create the album on the server
5565
Future<void> createSyncAlbum(
5666
String albumName,
5767
) async {
58-
final album = await getAlbumByName(albumName, remoteOnly: true);
68+
final album = await getAlbumByName(albumName, remote: true, owner: true);
5969
if (album != null) {
6070
return;
6171
}

mobile/lib/repositories/album.repository.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,25 @@ class AlbumRepository extends DatabaseRepository implements IAlbumRepository {
3434
Future<Album> create(Album album) => txn(() => db.albums.store(album));
3535

3636
@override
37-
Future<Album?> getByName(String name, {bool? shared, bool? remote}) {
37+
Future<Album?> getByName(
38+
String name, {
39+
bool? shared,
40+
bool? remote,
41+
bool? owner,
42+
}) {
3843
var query = db.albums.filter().nameEqualTo(name);
3944
if (shared != null) {
4045
query = query.sharedEqualTo(shared);
4146
}
47+
if (owner == true) {
48+
query = query.owner(
49+
(q) => q.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
50+
);
51+
} else if (owner == false) {
52+
query = query.owner(
53+
(q) => q.not().isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
54+
);
55+
}
4256
if (remote == true) {
4357
query = query.localIdIsNull();
4458
} else if (remote == false) {

mobile/lib/services/album.service.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ class AlbumService {
170170
try {
171171
await _userService.refreshUsers();
172172
final (sharedAlbum, ownedAlbum) = await (
173+
// Note: `shared: true` is required to get albums that don't belong to
174+
// us due to unusual behaviour on the API but this will also return our
175+
// own shared albums
173176
_albumApiRepository.getAll(shared: true),
177+
// Passing null (or nothing) for `shared` returns only albums that
178+
// explicitly belong to us
174179
_albumApiRepository.getAll(shared: null)
175180
).wait;
176181

@@ -212,7 +217,7 @@ class AlbumService {
212217
for (int round = 0;; round++) {
213218
final proposedName = "$baseName${round == 0 ? "" : " ($round)"}";
214219

215-
if (null == await _albumRepository.getByName(proposedName)) {
220+
if (null == await _albumRepository.getByName(proposedName, owner: true)) {
216221
return proposedName;
217222
}
218223
}
@@ -408,8 +413,18 @@ class AlbumService {
408413
}
409414
}
410415

411-
Future<Album?> getAlbumByName(String name, bool remoteOnly) =>
412-
_albumRepository.getByName(name, remote: remoteOnly ? true : null);
416+
Future<Album?> getAlbumByName(
417+
String name, {
418+
bool? remote,
419+
bool? shared,
420+
bool? owner,
421+
}) =>
422+
_albumRepository.getByName(
423+
name,
424+
remote: remote,
425+
shared: shared,
426+
owner: owner,
427+
);
413428

414429
///
415430
/// Add the uploaded asset to the selected albums
@@ -419,7 +434,7 @@ class AlbumService {
419434
List<String> assetIds,
420435
) async {
421436
for (final albumName in albumNames) {
422-
Album? album = await getAlbumByName(albumName, true);
437+
Album? album = await getAlbumByName(albumName, remote: true, owner: true);
423438
album ??= await createAlbum(albumName, []);
424439
if (album != null && album.remoteId != null) {
425440
await _albumApiRepository.addAssets(album.remoteId!, assetIds);

0 commit comments

Comments
 (0)