Add trending and ability to sort playlist by views
This commit is contained in:
parent
2c8af03164
commit
9fa3427308
5 changed files with 122 additions and 87 deletions
|
@ -162,7 +162,7 @@ func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("/")
|
log.Printf("/")
|
||||||
pl := a.Library.Playlist()
|
pl := a.Library.Playlist()
|
||||||
if len(pl) > 0 {
|
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 {
|
} else {
|
||||||
ctx := &struct {
|
ctx := &struct {
|
||||||
Playing *media.Video
|
Playing *media.Video
|
||||||
|
@ -312,6 +312,13 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
video.Views = views
|
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")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
ctx := &struct {
|
ctx := &struct {
|
||||||
Playing *media.Video
|
Playing *media.Video
|
||||||
|
|
148
app/rice-box.go
148
app/rice-box.go
File diff suppressed because one or more lines are too long
|
@ -6,7 +6,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -117,6 +116,5 @@ func (lib *Library) Playlist() Playlist {
|
||||||
pl[i] = v
|
pl[i] = v
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.Sort(pl)
|
|
||||||
return pl
|
return pl
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,48 @@
|
||||||
package media
|
package media
|
||||||
|
|
||||||
// Playlist holds an array of videos capable of sorting by Timestamp.
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
type Playlist []*Video
|
type Playlist []*Video
|
||||||
|
|
||||||
// Len returns length of array (for sorting).
|
// By is the type of a "less" function that defines the ordering of its Playlist arguments.
|
||||||
func (p Playlist) Len() int {
|
type By func(p1, p2 *Video) bool
|
||||||
return len(p)
|
|
||||||
|
// 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).
|
// playlistSorter joins a By function and a slice of Playlist to be sorted.
|
||||||
func (p Playlist) Swap(i, j int) {
|
type playlistSorter struct {
|
||||||
p[i], p[j] = p[j], p[i]
|
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).
|
// Len is part of sort.Interface.
|
||||||
func (p Playlist) Less(i, j int) bool {
|
func (s *playlistSorter) Len() int {
|
||||||
return p[i].Timestamp.After(p[j].Timestamp)
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
<body>
|
<body>
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Tube</a>
|
<a href="/">Tube</a>
|
||||||
|
<a href="/?trending=1">Trending</a>
|
||||||
<a class="centered" style="text-indent: 0;" href="/upload">Upload</a>
|
<a class="centered" style="text-indent: 0;" href="/upload">Upload</a>
|
||||||
</nav>
|
</nav>
|
||||||
<main>
|
<main>
|
||||||
|
|
Loading…
Reference in a new issue