From 04e2c51fac24a045abe1a85c8b661b6bc628686c Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 11 Jun 2023 17:49:14 +0200 Subject: [PATCH] feat: allow saving frontend settings via api --- pkg/migration/20230611170341.go | 43 ++++++++++++++++++++++++++++++ pkg/routes/api/v1/user_settings.go | 3 +++ pkg/routes/api/v1/user_show.go | 1 + pkg/user/user.go | 10 +++++++ 4 files changed, 57 insertions(+) create mode 100644 pkg/migration/20230611170341.go diff --git a/pkg/migration/20230611170341.go b/pkg/migration/20230611170341.go new file mode 100644 index 00000000000..38e37da23a9 --- /dev/null +++ b/pkg/migration/20230611170341.go @@ -0,0 +1,43 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2021 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package migration + +import ( + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" +) + +type users20230611170341 struct { + FrontendSettings interface{} `xorm:"json null" json:"-"` +} + +func (users20230611170341) TableName() string { + return "users" +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20230611170341", + Description: "", + Migrate: func(tx *xorm.Engine) error { + return tx.Sync2(users20230611170341{}) + }, + Rollback: func(tx *xorm.Engine) error { + return nil + }, + }) +} diff --git a/pkg/routes/api/v1/user_settings.go b/pkg/routes/api/v1/user_settings.go index 9e1053094c4..80b8119e04f 100644 --- a/pkg/routes/api/v1/user_settings.go +++ b/pkg/routes/api/v1/user_settings.go @@ -59,6 +59,8 @@ type UserSettings struct { Language string `json:"language"` // The user's time zone. Used to send task reminders in the time zone of the user. Timezone string `json:"timezone"` + // Additional settings only used by the frontend + FrontendSettings interface{} `json:"frontend_settings"` } // GetUserAvatarProvider returns the currently set user avatar @@ -198,6 +200,7 @@ func UpdateGeneralUserSettings(c echo.Context) error { user.Language = us.Language user.Timezone = us.Timezone user.OverdueTasksRemindersTime = us.OverdueTasksRemindersTime + user.FrontendSettings = us.FrontendSettings _, err = user2.UpdateUser(s, user, true) if err != nil { diff --git a/pkg/routes/api/v1/user_show.go b/pkg/routes/api/v1/user_show.go index 1f9843f8dbd..3a9248a24c3 100644 --- a/pkg/routes/api/v1/user_show.go +++ b/pkg/routes/api/v1/user_show.go @@ -76,6 +76,7 @@ func UserShow(c echo.Context) error { Language: u.Language, Timezone: u.Timezone, OverdueTasksRemindersTime: u.OverdueTasksRemindersTime, + FrontendSettings: u.FrontendSettings, }, DeletionScheduledAt: u.DeletionScheduledAt, IsLocalUser: u.Issuer == user.IssuerLocal, diff --git a/pkg/user/user.go b/pkg/user/user.go index 2ce6d5e0e33..b4865b3f455 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -17,6 +17,7 @@ package user import ( + "encoding/json" "errors" "fmt" "reflect" @@ -103,6 +104,8 @@ type User struct { DeletionScheduledAt time.Time `xorm:"datetime null" json:"-"` DeletionLastReminderSent time.Time `xorm:"datetime null" json:"-"` + FrontendSettings interface{} `xorm:"json null" json:"-"` + ExportFileID int64 `xorm:"bigint null" json:"-"` // A timestamp when this task was created. You cannot change this value. @@ -484,6 +487,12 @@ func UpdateUser(s *xorm.Session, user *User, forceOverride bool) (updatedUser *U return } + frontendSettingsJSON, err := json.Marshal(user.FrontendSettings) + if err != nil { + return nil, err + } + user.FrontendSettings = frontendSettingsJSON + // Update it _, err = s. ID(user.ID). @@ -503,6 +512,7 @@ func UpdateUser(s *xorm.Session, user *User, forceOverride bool) (updatedUser *U "language", "timezone", "overdue_tasks_reminders_time", + "frontend_settings", ). Update(user) if err != nil {