Skip to content

Commit

Permalink
Fix playcount tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
NodudeWasTaken committed Sep 11, 2023
1 parent 72d9c8a commit 7efdd54
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
66 changes: 32 additions & 34 deletions internal/heresphere/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ func (rs Routes) Routes() chi.Router {
r := chi.NewRouter()

r.Route("/", func(r chi.Router) {
r.Use(rs.HeresphereCtx)
r.Use(rs.heresphereCtx)

r.Post("/", rs.HeresphereIndex)
r.Get("/", rs.HeresphereIndex)
r.Head("/", rs.HeresphereIndex)
r.Post("/", rs.heresphereIndex)
r.Get("/", rs.heresphereIndex)
r.Head("/", rs.heresphereIndex)

r.Post("/auth", rs.HeresphereLoginToken)
r.Post("/auth", rs.heresphereLoginToken)
r.Route("/{sceneId}", func(r chi.Router) {
r.Use(rs.HeresphereSceneCtx)
r.Use(rs.heresphereSceneCtx)

r.Post("/", rs.HeresphereVideoData)
r.Get("/", rs.HeresphereVideoData)
r.Post("/", rs.heresphereVideoData)
r.Get("/", rs.heresphereVideoData)

r.Post("/event", rs.HeresphereVideoEvent)
r.Post("/event", rs.heresphereVideoEvent)
})
})

Expand All @@ -77,7 +77,7 @@ func (rs Routes) Routes() chi.Router {
* Intended for server-sided script playback.
* But since we dont need that, we just use it for timestamps.
*/
func (rs Routes) HeresphereVideoEvent(w http.ResponseWriter, r *http.Request) {
func (rs Routes) heresphereVideoEvent(w http.ResponseWriter, r *http.Request) {
scn := r.Context().Value(sceneKey).(*models.Scene)

var event HeresphereVideoEvent
Expand All @@ -88,27 +88,25 @@ func (rs Routes) HeresphereVideoEvent(w http.ResponseWriter, r *http.Request) {
return
}

if event.Event == HeresphereEventClose {
newTime := event.Time / 1000
newDuration := 0.0
if newTime > scn.ResumeTime {
newDuration += (newTime - scn.ResumeTime)
}
newTime := event.Time / 1000
newDuration := 0.0
if newTime > scn.ResumeTime {
newDuration += (newTime - scn.ResumeTime)
}

if err := updatePlayCount(r.Context(), scn, event, rs.TxnManager, rs.Repository.Scene); err != nil {
logger.Errorf("Heresphere HeresphereVideoEvent updatePlayCount error: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := updatePlayCount(r.Context(), scn, event, rs.TxnManager, rs.Repository.Scene); err != nil {
logger.Errorf("Heresphere HeresphereVideoEvent updatePlayCount error: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if err := txn.WithReadTxn(r.Context(), rs.TxnManager, func(ctx context.Context) error {
_, err := rs.Repository.Scene.SaveActivity(ctx, scn.ID, &newTime, &newDuration)
return err
}); err != nil {
logger.Errorf("Heresphere HeresphereVideoEvent SaveActivity error: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := txn.WithTxn(r.Context(), rs.TxnManager, func(ctx context.Context) error {
_, err := rs.Repository.Scene.SaveActivity(ctx, scn.ID, &newTime, &newDuration)
return err
}); err != nil {
logger.Errorf("Heresphere HeresphereVideoEvent SaveActivity error: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -178,7 +176,7 @@ func (rs Routes) HeresphereVideoDataUpdate(w http.ResponseWriter, r *http.Reques
/*
* This endpoint provides the main libraries that are available to browse.
*/
func (rs Routes) HeresphereIndex(w http.ResponseWriter, r *http.Request) {
func (rs Routes) heresphereIndex(w http.ResponseWriter, r *http.Request) {
// Banner
banner := HeresphereBanner{
Image: fmt.Sprintf("%s%s", manager.GetBaseURL(r), "/apple-touch-icon.png"),
Expand Down Expand Up @@ -225,7 +223,7 @@ func (rs Routes) HeresphereIndex(w http.ResponseWriter, r *http.Request) {
/*
* This endpoint provides a single scenes full information.
*/
func (rs Routes) HeresphereVideoData(w http.ResponseWriter, r *http.Request) {
func (rs Routes) heresphereVideoData(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(authKey).(HeresphereAuthReq)
c := config.GetInstance()

Expand Down Expand Up @@ -322,7 +320,7 @@ func (rs Routes) HeresphereVideoData(w http.ResponseWriter, r *http.Request) {
/*
* This endpoint function allows the user to login and receive a token if successful.
*/
func (rs Routes) HeresphereLoginToken(w http.ResponseWriter, r *http.Request) {
func (rs Routes) heresphereLoginToken(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(authKey).(HeresphereAuthReq)

// Try login
Expand Down Expand Up @@ -358,7 +356,7 @@ func (rs Routes) HeresphereLoginToken(w http.ResponseWriter, r *http.Request) {
/*
* This context function finds the applicable scene from the request and stores it.
*/
func (rs Routes) HeresphereSceneCtx(next http.Handler) http.Handler {
func (rs Routes) heresphereSceneCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get sceneId
sceneID, err := strconv.Atoi(chi.URLParam(r, "sceneId"))
Expand Down Expand Up @@ -399,7 +397,7 @@ func (rs Routes) HeresphereSceneCtx(next http.Handler) http.Handler {
/*
* This context function finds if the authentication is correct, otherwise rejects the request.
*/
func (rs Routes) HeresphereCtx(next http.Handler) http.Handler {
func (rs Routes) heresphereCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add JSON Header (using Add uses camel case and makes it invalid because "Json")
w.Header()["HereSphere-JSON-Version"] = []string{strconv.Itoa(HeresphereJsonVersion)}
Expand Down
7 changes: 4 additions & 3 deletions internal/heresphere/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func updatePlayCount(ctx context.Context, scn *models.Scene, event HeresphereVid
newTime := event.Time / 1000
file := scn.Files.Primary()

if file != nil && newTime/file.Duration > float64(per)/100.0 {
// TODO: Need temporal memory, we need to track "Open" videos to do this properly
if scn.PlayCount == 0 && file != nil && newTime/file.Duration > float64(per)/100.0 {
ret := &scene.UpdateSet{
ID: scn.ID,
ID: scn.ID,
Partial: models.NewScenePartial(),
}
ret.Partial = models.NewScenePartial()
ret.Partial.PlayCount.Set = true
ret.Partial.PlayCount.Value = scn.PlayCount + 1

Expand Down

0 comments on commit 7efdd54

Please sign in to comment.