From db45c0132ee71367d41d11a806c65a7ac1d3c5a4 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Mon, 8 Jul 2024 09:41:25 +0530 Subject: [PATCH] MM-57824: Export/import custom status (#27361) https://mattermost.atlassian.net/browse/MM-57824 ```release-note NONE ``` Co-authored-by: Mattermost Build --- server/channels/app/export.go | 5 +++ server/channels/app/export_test.go | 35 +++++++++++++++++++++ server/channels/app/import_functions.go | 6 ++++ server/channels/app/imports/import_types.go | 3 +- server/i18n/en.json | 4 +++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/server/channels/app/export.go b/server/channels/app/export.go index a73902b7ce..311bcc7014 100644 --- a/server/channels/app/export.go +++ b/server/channels/app/export.go @@ -469,6 +469,11 @@ func (a *App) exportAllUsers(ctx request.CTX, job *model.Job, writer io.Writer, userLine.User.NotifyProps = a.buildUserNotifyProps(user.NotifyProps) + // Adding custom status + if cs := user.GetCustomStatus(); cs != nil { + userLine.User.CustomStatus = cs + } + // Do the Team Memberships. members, err := a.buildUserTeamAndChannelMemberships(ctx, user.Id, includeArchivedChannels) if err != nil { diff --git a/server/channels/app/export_test.go b/server/channels/app/export_test.go index 6a21b6396b..112575bab7 100644 --- a/server/channels/app/export_test.go +++ b/server/channels/app/export_test.go @@ -10,6 +10,7 @@ import ( "path/filepath" "sort" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -574,6 +575,40 @@ func TestExportPostWithProps(t *testing.T) { assert.Contains(t, posts[1].Props["attachments"].([]any)[0], "footer") } +func TestExportUserCustomStatus(t *testing.T) { + th1 := Setup(t).InitBasic() + + cs := &model.CustomStatus{ + Emoji: "palm_tree", + Text: "on a vacation", + Duration: "this_week", + ExpiresAt: time.Now().Add(24 * time.Hour), + } + appErr := th1.App.SetCustomStatus(th1.Context, th1.BasicUser.Id, cs) + require.Nil(t, appErr) + + uname := th1.BasicUser.Username + + var b bytes.Buffer + appErr = th1.App.BulkExport(th1.Context, &b, "somePath", nil, model.BulkExportOpts{}) + require.Nil(t, appErr) + + th1.TearDown() + + th2 := Setup(t) + defer th2.TearDown() + + appErr, i := th2.App.BulkImport(th2.Context, &b, nil, false, 1) + require.Nil(t, appErr) + assert.Equal(t, 0, i) + + gotUser, err := th2.Server.Store().User().GetByUsername(uname) + require.NoError(t, err) + gotCs := gotUser.GetCustomStatus() + require.Equal(t, cs.Emoji, gotCs.Emoji) + require.Equal(t, cs.Text, gotCs.Text) +} + func TestExportDMPostWithSelf(t *testing.T) { th1 := Setup(t).InitBasic() diff --git a/server/channels/app/import_functions.go b/server/channels/app/import_functions.go index aaa030f875..0ec689b0d3 100644 --- a/server/channels/app/import_functions.go +++ b/server/channels/app/import_functions.go @@ -536,6 +536,12 @@ func (a *App) importUser(rctx request.CTX, data *imports.UserImportData, dryRun } } + if data.CustomStatus != nil { + if err := user.SetCustomStatus(data.CustomStatus); err != nil { + return model.NewAppError("importUser", "app.import.custom_status.error", nil, "", http.StatusBadRequest).Wrap(err) + } + } + var savedUser *model.User var err error if user.Id == "" { diff --git a/server/channels/app/imports/import_types.go b/server/channels/app/imports/import_types.go index a08397975b..ca33181bd2 100644 --- a/server/channels/app/imports/import_types.go +++ b/server/channels/app/imports/import_types.go @@ -85,7 +85,8 @@ type UserImportData struct { TutorialStep *string `json:"tutorial_step,omitempty"` EmailInterval *string `json:"email_interval,omitempty"` - NotifyProps *UserNotifyPropsImportData `json:"notify_props,omitempty"` + NotifyProps *UserNotifyPropsImportData `json:"notify_props,omitempty"` + CustomStatus *model.CustomStatus `json:"custom_status,omitempty"` } type UserNotifyPropsImportData struct { diff --git a/server/i18n/en.json b/server/i18n/en.json index 25610fdf6c..9aa1d8a933 100644 --- a/server/i18n/en.json +++ b/server/i18n/en.json @@ -5122,6 +5122,10 @@ "id": "app.import.bulk_import.unsupported_version.error", "translation": "Incorrect or missing version in the data import file. Make sure version is the first object in your import file and try again." }, + { + "id": "app.import.custom_status.error", + "translation": "Unable to set custom status." + }, { "id": "app.import.emoji.bad_file.error", "translation": "Error reading import emoji image file. Emoji with name: \"{{.EmojiName}}\""