Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watched state determined automatically #1350

Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bin
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/copilot/

# Generated files
.idea/**/contentModel.xml
Expand Down
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion api/info-pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (r infoPageRoutes) updateText(c *gin.Context) {
RawContent: reqBody.RawContent,
Type: reqBody.Type,
})

if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
Expand Down
29 changes: 29 additions & 0 deletions api/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package api

import (
"errors"
"math"
"net/http"
"slices"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -114,10 +116,37 @@ func (r progressRoutes) saveProgress(c *gin.Context) {
})
return
}

streamIDString := strconv.FormatUint(uint64(request.StreamID), 10)
if r.StreamsDao == nil {
logger.Error("StreamsDao is nil")
DawinYurtseven marked this conversation as resolved.
Show resolved Hide resolved
return
}
stream, err := r.StreamsDao.GetStreamByID(c, streamIDString)
SebiWrn marked this conversation as resolved.
Show resolved Hide resolved

if err == nil {
lastSilence := slices.MaxFunc(stream.Silences, func(silence model.Silence, other model.Silence) int {
return int(silence.End) - int(other.End)
})

if math.Abs(float64(lastSilence.End-uint(stream.Duration.Int32))) < 10 {
SebiWrn marked this conversation as resolved.
Show resolved Hide resolved
lastSilencePercent := float64(lastSilence.Start) / float64(stream.Duration.Int32)
SebiWrn marked this conversation as resolved.
Show resolved Hide resolved
if request.Progress >= lastSilencePercent {
progressBuff.add(model.StreamProgress{
Progress: request.Progress,
StreamID: request.StreamID,
UserID: tumLiveContext.User.ID,
Watched: true,
})
return
SebiWrn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
progressBuff.add(model.StreamProgress{
Progress: request.Progress,
StreamID: request.StreamID,
UserID: tumLiveContext.User.ID,
Watched: request.Progress > .9,
})
}

Expand Down
36 changes: 32 additions & 4 deletions dao/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,40 @@ func (d progressDao) GetProgressesForUser(userID uint) (r []model.StreamProgress
return r, DB.Where("user_id = ?", userID).Find(&r).Error
}

func filterProgress(progresses []model.StreamProgress, watched bool) []model.StreamProgress {
var result []model.StreamProgress
for _, progress := range progresses {
if progress.Watched == watched {
result = append(result, progress)
}
}
return result
}

// SaveProgresses saves a slice of stream progresses. If a progress already exists, it will be updated.
func (d progressDao) SaveProgresses(progresses []model.StreamProgress) error {
return DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
DoUpdates: clause.AssignmentColumns([]string{"progress"}), // column needed to be updated
}).Create(progresses).Error
noWatched := filterProgress(progresses, false)
SebiWrn marked this conversation as resolved.
Show resolved Hide resolved
watched := filterProgress(progresses, true)
var err error
if len(noWatched) > 0 {
err = DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
DoUpdates: clause.AssignmentColumns([]string{"progress"}), // column needed to be updated
}).Create(noWatched).Error
}
var err2 error

if len(watched) > 0 {
err2 = DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
DoUpdates: clause.AssignmentColumns([]string{"progress", "watched"}), // column needed to be updated
}).Create(watched).Error
}

if err != nil {
return err
}
return err2
}

// SaveWatchedState creates/updates a stream progress with its corresponding watched state.
Expand Down
1 change: 1 addition & 0 deletions tools/realtime/connector/melody.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"net/http"

"github.com/TUM-Dev/gocast/tools/realtime"
"github.com/gabstv/melody"
)
Expand Down
Loading