From 8347f3e1fadf98f162597151665f00e988bd2358 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Dieguez Date: Thu, 5 Dec 2019 15:58:38 +0100 Subject: [PATCH] Bulk import/export supports direct messages from a user to themselves (#13200) * Test added to check support for import of messages sent to your own DM channel * Support for single member direct channel messages and posts Now bulk import is able to export/import the self direct channel and the posts inside * Improve how the copy of channel members for DM and DM posts * Improved the tests for the export self-channels --- app/export.go | 12 ------------ app/export_converters.go | 12 ++++++++++-- app/export_test.go | 9 ++++++--- app/import_test.go | 2 ++ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/export.go b/app/export.go index a6d35e84ca..21c80854af 100644 --- a/app/export.go +++ b/app/export.go @@ -532,12 +532,6 @@ func (a *App) ExportAllDirectChannels(writer io.Writer) *model.AppError { continue } - // There's no import support for single member channels yet. - if len(*channel.Members) == 1 { - mlog.Debug("Bulk export for direct channels containing a single member is not supported.") - continue - } - channelLine := ImportLineFromDirectChannel(channel) if err := a.ExportWriteLine(writer, channelLine); err != nil { return err @@ -568,12 +562,6 @@ func (a *App) ExportAllDirectPosts(writer io.Writer) *model.AppError { continue } - // There's no import support for single member channels yet. - if len(*post.ChannelMembers) == 1 { - mlog.Debug("Bulk export for posts containing a single member is not supported.") - continue - } - // Do the Replies. replies, err := a.buildPostReplies(post.Id) if err != nil { diff --git a/app/export_converters.go b/app/export_converters.go index 876d8666b3..89d00c64f2 100644 --- a/app/export_converters.go +++ b/app/export_converters.go @@ -39,11 +39,15 @@ func ImportLineFromChannel(channel *model.ChannelForExport) *LineImportData { } func ImportLineFromDirectChannel(channel *model.DirectChannelForExport) *LineImportData { + channelMembers := *channel.Members + if len(channelMembers) == 1 { + channelMembers = []string{channelMembers[0], channelMembers[0]} + } return &LineImportData{ Type: "direct_channel", DirectChannel: &DirectChannelImportData{ Header: &channel.Header, - Members: channel.Members, + Members: &channelMembers, }, } } @@ -158,10 +162,14 @@ func ImportLineForPost(post *model.PostForExport) *LineImportData { } func ImportLineForDirectPost(post *model.DirectPostForExport) *LineImportData { + channelMembers := *post.ChannelMembers + if len(channelMembers) == 1 { + channelMembers = []string{channelMembers[0], channelMembers[0]} + } return &LineImportData{ Type: "direct_post", DirectPost: &DirectPostImportData{ - ChannelMembers: post.ChannelMembers, + ChannelMembers: &channelMembers, User: &post.User, Message: &post.Message, CreateAt: &post.CreateAt, diff --git a/app/export_test.go b/app/export_test.go index 47e14620c4..bf8d524223 100644 --- a/app/export_test.go +++ b/app/export_test.go @@ -284,10 +284,11 @@ func TestExportDMChannelToSelf(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 0, i) - // Ensure no channels were imported channels, err = th2.App.Srv.Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000") require.Nil(t, err) - assert.Equal(t, 0, len(channels)) + assert.Equal(t, 1, len(channels)) + assert.Equal(t, 1, len((*channels[0].Members))) + assert.Equal(t, th1.BasicUser.Username, (*channels[0].Members)[0]) } func TestExportGMChannel(t *testing.T) { @@ -478,5 +479,7 @@ func TestExportDMPostWithSelf(t *testing.T) { posts, err = th2.App.Srv.Store.Post().GetDirectPostParentsForExportAfter(1000, "0000000") require.Nil(t, err) - assert.Equal(t, 0, len(posts)) + assert.Equal(t, 1, len(posts)) + assert.Equal(t, 1, len((*posts[0].ChannelMembers))) + assert.Equal(t, th1.BasicUser.Username, (*posts[0].ChannelMembers)[0]) } diff --git a/app/import_test.go b/app/import_test.go index c7d7fd7ea0..4162ec7279 100644 --- a/app/import_test.go +++ b/app/import_test.go @@ -163,8 +163,10 @@ func TestImportBulkImport(t *testing.T) { {"type": "user", "user": {"username": "` + username3 + `", "email": "` + username3 + `@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}], "delete_at": 123456789016}]}} {"type": "post", "post": {"team": "` + teamName + `", "channel": "` + channelName + `", "user": "` + username + `", "message": "Hello World", "create_at": 123456789012, "attachments":[{"path": "` + testImage + `"}]}} {"type": "post", "post": {"team": "` + teamName + `", "channel": "` + channelName + `", "user": "` + username3 + `", "message": "Hey Everyone!", "create_at": 123456789013, "attachments":[{"path": "` + testImage + `"}]}} +{"type": "direct_channel", "direct_channel": {"members": ["` + username + `", "` + username + `"]}} {"type": "direct_channel", "direct_channel": {"members": ["` + username + `", "` + username2 + `"]}} {"type": "direct_channel", "direct_channel": {"members": ["` + username + `", "` + username2 + `", "` + username3 + `"]}} +{"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username + `"], "user": "` + username + `", "message": "Hello Direct Channel to myself", "create_at": 123456789014}} {"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username2 + `"], "user": "` + username + `", "message": "Hello Direct Channel", "create_at": 123456789014}} {"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username2 + `", "` + username3 + `"], "user": "` + username + `", "message": "Hello Group Channel", "create_at": 123456789015}} {"type": "emoji", "emoji": {"name": "` + emojiName + `", "image": "` + testImage + `"}}`