Add option to sort Recently Added by file's mtime instead of time of import.

This commit is contained in:
Deluan
2021-03-12 17:49:47 -05:00
committed by Deluan Quintão
parent 1ec105a245
commit 720e2357b7
6 changed files with 53 additions and 30 deletions
+13 -11
View File
@@ -31,17 +31,18 @@ type configOptions struct {
ImageCacheSize string ImageCacheSize string
AutoImportPlaylists bool AutoImportPlaylists bool
SearchFullString bool SearchFullString bool
IgnoredArticles string RecentlyAddedByModTime bool
IndexGroups string IgnoredArticles string
ProbeCommand string IndexGroups string
CoverArtPriority string ProbeCommand string
CoverJpegQuality int CoverArtPriority string
UIWelcomeMessage string CoverJpegQuality int
EnableGravatar bool UIWelcomeMessage string
GATrackingID string EnableGravatar bool
AuthRequestLimit int GATrackingID string
AuthWindowLength time.Duration AuthRequestLimit int
AuthWindowLength time.Duration
Scanner scannerOptions Scanner scannerOptions
@@ -134,6 +135,7 @@ func init() {
// Config options only valid for file/env configuration // Config options only valid for file/env configuration
viper.SetDefault("searchfullstring", false) viper.SetDefault("searchfullstring", false)
viper.SetDefault("recentlyaddedbymodtime", false)
viper.SetDefault("ignoredarticles", "The El La Los Las Le Les Os As O A") viper.SetDefault("ignoredarticles", "The El La Los Las Le Les Os As O A")
viper.SetDefault("indexgroups", "A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)") viper.SetDefault("indexgroups", "A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)")
viper.SetDefault("probecommand", "ffmpeg %s -f ffmetadata") viper.SetDefault("probecommand", "ffmpeg %s -f ffmetadata")
+2 -2
View File
@@ -46,8 +46,8 @@ type MediaFile struct {
MbzAlbumArtistID string `json:"mbzAlbumArtistId" orm:"column(mbz_album_artist_id)"` MbzAlbumArtistID string `json:"mbzAlbumArtistId" orm:"column(mbz_album_artist_id)"`
MbzAlbumType string `json:"mbzAlbumType"` MbzAlbumType string `json:"mbzAlbumType"`
MbzAlbumComment string `json:"mbzAlbumComment"` MbzAlbumComment string `json:"mbzAlbumComment"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"` // Time this entry was created in the DB
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"` // Time of file last update (mtime)
} }
func (mf *MediaFile) ContentType() string { func (mf *MediaFile) ContentType() string {
+34 -13
View File
@@ -31,10 +31,11 @@ func NewAlbumRepository(ctx context.Context, o orm.Ormer) model.AlbumRepository
r.ormer = o r.ormer = o
r.tableName = "album" r.tableName = "album"
r.sortMappings = map[string]string{ r.sortMappings = map[string]string{
"name": "order_album_name asc, order_album_artist_name asc", "name": "order_album_name asc, order_album_artist_name asc",
"artist": "compilation asc, order_album_artist_name asc, order_album_name asc", "artist": "compilation asc, order_album_artist_name asc, order_album_name asc",
"random": "RANDOM()", "random": "RANDOM()",
"max_year": "max_year asc, name, order_album_name asc", "max_year": "max_year asc, name, order_album_name asc",
"recently_added": recentlyAddedSort(),
} }
r.filterMappings = map[string]filterFunc{ r.filterMappings = map[string]filterFunc{
"name": fullTextFilter, "name": fullTextFilter,
@@ -48,6 +49,13 @@ func NewAlbumRepository(ctx context.Context, o orm.Ormer) model.AlbumRepository
return r return r
} }
func recentlyAddedSort() string {
if conf.Server.RecentlyAddedByModTime {
return "updated_at"
}
return "created_at"
}
func recentlyPlayedFilter(field string, value interface{}) Sqlizer { func recentlyPlayedFilter(field string, value interface{}) Sqlizer {
return Gt{"play_count": 0} return Gt{"play_count": 0}
} }
@@ -153,20 +161,27 @@ func (r *albumRepository) refresh(ids ...string) error {
DiscSubtitles string DiscSubtitles string
Comments string Comments string
Path string Path string
MaxUpdatedAt string
MaxCreatedAt string
} }
var albums []refreshAlbum var albums []refreshAlbum
const zwsp = string('\u200b') const zwsp = string('\u200b')
sel := Select(`f.album_id as id, f.album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id, sel := Select(`f.album_id as id, f.album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id,
f.sort_album_name, f.sort_artist_name, f.sort_album_artist_name, f.sort_album_name, f.sort_artist_name, f.sort_album_artist_name, f.order_album_name, f.order_album_artist_name,
f.order_album_name, f.order_album_artist_name, f.path, group_concat(f.comment, "` + zwsp + `") as comments, f.path, f.mbz_album_artist_id, f.mbz_album_type, f.mbz_album_comment, f.catalog_num, f.compilation, f.genre,
group_concat(f.mbz_album_id, ' ') as mbz_album_id, f.mbz_album_artist_id, f.mbz_album_type, f.mbz_album_comment, count(f.id) as song_count,
f.catalog_num, f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration, sum(f.duration) as duration,
count(f.id) as song_count, a.id as current_id, sum(f.size) as size,
max(f.year) as max_year,
max(f.updated_at) as max_updated_at,
max(f.created_at) as max_created_at,
a.id as current_id,
group_concat(f.comment, "` + zwsp + `") as comments,
group_concat(f.mbz_album_id, ' ') as mbz_album_id,
group_concat(f.disc_subtitle, ' ') as disc_subtitles, group_concat(f.disc_subtitle, ' ') as disc_subtitles,
group_concat(f.artist, ' ') as song_artists, group_concat(f.artist, ' ') as song_artists,
group_concat(f.artist_id, ' ') as song_artist_ids, group_concat(f.artist_id, ' ') as song_artist_ids,
group_concat(f.year, ' ') as years, group_concat(f.year, ' ') as years`).
sum(f.size) as size`).
From("media_file f"). From("media_file f").
LeftJoin("album a on f.album_id = a.id"). LeftJoin("album a on f.album_id = a.id").
Where(Eq{"f.album_id": ids}).GroupBy("f.album_id") Where(Eq{"f.album_id": ids}).GroupBy("f.album_id")
@@ -202,6 +217,14 @@ func (r *albumRepository) refresh(ids ...string) error {
log.Trace(r.ctx, "Could not find album art", "id", al.ID, "name", al.Name) log.Trace(r.ctx, "Could not find album art", "id", al.ID, "name", al.Name)
} }
// Somehow, beego cannot parse the datetimes for the query above
if al.UpdatedAt, err = time.Parse(time.RFC3339Nano, al.MaxUpdatedAt); err != nil {
al.UpdatedAt = time.Now()
}
if al.CreatedAt, err = time.Parse(time.RFC3339Nano, al.MaxCreatedAt); err != nil {
al.CreatedAt = al.UpdatedAt
}
if al.Compilation { if al.Compilation {
al.AlbumArtist = consts.VariousArtists al.AlbumArtist = consts.VariousArtists
al.AlbumArtistID = consts.VariousArtistsID al.AlbumArtistID = consts.VariousArtistsID
@@ -213,12 +236,10 @@ func (r *albumRepository) refresh(ids ...string) error {
al.MinYear = getMinYear(al.Years) al.MinYear = getMinYear(al.Years)
al.MbzAlbumID = getMbzId(r.ctx, al.MbzAlbumID, r.tableName, al.Name) al.MbzAlbumID = getMbzId(r.ctx, al.MbzAlbumID, r.tableName, al.Name)
al.Comment = getComment(al.Comments, zwsp) al.Comment = getComment(al.Comments, zwsp)
al.UpdatedAt = time.Now()
if al.CurrentId != "" { if al.CurrentId != "" {
toUpdate++ toUpdate++
} else { } else {
toInsert++ toInsert++
al.CreatedAt = time.Now()
} }
al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID) al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID)
al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists, al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists,
+2 -2
View File
@@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/kennygrant/sanitize" "github.com/kennygrant/sanitize"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
@@ -64,8 +65,7 @@ func (s *mediaFileMapper) toMediaFile(md metadata.Metadata) model.MediaFile {
mf.Comment = s.policy.Sanitize(md.Comment()) mf.Comment = s.policy.Sanitize(md.Comment())
mf.Lyrics = s.policy.Sanitize(md.Lyrics()) mf.Lyrics = s.policy.Sanitize(md.Lyrics())
// TODO Get Creation time. https://github.com/djherbis/times ? mf.CreatedAt = time.Now()
mf.CreatedAt = md.ModificationTime()
mf.UpdatedAt = md.ModificationTime() mf.UpdatedAt = md.ModificationTime()
return *mf return *mf
+1 -1
View File
@@ -10,7 +10,7 @@ import (
type Options model.QueryOptions type Options model.QueryOptions
func AlbumsByNewest() Options { func AlbumsByNewest() Options {
return Options{Sort: "createdAt", Order: "desc"} return Options{Sort: "recently_added", Order: "desc"}
} }
func AlbumsByRecent() Options { func AlbumsByRecent() Options {
+1 -1
View File
@@ -17,7 +17,7 @@ export default {
}, },
recentlyAdded: { recentlyAdded: {
icon: LibraryAddIcon, icon: LibraryAddIcon,
params: 'sort=created_at&order=DESC', params: 'sort=recently_added&order=DESC',
}, },
recentlyPlayed: { recentlyPlayed: {
icon: VideoLibraryIcon, icon: VideoLibraryIcon,