From 8bff1ad512f4160b946479dd8869e59283b4513d Mon Sep 17 00:00:00 2001 From: vvdveen Date: Sat, 27 Apr 2024 20:09:27 +0200 Subject: [PATCH] Add AlbumPlayCountMode config option (#2803) Closes #1032 * feat(album_repository.go): add kodi-style album playcount option - #1032 Signed-off-by: Victor van der Veen * fix format issue and remove reference to kodi (now normalized) Signed-off-by: Victor van der Veen * reduced complexity but added rounding Signed-off-by: Victor van der Veen * Use constants for AlbumPlayCountMode values --------- Signed-off-by: Victor van der Veen Co-authored-by: Deluan --- cmd/root.go | 2 ++ conf/configuration.go | 2 ++ consts/consts.go | 5 +++++ persistence/album_repository.go | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 44307cd4..6b91bfe4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -192,6 +192,7 @@ func init() { rootCmd.Flags().Bool("enabletranscodingconfig", viper.GetBool("enabletranscodingconfig"), "enables transcoding configuration in the UI") rootCmd.Flags().String("transcodingcachesize", viper.GetString("transcodingcachesize"), "size of transcoding cache") rootCmd.Flags().String("imagecachesize", viper.GetString("imagecachesize"), "size of image (art work) cache. set to 0 to disable cache") + rootCmd.Flags().String("albumplaycountmode", viper.GetString("albumplaycountmode"), "how to compute playcount for albums. absolute (default) or normalized") rootCmd.Flags().Bool("autoimportplaylists", viper.GetBool("autoimportplaylists"), "enable/disable .m3u playlist auto-import`") rootCmd.Flags().Bool("prometheus.enabled", viper.GetBool("prometheus.enabled"), "enable/disable prometheus metrics endpoint`") @@ -214,4 +215,5 @@ func init() { _ = viper.BindPFlag("enabletranscodingconfig", rootCmd.Flags().Lookup("enabletranscodingconfig")) _ = viper.BindPFlag("transcodingcachesize", rootCmd.Flags().Lookup("transcodingcachesize")) _ = viper.BindPFlag("imagecachesize", rootCmd.Flags().Lookup("imagecachesize")) + _ = viper.BindPFlag("albumplaycountmode", rootCmd.Flags().Lookup("albumplaycountmode")) } diff --git a/conf/configuration.go b/conf/configuration.go index cfb64414..db14f84c 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -44,6 +44,7 @@ type configOptions struct { EnableMediaFileCoverArt bool TranscodingCacheSize string ImageCacheSize string + AlbumPlayCountMode string EnableArtworkPrecache bool AutoImportPlaylists bool PlaylistsPath string @@ -289,6 +290,7 @@ func init() { viper.SetDefault("enabletranscodingconfig", false) viper.SetDefault("transcodingcachesize", "100MB") viper.SetDefault("imagecachesize", "100MB") + viper.SetDefault("albumplaycountmode", consts.AlbumPlayCountModeAbsolute) viper.SetDefault("enableartworkprecache", true) viper.SetDefault("autoimportplaylists", true) viper.SetDefault("playlistspath", consts.DefaultPlaylistsPath) diff --git a/consts/consts.go b/consts/consts.go index b1f284ab..b52b2168 100644 --- a/consts/consts.go +++ b/consts/consts.go @@ -81,6 +81,11 @@ const ( DefaultCacheCleanUpInterval = 10 * time.Minute ) +const ( + AlbumPlayCountModeAbsolute = "absolute" + AlbumPlayCountModeNormalized = "normalized" +) + var ( DefaultDownsamplingFormat = "opus" DefaultTranscodings = []map[string]interface{}{ diff --git a/persistence/album_repository.go b/persistence/album_repository.go index c4aeb767..981e55eb 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -9,6 +9,7 @@ import ( . "github.com/Masterminds/squirrel" "github.com/deluan/rest" "github.com/navidrome/navidrome/conf" + "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" "github.com/pocketbase/dbx" @@ -172,6 +173,9 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e func (r *albumRepository) toModels(dba []dbAlbum) model.Albums { res := model.Albums{} for i := range dba { + if conf.Server.AlbumPlayCountMode == consts.AlbumPlayCountModeNormalized && dba[i].Album.SongCount != 0 { + dba[i].Album.PlayCount = (dba[i].Album.PlayCount + (int64(dba[i].Album.SongCount) - 1)) / int64(dba[i].Album.SongCount) + } res = append(res, *dba[i].Album) } return res