From 7a3cf112c526cf8832d5a044ac590fb6c7ff8686 Mon Sep 17 00:00:00 2001 From: Pradeep Murugesan Date: Wed, 29 Aug 2018 14:27:44 +0200 Subject: 9304 added the attachments import to direct post (#9308) --- app/import_functions.go | 9 ++++++++ app/import_functions_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ app/import_types.go | 7 +++--- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/import_functions.go b/app/import_functions.go index e370099d9..1490dc6fa 100644 --- a/app/import_functions.go +++ b/app/import_functions.go @@ -1118,6 +1118,14 @@ func (a *App) ImportDirectPost(data *DirectPostImportData, dryRun bool) *model.A post.Hashtags, _ = model.ParseHashtags(post.Message) + if data.Attachments != nil { + fileIds, err := a.uploadAttachments(data.Attachments, post, "noteam", dryRun) + if err != nil { + return err + } + post.FileIds = append(post.FileIds, fileIds...) + } + if post.Id == "" { if result := <-a.Srv.Store.Post().Save(post); result.Err != nil { return result.Err @@ -1171,6 +1179,7 @@ func (a *App) ImportDirectPost(data *DirectPostImportData, dryRun bool) *model.A } } + a.UpdateFileInfoWithPostId(post) return nil } diff --git a/app/import_functions_test.go b/app/import_functions_test.go index fd8ae462e..10b1d5587 100644 --- a/app/import_functions_test.go +++ b/app/import_functions_test.go @@ -2553,3 +2553,54 @@ func TestImportPostAndRepliesWithAttachments(t *testing.T) { AssertFileIdsInPost(attachments, th, t) } + +func TestImportDirectPostWithAttachments(t *testing.T) { + + th := Setup() + defer th.TearDown() + + testsDir, _ := utils.FindDir("tests") + testImage := filepath.Join(testsDir, "test.png") + + // Create a user. + username := model.NewId() + th.App.ImportUser(&UserImportData{ + Username: &username, + Email: ptrStr(model.NewId() + "@example.com"), + }, false) + user1, err := th.App.GetUserByUsername(username) + if err != nil { + t.Fatalf("Failed to get user1 from database.") + } + + username2 := model.NewId() + th.App.ImportUser(&UserImportData{ + Username: &username2, + Email: ptrStr(model.NewId() + "@example.com"), + }, false) + + user2, err := th.App.GetUserByUsername(username2) + if err != nil { + t.Fatalf("Failed to get user2 from database.") + } + + directImportData := &DirectPostImportData{ + ChannelMembers: &[]string{ + user1.Username, + user2.Username, + }, + User: &user1.Username, + Message: ptrStr("Direct message"), + CreateAt: ptrInt64(model.GetMillis()), + Attachments: &[]AttachmentImportData{{Path: &testImage}}, + } + + if err := th.App.ImportDirectPost(directImportData, false); err != nil { + t.Fatalf("Expected success.") + } + + attachments := GetAttachments(user1.Id, th, t) + assert.Equal(t, len(attachments), 1) + assert.Contains(t, attachments[0].Path, "noteam") + AssertFileIdsInPost(attachments, th, t) +} diff --git a/app/import_types.go b/app/import_types.go index 45119bffa..7d53e5b16 100644 --- a/app/import_types.go +++ b/app/import_types.go @@ -148,9 +148,10 @@ type DirectPostImportData struct { Message *string `json:"message"` CreateAt *int64 `json:"create_at"` - FlaggedBy *[]string `json:"flagged_by"` - Reactions *[]ReactionImportData `json:"reactions"` - Replies *[]ReplyImportData `json:"replies"` + FlaggedBy *[]string `json:"flagged_by"` + Reactions *[]ReactionImportData `json:"reactions"` + Replies *[]ReplyImportData `json:"replies"` + Attachments *[]AttachmentImportData `json:"attachments"` } type SchemeImportData struct { -- cgit v1.2.3-1-g7c22