From 3f5eb5f6f7e3f24b3bb12bba9e2792f7435f55bb Mon Sep 17 00:00:00 2001 From: Fareck Allony <98941350+allonios@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:29:42 +0300 Subject: [PATCH] Adding is_pinned attribute to post and direct_post objects when doing bulk import (#19487) * refactor: added is_pinned attribute to post and direct_post objects when doing bulk import * fix: handling IsPinned nil value in post and direct_post objects import * refactor: added pinned post tests for post and direct_post objects import * refactor: TestImportimportMultiplePostLines code enhancements + added pinned post tests in TestImportImportDirectPost * fix: test message text * fix: typo Post => DirectPost * fix: typo Post => DirectPost * refactor: remove redundant test * refactor: test enhancements * refactor: remove print statements. * refactor: using require instead of assert Co-authored-by: Mattermod --- app/import_functions.go | 6 ++++ app/import_functions_test.go | 58 +++++++++++++++++++++++++++++++++++- app/import_types.go | 2 ++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/app/import_functions.go b/app/import_functions.go index cf1925330e..ad5d23a535 100644 --- a/app/import_functions.go +++ b/app/import_functions.go @@ -1407,6 +1407,9 @@ func (a *App) importMultiplePostLines(c *request.Context, lines []LineImportWork if line.Post.Props != nil { post.Props = *line.Post.Props } + if line.Post.IsPinned != nil { + post.IsPinned = *line.Post.IsPinned + } fileIDs := a.uploadAttachments(c, line.Post.Attachments, post, team.Id) for _, fileID := range post.FileIds { @@ -1715,6 +1718,9 @@ func (a *App) importMultipleDirectPostLines(c *request.Context, lines []LineImpo if line.DirectPost.Props != nil { post.Props = *line.DirectPost.Props } + if line.DirectPost.IsPinned != nil { + post.IsPinned = *line.DirectPost.IsPinned + } fileIDs := a.uploadAttachments(c, line.DirectPost.Attachments, post, "noteam") for _, fileID := range post.FileIds { diff --git a/app/import_functions_test.go b/app/import_functions_test.go index 6b30631081..7d55b96e9e 100644 --- a/app/import_functions_test.go +++ b/app/import_functions_test.go @@ -2501,9 +2501,34 @@ func TestImportimportMultiplePostLines(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 0, errLine) + // Create a pinned message. + data = LineImportWorkerData{ + LineImportData{ + Post: &PostImportData{ + Team: &teamName, + Channel: &channelName, + User: &user2.Username, + Message: ptrStr("Pinned Message"), + CreateAt: ptrInt64(model.GetMillis()), + IsPinned: ptrBool(true), + }, + }, + 1, + } + errLine, err = th.App.importMultiplePostLines(th.Context, []LineImportWorkerData{data}, false) + require.Nil(t, err) + require.Equal(t, 0, errLine) + + resultPosts, nErr := th.App.Srv().Store.Post().GetPostsCreatedAt(channel.Id, *data.Post.CreateAt) + require.NoError(t, nErr, "Expected success.") + // Should be one post only created at this time. + require.Equal(t, 1, len(resultPosts)) + resultPost := resultPosts[0] + require.True(t, resultPost.IsPinned, "This post should be pinned.") + // Posts should be added to the right team AssertAllPostsCount(t, th.App, initialPostCountForTeam2, 1, team2.Id) - AssertAllPostsCount(t, th.App, initialPostCount, 14, team.Id) + AssertAllPostsCount(t, th.App, initialPostCount, 15, team.Id) } func TestImportImportPost(t *testing.T) { @@ -3561,6 +3586,37 @@ func TestImportImportDirectPost(t *testing.T) { assert.Equal(t, post.UserId, th.BasicUser.Id) }) + t.Run("Test with IsPinned", func(t *testing.T) { + pinnedValue := true + creationTime := model.GetMillis() + data := LineImportWorkerData{ + LineImportData{ + DirectPost: &DirectPostImportData{ + ChannelMembers: &[]string{ + th.BasicUser.Username, + th.BasicUser2.Username, + }, + User: ptrStr(th.BasicUser.Username), + Message: ptrStr("Message with EditAt"), + CreateAt: &creationTime, + IsPinned: &pinnedValue, + }, + }, + 1, + } + errLine, err := th.App.importMultipleDirectPostLines(th.Context, []LineImportWorkerData{data}, false) + require.Nil(t, err) + require.Equal(t, 0, errLine) + AssertAllPostsCount(t, th.App, initialPostCount, 8, "") + + posts, nErr := th.App.Srv().Store.Post().GetPostsCreatedAt(directChannel.Id, *data.DirectPost.CreateAt) + require.NoError(t, nErr) + require.Len(t, posts, 1) + + post := posts[0] + require.True(t, post.IsPinned) + }) + // ------------------ Group Channel ------------------------- // Create the GROUP channel. diff --git a/app/import_types.go b/app/import_types.go index d7e4795570..d742bee2c4 100644 --- a/app/import_types.go +++ b/app/import_types.go @@ -149,6 +149,7 @@ type PostImportData struct { Reactions *[]ReactionImportData `json:"reactions,omitempty"` Replies *[]ReplyImportData `json:"replies,omitempty"` Attachments *[]AttachmentImportData `json:"attachments,omitempty"` + IsPinned *bool `json:"is_pinned,omitempty"` } type DirectChannelImportData struct { @@ -172,6 +173,7 @@ type DirectPostImportData struct { Reactions *[]ReactionImportData `json:"reactions"` Replies *[]ReplyImportData `json:"replies"` Attachments *[]AttachmentImportData `json:"attachments"` + IsPinned *bool `json:"is_pinned,omitempty"` } type SchemeImportData struct {