mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-09 19:07:12 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
275 lines
7.3 KiB
Go
275 lines
7.3 KiB
Go
package model
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/deluan/rest"
|
|
"github.com/navidrome/navidrome/model/id"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"github.com/zeebo/xxh3"
|
|
)
|
|
|
|
type Tag struct {
|
|
ID string `json:"id,omitempty"`
|
|
TagName TagName `json:"tagName,omitempty"`
|
|
TagValue string `json:"tagValue,omitempty"`
|
|
AlbumCount int `json:"albumCount,omitempty"`
|
|
SongCount int `json:"songCount,omitempty"`
|
|
}
|
|
|
|
type TagList []Tag
|
|
|
|
func (l TagList) GroupByFrequency() Tags {
|
|
grouped := map[string]map[string]int{}
|
|
values := map[string]string{}
|
|
firstSeen := map[string]int{}
|
|
for i, t := range l {
|
|
if m, ok := grouped[string(t.TagName)]; !ok {
|
|
grouped[string(t.TagName)] = map[string]int{t.ID: 1}
|
|
} else {
|
|
m[t.ID]++
|
|
}
|
|
values[t.ID] = t.TagValue
|
|
if _, ok := firstSeen[t.ID]; !ok {
|
|
firstSeen[t.ID] = i
|
|
}
|
|
}
|
|
|
|
tags := Tags{}
|
|
for name, counts := range grouped {
|
|
idList := make([]string, 0, len(counts))
|
|
for tid := range counts {
|
|
idList = append(idList, tid)
|
|
}
|
|
slices.SortFunc(idList, func(a, b string) int {
|
|
return cmp.Or(
|
|
cmp.Compare(counts[b], counts[a]),
|
|
cmp.Compare(firstSeen[a], firstSeen[b]),
|
|
)
|
|
})
|
|
tags[TagName(name)] = slice.Map(idList, func(id string) string { return values[id] })
|
|
}
|
|
return tags
|
|
}
|
|
|
|
func (t Tag) String() string {
|
|
return fmt.Sprintf("%s=%s", t.TagName, t.TagValue)
|
|
}
|
|
|
|
func NewTag(name TagName, value string) Tag {
|
|
name = name.ToLower()
|
|
hashID := tagID(name, value)
|
|
return Tag{
|
|
ID: hashID,
|
|
TagName: name,
|
|
TagValue: value,
|
|
}
|
|
}
|
|
|
|
func tagID(name TagName, value string) string {
|
|
return id.NewTagID(string(name), value)
|
|
}
|
|
|
|
type RawTags map[string][]string
|
|
|
|
type Tags map[TagName][]string
|
|
|
|
func (t Tags) Values(name TagName) []string {
|
|
return t[name]
|
|
}
|
|
|
|
func (t Tags) First(name TagName) string {
|
|
if v := t[name]; len(v) > 0 {
|
|
return v[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (t Tags) IDs() []string {
|
|
var ids []string
|
|
for name, tag := range t {
|
|
name = name.ToLower()
|
|
for _, v := range tag {
|
|
ids = append(ids, tagID(name, v))
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func (t Tags) Flatten(name TagName) TagList {
|
|
var tags TagList
|
|
for _, v := range t[name] {
|
|
tags = append(tags, NewTag(name, v))
|
|
}
|
|
return tags
|
|
}
|
|
|
|
func (t Tags) FlattenAll() TagList {
|
|
var tags TagList
|
|
for name, values := range t {
|
|
for _, v := range values {
|
|
tags = append(tags, NewTag(name, v))
|
|
}
|
|
}
|
|
return tags
|
|
}
|
|
|
|
func (t Tags) Sort() {
|
|
for _, values := range t {
|
|
slices.Sort(values)
|
|
}
|
|
}
|
|
|
|
func (t Tags) Hash() []byte {
|
|
if len(t) == 0 {
|
|
return nil
|
|
}
|
|
ids := t.IDs()
|
|
slices.Sort(ids)
|
|
sum := xxh3.New()
|
|
sum.Write([]byte(strings.Join(ids, "|")))
|
|
return sum.Sum(nil)
|
|
}
|
|
|
|
func (t Tags) ToGenres() (string, Genres) {
|
|
values := t.Values("genre")
|
|
if len(values) == 0 {
|
|
return "", nil
|
|
}
|
|
genres := slice.Map(values, func(g string) Genre {
|
|
t := NewTag("genre", g)
|
|
return Genre{ID: t.ID, Name: g}
|
|
})
|
|
return genres[0].Name, genres
|
|
}
|
|
|
|
// Merge merges the tags from another Tags object into this one, removing any duplicates
|
|
func (t Tags) Merge(tags Tags) {
|
|
for name, values := range tags {
|
|
for _, v := range values {
|
|
t.Add(name, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t Tags) Add(name TagName, v string) {
|
|
if slices.Contains(t[name], v) {
|
|
return
|
|
}
|
|
t[name] = append(t[name], v)
|
|
}
|
|
|
|
type TagRepository interface {
|
|
rest.Repository[Tag]
|
|
Add(ctx context.Context, libraryID int, tags ...Tag) error
|
|
UpdateCounts(ctx context.Context) error
|
|
GetAll(ctx context.Context, name TagName, options ...QueryOptions) (TagList, error)
|
|
}
|
|
|
|
type TagName string
|
|
|
|
func (t TagName) ToLower() TagName {
|
|
return TagName(strings.ToLower(string(t)))
|
|
}
|
|
|
|
func (t TagName) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
// Tag names, as defined in the mappings.yaml file
|
|
const (
|
|
TagAlbum TagName = "album"
|
|
TagTitle TagName = "title"
|
|
TagTrackNumber TagName = "track"
|
|
TagDiscNumber TagName = "disc"
|
|
TagTotalTracks TagName = "tracktotal"
|
|
TagTotalDiscs TagName = "disctotal"
|
|
TagDiscSubtitle TagName = "discsubtitle"
|
|
TagSubtitle TagName = "subtitle"
|
|
TagGenre TagName = "genre"
|
|
TagMood TagName = "mood"
|
|
TagComment TagName = "comment"
|
|
TagAlbumSort TagName = "albumsort"
|
|
TagAlbumVersion TagName = "albumversion"
|
|
TagTitleSort TagName = "titlesort"
|
|
TagCompilation TagName = "compilation"
|
|
TagGrouping TagName = "grouping"
|
|
TagLyrics TagName = "lyrics"
|
|
TagRecordLabel TagName = "recordlabel"
|
|
TagReleaseType TagName = "releasetype"
|
|
TagReleaseCountry TagName = "releasecountry"
|
|
TagMedia TagName = "media"
|
|
TagCatalogNumber TagName = "catalognumber"
|
|
TagISRC TagName = "isrc"
|
|
TagBPM TagName = "bpm"
|
|
TagExplicitStatus TagName = "explicitstatus"
|
|
TagWork TagName = "work"
|
|
TagMovementName TagName = "movementname"
|
|
TagMovementNumber TagName = "movement"
|
|
TagMovementTotal TagName = "movementtotal"
|
|
|
|
// Dates and years
|
|
|
|
TagOriginalDate TagName = "originaldate"
|
|
TagReleaseDate TagName = "releasedate"
|
|
TagRecordingDate TagName = "recordingdate"
|
|
|
|
// Artists and roles
|
|
|
|
TagAlbumArtist TagName = "albumartist"
|
|
TagAlbumArtists TagName = "albumartists"
|
|
TagAlbumArtistSort TagName = "albumartistsort"
|
|
TagAlbumArtistsSort TagName = "albumartistssort"
|
|
TagTrackArtist TagName = "artist"
|
|
TagTrackArtists TagName = "artists"
|
|
TagTrackArtistSort TagName = "artistsort"
|
|
TagTrackArtistsSort TagName = "artistssort"
|
|
TagComposer TagName = "composer"
|
|
TagComposerSort TagName = "composersort"
|
|
TagLyricist TagName = "lyricist"
|
|
TagLyricistSort TagName = "lyricistsort"
|
|
TagDirector TagName = "director"
|
|
TagProducer TagName = "producer"
|
|
TagEngineer TagName = "engineer"
|
|
TagMixer TagName = "mixer"
|
|
TagRemixer TagName = "remixer"
|
|
TagDJMixer TagName = "djmixer"
|
|
TagConductor TagName = "conductor"
|
|
TagArranger TagName = "arranger"
|
|
TagPerformer TagName = "performer"
|
|
|
|
// ReplayGain
|
|
|
|
TagReplayGainAlbumGain TagName = "replaygain_album_gain"
|
|
TagReplayGainAlbumPeak TagName = "replaygain_album_peak"
|
|
TagReplayGainTrackGain TagName = "replaygain_track_gain"
|
|
TagReplayGainTrackPeak TagName = "replaygain_track_peak"
|
|
TagR128AlbumGain TagName = "r128_album_gain"
|
|
TagR128TrackGain TagName = "r128_track_gain"
|
|
|
|
// MusicBrainz
|
|
|
|
TagMusicBrainzArtistID TagName = "musicbrainz_artistid"
|
|
TagMusicBrainzRecordingID TagName = "musicbrainz_recordingid"
|
|
TagMusicBrainzTrackID TagName = "musicbrainz_trackid"
|
|
TagMusicBrainzAlbumArtistID TagName = "musicbrainz_albumartistid"
|
|
TagMusicBrainzAlbumID TagName = "musicbrainz_albumid"
|
|
TagMusicBrainzReleaseGroupID TagName = "musicbrainz_releasegroupid"
|
|
TagMusicBrainzWorkID TagName = "musicbrainz_workid"
|
|
|
|
TagMusicBrainzComposerID TagName = "musicbrainz_composerid"
|
|
TagMusicBrainzLyricistID TagName = "musicbrainz_lyricistid"
|
|
TagMusicBrainzDirectorID TagName = "musicbrainz_directorid"
|
|
TagMusicBrainzProducerID TagName = "musicbrainz_producerid"
|
|
TagMusicBrainzEngineerID TagName = "musicbrainz_engineerid"
|
|
TagMusicBrainzMixerID TagName = "musicbrainz_mixerid"
|
|
TagMusicBrainzRemixerID TagName = "musicbrainz_remixerid"
|
|
TagMusicBrainzDJMixerID TagName = "musicbrainz_djmixerid"
|
|
TagMusicBrainzConductorID TagName = "musicbrainz_conductorid"
|
|
TagMusicBrainzArrangerID TagName = "musicbrainz_arrangerid"
|
|
TagMusicBrainzPerformerID TagName = "musicbrainz_performerid"
|
|
)
|