@@ -79,8 +79,8 @@ func IsErrDataExpired(err error) bool {
79
79
return ok
80
80
}
81
81
82
- // GetSettingNoCache returns specific setting without using the cache
83
- func GetSettingNoCache (ctx context.Context , key string ) (* Setting , error ) {
82
+ // GetSetting returns specific setting without using the cache
83
+ func GetSetting (ctx context.Context , key string ) (* Setting , error ) {
84
84
v , err := GetSettings (ctx , []string {key })
85
85
if err != nil {
86
86
return nil , err
@@ -93,11 +93,11 @@ func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {
93
93
94
94
const contextCacheKey = "system_setting"
95
95
96
- // GetSetting returns the setting value via the key
97
- func GetSetting (ctx context.Context , key string ) (string , error ) {
96
+ // GetSettingWithCache returns the setting value via the key
97
+ func GetSettingWithCache (ctx context.Context , key string ) (string , error ) {
98
98
return cache .GetWithContextCache (ctx , contextCacheKey , key , func () (string , error ) {
99
99
return cache .GetString (genSettingCacheKey (key ), func () (string , error ) {
100
- res , err := GetSettingNoCache (ctx , key )
100
+ res , err := GetSetting (ctx , key )
101
101
if err != nil {
102
102
return "" , err
103
103
}
@@ -110,6 +110,15 @@ func GetSetting(ctx context.Context, key string) (string, error) {
110
110
// none existing keys and errors are ignored and result in false
111
111
func GetSettingBool (ctx context.Context , key string ) bool {
112
112
s , _ := GetSetting (ctx , key )
113
+ if s == nil {
114
+ return false
115
+ }
116
+ v , _ := strconv .ParseBool (s .SettingValue )
117
+ return v
118
+ }
119
+
120
+ func GetSettingWithCacheBool (ctx context.Context , key string ) bool {
121
+ s , _ := GetSettingWithCache (ctx , key )
113
122
v , _ := strconv .ParseBool (s )
114
123
return v
115
124
}
@@ -120,7 +129,7 @@ func GetSettings(ctx context.Context, keys []string) (map[string]*Setting, error
120
129
keys [i ] = strings .ToLower (keys [i ])
121
130
}
122
131
settings := make ([]* Setting , 0 , len (keys ))
123
- if err := db .GetEngine (db . DefaultContext ).
132
+ if err := db .GetEngine (ctx ).
124
133
Where (builder .In ("setting_key" , keys )).
125
134
Find (& settings ); err != nil {
126
135
return nil , err
@@ -151,9 +160,9 @@ func (settings AllSettings) GetVersion(key string) int {
151
160
}
152
161
153
162
// GetAllSettings returns all settings from user
154
- func GetAllSettings () (AllSettings , error ) {
163
+ func GetAllSettings (ctx context. Context ) (AllSettings , error ) {
155
164
settings := make ([]* Setting , 0 , 5 )
156
- if err := db .GetEngine (db . DefaultContext ).
165
+ if err := db .GetEngine (ctx ).
157
166
Find (& settings ); err != nil {
158
167
return nil , err
159
168
}
@@ -168,12 +177,12 @@ func GetAllSettings() (AllSettings, error) {
168
177
func DeleteSetting (ctx context.Context , setting * Setting ) error {
169
178
cache .RemoveContextData (ctx , contextCacheKey , setting .SettingKey )
170
179
cache .Remove (genSettingCacheKey (setting .SettingKey ))
171
- _ , err := db .GetEngine (db . DefaultContext ).Delete (setting )
180
+ _ , err := db .GetEngine (ctx ).Delete (setting )
172
181
return err
173
182
}
174
183
175
184
func SetSettingNoVersion (ctx context.Context , key , value string ) error {
176
- s , err := GetSettingNoCache (ctx , key )
185
+ s , err := GetSetting (ctx , key )
177
186
if IsErrSettingIsNotExist (err ) {
178
187
return SetSetting (ctx , & Setting {
179
188
SettingKey : key ,
@@ -189,7 +198,7 @@ func SetSettingNoVersion(ctx context.Context, key, value string) error {
189
198
190
199
// SetSetting updates a users' setting for a specific key
191
200
func SetSetting (ctx context.Context , setting * Setting ) error {
192
- if err := upsertSettingValue (strings .ToLower (setting .SettingKey ), setting .SettingValue , setting .Version ); err != nil {
201
+ if err := upsertSettingValue (ctx , strings .ToLower (setting .SettingKey ), setting .SettingValue , setting .Version ); err != nil {
193
202
return err
194
203
}
195
204
@@ -205,8 +214,8 @@ func SetSetting(ctx context.Context, setting *Setting) error {
205
214
return nil
206
215
}
207
216
208
- func upsertSettingValue (key , value string , version int ) error {
209
- return db .WithTx (db . DefaultContext , func (ctx context.Context ) error {
217
+ func upsertSettingValue (parentCtx context. Context , key , value string , version int ) error {
218
+ return db .WithTx (parentCtx , func (ctx context.Context ) error {
210
219
e := db .GetEngine (ctx )
211
220
212
221
// here we use a general method to do a safe upsert for different databases (and most transaction levels)
@@ -249,9 +258,9 @@ var (
249
258
LibravatarService * libravatar.Libravatar
250
259
)
251
260
252
- func Init () error {
261
+ func Init (ctx context. Context ) error {
253
262
var disableGravatar bool
254
- disableGravatarSetting , err := GetSettingNoCache ( db . DefaultContext , KeyPictureDisableGravatar )
263
+ disableGravatarSetting , err := GetSetting ( ctx , KeyPictureDisableGravatar )
255
264
if IsErrSettingIsNotExist (err ) {
256
265
disableGravatar = setting_module .GetDefaultDisableGravatar ()
257
266
disableGravatarSetting = & Setting {SettingValue : strconv .FormatBool (disableGravatar )}
@@ -262,7 +271,7 @@ func Init() error {
262
271
}
263
272
264
273
var enableFederatedAvatar bool
265
- enableFederatedAvatarSetting , err := GetSettingNoCache ( db . DefaultContext , KeyPictureEnableFederatedAvatar )
274
+ enableFederatedAvatarSetting , err := GetSetting ( ctx , KeyPictureEnableFederatedAvatar )
266
275
if IsErrSettingIsNotExist (err ) {
267
276
enableFederatedAvatar = setting_module .GetDefaultEnableFederatedAvatar (disableGravatar )
268
277
enableFederatedAvatarSetting = & Setting {SettingValue : strconv .FormatBool (enableFederatedAvatar )}
@@ -275,13 +284,13 @@ func Init() error {
275
284
if setting_module .OfflineMode {
276
285
disableGravatar = true
277
286
enableFederatedAvatar = false
278
- if ! GetSettingBool (db . DefaultContext , KeyPictureDisableGravatar ) {
279
- if err := SetSettingNoVersion (db . DefaultContext , KeyPictureDisableGravatar , "true" ); err != nil {
287
+ if ! GetSettingBool (ctx , KeyPictureDisableGravatar ) {
288
+ if err := SetSettingNoVersion (ctx , KeyPictureDisableGravatar , "true" ); err != nil {
280
289
return fmt .Errorf ("Failed to set setting %q: %w" , KeyPictureDisableGravatar , err )
281
290
}
282
291
}
283
- if GetSettingBool (db . DefaultContext , KeyPictureEnableFederatedAvatar ) {
284
- if err := SetSettingNoVersion (db . DefaultContext , KeyPictureEnableFederatedAvatar , "false" ); err != nil {
292
+ if GetSettingBool (ctx , KeyPictureEnableFederatedAvatar ) {
293
+ if err := SetSettingNoVersion (ctx , KeyPictureEnableFederatedAvatar , "false" ); err != nil {
285
294
return fmt .Errorf ("Failed to set setting %q: %w" , KeyPictureEnableFederatedAvatar , err )
286
295
}
287
296
}
0 commit comments