Skip to content

Commit

Permalink
fix(mobile): 14983 Images upload to shared album with common name (#1…
Browse files Browse the repository at this point in the history
…5127)

* 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]>
  • Loading branch information
3 people authored Jan 17, 2025
1 parent fd99bd0 commit efbc0cb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
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
23 changes: 19 additions & 4 deletions mobile/lib/services/album.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ class AlbumService {
try {
await _userService.refreshUsers();
final (sharedAlbum, ownedAlbum) = await (
// Note: `shared: true` is required to get albums that don't belong to
// us due to unusual behaviour on the API but this will also return our
// own shared albums
_albumApiRepository.getAll(shared: true),
// Passing null (or nothing) for `shared` returns only albums that
// explicitly belong to us
_albumApiRepository.getAll(shared: null)
).wait;

Expand Down Expand Up @@ -212,7 +217,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 +413,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 +434,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

0 comments on commit efbc0cb

Please sign in to comment.