summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-02-14 13:38:48 +0000
committerJoram Wilander <jwawilander@gmail.com>2017-02-14 08:38:48 -0500
commit6251e836b048f8f5aeeee97f3b2ad216f2a2c4bb (patch)
tree52187aff064da0b04b3cb82cad938d8bb9ee8f2a
parent8da1c696d4488890bc1670b8189ddfcd694c0406 (diff)
downloadchat-6251e836b048f8f5aeeee97f3b2ad216f2a2c4bb.tar.gz
chat-6251e836b048f8f5aeeee97f3b2ad216f2a2c4bb.tar.bz2
chat-6251e836b048f8f5aeeee97f3b2ad216f2a2c4bb.zip
PLT-5398 Bulk loading of user display preferences. (#5382)
-rw-r--r--app/import.go80
-rw-r--r--app/import_test.go124
-rw-r--r--i18n/en.json4
3 files changed, 208 insertions, 0 deletions
diff --git a/app/import.go b/app/import.go
index 599c08f9d..1241048d3 100644
--- a/app/import.go
+++ b/app/import.go
@@ -57,6 +57,14 @@ type UserImportData struct {
Locale *string `json:"locale"`
Teams *[]UserTeamImportData `json:"teams"`
+
+ Theme *string `json:"theme"`
+ SelectedFont *string `json:"display_font"`
+ UseMilitaryTime *string `json:"military_time"`
+ NameFormat *string `json:"teammate_name_display"`
+ CollapsePreviews *string `json:"link_previews"`
+ MessageDisplay *string `json:"message_display"`
+ ChannelDisplayMode *string `json:"channel_display_mode"`
}
type UserTeamImportData struct {
@@ -395,6 +403,78 @@ func ImportUser(data *UserImportData, dryRun bool) *model.AppError {
}
}
+ // Preferences.
+ var preferences model.Preferences
+
+ if data.Theme != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_THEME,
+ Name: "",
+ Value: *data.Theme,
+ })
+ }
+
+ if data.SelectedFont != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "selected_font",
+ Value: *data.SelectedFont,
+ })
+ }
+
+ if data.UseMilitaryTime != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "use_military_time",
+ Value: *data.UseMilitaryTime,
+ })
+ }
+
+ if data.NameFormat != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "name_format",
+ Value: *data.NameFormat,
+ })
+ }
+
+ if data.CollapsePreviews != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "collapse_previews",
+ Value: *data.CollapsePreviews,
+ })
+ }
+
+ if data.MessageDisplay != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "message_display",
+ Value: *data.MessageDisplay,
+ })
+ }
+
+ if data.ChannelDisplayMode != nil {
+ preferences = append(preferences, model.Preference{
+ UserId: user.Id,
+ Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
+ Name: "channel_display_mode",
+ Value: *data.ChannelDisplayMode,
+ })
+ }
+
+ if len(preferences) > 0 {
+ if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil {
+ return model.NewAppError("BulkImport", "app.import.import_user.save_preferences.error", nil, "", http.StatusInternalServerError)
+ }
+ }
+
return ImportUserTeams(*data.Username, data.Teams)
}
diff --git a/app/import_test.go b/app/import_test.go
index e163ddc45..008267942 100644
--- a/app/import_test.go
+++ b/app/import_test.go
@@ -1203,6 +1203,130 @@ func TestImportImportUser(t *testing.T) {
} else if cmc != channelMemberCount+1 {
t.Fatalf("Number of channel members not as expected")
}
+
+ // Add a user with some preferences.
+ username = model.NewId()
+ data = UserImportData{
+ Username: &username,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ Theme: ptrStr(`{"awayIndicator":"#DCBD4E","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`),
+ SelectedFont: ptrStr("Roboto Slab"),
+ UseMilitaryTime: ptrStr("true"),
+ NameFormat: ptrStr("nickname_full_name"),
+ CollapsePreviews: ptrStr("true"),
+ MessageDisplay: ptrStr("compact"),
+ ChannelDisplayMode: ptrStr("centered"),
+ }
+ if err := ImportUser(&data, false); err != nil {
+ t.Fatalf("Should have succeeded.")
+ }
+
+ // Check their values.
+ user, err = GetUserByUsername(username);
+ if err != nil {
+ t.Fatalf("Failed to get user from database.")
+ }
+
+ if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil {
+ t.Fatalf("Failed to get theme category preferences")
+ } else {
+ preferences := res.Data.(model.Preferences)
+ for _, preference := range preferences {
+ if preference.Name == "" && preference.Value != *data.Theme {
+ t.Fatalf("Preference does not match.")
+ }
+ }
+ }
+
+ if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil {
+ t.Fatalf("Failed to get display category preferences")
+ } else {
+ preferences := res.Data.(model.Preferences)
+ for _, preference := range preferences {
+ if preference.Name == "selected_font" && preference.Value != *data.SelectedFont {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "use_military_time" && preference.Value != *data.UseMilitaryTime {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "name_format" && preference.Value != *data.NameFormat {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "collapse_previews" && preference.Value != *data.CollapsePreviews {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "message_display" && preference.Value != *data.MessageDisplay {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "channel_display_mode" && preference.Value != *data.ChannelDisplayMode {
+ t.Fatalf("Preference does not match.")
+ }
+ }
+ }
+
+ // Change those preferences.
+ data = UserImportData{
+ Username: &username,
+ Email: ptrStr(model.NewId() + "@example.com"),
+ Theme: ptrStr(`{"awayIndicator":"#123456","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`),
+ SelectedFont: ptrStr("Lato"),
+ UseMilitaryTime: ptrStr("false"),
+ NameFormat: ptrStr("full_name"),
+ CollapsePreviews: ptrStr("false"),
+ MessageDisplay: ptrStr("clean"),
+ ChannelDisplayMode: ptrStr("full"),
+ }
+ if err := ImportUser(&data, false); err != nil {
+ t.Fatalf("Should have succeeded.")
+ }
+
+ // Check their values again.
+ if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil {
+ t.Fatalf("Failed to get theme category preferences")
+ } else {
+ preferences := res.Data.(model.Preferences)
+ for _, preference := range preferences {
+ if preference.Name == "" && preference.Value != *data.Theme {
+ t.Fatalf("Preference does not match.")
+ }
+ }
+ }
+
+ if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil {
+ t.Fatalf("Failed to get display category preferences")
+ } else {
+ preferences := res.Data.(model.Preferences)
+ for _, preference := range preferences {
+ if preference.Name == "selected_font" && preference.Value != *data.SelectedFont {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "use_military_time" && preference.Value != *data.UseMilitaryTime {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "name_format" && preference.Value != *data.NameFormat {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "collapse_previews" && preference.Value != *data.CollapsePreviews {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "message_display" && preference.Value != *data.MessageDisplay {
+ t.Fatalf("Preference does not match.")
+ }
+
+ if preference.Name == "channel_display_mode" && preference.Value != *data.ChannelDisplayMode {
+ t.Fatalf("Preference does not match.")
+ }
+ }
+ }
}
func TestImportImportLine(t *testing.T) {
diff --git a/i18n/en.json b/i18n/en.json
index 9aa39dfa4..619d711b8 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2976,6 +2976,10 @@
"translation": "Invalid MarkUnread NotifyProps for User's Channel Membership."
},
{
+ "id": "app.import.import_user.save_preferences.error",
+ "translation": "Failed to save the provided preferences for the user."
+ },
+ {
"id": "authentication.permissions.create_team_roles.description",
"translation": "Ability to create new teams"
},