Skip to content

Commit

Permalink
Search: Allow searching for favorite:false to find other pictures
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Mayer <michael@photoprism.app>
  • Loading branch information
lastzero committed Sep 1, 2023
1 parent afe2190 commit 20d20c7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/form/search_photos.go
Expand Up @@ -41,7 +41,7 @@ type SearchPhotos struct {
Archived bool `form:"archived" notes:"Finds archived pictures"`
Public bool `form:"public" notes:"Excludes private pictures"`
Private bool `form:"private" notes:"Finds private pictures"`
Favorite bool `form:"favorite" notes:"Finds favorites only"`
Favorite string `form:"favorite" example:"favorite:yes" notes:"Finds favorites only"`
Unsorted bool `form:"unsorted" notes:"Finds pictures not in an album"`
Lat float32 `form:"lat" notes:"Latitude (GPS Position)"`
Lng float32 `form:"lng" notes:"Longitude (GPS Position)"`
Expand Down
2 changes: 1 addition & 1 deletion internal/form/search_photos_geo.go
Expand Up @@ -21,7 +21,7 @@ type SearchPhotosGeo struct {
Title string `form:"title"`
Before time.Time `form:"before" time_format:"2006-01-02"`
After time.Time `form:"after" time_format:"2006-01-02"`
Favorite bool `form:"favorite"`
Favorite string `form:"favorite" example:"favorite:yes" notes:"Finds favorites only"`
Unsorted bool `form:"unsorted"`
Video bool `form:"video"`
Vector bool `form:"vector"`
Expand Down
6 changes: 3 additions & 3 deletions internal/form/search_photos_geo_test.go
Expand Up @@ -193,7 +193,7 @@ func TestSearchPhotosGeo(t *testing.T) {
t.Fatal(err)
}

assert.True(t, form.Favorite)
assert.Equal(t, "cat", form.Favorite)
})
t.Run("query for before with invalid type", func(t *testing.T) {
form := &SearchPhotosGeo{Query: "before:cat"}
Expand Down Expand Up @@ -242,13 +242,13 @@ func TestSearchPhotosGeo(t *testing.T) {
}

func TestSearchPhotosGeo_Serialize(t *testing.T) {
form := &SearchPhotosGeo{Query: "q:\"fooBar baz\"", Favorite: true}
form := &SearchPhotosGeo{Query: "q:\"fooBar baz\"", Favorite: "true"}

assert.Equal(t, "q:\"q:fooBar baz\" favorite:true", form.Serialize())
}

func TestSearchPhotosGeo_SerializeAll(t *testing.T) {
form := &SearchPhotosGeo{Query: "q:\"fooBar baz\"", Favorite: true}
form := &SearchPhotosGeo{Query: "q:\"fooBar baz\"", Favorite: "true"}

assert.Equal(t, "q:\"q:fooBar baz\" favorite:true", form.SerializeAll())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/form/search_photos_test.go
Expand Up @@ -120,7 +120,7 @@ func TestParseQueryString(t *testing.T) {
assert.Equal(t, "fooBar baz", form.Query)
assert.Equal(t, "23", form.Camera)
assert.Equal(t, time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), form.Before)
assert.Equal(t, false, form.Favorite)
assert.Equal(t, "false", form.Favorite)
assert.Equal(t, uint(0x61a8), form.Dist)
assert.Equal(t, float32(33.45343), form.Lat)
})
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestParseQueryString(t *testing.T) {
t.Fatal(err)
}

assert.True(t, form.Favorite)
assert.Equal(t, "cat", form.Favorite)
})
t.Run("query for primary with uncommon bool value", func(t *testing.T) {
form := &SearchPhotos{Query: "primary:&cat"}
Expand Down
8 changes: 5 additions & 3 deletions internal/search/photos.go
Expand Up @@ -300,7 +300,7 @@ func searchPhotos(f form.SearchPhotos, sess *entity.Session, resultCols string)
f.Raw = true
case terms["favorites"]:
f.Query = strings.ReplaceAll(f.Query, "favorites", "")
f.Favorite = true
f.Favorite = "true"
case terms["stacks"]:
f.Query = strings.ReplaceAll(f.Query, "stacks", "")
f.Stack = true
Expand Down Expand Up @@ -478,8 +478,10 @@ func searchPhotos(f form.SearchPhotos, sess *entity.Session, resultCols string)
s = s.Where("files.file_main_color IN (?)", SplitOr(strings.ToLower(f.Color)))
}

// Find favorites only.
if f.Favorite {
// Filter by favorite flag.
if txt.No(f.Favorite) {
s = s.Where("photos.photo_favorite = 0")
} else if txt.NotEmpty(f.Favorite) {
s = s.Where("photos.photo_favorite = 1")
}

Expand Down
8 changes: 5 additions & 3 deletions internal/search/photos_geo.go
Expand Up @@ -230,7 +230,7 @@ func UserPhotosGeo(f form.SearchPhotosGeo, sess *entity.Session) (results GeoRes
f.Raw = true
case terms["favorites"]:
f.Query = strings.ReplaceAll(f.Query, "favorites", "")
f.Favorite = true
f.Favorite = "true"
case terms["panoramas"]:
f.Query = strings.ReplaceAll(f.Query, "panoramas", "")
f.Panorama = true
Expand Down Expand Up @@ -386,8 +386,10 @@ func UserPhotosGeo(f form.SearchPhotosGeo, sess *entity.Session) (results GeoRes
s = s.Where("files.file_main_color IN (?)", SplitOr(strings.ToLower(f.Color)))
}

// Find favorites only.
if f.Favorite {
// Filter by favorite flag.
if txt.No(f.Favorite) {
s = s.Where("photos.photo_favorite = 0")
} else if txt.NotEmpty(f.Favorite) {
s = s.Where("photos.photo_favorite = 1")
}

Expand Down
8 changes: 4 additions & 4 deletions internal/search/photos_geo_test.go
Expand Up @@ -126,7 +126,7 @@ func TestGeo(t *testing.T) {
Query: "",
Before: time.Time{},
After: time.Time{},
Favorite: true,
Favorite: "true",
Lat: 1.234,
Lng: 4.321,
S2: "",
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestGeo(t *testing.T) {
Query: "",
Before: time.Time{},
After: time.Time{},
Favorite: false,
Favorite: "false",
Lat: 0,
Lng: 0,
S2: "",
Expand All @@ -177,7 +177,7 @@ func TestGeo(t *testing.T) {
Query: "",
Before: time.Time{},
After: time.Time{},
Favorite: false,
Favorite: "false",
Lat: 0,
Lng: 0,
S2: "85",
Expand All @@ -200,7 +200,7 @@ func TestGeo(t *testing.T) {
Query: "",
Before: time.Time{},
After: time.Time{},
Favorite: false,
Favorite: "false",
Lat: 0,
Lng: 0,
S2: "",
Expand Down

0 comments on commit 20d20c7

Please sign in to comment.