-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_history.go
107 lines (87 loc) 路 2.48 KB
/
get_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package rest
import (
"fmt"
"github.com/boggydigital/yet/data"
"github.com/boggydigital/yet/rest/view_models"
"net/http"
"time"
)
const (
recentGroup = "Less than a week ago"
thisMonthGroup = "Less than a month ago"
thisYearGroup = "Less than a year ago"
olderGroup = "More than a year ago"
endedVideosLimit = 100
)
var groupsOrder = []string{recentGroup, thisMonthGroup, thisYearGroup, olderGroup}
type HistoryViewModel struct {
Title string
ShowAll bool
OpenGroup string
GroupsOrder []string
Groups map[string][]*view_models.VideoViewModel
}
func GetHistory(w http.ResponseWriter, r *http.Request) {
var err error
rdx, err = rdx.RefreshReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
showAll := r.URL.Query().Has("showAll")
w.Header().Set("Content-Type", "text/html")
whKeys := rdx.Keys(data.VideoEndedProperty)
pageTitle := fmt.Sprintf("Last %d watched videos (out of %d)", endedVideosLimit, len(whKeys))
if showAll {
pageTitle = fmt.Sprintf("All %d watched videos", len(whKeys))
}
hvm := &HistoryViewModel{
Title: pageTitle,
ShowAll: showAll,
OpenGroup: recentGroup,
GroupsOrder: groupsOrder,
Groups: make(map[string][]*view_models.VideoViewModel),
}
endedGroups := make(map[string][]string)
for _, id := range whKeys {
group := olderGroup
if ets, ok := rdx.GetLastVal(data.VideoEndedProperty, id); ok && ets != "" {
if et, err := time.Parse(time.RFC3339, ets); err == nil {
days := time.Now().Sub(et).Hours() / 24
if days <= 7 {
group = recentGroup
} else if days <= 30 {
group = thisMonthGroup
} else if days <= 365 {
group = thisYearGroup
}
}
}
endedGroups[group] = append(endedGroups[group], id)
}
writtenVideos := 0
for _, grp := range groupsOrder {
if writtenVideos == endedVideosLimit && !showAll {
break
}
if len(endedGroups[grp]) == 0 {
continue
}
sortedIds, err := rdx.Sort(endedGroups[grp], true, data.VideoEndedProperty)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, id := range sortedIds {
if writtenVideos == endedVideosLimit && !showAll {
break
}
hvm.Groups[grp] = append(hvm.Groups[grp], view_models.GetVideoViewModel(id, rdx, view_models.ShowEndedDate))
writtenVideos++
}
}
if err := tmpl.ExecuteTemplate(w, "history", hvm); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}