From 2d44bc8515649b9fbbd33d49a3295229b0db327a Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Tue, 11 Feb 2020 00:11:09 +0100 Subject: [PATCH 1/5] Add related soundtracks to soundtrack page --- pages/soundtrack/soundtrack.go | 10 +++++++++- pages/soundtrack/soundtrack.pixy | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pages/soundtrack/soundtrack.go b/pages/soundtrack/soundtrack.go index 754d3d50..f2fa1b3d 100644 --- a/pages/soundtrack/soundtrack.go +++ b/pages/soundtrack/soundtrack.go @@ -19,7 +19,15 @@ func Get(ctx aero.Context) error { return ctx.Error(http.StatusNotFound, "Track not found", err) } + var relatedTracks []*arn.SoundTrack + for _, anime := range track.Anime() { + tracks := arn.FilterSoundTracks(func(t *arn.SoundTrack) bool { + return !t.IsDraft && len(t.Media) > 0 && t.ID != track.ID && arn.Contains(t.Tags, "anime:"+anime.ID) + }) + relatedTracks = append(relatedTracks, tracks...) + } + customCtx := ctx.(*middleware.OpenGraphContext) customCtx.OpenGraph = getOpenGraph(track) - return ctx.HTML(components.SoundTrackPage(track, user)) + return ctx.HTML(components.SoundTrackPage(track, relatedTracks, user)) } diff --git a/pages/soundtrack/soundtrack.pixy b/pages/soundtrack/soundtrack.pixy index 03f87cac..a16852f1 100644 --- a/pages/soundtrack/soundtrack.pixy +++ b/pages/soundtrack/soundtrack.pixy @@ -1,4 +1,4 @@ -component SoundTrackPage(track *arn.SoundTrack, user *arn.User) +component SoundTrackPage(track *arn.SoundTrack, relatedTracks []*arn.SoundTrack, user *arn.User) SoundTrackTabs(track, user) .soundtrack-full-page @@ -37,6 +37,13 @@ component SoundTrackPage(track *arn.SoundTrack, user *arn.User) li a.soundtrack-link(href=link.URL, target="_blank")= link.Title + if len(relatedTracks) > 0 + .widget.mountable + h3.widget-title Related SoundTracks + .soundtracks.anime-soundtracks + each relatedTrack in relatedTracks + SoundTrackMini(relatedTrack, user) + //- TODO: This is quite ineffective, performance-wise if len(track.OsuBeatmaps()) > 0 || len(track.EtternaBeatmaps()) > 0 .widget.mountable From 3be01b0c4a186b87e7ceea37b24dcc25a7b4cd0e Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Tue, 11 Feb 2020 00:33:28 +0100 Subject: [PATCH 2/5] Preallocate `relatedTracks` --- pages/soundtrack/soundtrack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/soundtrack/soundtrack.go b/pages/soundtrack/soundtrack.go index f2fa1b3d..37033251 100644 --- a/pages/soundtrack/soundtrack.go +++ b/pages/soundtrack/soundtrack.go @@ -19,7 +19,7 @@ func Get(ctx aero.Context) error { return ctx.Error(http.StatusNotFound, "Track not found", err) } - var relatedTracks []*arn.SoundTrack + relatedTracks := make([]*arn.SoundTrack, 0, 5) for _, anime := range track.Anime() { tracks := arn.FilterSoundTracks(func(t *arn.SoundTrack) bool { return !t.IsDraft && len(t.Media) > 0 && t.ID != track.ID && arn.Contains(t.Tags, "anime:"+anime.ID) From d0135e93ee93ad7345560b25c507ce9009d17d8a Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Tue, 11 Feb 2020 00:40:19 +0100 Subject: [PATCH 3/5] Fix scopelint for variable `anime` --- pages/soundtrack/soundtrack.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/soundtrack/soundtrack.go b/pages/soundtrack/soundtrack.go index 37033251..5d20c3ab 100644 --- a/pages/soundtrack/soundtrack.go +++ b/pages/soundtrack/soundtrack.go @@ -21,6 +21,7 @@ func Get(ctx aero.Context) error { relatedTracks := make([]*arn.SoundTrack, 0, 5) for _, anime := range track.Anime() { + anime := anime tracks := arn.FilterSoundTracks(func(t *arn.SoundTrack) bool { return !t.IsDraft && len(t.Media) > 0 && t.ID != track.ID && arn.Contains(t.Tags, "anime:"+anime.ID) }) From 8c5ddec4509997d760cea037bac3596394ff2cde Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Tue, 11 Feb 2020 12:43:50 +0100 Subject: [PATCH 4/5] Avoid calling `FilterSoundTracks` for every `Anime` as it can potentially be costly --- pages/soundtrack/soundtrack.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pages/soundtrack/soundtrack.go b/pages/soundtrack/soundtrack.go index 5d20c3ab..b3ac9e4f 100644 --- a/pages/soundtrack/soundtrack.go +++ b/pages/soundtrack/soundtrack.go @@ -19,16 +19,21 @@ func Get(ctx aero.Context) error { return ctx.Error(http.StatusNotFound, "Track not found", err) } - relatedTracks := make([]*arn.SoundTrack, 0, 5) - for _, anime := range track.Anime() { - anime := anime - tracks := arn.FilterSoundTracks(func(t *arn.SoundTrack) bool { - return !t.IsDraft && len(t.Media) > 0 && t.ID != track.ID && arn.Contains(t.Tags, "anime:"+anime.ID) - }) - relatedTracks = append(relatedTracks, tracks...) - } + relatedTracks := arn.FilterSoundTracks(func(t *arn.SoundTrack) bool { + return !t.IsDraft && len(t.Media) > 0 && t.ID != track.ID && isRelated(track.Anime(), t) + }) customCtx := ctx.(*middleware.OpenGraphContext) customCtx.OpenGraph = getOpenGraph(track) return ctx.HTML(components.SoundTrackPage(track, relatedTracks, user)) } + +func isRelated(anime []*arn.Anime, track *arn.SoundTrack) bool { + for _, anime := range anime { + if arn.Contains(track.Tags, "anime:"+anime.ID) { + return true + } + } + + return false +} From f5dc253199274f8aa6986a133bd98acc4e23237d Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Tue, 11 Feb 2020 12:45:03 +0100 Subject: [PATCH 5/5] Change `Related SoundTracks` title to `Related` --- pages/soundtrack/soundtrack.pixy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/soundtrack/soundtrack.pixy b/pages/soundtrack/soundtrack.pixy index a16852f1..3df15aae 100644 --- a/pages/soundtrack/soundtrack.pixy +++ b/pages/soundtrack/soundtrack.pixy @@ -39,7 +39,7 @@ component SoundTrackPage(track *arn.SoundTrack, relatedTracks []*arn.SoundTrack, if len(relatedTracks) > 0 .widget.mountable - h3.widget-title Related SoundTracks + h3.widget-title Related .soundtracks.anime-soundtracks each relatedTrack in relatedTracks SoundTrackMini(relatedTrack, user)