Skip to content

Commit 9aa428e

Browse files
authored
Watched state determined automatically (#1350)
* Watched state determined automatically * Fixed gofumpt * Added watched state if only silence after it * Fixed errors * Fixed gofumpt * Fixed bug * Fixed bug * Fixed bug * Fixed some things and cleaned up code * Fixed gofumpt * Fixed test * Fixed golangci-lint * Removed test * Removed debug messages * Added comment * gofumpted * Fixed a bug
1 parent 759d9de commit 9aa428e

File tree

8 files changed

+92
-12
lines changed

8 files changed

+92
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ bin
2929
.idea/**/usage.statistics.xml
3030
.idea/**/dictionaries
3131
.idea/**/shelf
32+
.idea/copilot/
3233

3334
# Generated files
3435
.idea/**/contentModel.xml

.golangci.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ linters:
2727
run:
2828
go: '1.21'
2929
timeout: 10m
30-
skip-dirs:
31-
- node_modules
32-
- template
30+
# skip-dirs:
31+
# - node_modules
32+
# - template
3333

3434
linters-settings:
3535
stylecheck:
@@ -67,14 +67,17 @@ linters-settings:
6767
- name: modifies-value-receiver
6868
gofumpt:
6969
extra-rules: false
70-
lang-version: "1.21"
70+
#lang-version: "1.21"
7171

7272
issues:
7373
max-issues-per-linter: 0
7474
max-same-issues: 0
7575
new: true
7676
new-from-rev: dev
77-
fix: false
77+
fix: false
78+
exclude-dirs:
79+
- node_modules
80+
- template
7881
exclude-rules:
7982
# Exclude some linters from running on tests files.
8083
- path: _test\.go

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/info-pages.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func (r infoPageRoutes) updateText(c *gin.Context) {
5858
RawContent: reqBody.RawContent,
5959
Type: reqBody.Type,
6060
})
61-
6261
if err != nil {
6362
_ = c.Error(tools.RequestError{
6463
Status: http.StatusInternalServerError,

api/progress.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package api
22

33
import (
44
"errors"
5+
"math"
56
"net/http"
7+
"slices"
68
"strconv"
79
"sync"
810
"time"
@@ -114,10 +116,40 @@ func (r progressRoutes) saveProgress(c *gin.Context) {
114116
})
115117
return
116118
}
119+
120+
stream, err := r.DaoWrapper.StreamsDao.GetStreamByID(c, strconv.FormatUint(uint64(request.StreamID), 10))
121+
if err != nil {
122+
return
123+
}
124+
125+
watchedToLastSilence := false
126+
127+
// logger.Debug("Save progress")
128+
duration := stream.Duration.Int32
129+
if duration == 0 {
130+
dur := stream.End.Sub(stream.Start)
131+
duration += int32(dur.Seconds()) + int32(dur.Minutes())*60 + int32(dur.Minutes())*60*60
132+
}
133+
// logger.Debug("Duration", "duration", duration)
134+
if duration != 0 && len(stream.Silences) > 0 {
135+
lastSilence := slices.MaxFunc(stream.Silences, func(silence model.Silence, other model.Silence) int {
136+
return int(silence.End) - int(other.End)
137+
})
138+
139+
// Add a little wiggle time to the end if ffmpeg didn't detect the silence till the end
140+
if math.Abs(float64(lastSilence.End-uint(duration))) < 10 {
141+
lastSilencePercent := float64(lastSilence.Start) / float64(duration)
142+
if request.Progress >= lastSilencePercent {
143+
watchedToLastSilence = true
144+
}
145+
}
146+
}
147+
117148
progressBuff.add(model.StreamProgress{
118149
Progress: request.Progress,
119150
StreamID: request.StreamID,
120151
UserID: tumLiveContext.User.ID,
152+
Watched: request.Progress > .9 || watchedToLastSilence,
121153
})
122154
}
123155

api/progress_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"database/sql"
45
"errors"
56
"fmt"
67
"net/http"
@@ -29,7 +30,7 @@ func TestProgressReport(t *testing.T) {
2930

3031
req := progressRequest{
3132
StreamID: uint(1),
32-
Progress: 0,
33+
Progress: float64(.5),
3334
}
3435

3536
gomino.TestCases{
@@ -51,7 +52,19 @@ func TestProgressReport(t *testing.T) {
5152
ExpectedCode: http.StatusForbidden,
5253
},
5354
"success": {
54-
Router: ProgressRouterWrapper,
55+
Router: func(r *gin.Engine) {
56+
wrapper := dao.DaoWrapper{
57+
StreamsDao: func() dao.StreamsDao {
58+
streamsMock := mock_dao.NewMockStreamsDao(gomock.NewController(t))
59+
streamsMock.
60+
EXPECT().
61+
GetStreamByID(gomock.Any(), "1").
62+
Return(model.Stream{Duration: sql.NullInt32{Int32: int32(20)}}, nil)
63+
return streamsMock
64+
}(),
65+
}
66+
configProgressRouter(r, wrapper)
67+
},
5568
Body: req,
5669
Middlewares: testutils.GetMiddlewares(tools.ErrorHandler, testutils.TUMLiveContext(testutils.TUMLiveContextStudent)),
5770
ExpectedCode: http.StatusOK,

dao/progress.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,41 @@ func (d progressDao) GetProgressesForUser(userID uint) (r []model.StreamProgress
3030
return r, DB.Where("user_id = ?", userID).Find(&r).Error
3131
}
3232

33+
func filterProgress(progresses []model.StreamProgress, watched bool) []model.StreamProgress {
34+
var result []model.StreamProgress
35+
for _, progress := range progresses {
36+
if progress.Watched == watched {
37+
result = append(result, progress)
38+
}
39+
}
40+
return result
41+
}
42+
3343
// SaveProgresses saves a slice of stream progresses. If a progress already exists, it will be updated.
44+
// We need two different methods for that because else the watched state will be overwritten.
3445
func (d progressDao) SaveProgresses(progresses []model.StreamProgress) error {
35-
return DB.Clauses(clause.OnConflict{
36-
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
37-
DoUpdates: clause.AssignmentColumns([]string{"progress"}), // column needed to be updated
38-
}).Create(progresses).Error
46+
noWatched := filterProgress(progresses, false)
47+
watched := filterProgress(progresses, true)
48+
var err error
49+
if len(noWatched) > 0 {
50+
err = DB.Clauses(clause.OnConflict{
51+
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
52+
DoUpdates: clause.AssignmentColumns([]string{"progress"}), // column needed to be updated
53+
}).Create(noWatched).Error
54+
}
55+
var err2 error
56+
57+
if len(watched) > 0 {
58+
err2 = DB.Clauses(clause.OnConflict{
59+
Columns: []clause.Column{{Name: "stream_id"}, {Name: "user_id"}}, // key column
60+
DoUpdates: clause.AssignmentColumns([]string{"progress", "watched"}), // column needed to be updated
61+
}).Create(watched).Error
62+
}
63+
64+
if err != nil {
65+
return err
66+
}
67+
return err2
3968
}
4069

4170
// SaveWatchedState creates/updates a stream progress with its corresponding watched state.

tools/realtime/connector/melody.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package connector
22

33
import (
44
"net/http"
5+
56
"github.com/TUM-Dev/gocast/tools/realtime"
67
"github.com/gabstv/melody"
78
)

0 commit comments

Comments
 (0)