diff options
author | Harrison Healey <harrisonmhealey@gmail.com> | 2016-07-14 10:08:36 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-07-14 10:08:36 -0400 |
commit | caabfbcdd56bdced7c5c1d38e00f488adffe7c60 (patch) | |
tree | 4d52326767246f331da352f0427e34bf714e3130 /model/preference.go | |
parent | 8e810bc2ebb743717b5b0fc4541fcd3b2565966c (diff) | |
download | chat-caabfbcdd56bdced7c5c1d38e00f488adffe7c60.tar.gz chat-caabfbcdd56bdced7c5c1d38e00f488adffe7c60.tar.bz2 chat-caabfbcdd56bdced7c5c1d38e00f488adffe7c60.zip |
PLT-2992 Added the ability to use different themes for each team (#3411)
* Cleaned up user_settings_theme.jsx and import_theme_modal.jsx
* Made ImportThemeModal use a callback to return the theme to the user settings modal instead of saving it directly
* Moved user theme from model to preferences
* Added serverside API to delete preferences TODO update package with client stuff
* Changed constants.jsx so that Preferences and ActionTypes can be imported on their own
* Updated ThemeProps migration code to properly rename solarized code themes
* Fixed warnings thrown by AppDispatcher
* Added clientside UI to support team-specific themes
* Removed debugging code from test
* Fixed setting a user's theme when they haven't set their theme before
Diffstat (limited to 'model/preference.go')
-rw-r--r-- | model/preference.go | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/model/preference.go b/model/preference.go index 22858e043..779c41e50 100644 --- a/model/preference.go +++ b/model/preference.go @@ -6,6 +6,8 @@ package model import ( "encoding/json" "io" + "regexp" + "strings" "unicode/utf8" ) @@ -17,6 +19,9 @@ const ( PREFERENCE_CATEGORY_DISPLAY_SETTINGS = "display_settings" PREFERENCE_NAME_COLLAPSE_SETTING = "collapse_previews" + PREFERENCE_CATEGORY_THEME = "theme" + // the name for theme props is the team id + PREFERENCE_CATEGORY_LAST = "last" PREFERENCE_NAME_LAST_CHANNEL = "channel" ) @@ -57,13 +62,48 @@ func (o *Preference) IsValid() *AppError { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.category.app_error", nil, "category="+o.Category) } - if len(o.Name) == 0 || len(o.Name) > 32 { + if len(o.Name) > 32 { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.name.app_error", nil, "name="+o.Name) } - if utf8.RuneCountInString(o.Value) > 128 { + if utf8.RuneCountInString(o.Value) > 2000 { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.value.app_error", nil, "value="+o.Value) } + if o.Category == PREFERENCE_CATEGORY_THEME { + var unused map[string]string + if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&unused); err != nil { + return NewLocAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value) + } + } + return nil } + +func (o *Preference) PreUpdate() { + if o.Category == PREFERENCE_CATEGORY_THEME { + // decode the value of theme (a map of strings to string) and eliminate any invalid values + var props map[string]string + if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&props); err != nil { + // just continue, the invalid preference value should get caught by IsValid before saving + return + } + + colorPattern := regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`) + + // blank out any invalid theme values + for name, value := range props { + if name == "image" || name == "type" || name == "codeTheme" { + continue + } + + if !colorPattern.MatchString(value) { + props[name] = "#ffffff" + } + } + + if b, err := json.Marshal(props); err == nil { + o.Value = string(b) + } + } +} |