2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-04-17 21:20:38 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2020-05-26 11:00:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2020-02-02 02:00:47 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-04 18:26:35 +02:00
|
|
|
)
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
type Labels []Label
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Label is used for photo, album and location categorization
|
2019-06-04 18:26:35 +02:00
|
|
|
type Label struct {
|
2020-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"-"`
|
|
|
|
LabelUID string `gorm:"type:varbinary(36);unique_index;" json:"UID" yaml:"UID"`
|
|
|
|
LabelSlug string `gorm:"type:varbinary(255);unique_index;" json:"Slug" yaml:"-"`
|
|
|
|
CustomSlug string `gorm:"type:varbinary(255);index;" json:"CustomSlug" yaml:"-"`
|
|
|
|
LabelName string `gorm:"type:varchar(255);" json:"Name" yaml:"Name"`
|
|
|
|
LabelPriority int `gorm:"type:varchar(255);" json:"Priority" yaml:"Priority,omitempty"`
|
|
|
|
LabelFavorite bool `gorm:"type:varchar(255);" json:"Favorite" yaml:"Favorite,omitempty"`
|
|
|
|
LabelDescription string `gorm:"type:text;" json:"Description" yaml:"Description,omitempty"`
|
|
|
|
LabelNotes string `gorm:"type:text;" json:"Notes" yaml:"Notes,omitempty"`
|
|
|
|
LabelCategories []*Label `gorm:"many2many:categories;association_jointable_foreignkey:category_id" json:"-" yaml:"-"`
|
2020-05-25 19:10:44 +02:00
|
|
|
Links []Link `gorm:"foreignkey:share_uid;association_foreignkey:label_uid" json:"Links" yaml:"-"`
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoCount int `gorm:"default:1" json:"PhotoCount" yaml:"-"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"DeletedAt,omitempty" yaml:"-"`
|
|
|
|
New bool `gorm:"-" json:"-" yaml:"-"`
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
|
2019-12-16 23:33:52 +01:00
|
|
|
func (m *Label) BeforeCreate(scope *gorm.Scope) error {
|
2020-05-27 13:40:21 +02:00
|
|
|
if rnd.IsUID(m.LabelUID, 'l') {
|
2020-05-01 12:57:26 +02:00
|
|
|
return nil
|
2019-12-16 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
return scope.SetColumn("LabelUID", rnd.PPID('l'))
|
2019-12-16 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
// NewLabel returns a new label.
|
2020-04-26 14:31:33 +02:00
|
|
|
func NewLabel(name string, priority int) *Label {
|
|
|
|
labelName := txt.Clip(name, txt.ClipDefault)
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
if labelName == "" {
|
|
|
|
labelName = "Unknown"
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
labelName = txt.Title(labelName)
|
|
|
|
labelSlug := slug.Make(txt.Clip(labelName, txt.ClipSlug))
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
result := &Label{
|
2019-06-09 04:37:02 +02:00
|
|
|
LabelSlug: labelSlug,
|
2020-04-17 21:20:38 +02:00
|
|
|
CustomSlug: labelSlug,
|
|
|
|
LabelName: labelName,
|
2020-04-26 14:31:33 +02:00
|
|
|
LabelPriority: priority,
|
2020-05-10 19:43:49 +02:00
|
|
|
PhotoCount: 1,
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
// Save updates the existing or inserts a new label.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Label) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
// Create inserts the label to the database.
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Label) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
// Delete removes the label from the database.
|
|
|
|
func (m *Label) Delete() error {
|
|
|
|
Db().Where("label_id = ? OR category_id = ?", m.ID, m.ID).Delete(&Category{})
|
|
|
|
Db().Where("label_id = ?", m.ID).Delete(&PhotoLabel{})
|
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleted returns true if the label is deleted.
|
|
|
|
func (m *Label) Deleted() bool {
|
|
|
|
return m.DeletedAt != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the label from the database.
|
|
|
|
func (m *Label) Restore() error {
|
|
|
|
if m.Deleted() {
|
|
|
|
return UnscopedDb().Model(m).Update("DeletedAt", nil).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a label property in the database.
|
2020-05-30 14:52:47 +02:00
|
|
|
func (m *Label) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
// FirstOrCreateLabel returns the existing label, inserts a new label or nil in case of errors.
|
2020-05-26 11:00:39 +02:00
|
|
|
func FirstOrCreateLabel(m *Label) *Label {
|
|
|
|
result := Label{}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
if err := UnscopedDb().Where("label_slug = ? OR custom_slug = ?", m.LabelSlug, m.CustomSlug).First(&result).Error; err == nil {
|
2020-05-26 11:00:39 +02:00
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("label: %s", err)
|
2020-05-26 11:00:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.LabelPriority >= 0 {
|
|
|
|
event.EntitiesCreated("labels", []*Label{m})
|
|
|
|
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2019-12-11 04:12:54 +01:00
|
|
|
|
2020-05-28 21:20:42 +02:00
|
|
|
// FindLabel returns an existing row if exists.
|
|
|
|
func FindLabel(s string) *Label {
|
|
|
|
labelSlug := slug.Make(txt.Clip(s, txt.ClipSlug))
|
|
|
|
|
|
|
|
result := Label{}
|
|
|
|
|
|
|
|
if err := Db().Where("label_slug = ? OR custom_slug = ?", labelSlug, labelSlug).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// AfterCreate sets the New column used for database callback
|
2019-12-11 04:12:54 +01:00
|
|
|
func (m *Label) AfterCreate(scope *gorm.Scope) error {
|
2020-05-19 12:30:26 +02:00
|
|
|
m.New = true
|
|
|
|
return nil
|
2019-12-11 04:12:54 +01:00
|
|
|
}
|
2020-02-02 02:00:47 +01:00
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
// SetName changes the label name.
|
|
|
|
func (m *Label) SetName(name string) {
|
|
|
|
newName := txt.Clip(name, txt.ClipDefault)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
if newName == "" {
|
2020-04-17 21:20:38 +02:00
|
|
|
return
|
2020-02-02 02:00:47 +01:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
m.LabelName = txt.Title(newName)
|
|
|
|
m.CustomSlug = slug.Make(txt.Clip(name, txt.ClipSlug))
|
2020-04-17 21:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a label if necessary
|
2020-05-26 11:00:39 +02:00
|
|
|
func (m *Label) UpdateClassify(label classify.Label) error {
|
2020-04-17 21:20:38 +02:00
|
|
|
save := false
|
2020-04-30 20:07:03 +02:00
|
|
|
db := Db()
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
if m.LabelPriority != label.Priority {
|
|
|
|
m.LabelPriority = label.Priority
|
|
|
|
save = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.CustomSlug == "" {
|
|
|
|
m.CustomSlug = m.LabelSlug
|
|
|
|
save = true
|
|
|
|
} else if m.LabelSlug == "" {
|
|
|
|
m.LabelSlug = m.CustomSlug
|
|
|
|
save = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.CustomSlug == m.LabelSlug && label.Title() != m.LabelName {
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetName(label.Title())
|
2020-04-17 21:20:38 +02:00
|
|
|
save = true
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
if save {
|
|
|
|
if err := db.Save(m).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-17 21:20:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
// Add categories
|
|
|
|
for _, category := range label.Categories {
|
2020-05-26 11:00:39 +02:00
|
|
|
sn := FirstOrCreateLabel(NewLabel(txt.Title(category), -3))
|
|
|
|
|
|
|
|
if sn == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:57:12 +02:00
|
|
|
if sn.Deleted() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:20:54 +02:00
|
|
|
if err := db.Model(m).Association("LabelCategories").Append(sn).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-02 02:00:47 +01:00
|
|
|
}
|