From 91df3423cefb78c1b36aabf0561783c052e69f6d Mon Sep 17 00:00:00 2001 From: barisgul15 Date: Mon, 13 Nov 2023 10:54:24 +0100 Subject: [PATCH 01/11] SeekingTime enum is created for posting the request. --- web/ts/user-settings.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/web/ts/user-settings.ts b/web/ts/user-settings.ts index de13f43fb..e955a26fd 100644 --- a/web/ts/user-settings.ts +++ b/web/ts/user-settings.ts @@ -6,6 +6,7 @@ export enum UserSetting { Name = "name", Greeting = "greeting", PlaybackSpeeds = "playbackSpeeds", + SeekingTime = "seekingTime", } export function updatePreference(t: UserSetting, value: string | boolean | number[]): Promise { From cc4256bc49efa79aaf75345bd85a05b7fa3ba729 Mon Sep 17 00:00:00 2001 From: barisgul15 Date: Mon, 13 Nov 2023 11:09:25 +0100 Subject: [PATCH 02/11] Seeking time user setting and selection button are added. --- api/users.go | 44 +++++++++++++++++++++++++++++-- model/user.go | 24 +++++++++++++++++ web/template/user-settings.gohtml | 17 ++++++++++++ web/template/video_only.gohtml | 2 +- web/template/watch.gohtml | 12 ++++----- web/ts/TUMLiveVjs.ts | 7 ++--- 6 files changed, 94 insertions(+), 12 deletions(-) diff --git a/api/users.go b/api/users.go index e96921e63..7b620c13f 100644 --- a/api/users.go +++ b/api/users.go @@ -11,11 +11,11 @@ import ( "strings" "time" - "github.com/getsentry/sentry-go" - "github.com/gin-gonic/gin" "github.com/TUM-Dev/gocast/dao" "github.com/TUM-Dev/gocast/model" "github.com/TUM-Dev/gocast/tools" + "github.com/getsentry/sentry-go" + "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -26,6 +26,7 @@ func configGinUsersRouter(router *gin.Engine, daoWrapper dao.DaoWrapper) { router.POST("/api/users/settings/name", routes.updatePreferredName) router.POST("/api/users/settings/greeting", routes.updatePreferredGreeting) router.POST("/api/users/settings/playbackSpeeds", routes.updatePlaybackSpeeds) + router.POST("/api/users/settings/seekingTime", routes.updateSeekingTime) router.POST("/api/users/resetPassword", routes.resetPassword) @@ -656,6 +657,45 @@ func (r usersRoutes) updatePlaybackSpeeds(c *gin.Context) { } } +func (r usersRoutes) updateSeekingTime(c *gin.Context) { + // Extract the user from the Gin context + u := c.MustGet("TUMLiveContext").(tools.TUMLiveContext).User + // Check if the user is authenticated. + if u == nil { + _ = c.Error(tools.RequestError{ + Status: http.StatusUnauthorized, + CustomMessage: "login required", + }) + return + } + // Decode the JSON request body into a userSettingsRequest struct. + var request userSettingsRequest + err := json.NewDecoder(c.Request.Body).Decode(&request) + if err != nil { + _ = c.Error(tools.RequestError{ + Status: http.StatusBadRequest, + CustomMessage: "can not bind body", + Err: err, + }) + return + } + // Add the user's seeking time setting to the database. + err = r.UsersDao.AddUserSetting(&model.UserSetting{ + UserID: u.ID, + Type: model.SeekingTime, + Value: request.Value, + }) + // Handle errors that may occur during the database operation. + if err != nil { + _ = c.Error(tools.RequestError{ + Status: http.StatusInternalServerError, + CustomMessage: "can not add user setting", + Err: err, + }) + return + } +} + func (r usersRoutes) exportPersonalData(c *gin.Context) { var resp personalData u := c.MustGet("TUMLiveContext").(tools.TUMLiveContext).User diff --git a/model/user.go b/model/user.go index 5856e127f..846478668 100755 --- a/model/user.go +++ b/model/user.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "strconv" "strings" "time" @@ -54,6 +55,7 @@ const ( PreferredName UserSettingType = iota + 1 Greeting CustomPlaybackSpeeds + SeekingTime ) type UserSetting struct { @@ -130,6 +132,28 @@ func (u User) GetPreferredGreeting() string { return "Moin" } +// GetSeekingTime returns the seeking time preference for the user. +// If the user is nil, the default seeking time of 15 seconds is returned. +func (u *User) GetSeekingTime() int { + // Check if the user is nil + if u == nil { + return 15 + } + // Check if the setting type is SeekingTime + for _, setting := range u.Settings { + if setting.Type == SeekingTime { + // Attempt to convert the setting value from string to an integer + seekingTime, err := strconv.Atoi(setting.Value) + if err != nil { + break + } + return seekingTime + } + } + // If no seeking time setting is found, return the default seeking time + return 15 +} + // PreferredNameChangeAllowed returns false if the user has set a preferred name within the last 3 months, otherwise true func (u User) PreferredNameChangeAllowed() bool { for _, setting := range u.Settings { diff --git a/web/template/user-settings.gohtml b/web/template/user-settings.gohtml index 135495f1f..731f76e5b 100644 --- a/web/template/user-settings.gohtml +++ b/web/template/user-settings.gohtml @@ -87,6 +87,23 @@ +
+

Seeking Time

+
+ + + + + + +
+

Privacy & Data Protection