2020-01-05 14:18:40 +01:00
|
|
|
package query
|
2019-12-11 07:37:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
"github.com/gosimple/slug"
|
2019-12-11 16:55:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/capture"
|
2019-12-11 07:37:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// LabelResult contains found labels
|
|
|
|
type LabelResult struct {
|
|
|
|
// Label
|
|
|
|
ID uint
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt time.Time
|
2019-12-16 23:33:52 +01:00
|
|
|
LabelUUID string
|
2019-12-11 07:37:39 +01:00
|
|
|
LabelSlug string
|
|
|
|
LabelName string
|
|
|
|
LabelPriority int
|
|
|
|
LabelCount int
|
|
|
|
LabelFavorite bool
|
|
|
|
LabelDescription string
|
|
|
|
LabelNotes string
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// LabelBySlug returns a Label based on the slug name.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) LabelBySlug(labelSlug string) (label entity.Label, err error) {
|
|
|
|
if err := q.db.Where("label_slug = ?", labelSlug).First(&label).Error; err != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// LabelByUUID returns a Label based on the label UUID.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) LabelByUUID(labelUUID string) (label entity.Label, err error) {
|
|
|
|
if err := q.db.Where("label_uuid = ?", labelUUID).First(&label).Error; err != nil {
|
2019-12-16 23:33:52 +01:00
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// LabelThumbBySlug returns a label preview file based on the slug name.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) LabelThumbBySlug(labelSlug string) (file entity.File, err error) {
|
|
|
|
// q.db.LogMode(true)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if err := q.db.Where("files.file_primary AND files.deleted_at IS NULL").
|
2019-12-11 07:37:39 +01:00
|
|
|
Joins("JOIN labels ON labels.label_slug = ?", labelSlug).
|
|
|
|
Joins("JOIN photos_labels ON photos_labels.label_id = labels.id AND photos_labels.photo_id = files.photo_id").
|
|
|
|
Order("photos_labels.label_uncertainty ASC").
|
|
|
|
First(&file).Error; err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// LabelThumbByUUID returns a label preview file based on the label UUID.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) LabelThumbByUUID(labelUUID string) (file entity.File, err error) {
|
2019-12-17 04:39:23 +01:00
|
|
|
// Search matching label
|
2020-03-28 17:17:41 +01:00
|
|
|
err = q.db.Where("files.file_primary AND files.deleted_at IS NULL").
|
2019-12-16 23:33:52 +01:00
|
|
|
Joins("JOIN labels ON labels.label_uuid = ?", labelUUID).
|
|
|
|
Joins("JOIN photos_labels ON photos_labels.label_id = labels.id AND photos_labels.photo_id = files.photo_id").
|
|
|
|
Order("photos_labels.label_uncertainty ASC").
|
2019-12-17 04:39:23 +01:00
|
|
|
First(&file).Error
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return file, nil
|
2019-12-16 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 04:39:23 +01:00
|
|
|
// If failed, search for category instead
|
2020-03-28 17:17:41 +01:00
|
|
|
err = q.db.Where("files.file_primary AND files.deleted_at IS NULL").
|
2019-12-17 04:39:23 +01:00
|
|
|
Joins("JOIN photos_labels ON photos_labels.photo_id = files.photo_id").
|
|
|
|
Joins("JOIN categories c ON photos_labels.label_id = c.label_id").
|
|
|
|
Joins("JOIN labels ON c.category_id = labels.id AND labels.label_uuid= ?", labelUUID).
|
|
|
|
Order("photos_labels.label_uncertainty ASC").
|
|
|
|
First(&file).Error
|
|
|
|
|
|
|
|
return file, err
|
2019-12-16 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
// Labels searches labels based on their name.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) Labels(f form.LabelSearch) (results []LabelResult, err error) {
|
2019-12-11 07:37:39 +01:00
|
|
|
if err := f.ParseQueryString(); err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("labels: %+v", f)))
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s := q.db.NewScope(nil).DB()
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
// s.LogMode(true)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Table("labels").
|
2019-12-16 23:33:52 +01:00
|
|
|
Select(`labels.*`).
|
2019-12-11 07:37:39 +01:00
|
|
|
Where("labels.deleted_at IS NULL").
|
|
|
|
Group("labels.id")
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
if f.ID != "" {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.label_uuid = ?", f.ID)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2020-01-30 18:19:26 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
if f.Query != "" {
|
|
|
|
var labelIds []uint
|
2019-12-11 16:55:18 +01:00
|
|
|
var categories []entity.Category
|
|
|
|
var label entity.Label
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
slugString := slug.Make(f.Query)
|
2019-12-11 07:37:39 +01:00
|
|
|
likeString := "%" + strings.ToLower(f.Query) + "%"
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := q.db.First(&label, "label_slug = ?", slugString); result.Error != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
log.Infof("search: label \"%s\" not found", f.Query)
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("LOWER(labels.label_name) LIKE ?", likeString)
|
2019-12-11 07:37:39 +01:00
|
|
|
} else {
|
|
|
|
labelIds = append(labelIds, label.ID)
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
q.db.Where("category_id = ?", label.ID).Find(&categories)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
|
|
|
for _, category := range categories {
|
|
|
|
labelIds = append(labelIds, category.LabelID)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("search: label \"%s\" includes %d categories", label.LabelName, len(labelIds))
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.id IN (?)", labelIds)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Favorites {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.label_favorite = 1")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:13:09 +01:00
|
|
|
if !f.All {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.label_priority >= 0 OR labels.label_favorite = 1")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Order {
|
|
|
|
case "slug":
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Order("labels.label_favorite DESC, label_slug ASC")
|
2019-12-11 07:37:39 +01:00
|
|
|
default:
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Order("labels.label_favorite DESC, label_slug ASC")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Count > 0 && f.Count <= 1000 {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Limit(f.Count).Offset(f.Offset)
|
2019-12-11 07:37:39 +01:00
|
|
|
} else {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Limit(100).Offset(0)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|