@ -79,8 +79,8 @@ func IsErrDataExpired(err error) bool {
return ok
}
// GetSettingNoCache returns specific setting without using the cache
func GetSettingNoCache ( ctx context . Context , key string ) ( * Setting , error ) {
// GetSetting returns specific setting without using the cache
func GetSetting ( ctx context . Context , key string ) ( * Setting , error ) {
v , err := GetSettings ( ctx , [ ] string { key } )
if err != nil {
return nil , err
@ -93,11 +93,11 @@ func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {
const contextCacheKey = "system_setting"
// GetSetting returns the setting value via the key
func GetSetting ( ctx context . Context , key string ) ( string , error ) {
// GetSettingWithCache returns the setting value via the key
func GetSettingWithCache ( ctx context . Context , key string ) ( string , error ) {
return cache . GetWithContextCache ( ctx , contextCacheKey , key , func ( ) ( string , error ) {
return cache . GetString ( genSettingCacheKey ( key ) , func ( ) ( string , error ) {
res , err := GetSettingNoCache ( ctx , key )
res , err := GetSetting ( ctx , key )
if err != nil {
return "" , err
}
@ -110,6 +110,15 @@ func GetSetting(ctx context.Context, key string) (string, error) {
// none existing keys and errors are ignored and result in false
func GetSettingBool ( ctx context . Context , key string ) bool {
s , _ := GetSetting ( ctx , key )
if s == nil {
return false
}
v , _ := strconv . ParseBool ( s . SettingValue )
return v
}
func GetSettingWithCacheBool ( ctx context . Context , key string ) bool {
s , _ := GetSettingWithCache ( ctx , key )
v , _ := strconv . ParseBool ( s )
return v
}
@ -120,7 +129,7 @@ func GetSettings(ctx context.Context, keys []string) (map[string]*Setting, error
keys [ i ] = strings . ToLower ( keys [ i ] )
}
settings := make ( [ ] * Setting , 0 , len ( keys ) )
if err := db . GetEngine ( db . Defaul tConte xt ) .
if err := db . GetEngine ( c tx) .
Where ( builder . In ( "setting_key" , keys ) ) .
Find ( & settings ) ; err != nil {
return nil , err
@ -151,9 +160,9 @@ func (settings AllSettings) GetVersion(key string) int {
}
// GetAllSettings returns all settings from user
func GetAllSettings ( ) ( AllSettings , error ) {
func GetAllSettings ( ctx context . Context ) ( AllSettings , error ) {
settings := make ( [ ] * Setting , 0 , 5 )
if err := db . GetEngine ( db . Defaul tConte xt ) .
if err := db . GetEngine ( c tx) .
Find ( & settings ) ; err != nil {
return nil , err
}
@ -168,12 +177,12 @@ func GetAllSettings() (AllSettings, error) {
func DeleteSetting ( ctx context . Context , setting * Setting ) error {
cache . RemoveContextData ( ctx , contextCacheKey , setting . SettingKey )
cache . Remove ( genSettingCacheKey ( setting . SettingKey ) )
_ , err := db . GetEngine ( db . Defaul tConte xt ) . Delete ( setting )
_ , err := db . GetEngine ( c tx) . Delete ( setting )
return err
}
func SetSettingNoVersion ( ctx context . Context , key , value string ) error {
s , err := GetSettingNoCache ( ctx , key )
s , err := GetSetting ( ctx , key )
if IsErrSettingIsNotExist ( err ) {
return SetSetting ( ctx , & Setting {
SettingKey : key ,
@ -189,7 +198,7 @@ func SetSettingNoVersion(ctx context.Context, key, value string) error {
// SetSetting updates a users' setting for a specific key
func SetSetting ( ctx context . Context , setting * Setting ) error {
if err := upsertSettingValue ( strings . ToLower ( setting . SettingKey ) , setting . SettingValue , setting . Version ) ; err != nil {
if err := upsertSettingValue ( ctx , strings . ToLower ( setting . SettingKey ) , setting . SettingValue , setting . Version ) ; err != nil {
return err
}
@ -205,8 +214,8 @@ func SetSetting(ctx context.Context, setting *Setting) error {
return nil
}
func upsertSettingValue ( key , value string , version int ) error {
return db . WithTx ( db . Defaul tCon te xt , func ( ctx context . Context ) error {
func upsertSettingValue ( parentCtx context . Context , key , value string , version int ) error {
return db . WithTx ( paren tCtx, func ( ctx context . Context ) error {
e := db . GetEngine ( ctx )
// here we use a general method to do a safe upsert for different databases (and most transaction levels)
@ -249,9 +258,9 @@ var (
LibravatarService * libravatar . Libravatar
)
func Init ( ) error {
func Init ( ctx context . Context ) error {
var disableGravatar bool
disableGravatarSetting , err := GetSettingNoCache ( db . Defaul tConte xt , KeyPictureDisableGravatar )
disableGravatarSetting , err := GetSetting ( c tx, KeyPictureDisableGravatar )
if IsErrSettingIsNotExist ( err ) {
disableGravatar = setting_module . GetDefaultDisableGravatar ( )
disableGravatarSetting = & Setting { SettingValue : strconv . FormatBool ( disableGravatar ) }
@ -262,7 +271,7 @@ func Init() error {
}
var enableFederatedAvatar bool
enableFederatedAvatarSetting , err := GetSettingNoCache ( db . Defaul tConte xt , KeyPictureEnableFederatedAvatar )
enableFederatedAvatarSetting , err := GetSetting ( c tx, KeyPictureEnableFederatedAvatar )
if IsErrSettingIsNotExist ( err ) {
enableFederatedAvatar = setting_module . GetDefaultEnableFederatedAvatar ( disableGravatar )
enableFederatedAvatarSetting = & Setting { SettingValue : strconv . FormatBool ( enableFederatedAvatar ) }
@ -275,13 +284,13 @@ func Init() error {
if setting_module . OfflineMode {
disableGravatar = true
enableFederatedAvatar = false
if ! GetSettingBool ( db . Defaul tConte xt , KeyPictureDisableGravatar ) {
if err := SetSettingNoVersion ( db . Defaul tConte xt , KeyPictureDisableGravatar , "true" ) ; err != nil {
if ! GetSettingBool ( c tx, KeyPictureDisableGravatar ) {
if err := SetSettingNoVersion ( c tx, KeyPictureDisableGravatar , "true" ) ; err != nil {
return fmt . Errorf ( "Failed to set setting %q: %w" , KeyPictureDisableGravatar , err )
}
}
if GetSettingBool ( db . Defaul tConte xt , KeyPictureEnableFederatedAvatar ) {
if err := SetSettingNoVersion ( db . Defaul tConte xt , KeyPictureEnableFederatedAvatar , "false" ) ; err != nil {
if GetSettingBool ( c tx, KeyPictureEnableFederatedAvatar ) {
if err := SetSettingNoVersion ( c tx, KeyPictureEnableFederatedAvatar , "false" ) ; err != nil {
return fmt . Errorf ( "Failed to set setting %q: %w" , KeyPictureEnableFederatedAvatar , err )
}
}