-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_ended.go
61 lines (52 loc) 路 1.48 KB
/
post_ended.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
package rest
import (
"encoding/json"
"github.com/boggydigital/kvas"
"github.com/boggydigital/yet/data"
"net/http"
"time"
)
type EndedRequest struct {
VideoId string `json:"v"`
}
func PostEnded(w http.ResponseWriter, r *http.Request) {
// POST /ended
// {v}
var err error
progressRdx, err = progressRdx.RefreshWriter()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
decoder := json.NewDecoder(r.Body)
var er EndedRequest
err = decoder.Decode(&er)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// store completion timestamp
currentTime := time.Now().Format(time.RFC3339)
if err := progressRdx.ReplaceValues(data.VideoEndedProperty, er.VideoId, currentTime); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// remove the video from playlist new videos
if err := rmVideoFromPlaylistNewVideos(er.VideoId, progressRdx); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func rmVideoFromPlaylistNewVideos(videoId string, rdx kvas.WriteableRedux) error {
if err := rdx.MustHave(data.PlaylistNewVideosProperty); err != nil {
return err
}
for _, playlistId := range progressRdx.Keys(data.PlaylistNewVideosProperty) {
if progressRdx.HasValue(data.PlaylistNewVideosProperty, playlistId, videoId) {
if err := progressRdx.CutValues(data.PlaylistNewVideosProperty, playlistId, videoId); err != nil {
return err
}
}
}
return nil
}