Add trending and ability to sort playlist by views

This commit is contained in:
James Mills 2020-03-25 16:22:41 +10:00
parent 2c8af03164
commit 9fa3427308
No known key found for this signature in database
GPG key ID: AC4C014F1440EBD6
5 changed files with 122 additions and 87 deletions

View file

@ -162,7 +162,7 @@ func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("/")
pl := a.Library.Playlist()
if len(pl) > 0 {
http.Redirect(w, r, "/v/"+pl[0].ID, 302)
http.Redirect(w, r, fmt.Sprintf("/v/%s?%s", pl[0].ID, r.URL.RawQuery), 302)
} else {
ctx := &struct {
Playing *media.Video
@ -312,6 +312,13 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
video.Views = views
}
if r.URL.Query().Get("trending") != "" {
log.Info("sorting by trending")
media.By(media.SortByViews).Sort(playlist)
} else {
media.By(media.SortByTimestamp).Sort(playlist)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
ctx := &struct {
Playing *media.Video

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,6 @@ import (
"log"
"path"
"path/filepath"
"sort"
"strings"
"sync"
)
@ -117,6 +116,5 @@ func (lib *Library) Playlist() Playlist {
pl[i] = v
i++
}
sort.Sort(pl)
return pl
}

View file

@ -1,19 +1,48 @@
package media
// Playlist holds an array of videos capable of sorting by Timestamp.
import (
"sort"
)
type Playlist []*Video
// Len returns length of array (for sorting).
func (p Playlist) Len() int {
return len(p)
// By is the type of a "less" function that defines the ordering of its Playlist arguments.
type By func(p1, p2 *Video) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(pl Playlist) {
ps := &playlistSorter{
pl: pl,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ps)
}
// Swap swaps two values in array by index (for sorting).
func (p Playlist) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
// playlistSorter joins a By function and a slice of Playlist to be sorted.
type playlistSorter struct {
pl Playlist
by func(p1, p2 *Video) bool // Closure used in the Less method.
}
// Less returns true if p[i] Timestamp is after p[j] (for sorting).
func (p Playlist) Less(i, j int) bool {
return p[i].Timestamp.After(p[j].Timestamp)
// Len is part of sort.Interface.
func (s *playlistSorter) Len() int {
return len(s.pl)
}
// Swap is part of sort.Interface.
func (s *playlistSorter) Swap(i, j int) {
s.pl[i], s.pl[j] = s.pl[j], s.pl[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *playlistSorter) Less(i, j int) bool {
return s.by(s.pl[i], s.pl[j])
}
func SortByTimestamp(v1, v2 *Video) bool {
return v1.Timestamp.After(v2.Timestamp)
}
func SortByViews(v1, v2 *Video) bool {
return v1.Views < v2.Views
}

View file

@ -15,6 +15,7 @@
<body>
<nav>
<a href="/">Tube</a>
<a href="/?trending=1">Trending</a>
<a class="centered" style="text-indent: 0;" href="/upload">Upload</a>
</nav>
<main>