From 4623c6f06dcf4d3c35c546533d47d7e3b1d6bc81 Mon Sep 17 00:00:00 2001 From: Matthew Humphreys Date: Wed, 24 Apr 2024 11:39:35 +0200 Subject: [PATCH] 1032 option to make beta default mode (#1328) * added api endpoint to update user setting Signed-off-by: Matthew Humphreys * added frontend components to update user setting Signed-off-by: Matthew Humphreys * watchPageData set according to user setting Signed-off-by: Matthew Humphreys * removed some duplicate code Signed-off-by: Matthew Humphreys * consistent spelling Signed-off-by: Matthew Humphreys * make sure user is signed in Signed-off-by: Matthew Humphreys --------- Signed-off-by: Matthew Humphreys --- api/users.go | 29 ++++++++++++++++++++++++++++- model/user.go | 20 ++++++++++++++++++++ web/template/user-settings.gohtml | 15 ++++++++++++++- web/ts/user-settings.ts | 1 + web/watch.go | 9 ++++++++- 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/api/users.go b/api/users.go index 175a70eff..fa1b910ac 100644 --- a/api/users.go +++ b/api/users.go @@ -28,6 +28,7 @@ func configGinUsersRouter(router *gin.Engine, daoWrapper dao.DaoWrapper) { router.POST("/api/users/settings/seekingTime", routes.updateSeekingTime) router.POST("/api/users/settings/customSpeeds", routes.updateCustomSpeeds) router.POST("/api/users/settings/autoSkip", routes.updateAutoSkip) + router.POST("/api/users/settings/defaultMode", routes.updateDefaultMode) router.POST("/api/users/resetPassword", routes.resetPassword) @@ -729,10 +730,11 @@ func (r usersRoutes) updateAutoSkip(c *gin.Context) { if u == nil { _ = c.Error(tools.RequestError{ Status: http.StatusUnauthorized, - CustomMessage: "login required", + CustomMessage: "login required for updating user settings", }) return } + var req struct{ Value model.AutoSkipSetting } if err := c.BindJSON(&req); err != nil { _ = c.Error(tools.RequestError{ @@ -755,6 +757,31 @@ func (r usersRoutes) updateAutoSkip(c *gin.Context) { } } +// updateDefaultMode updates whether the default stream mode for a user should be "beta" +func (r usersRoutes) updateDefaultMode(c *gin.Context) { + u := getUserFromContext(c) + var req struct{ Value model.DefaultModeSetting } + if err := c.BindJSON(&req); err != nil { + _ = c.Error(tools.RequestError{ + Status: http.StatusBadRequest, + CustomMessage: "can not bind body to request", + Err: err, + }) + return + } + + settingBytes, _ := json.Marshal(req.Value) + err := r.DaoWrapper.UsersDao.AddUserSetting(&model.UserSetting{UserID: u.ID, Type: model.DefaultMode, Value: string(settingBytes)}) + 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 5b30425b8..e0e6fc25f 100755 --- a/model/user.go +++ b/model/user.go @@ -59,6 +59,7 @@ const ( SeekingTime UserDefinedSpeeds AutoSkip + DefaultMode ) type UserSetting struct { @@ -218,6 +219,25 @@ func (u User) GetAutoSkipEnabled() (AutoSkipSetting, error) { return AutoSkipSetting{Enabled: false}, nil } +// DefaultModeSetting wraps whether the default stream mode for the user is beta +type DefaultModeSetting struct { + Beta bool `json:"beta"` +} + +func (u User) GetDefaultMode() (DefaultModeSetting, error) { + for _, setting := range u.Settings { + if setting.Type == DefaultMode { + var m DefaultModeSetting + err := json.Unmarshal([]byte(setting.Value), &m) + if err != nil { + return DefaultModeSetting{Beta: false}, err + } + return m, nil + } + } + return DefaultModeSetting{Beta: false}, nil +} + type argonParams struct { memory uint32 iterations uint32 diff --git a/web/template/user-settings.gohtml b/web/template/user-settings.gohtml index b3857a061..fcf662ff8 100644 --- a/web/template/user-settings.gohtml +++ b/web/template/user-settings.gohtml @@ -150,7 +150,20 @@ peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600 dark:peer-checked:bg-indigo-600"> - Skip + + + +
+

Always use beta Stream when Available

+ +
diff --git a/web/ts/user-settings.ts b/web/ts/user-settings.ts index 2180ca783..d466ee3bd 100644 --- a/web/ts/user-settings.ts +++ b/web/ts/user-settings.ts @@ -10,6 +10,7 @@ export enum UserSetting { SeekingTime = "seekingTime", CustomSpeeds = "customSpeeds", AutoSkip = "autoSkip", + DefaultMode = "defaultMode", } export function updatePreference(t: UserSetting, value: string | boolean | number[]): Promise { diff --git a/web/watch.go b/web/watch.go index f866d0f10..08ed26005 100644 --- a/web/watch.go +++ b/web/watch.go @@ -110,11 +110,18 @@ func (r mainRoutes) WatchPage(c *gin.Context) { c.Redirect(http.StatusFound, strings.Split(c.Request.RequestURI, "?")[0]) return } - if _, dvr := c.GetQuery("dvr"); dvr { + // Check if user wants to use beta stream mode + mode, err := tumLiveContext.User.GetDefaultMode() + if err != nil { + logger.Error("Couldn't decode user setting", "err", err) + } + + if _, dvr := c.GetQuery("dvr"); dvr || mode.Beta { data.DVR = "?dvr" } else { data.DVR = "" } + data.CutOffLength = api.CutOffLength if strings.HasPrefix(data.Version, "unit-") { data.Description = data.Unit.GetDescriptionHTML()