2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2019-06-04 18:26:35 +02:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PhotoLabel represents the many-to-many relation between Photo and label.
|
|
|
|
// Labels are weighted by uncertainty (100 - confidence)
|
2019-06-04 18:26:35 +02:00
|
|
|
type PhotoLabel struct {
|
2019-06-09 04:37:02 +02:00
|
|
|
PhotoID uint `gorm:"primary_key;auto_increment:false"`
|
2019-07-03 19:58:53 +02:00
|
|
|
LabelID uint `gorm:"primary_key;auto_increment:false;index"`
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelUncertainty int
|
|
|
|
LabelSource string
|
|
|
|
Photo *Photo
|
|
|
|
Label *Label
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns PhotoLabel table identifier "photos_labels"
|
2019-06-04 18:26:35 +02:00
|
|
|
func (PhotoLabel) TableName() string {
|
2019-06-09 04:37:02 +02:00
|
|
|
return "photos_labels"
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewPhotoLabel registers a new PhotoLabel relation with an uncertainty and a source of label
|
|
|
|
func NewPhotoLabel(photoID, labelID uint, uncertainty int, source string) *PhotoLabel {
|
2019-06-04 18:26:35 +02:00
|
|
|
result := &PhotoLabel{
|
2020-02-21 01:14:45 +01:00
|
|
|
PhotoID: photoID,
|
|
|
|
LabelID: labelID,
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelUncertainty: uncertainty,
|
|
|
|
LabelSource: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// FirstOrCreate checks wether the PhotoLabel relation already exist in the database before the creation
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *PhotoLabel) FirstOrCreate(db *gorm.DB) *PhotoLabel {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2019-12-19 09:37:10 +01:00
|
|
|
if err := db.FirstOrCreate(m, "photo_id = ? AND label_id = ?", m.PhotoID, m.LabelID).Error; err != nil {
|
|
|
|
log.Errorf("photo label: %s", err)
|
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|