2021-08-16 00:29:36 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2021-08-16 01:45:36 +02:00
|
|
|
"encoding/json"
|
2021-08-16 00:29:36 +02:00
|
|
|
"fmt"
|
2021-09-23 23:46:17 +02:00
|
|
|
"strings"
|
2021-08-16 00:29:36 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2021-10-06 15:27:17 +02:00
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2021-10-06 15:27:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2021-12-15 12:24:05 +01:00
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2021-08-16 00:29:36 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var subjectMutex = sync.Mutex{}
|
|
|
|
|
|
|
|
// Subject represents a named photo subject, typically a person.
|
|
|
|
type Subject struct {
|
2021-09-17 14:26:12 +02:00
|
|
|
SubjUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"UID" yaml:"UID"`
|
2021-09-21 12:11:51 +02:00
|
|
|
SubjType string `gorm:"type:VARBINARY(8);default:'';" json:"Type,omitempty" yaml:"Type,omitempty"`
|
|
|
|
SubjSrc string `gorm:"type:VARBINARY(8);default:'';" json:"Src,omitempty" yaml:"Src,omitempty"`
|
2021-09-23 23:46:17 +02:00
|
|
|
SubjSlug string `gorm:"type:VARBINARY(160);index;default:'';" json:"Slug" yaml:"-"`
|
|
|
|
SubjName string `gorm:"type:VARCHAR(160);unique_index;default:'';" json:"Name" yaml:"Name"`
|
|
|
|
SubjAlias string `gorm:"type:VARCHAR(160);default:'';" json:"Alias" yaml:"Alias"`
|
2021-09-17 14:39:08 +02:00
|
|
|
SubjBio string `gorm:"type:TEXT;" json:"Bio" yaml:"Bio,omitempty"`
|
|
|
|
SubjNotes string `gorm:"type:TEXT;" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
2021-09-23 23:46:17 +02:00
|
|
|
SubjFavorite bool `gorm:"default:false;" json:"Favorite" yaml:"Favorite,omitempty"`
|
2021-10-06 15:27:17 +02:00
|
|
|
SubjHidden bool `gorm:"default:false;" json:"Hidden" yaml:"Hidden,omitempty"`
|
2021-09-23 23:46:17 +02:00
|
|
|
SubjPrivate bool `gorm:"default:false;" json:"Private" yaml:"Private,omitempty"`
|
|
|
|
SubjExcluded bool `gorm:"default:false;" json:"Excluded" yaml:"Excluded,omitempty"`
|
|
|
|
FileCount int `gorm:"default:0;" json:"FileCount" yaml:"-"`
|
2021-10-01 16:34:29 +02:00
|
|
|
PhotoCount int `gorm:"default:0;" json:"PhotoCount" yaml:"-"`
|
2021-09-23 23:46:17 +02:00
|
|
|
Thumb string `gorm:"type:VARBINARY(128);index;default:'';" json:"Thumb" yaml:"Thumb,omitempty"`
|
|
|
|
ThumbSrc string `gorm:"type:VARBINARY(8);default:'';" json:"ThumbSrc,omitempty" yaml:"ThumbSrc,omitempty"`
|
2021-08-30 18:58:27 +02:00
|
|
|
MetadataJSON json.RawMessage `gorm:"type:MEDIUMBLOB;" json:"Metadata,omitempty" yaml:"Metadata,omitempty"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"DeletedAt,omitempty" yaml:"-"`
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (Subject) TableName() string {
|
2021-09-21 12:11:51 +02:00
|
|
|
return "subjects"
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
|
|
|
func (m *Subject) BeforeCreate(scope *gorm.Scope) error {
|
2021-09-17 14:26:12 +02:00
|
|
|
if rnd.IsUID(m.SubjUID, 'j') {
|
2021-08-16 00:29:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
return scope.SetColumn("SubjUID", rnd.PPID('j'))
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSubject returns a new entity.
|
2021-09-17 14:26:12 +02:00
|
|
|
func NewSubject(name, subjType, subjSrc string) *Subject {
|
2021-08-24 14:27:34 +02:00
|
|
|
// Name is required.
|
2021-09-23 23:46:17 +02:00
|
|
|
if strings.TrimSpace(name) == "" {
|
2021-08-24 14:27:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if subjType == "" {
|
|
|
|
subjType = SubjPerson
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
result := &Subject{
|
2021-09-17 14:26:12 +02:00
|
|
|
SubjType: subjType,
|
|
|
|
SubjSrc: subjSrc,
|
|
|
|
FileCount: 1,
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
if err := result.SetName(name); err != nil {
|
|
|
|
log.Errorf("subject: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new entity.
|
|
|
|
func (m *Subject) Save() error {
|
|
|
|
subjectMutex.Lock()
|
|
|
|
defer subjectMutex.Unlock()
|
|
|
|
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts the entity to the database.
|
|
|
|
func (m *Subject) Create() error {
|
|
|
|
subjectMutex.Lock()
|
|
|
|
defer subjectMutex.Unlock()
|
|
|
|
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
// Delete marks the entity as deleted in the database.
|
2021-08-16 00:29:36 +02:00
|
|
|
func (m *Subject) Delete() error {
|
2021-09-03 16:14:09 +02:00
|
|
|
if m.Deleted() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:09:34 +02:00
|
|
|
subjectMutex.Lock()
|
|
|
|
defer subjectMutex.Unlock()
|
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("subject: deleting %s %s", TypeString(m.SubjType), sanitize.Log(m.SubjName))
|
2021-09-17 14:26:12 +02:00
|
|
|
|
|
|
|
event.EntitiesDeleted("subjects", []string{m.SubjUID})
|
2021-09-03 16:14:09 +02:00
|
|
|
|
|
|
|
if m.IsPerson() {
|
2021-09-17 14:26:12 +02:00
|
|
|
event.EntitiesDeleted("people", []string{m.SubjUID})
|
2021-09-03 16:14:09 +02:00
|
|
|
event.Publish("count.people", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:09:34 +02:00
|
|
|
if err := Db().Model(&Face{}).Where("subj_uid = ?", m.SubjUID).Update("subj_uid", "").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:19:07 +02:00
|
|
|
// AfterDelete resets file and photo counters when the entity was deleted.
|
|
|
|
func (m *Subject) AfterDelete(tx *gorm.DB) (err error) {
|
|
|
|
tx.Model(m).Updates(Values{
|
|
|
|
"FileCount": 0,
|
|
|
|
"PhotoCount": 0,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
// Deleted returns true if the entity is deleted.
|
|
|
|
func (m *Subject) Deleted() bool {
|
|
|
|
return m.DeletedAt != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores the entity in the database.
|
|
|
|
func (m *Subject) Restore() error {
|
|
|
|
if m.Deleted() {
|
2021-09-01 21:16:08 +02:00
|
|
|
m.DeletedAt = nil
|
2021-09-03 16:14:09 +02:00
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("subject: restoring %s %s", TypeString(m.SubjType), sanitize.Log(m.SubjName))
|
2021-09-17 14:26:12 +02:00
|
|
|
|
|
|
|
event.EntitiesCreated("subjects", []*Subject{m})
|
2021-09-03 16:14:09 +02:00
|
|
|
|
|
|
|
if m.IsPerson() {
|
|
|
|
event.EntitiesCreated("people", []*Person{m.Person()})
|
|
|
|
event.Publish("count.people", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:46:03 +02:00
|
|
|
return UnscopedDb().Model(m).UpdateColumn("DeletedAt", nil).Error
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates an entity value in the database.
|
|
|
|
func (m *Subject) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates multiple values in the database.
|
|
|
|
func (m *Subject) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).Updates(values).Error
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:12:51 +02:00
|
|
|
// FirstOrCreateSubject returns the existing entity, inserts a new entity or nil in case of errors.
|
2021-08-16 00:29:36 +02:00
|
|
|
func FirstOrCreateSubject(m *Subject) *Subject {
|
2021-09-01 20:46:15 +02:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if m.SubjName == "" {
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
if found := FindSubjectByName(m.SubjName); found != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
return found
|
2021-08-16 00:29:36 +02:00
|
|
|
} else if createErr := m.Create(); createErr == nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("subject: added %s %s", TypeString(m.SubjType), sanitize.Log(m.SubjName))
|
2021-09-17 14:26:12 +02:00
|
|
|
|
|
|
|
event.EntitiesCreated("subjects", []*Subject{m})
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
if m.IsPerson() {
|
|
|
|
event.EntitiesCreated("people", []*Person{m.Person()})
|
2021-08-16 00:29:36 +02:00
|
|
|
event.Publish("count.people", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if found = FindSubjectByName(m.SubjName); found != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
return found
|
2021-08-16 00:29:36 +02:00
|
|
|
} else {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Errorf("subject: %s while creating %s", createErr, sanitize.Log(m.SubjName))
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:12:51 +02:00
|
|
|
// FindSubject returns an existing entity if exists.
|
2021-08-16 00:29:36 +02:00
|
|
|
func FindSubject(s string) *Subject {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := Subject{}
|
|
|
|
|
2021-09-24 22:46:03 +02:00
|
|
|
if err := UnscopedDb().Where("subj_uid = ?", s).First(&result).Error; err != nil {
|
2021-08-16 00:29:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// FindSubjectByName find an existing subject by name.
|
2021-09-19 13:35:44 +02:00
|
|
|
func FindSubjectByName(name string) *Subject {
|
2021-12-15 12:24:05 +01:00
|
|
|
name = sanitize.Name(name)
|
2021-09-19 13:35:44 +02:00
|
|
|
|
|
|
|
if name == "" {
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := Subject{}
|
|
|
|
|
2021-09-01 21:16:08 +02:00
|
|
|
// Search database.
|
2021-09-24 22:46:03 +02:00
|
|
|
if err := UnscopedDb().Where("subj_name LIKE ?", name).First(&result).Error; err != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:16:08 +02:00
|
|
|
// Restore if currently deleted.
|
|
|
|
if err := result.Restore(); err != nil {
|
2021-09-17 14:26:12 +02:00
|
|
|
log.Errorf("subject: %s could not be restored", result.SubjUID)
|
2021-09-01 21:16:08 +02:00
|
|
|
} else {
|
2021-09-17 14:26:12 +02:00
|
|
|
log.Debugf("subject: %s restored", result.SubjUID)
|
2021-09-01 21:16:08 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
// IsPerson tests if the subject is a person.
|
|
|
|
func (m *Subject) IsPerson() bool {
|
2021-09-17 14:26:12 +02:00
|
|
|
return m.SubjType == SubjPerson
|
2021-09-03 16:14:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Person creates and returns a Person based on this subject.
|
|
|
|
func (m *Subject) Person() *Person {
|
|
|
|
return NewPerson(*m)
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
// SetName changes the subject's name.
|
|
|
|
func (m *Subject) SetName(name string) error {
|
2021-12-15 12:24:05 +01:00
|
|
|
name = sanitize.Name(name)
|
2021-08-16 00:29:36 +02:00
|
|
|
|
2021-10-06 15:27:17 +02:00
|
|
|
if name == m.SubjName {
|
|
|
|
// Nothing to do.
|
|
|
|
return nil
|
|
|
|
} else if name == "" {
|
2021-09-23 23:46:17 +02:00
|
|
|
return fmt.Errorf("name must not be empty")
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:35:44 +02:00
|
|
|
m.SubjName = name
|
2021-09-23 23:46:17 +02:00
|
|
|
m.SubjSlug = txt.Slug(name)
|
2021-08-16 00:29:36 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:27:17 +02:00
|
|
|
// Visible tests if the subject is generally visible and not hidden in any way.
|
|
|
|
func (m *Subject) Visible() bool {
|
|
|
|
return m.DeletedAt == nil && !m.SubjHidden && !m.SubjExcluded && !m.SubjPrivate
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveForm updates the subject from form values.
|
|
|
|
func (m *Subject) SaveForm(f form.Subject) (changed bool, err error) {
|
|
|
|
if m.SubjUID == "" {
|
|
|
|
return false, fmt.Errorf("subject uid is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change name?
|
2021-12-15 12:24:05 +01:00
|
|
|
if name := sanitize.Name(f.SubjName); name != "" && name != m.SubjName {
|
2021-10-06 15:27:17 +02:00
|
|
|
existing, err := m.UpdateName(name)
|
|
|
|
|
|
|
|
if existing.SubjUID != m.SubjUID || err != nil {
|
|
|
|
return err != nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change favorite status?
|
|
|
|
if m.SubjFavorite != f.SubjFavorite {
|
|
|
|
m.SubjFavorite = f.SubjFavorite
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change visibility?
|
|
|
|
if m.SubjHidden != f.SubjHidden || m.SubjPrivate != f.SubjPrivate || m.SubjExcluded != f.SubjExcluded {
|
|
|
|
m.SubjHidden = f.SubjHidden
|
|
|
|
m.SubjPrivate = f.SubjPrivate
|
|
|
|
m.SubjExcluded = f.SubjExcluded
|
|
|
|
|
|
|
|
// Update counter.
|
|
|
|
if !m.IsPerson() {
|
|
|
|
// Ignore.
|
|
|
|
} else if m.Visible() {
|
|
|
|
event.Publish("count.people", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
event.Publish("count.people", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update index?
|
|
|
|
if changed {
|
|
|
|
values := Values{
|
|
|
|
"SubjFavorite": m.SubjFavorite,
|
|
|
|
"SubjHidden": m.SubjHidden,
|
|
|
|
"SubjPrivate": m.SubjPrivate,
|
|
|
|
"SubjExcluded": m.SubjExcluded,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Updates(values); err == nil {
|
|
|
|
event.EntitiesUpdated("subjects", []*Subject{m})
|
|
|
|
|
|
|
|
if m.IsPerson() {
|
|
|
|
event.EntitiesUpdated("people", []*Person{m.Person()})
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
// UpdateName changes and saves the subject's name in the index.
|
2021-09-01 20:46:15 +02:00
|
|
|
func (m *Subject) UpdateName(name string) (*Subject, error) {
|
2021-08-16 00:29:36 +02:00
|
|
|
if err := m.SetName(name); err != nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
return m, err
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if err := m.Updates(Values{"SubjName": m.SubjName, "SubjSlug": m.SubjSlug}); err == nil {
|
2021-12-14 20:01:39 +01:00
|
|
|
log.Infof("subject: renamed %s %s", TypeString(m.SubjType), sanitize.Log(m.SubjName))
|
2021-09-17 14:26:12 +02:00
|
|
|
|
|
|
|
event.EntitiesUpdated("subjects", []*Subject{m})
|
2021-09-03 16:14:09 +02:00
|
|
|
|
|
|
|
if m.IsPerson() {
|
|
|
|
event.EntitiesUpdated("people", []*Person{m.Person()})
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
return m, m.UpdateMarkerNames()
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if existing := FindSubjectByName(m.SubjName); existing == nil {
|
2021-09-01 20:46:15 +02:00
|
|
|
return m, err
|
|
|
|
} else {
|
|
|
|
return existing, m.MergeWith(existing)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMarkerNames updates related marker names.
|
|
|
|
func (m *Subject) UpdateMarkerNames() error {
|
2021-09-17 14:26:12 +02:00
|
|
|
if m.SubjName == "" {
|
2021-09-05 21:34:51 +02:00
|
|
|
return fmt.Errorf("subject name is empty")
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if m.SubjUID == "" {
|
2021-09-05 21:34:51 +02:00
|
|
|
return fmt.Errorf("subject uid is empty")
|
|
|
|
}
|
|
|
|
|
2021-09-06 00:52:10 +02:00
|
|
|
if err := Db().Model(&Marker{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_uid = ? AND subj_src <> ?", m.SubjUID, SrcAuto).
|
|
|
|
Where("marker_name <> ?", m.SubjName).
|
2021-09-24 22:46:03 +02:00
|
|
|
UpdateColumn("marker_name", m.SubjName).Error; err != nil {
|
2021-09-06 00:52:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.RefreshPhotos()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshPhotos flags related photos for metadata maintenance.
|
2021-09-06 01:16:36 +02:00
|
|
|
func (m *Subject) RefreshPhotos() error {
|
2021-09-17 14:26:12 +02:00
|
|
|
if m.SubjUID == "" {
|
2021-09-06 00:52:10 +02:00
|
|
|
return fmt.Errorf("empty subject uid")
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:12:08 +01:00
|
|
|
var err error
|
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
update := fmt.Sprintf(`UPDATE photos p JOIN files f ON f.photo_id = p.id JOIN %s m ON m.file_uid = f.file_uid
|
|
|
|
SET p.checked_at = NULL WHERE m.subj_uid = ?`, Marker{}.TableName())
|
|
|
|
err = UnscopedDb().Exec(update, m.SubjUID).Error
|
|
|
|
default:
|
|
|
|
update := fmt.Sprintf(`UPDATE photos SET checked_at = NULL WHERE id IN (SELECT f.photo_id FROM files f
|
|
|
|
JOIN %s m ON m.file_uid = f.file_uid WHERE m.subj_uid = ?)`, Marker{}.TableName())
|
|
|
|
err = UnscopedDb().Exec(update, m.SubjUID).Error
|
|
|
|
}
|
2021-09-24 22:46:03 +02:00
|
|
|
|
2022-01-03 11:12:08 +01:00
|
|
|
return err
|
2021-09-01 20:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeWith merges this subject with another subject and then deletes it.
|
|
|
|
func (m *Subject) MergeWith(other *Subject) error {
|
|
|
|
if other == nil {
|
|
|
|
return fmt.Errorf("other subject is nil")
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if other.SubjUID == "" {
|
2021-09-01 20:46:15 +02:00
|
|
|
return fmt.Errorf("other subject's uid is empty")
|
2021-09-17 14:26:12 +02:00
|
|
|
} else if m.SubjUID == "" {
|
2021-09-01 20:46:15 +02:00
|
|
|
return fmt.Errorf("subject uid is empty")
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
// Update markers and faces with new SubjUID.
|
2021-09-01 20:46:15 +02:00
|
|
|
if err := Db().Model(&Marker{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_uid = ?", m.SubjUID).
|
2021-09-24 22:46:03 +02:00
|
|
|
UpdateColumn("subj_uid", other.SubjUID).Error; err != nil {
|
2021-08-16 00:29:36 +02:00
|
|
|
return err
|
2021-09-01 20:46:15 +02:00
|
|
|
} else if err := Db().Model(&Face{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_uid = ?", m.SubjUID).
|
2021-09-24 22:46:03 +02:00
|
|
|
UpdateColumn("subj_uid", other.SubjUID).Error; err != nil {
|
2021-08-29 13:42:34 +02:00
|
|
|
return err
|
2021-09-01 20:46:15 +02:00
|
|
|
} else if err := other.UpdateMarkerNames(); err != nil {
|
2021-08-29 13:42:34 +02:00
|
|
|
return err
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-06 11:19:07 +02:00
|
|
|
// Update file and photo counts.
|
|
|
|
if err := Db().Model(other).Updates(Values{
|
|
|
|
"FileCount": other.FileCount + m.FileCount,
|
|
|
|
"PhotoCount": other.PhotoCount + m.PhotoCount,
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
return m.Delete()
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Links returns all share links for this entity.
|
|
|
|
func (m *Subject) Links() Links {
|
2021-09-17 14:26:12 +02:00
|
|
|
return FindLinks("", m.SubjUID)
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|