Skip to content

Commit

Permalink
PLT-5398 Bulk loading of user display preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
grundleborg committed Feb 13, 2017
1 parent fac85b6 commit 422daaf
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app/import.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
124 changes: 124 additions & 0 deletions app/import_test.go
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions i18n/en.json
Expand Up @@ -2975,6 +2975,10 @@
"id": "app.import.validate_user_channels_import_data.invalid_notify_props_mark_unread.error",
"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"
Expand Down

0 comments on commit 422daaf

Please sign in to comment.