diff --git a/app/import_functions.go b/app/import_functions.go index 9a51b8c103..04718cdf5c 100644 --- a/app/import_functions.go +++ b/app/import_functions.go @@ -1208,20 +1208,24 @@ func (a *App) getChannelsByNames(names []string, teamId string) (map[string]*mod return channels, nil } -func (a *App) getChannelsForPosts(teams map[string]*model.Team, data []*PostImportData) (map[string]*model.Channel, *model.AppError) { - channels := make(map[string]*model.Channel) +// getChannelsForPosts returns map[teamName]map[channelName]*model.Channel +func (a *App) getChannelsForPosts(teams map[string]*model.Team, data []*PostImportData) (map[string]map[string]*model.Channel, *model.AppError) { + teamChannels := make(map[string]map[string]*model.Channel) for _, postData := range data { - team := teams[*postData.Team] - if channel, ok := channels[*postData.Channel]; !ok || channel == nil { + teamName := *postData.Team + if _, ok := teamChannels[teamName]; !ok { + teamChannels[teamName] = make(map[string]*model.Channel) + } + if channel, ok := teamChannels[teamName][*postData.Channel]; !ok || channel == nil { var err error - channel, err = a.Srv().Store.Channel().GetByName(team.Id, *postData.Channel, true) + channel, err = a.Srv().Store.Channel().GetByName(teams[teamName].Id, *postData.Channel, true) if err != nil { return nil, model.NewAppError("BulkImport", "app.import.import_post.channel_not_found.error", map[string]interface{}{"ChannelName": *postData.Channel}, err.Error(), http.StatusBadRequest) } - channels[*postData.Channel] = channel + teamChannels[teamName][*postData.Channel] = channel } } - return channels, nil + return teamChannels, nil } // getPostStrID returns a string ID composed of several post fields to @@ -1282,7 +1286,7 @@ func (a *App) importMultiplePostLines(lines []LineImportWorkerData, dryRun bool) for _, line := range lines { team := teams[*line.Post.Team] - channel := channels[*line.Post.Channel] + channel := channels[*line.Post.Team][*line.Post.Channel] user := users[*line.Post.User] // Check if this post already exists. diff --git a/app/import_functions_test.go b/app/import_functions_test.go index 84137327b4..4c1d79bb1a 100644 --- a/app/import_functions_test.go +++ b/app/import_functions_test.go @@ -2403,6 +2403,63 @@ func TestImportimportMultiplePostLines(t *testing.T) { assert.Equal(t, 0, errLine) AssertAllPostsCount(t, th.App, initialPostCount, 11, team.Id) + + // Create another Team. + teamName2 := model.NewRandomTeamName() + th.App.importTeam(&TeamImportData{ + Name: &teamName2, + DisplayName: ptrStr("Display Name 2"), + Type: ptrStr("O"), + }, false) + team2, err := th.App.GetTeamByName(teamName2) + require.Nil(t, err, "Failed to get team from database.") + + // Create another Channel for the another team. + th.App.importChannel(&ChannelImportData{ + Team: &teamName2, + Name: &channelName, + DisplayName: ptrStr("Display Name"), + Type: ptrStr("O"), + }, false) + _, err = th.App.GetChannelByName(channelName, team2.Id, false) + require.Nil(t, err, "Failed to get channel from database.") + + // Count the number of posts in the team2. + initialPostCountForTeam2, nErr := th.App.Srv().Store.Post().AnalyticsPostCount(team2.Id, false, false) + require.Nil(t, nErr) + + // Try adding two valid posts in apply mode. + data = LineImportWorkerData{ + LineImportData{ + Post: &PostImportData{ + Team: &teamName, + Channel: &channelName, + User: &username, + Message: ptrStr("another message"), + CreateAt: &time, + }, + }, + 1, + } + data2 := LineImportWorkerData{ + LineImportData{ + Post: &PostImportData{ + Team: &teamName2, + Channel: &channelName, + User: &username, + Message: ptrStr("another message"), + CreateAt: &time, + }, + }, + 1, + } + errLine, err = th.App.importMultiplePostLines([]LineImportWorkerData{data, data2}, false) + assert.Nil(t, err) + assert.Equal(t, 0, errLine) + + // Posts should be added to the right team + AssertAllPostsCount(t, th.App, initialPostCountForTeam2, 1, team2.Id) + AssertAllPostsCount(t, th.App, initialPostCount, 12, team.Id) } func TestImportImportPost(t *testing.T) {