2021-08-12 04:54:20 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-08-19 23:12:51 +02:00
|
|
|
"fmt"
|
|
|
|
|
2021-08-12 04:54:20 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2021-08-12 04:54:20 +02:00
|
|
|
)
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
// People returns the sorted names of the first 2000 people.
|
|
|
|
func People() (people entity.People, err error) {
|
|
|
|
err = UnscopedDb().
|
|
|
|
Table(entity.Subject{}.TableName()).
|
2021-10-06 15:27:17 +02:00
|
|
|
Select("subj_uid, subj_name, subj_alias, subj_favorite, subj_hidden").
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("deleted_at IS NULL AND subj_type = ?", entity.SubjPerson).
|
2021-09-30 13:44:23 +02:00
|
|
|
Order("subj_name").
|
2021-09-03 16:14:09 +02:00
|
|
|
Limit(2000).Offset(0).
|
|
|
|
Scan(&people).Error
|
|
|
|
|
|
|
|
return people, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeopleCount returns the total number of people in the index.
|
|
|
|
func PeopleCount() (count int, err error) {
|
|
|
|
err = Db().
|
|
|
|
Table(entity.Subject{}.TableName()).
|
|
|
|
Where("deleted_at IS NULL").
|
2021-10-06 15:27:17 +02:00
|
|
|
Where("subj_hidden = 0").
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_type = ?", entity.SubjPerson).
|
2021-09-03 16:14:09 +02:00
|
|
|
Count(&count).Error
|
|
|
|
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
// Subjects returns subjects from the index.
|
|
|
|
func Subjects(limit, offset int) (result entity.Subjects, err error) {
|
2021-08-12 04:54:20 +02:00
|
|
|
stmt := Db()
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
stmt = stmt.Order("subj_name").Limit(limit).Offset(offset)
|
2021-08-12 04:54:20 +02:00
|
|
|
err = stmt.Find(&result).Error
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
2021-08-19 23:12:51 +02:00
|
|
|
|
2021-08-28 15:26:26 +02:00
|
|
|
// SubjectMap returns a map of subjects indexed by UID.
|
|
|
|
func SubjectMap() (result map[string]entity.Subject, err error) {
|
|
|
|
result = make(map[string]entity.Subject)
|
|
|
|
|
|
|
|
var subj entity.Subjects
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
stmt := Db()
|
2021-08-28 15:26:26 +02:00
|
|
|
|
|
|
|
if err = stmt.Find(&subj).Error; err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range subj {
|
2021-09-17 14:26:12 +02:00
|
|
|
result[s.SubjUID] = s
|
2021-08-28 15:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// RemoveOrphanSubjects permanently removes dangling marker subjects from the index.
|
2021-09-24 13:13:59 +02:00
|
|
|
func RemoveOrphanSubjects() (removed int64, err error) {
|
2021-08-22 16:14:34 +02:00
|
|
|
res := UnscopedDb().
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_src = ?", entity.SrcMarker).
|
|
|
|
Where(fmt.Sprintf("subj_uid NOT IN (SELECT subj_uid FROM %s)", entity.Face{}.TableName())).
|
|
|
|
Where(fmt.Sprintf("subj_uid NOT IN (SELECT subj_uid FROM %s)", entity.Marker{}.TableName())).
|
2021-08-22 16:14:34 +02:00
|
|
|
Delete(&entity.Subject{})
|
|
|
|
|
|
|
|
return res.RowsAffected, res.Error
|
2021-08-19 23:12:51 +02:00
|
|
|
}
|
2021-08-24 14:27:34 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// CreateMarkerSubjects adds and references known marker subjects.
|
|
|
|
func CreateMarkerSubjects() (affected int64, err error) {
|
2021-08-24 14:27:34 +02:00
|
|
|
var markers entity.Markers
|
|
|
|
|
|
|
|
if err := Db().
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_uid = '' AND marker_name <> '' AND subj_src <> ?", entity.SrcAuto).
|
2021-08-24 14:27:34 +02:00
|
|
|
Where("marker_invalid = 0 AND marker_type = ?", entity.MarkerFace).
|
|
|
|
Order("marker_name").
|
|
|
|
Find(&markers).Error; err != nil {
|
|
|
|
return affected, err
|
|
|
|
} else if len(markers) == 0 {
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var name string
|
|
|
|
var subj *entity.Subject
|
|
|
|
|
|
|
|
for _, m := range markers {
|
|
|
|
if name == m.MarkerName && subj != nil {
|
|
|
|
// Do nothing.
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if subj = entity.NewSubject(m.MarkerName, entity.SubjPerson, entity.SrcMarker); subj == nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("faces: invalid subject %s", sanitize.Log(m.MarkerName))
|
2021-08-24 14:27:34 +02:00
|
|
|
continue
|
|
|
|
} else if subj = entity.FirstOrCreateSubject(subj); subj == nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("faces: failed adding subject %s", sanitize.Log(m.MarkerName))
|
2021-08-24 14:27:34 +02:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
affected++
|
|
|
|
}
|
|
|
|
|
|
|
|
name = m.MarkerName
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
if err := m.Updates(entity.Values{"SubjUID": subj.SubjUID, "MarkerReview": false}); err != nil {
|
2021-08-24 14:27:34 +02:00
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.FaceID == "" {
|
|
|
|
continue
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if err := Db().Model(&entity.Face{}).Where("id = ? AND subj_uid = ''", m.FaceID).Update("SubjUID", subj.SubjUID).Error; err != nil {
|
2021-08-24 14:27:34 +02:00
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, err
|
|
|
|
}
|