diff --git a/api/graphql/models/actions/album_actions.go b/api/graphql/models/actions/album_actions.go index 2c429c8a..c8f87edf 100644 --- a/api/graphql/models/actions/album_actions.go +++ b/api/graphql/models/actions/album_actions.go @@ -23,7 +23,24 @@ func MyAlbums(db *gorm.DB, user *models.User, order *models.Ordering, paginate * query := db.Model(models.Album{}).Where("id IN (?)", userAlbumIDs) if onlyRoot != nil && *onlyRoot { - query = query.Where("parent_album_id IS NULL") + + var singleRootAlbumID int = -1 + for _, album := range user.Albums { + if album.ParentAlbumID == nil { + if singleRootAlbumID == -1 { + singleRootAlbumID = album.ID + } else { + singleRootAlbumID = -1 + break + } + } + } + + if singleRootAlbumID != -1 && len(user.Albums) > 1 { + query = query.Where("parent_album_id = ?", singleRootAlbumID) + } else { + query = query.Where("parent_album_id IS NULL") + } } if showEmpty == nil || !*showEmpty { diff --git a/api/graphql/models/actions/album_actions_test.go b/api/graphql/models/actions/album_actions_test.go index 99a76bfb..3122dfe5 100644 --- a/api/graphql/models/actions/album_actions_test.go +++ b/api/graphql/models/actions/album_actions_test.go @@ -206,3 +206,64 @@ func TestAlbumCover(t *testing.T) { }) } + +func TestAlbumsSingleRootExpand(t *testing.T) { + db := test_utils.DatabaseTest(t) + boolFalse := false + boolTrue := true + + unrelatedAlbum := models.Album{ + Title: "unrelated_album", + Path: "/another_place", + } + err := db.Create(&unrelatedAlbum).Error + assert.NoError(t, err) + + user, err := models.RegisterUser(db, "user", nil, false) + assert.NoError(t, err) + + rootAlbum := models.Album{ + Title: "root", + Path: "/root", + } + + err = db.Model(&user).Association("Albums").Replace(&rootAlbum) + assert.NoError(t, err) + + t.Run("Single root album, no children", func(t *testing.T) { + returnedAlbums, err := actions.MyAlbums(db, user, nil, nil, &boolTrue, &boolTrue, &boolFalse) + assert.NoError(t, err) + + assert.Len(t, returnedAlbums, 1) + }) + + childAlbums := []models.Album{ + { + Title: "child1", + Path: "/root/child1", + ParentAlbumID: &rootAlbum.ID, + }, + { + Title: "child2", + Path: "/root/child2", + ParentAlbumID: &rootAlbum.ID, + }, + { + Title: "child3", + Path: "/root/child3", + ParentAlbumID: &rootAlbum.ID, + }, + } + + err = db.Model(&user).Association("Albums").Append(&childAlbums) + assert.NoError(t, err) + + t.Run("Single root album, multiple children", func(t *testing.T) { + + returnedAlbums, err := actions.MyAlbums(db, user, nil, nil, &boolTrue, &boolTrue, &boolFalse) + assert.NoError(t, err) + + assert.Len(t, returnedAlbums, 3) + }) + +}