photoprism/internal/entity/subject_person.go
Michael Mayer c74fcbf282 People: Show real name instead of uid in logs #1438 #2182
Since caching all subject data proved too complex in the time available,
this implementation uses a simple key/value lookup table to cache
subject names and perform backward searches by uid.
2022-04-04 14:21:43 +02:00

59 lines
1.3 KiB
Go

package entity
import (
"encoding/json"
"github.com/photoprism/photoprism/pkg/txt"
)
const (
SubjPerson = "person" // SubjType for people.
)
// People represents a list of people.
type People []Person
// Person represents a subject with type person.
type Person struct {
SubjUID string `json:"UID"`
SubjName string `json:"Name"`
SubjAlias string `json:"Alias"`
SubjFavorite bool `json:"Favorite"`
SubjHidden bool `json:"Hidden"`
}
// AfterFind is a hook that updates the name cache after querying.
func (m *Person) AfterFind() (err error) {
SubjNames.Set(m.SubjUID, m.SubjName)
return
}
// NewPerson returns a new entity.
func NewPerson(subj Subject) *Person {
result := &Person{
SubjUID: subj.SubjUID,
SubjName: subj.SubjName,
SubjAlias: subj.SubjAlias,
SubjFavorite: subj.SubjFavorite,
SubjHidden: subj.SubjHidden,
}
return result
}
// MarshalJSON returns the JSON encoding.
func (m *Person) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
UID string
Name string
Keywords []string `json:",omitempty"`
Favorite bool `json:",omitempty"`
Hidden bool `json:",omitempty"`
}{
UID: m.SubjUID,
Name: m.SubjName,
Keywords: txt.NameKeywords(m.SubjName, m.SubjAlias),
Favorite: m.SubjFavorite,
Hidden: m.SubjHidden,
})
}