|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package user |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models/db" |
| 13 | + |
| 14 | + "xorm.io/builder" |
| 15 | +) |
| 16 | + |
| 17 | +// Setting is a key value store of user settings |
| 18 | +type Setting struct { |
| 19 | + ID int64 `xorm:"pk autoincr"` |
| 20 | + UserID int64 `xorm:"index unique(key_userid)"` // to load all of someone's settings |
| 21 | + SettingKey string `xorm:"varchar(255) index unique(key_userid)"` // ensure key is always lowercase |
| 22 | + SettingValue string `xorm:"text"` |
| 23 | +} |
| 24 | + |
| 25 | +// TableName sets the table name for the settings struct |
| 26 | +func (s *Setting) TableName() string { |
| 27 | + return "user_setting" |
| 28 | +} |
| 29 | + |
| 30 | +func init() { |
| 31 | + db.RegisterModel(new(Setting)) |
| 32 | +} |
| 33 | + |
| 34 | +// GetSettings returns specific settings from user |
| 35 | +func GetSettings(uid int64, keys []string) (map[string]*Setting, error) { |
| 36 | + settings := make([]*Setting, 0, len(keys)) |
| 37 | + if err := db.GetEngine(db.DefaultContext). |
| 38 | + Where("user_id=?", uid). |
| 39 | + And(builder.In("setting_key", keys)). |
| 40 | + Find(&settings); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + settingsMap := make(map[string]*Setting) |
| 44 | + for _, s := range settings { |
| 45 | + settingsMap[s.SettingKey] = s |
| 46 | + } |
| 47 | + return settingsMap, nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetUserAllSettings returns all settings from user |
| 51 | +func GetUserAllSettings(uid int64) (map[string]*Setting, error) { |
| 52 | + settings := make([]*Setting, 0, 5) |
| 53 | + if err := db.GetEngine(db.DefaultContext). |
| 54 | + Where("user_id=?", uid). |
| 55 | + Find(&settings); err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + settingsMap := make(map[string]*Setting) |
| 59 | + for _, s := range settings { |
| 60 | + settingsMap[s.SettingKey] = s |
| 61 | + } |
| 62 | + return settingsMap, nil |
| 63 | +} |
| 64 | + |
| 65 | +// DeleteSetting deletes a specific setting for a user |
| 66 | +func DeleteSetting(setting *Setting) error { |
| 67 | + _, err := db.GetEngine(db.DefaultContext).Delete(setting) |
| 68 | + return err |
| 69 | +} |
| 70 | + |
| 71 | +// SetSetting updates a users' setting for a specific key |
| 72 | +func SetSetting(setting *Setting) error { |
| 73 | + if strings.ToLower(setting.SettingKey) != setting.SettingKey { |
| 74 | + return fmt.Errorf("setting key should be lowercase") |
| 75 | + } |
| 76 | + return upsertSettingValue(setting.UserID, setting.SettingKey, setting.SettingValue) |
| 77 | +} |
| 78 | + |
| 79 | +func upsertSettingValue(userID int64, key string, value string) error { |
| 80 | + return db.WithTx(func(ctx context.Context) error { |
| 81 | + e := db.GetEngine(ctx) |
| 82 | + |
| 83 | + // here we use a general method to do a safe upsert for different databases (and most transaction levels) |
| 84 | + // 1. try to UPDATE the record and acquire the transaction write lock |
| 85 | + // if UPDATE returns non-zero rows are changed, OK, the setting is saved correctly |
| 86 | + // if UPDATE returns "0 rows changed", two possibilities: (a) record doesn't exist (b) value is not changed |
| 87 | + // 2. do a SELECT to check if the row exists or not (we already have the transaction lock) |
| 88 | + // 3. if the row doesn't exist, do an INSERT (we are still protected by the transaction lock, so it's safe) |
| 89 | + // |
| 90 | + // to optimize the SELECT in step 2, we can use an extra column like `revision=revision+1` |
| 91 | + // to make sure the UPDATE always returns a non-zero value for existing (unchanged) records. |
| 92 | + |
| 93 | + res, err := e.Exec("UPDATE user_setting SET setting_value=? WHERE setting_key=? AND user_id=?", value, key, userID) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + rows, _ := res.RowsAffected() |
| 98 | + if rows > 0 { |
| 99 | + // the existing row is updated, so we can return |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + // in case the value isn't changed, update would return 0 rows changed, so we need this check |
| 104 | + has, err := e.Exist(&Setting{UserID: userID, SettingKey: key}) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if has { |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + // if no existing row, insert a new row |
| 113 | + _, err = e.Insert(&Setting{UserID: userID, SettingKey: key, SettingValue: value}) |
| 114 | + return err |
| 115 | + }) |
| 116 | +} |
0 commit comments