From cc6c385d3e8903546fc6fc856bf468ad09b70913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20H=C3=BCster?= Date: Tue, 4 Jun 2019 23:11:29 +0200 Subject: [PATCH] Add Slack corporate import ability (#10905) * add support for slack corporate export data * changed tempChannels for channel types. concat after parsing channels * fix typo * add fixes suggested by @lieut-data --- app/slackimport.go | 29 ++++++-- app/slackimport_test.go | 32 ++++++++- tests/slack-import-test-direct-messages.json | 34 +++++++++ ...ack-import-test-group-direct-messages.json | 69 +++++++++++++++++++ tests/slack-import-test-private-channels.json | 25 +++++++ 5 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 tests/slack-import-test-direct-messages.json create mode 100644 tests/slack-import-test-group-direct-messages.json create mode 100644 tests/slack-import-test-private-channels.json diff --git a/app/slackimport.go b/app/slackimport.go index 63baed4515..7450f953ae 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -30,6 +30,7 @@ type SlackChannel struct { Members []string `json:"members"` Topic map[string]string `json:"topic"` Purpose map[string]string `json:"purpose"` + Type string } type SlackProfile struct { @@ -102,7 +103,7 @@ func SlackConvertChannelName(channelName string, channelId string) string { return strings.ToLower(channelId) } -func SlackParseChannels(data io.Reader) ([]SlackChannel, error) { +func SlackParseChannels(data io.Reader, channelType string) ([]SlackChannel, error) { decoder := json.NewDecoder(data) var channels []SlackChannel @@ -110,6 +111,11 @@ func SlackParseChannels(data io.Reader) ([]SlackChannel, error) { mlog.Warn("Slack Import: Error occurred when parsing some Slack channels. Import may work anyway.") return channels, err } + + for i := range channels { + channels[i].Type = channelType + } + return channels, nil } @@ -492,7 +498,7 @@ func (a *App) SlackAddChannels(teamId string, slackchannels []SlackChannel, post for _, sChannel := range slackchannels { newChannel := model.Channel{ TeamId: teamId, - Type: model.CHANNEL_OPEN, + Type: sChannel.Type, DisplayName: sChannel.Name, Name: SlackConvertChannelName(sChannel.Name, sChannel.Id), Purpose: sChannel.Purpose["value"], @@ -656,6 +662,11 @@ func (a *App) SlackImport(fileData multipart.File, fileSize int64, teamID string } var channels []SlackChannel + var publicChannels []SlackChannel + var privateChannels []SlackChannel + var groupChannels []SlackChannel + var directChannels []SlackChannel + var users []SlackUser posts := make(map[string][]SlackPost) uploads := make(map[string]*zip.File) @@ -666,7 +677,17 @@ func (a *App) SlackImport(fileData multipart.File, fileSize int64, teamID string return model.NewAppError("SlackImport", "api.slackimport.slack_import.open.app_error", map[string]interface{}{"Filename": file.Name}, err.Error(), http.StatusInternalServerError), log } if file.Name == "channels.json" { - channels, _ = SlackParseChannels(reader) + publicChannels, _ = SlackParseChannels(reader, model.CHANNEL_OPEN) + channels = append(channels, publicChannels...) + } else if file.Name == "dms.json" { + directChannels, _ = SlackParseChannels(reader, model.CHANNEL_DIRECT) + channels = append(channels, directChannels...) + } else if file.Name == "groups.json" { + privateChannels, _ = SlackParseChannels(reader, model.CHANNEL_PRIVATE) + channels = append(channels, privateChannels...) + } else if file.Name == "mpims.json" { + groupChannels, _ = SlackParseChannels(reader, model.CHANNEL_GROUP) + channels = append(channels, groupChannels...) } else if file.Name == "users.json" { users, _ = SlackParseUsers(reader) } else { @@ -712,7 +733,7 @@ func (a *App) SlackImport(fileData multipart.File, fileSize int64, teamID string // // -- Old SlackImport Functions -- -// Import functions are sutible for entering posts and users into the database without +// Import functions are suitable for entering posts and users into the database without // some of the usual checks. (IsValid is still run) // diff --git a/app/slackimport_test.go b/app/slackimport_test.go index 80b33b95e3..66662bfa27 100644 --- a/app/slackimport_test.go +++ b/app/slackimport_test.go @@ -116,11 +116,41 @@ func TestSlackParseChannels(t *testing.T) { require.NoError(t, err) defer file.Close() - channels, err := SlackParseChannels(file) + channels, err := SlackParseChannels(file, "O") require.NoError(t, err) assert.Equal(t, 6, len(channels)) } +func TestSlackParseDirectMessages(t *testing.T) { + file, err := os.Open("tests/slack-import-test-direct-messages.json") + require.NoError(t, err) + defer file.Close() + + channels, err := SlackParseChannels(file, "D") + require.NoError(t, err) + assert.Equal(t, 4, len(channels)) +} + +func TestSlackParsePrivateChannels(t *testing.T) { + file, err := os.Open("tests/slack-import-test-private-channels.json") + require.NoError(t, err) + defer file.Close() + + channels, err := SlackParseChannels(file, "P") + require.NoError(t, err) + assert.Equal(t, 1, len(channels)) +} + +func TestSlackParseGroupDirectMessages(t *testing.T) { + file, err := os.Open("tests/slack-import-test-group-direct-messages.json") + require.NoError(t, err) + defer file.Close() + + channels, err := SlackParseChannels(file, "G") + require.NoError(t, err) + assert.Equal(t, 3, len(channels)) +} + func TestSlackParseUsers(t *testing.T) { file, err := os.Open("tests/slack-import-test-users.json") require.NoError(t, err) diff --git a/tests/slack-import-test-direct-messages.json b/tests/slack-import-test-direct-messages.json new file mode 100644 index 0000000000..c13fe1b336 --- /dev/null +++ b/tests/slack-import-test-direct-messages.json @@ -0,0 +1,34 @@ +[ + { + "id": "D24TJMP61", + "created": "1472131245", + "members": [ + "U1VVY70HK", + "U1W4XLR62" + ] + }, + { + "id": "D24TJNXUG", + "created": "1472132335", + "members": [ + "U1XG9JUH1", + "U20APGU90" + ] + }, + { + "id": "D24TLE5V5", + "created": "1472131113", + "members": [ + "U1TQ3A3L7", + "U1URR19QV" + ] + }, + { + "id": "D24TM4MPT", + "created": "1472131021", + "members": [ + "U1V47GAM9", + "U1XVNP6DT" + ] + } +] \ No newline at end of file diff --git a/tests/slack-import-test-group-direct-messages.json b/tests/slack-import-test-group-direct-messages.json new file mode 100644 index 0000000000..4e00c78c8a --- /dev/null +++ b/tests/slack-import-test-group-direct-messages.json @@ -0,0 +1,69 @@ +[ + { + "id": "G1XSSNBTP", + "name": "mpdm-mcauly--sutton--barker--lankford-1", + "created": 1470239933, + "creator": "U1FFMLSKC", + "is_archived": false, + "members": [ + "U1934896U", + "U1TTSPAGP", + "U1FFMLSKC", + "U1U234D4P" + ], + "topic": { + "value": "Group messaging", + "creator": "U1FFMLSKC", + "last_set": "1470239933" + }, + "purpose": { + "value": "Group messaging with: @mcauly @sutton @barker @lankford", + "creator": "U1FFMLSKC", + "last_set": "1470239933" + } + }, + { + "id": "G22KV02G2", + "name": "mpdm-mcauly--wolfgram--weddington-1", + "created": 1471532476, + "creator": "U1K9SEY9K", + "is_archived": false, + "members": [ + "U1934896U", + "U1K9SEY9K", + "U1YTGR5CP" + ], + "topic": { + "value": "Group messaging", + "creator": "U1K9SEY9K", + "last_set": "1471532476" + }, + "purpose": { + "value": "Group messaging with: @mcauly @wolfgram @weddington", + "creator": "U1K9SEY9K", + "last_set": "1471532476" + } + }, + { + "id": "G2436812A", + "name": "mpdm-mcauly--lovell--buck-1", + "created": 1471964085, + "creator": "U1934896U", + "is_archived": false, + "members": [ + "U1934896U", + "U1YYZNDJP", + "U1JSZSPR7" + ], + "topic": { + "value": "Group messaging", + "creator": "U1934896U", + "last_set": "1471964085" + }, + "purpose": { + "value": "Group messaging with: @mcauly @lovell @buck", + "creator": "U1934896U", + "last_set": "1471964085" + } + } +] \ No newline at end of file diff --git a/tests/slack-import-test-private-channels.json b/tests/slack-import-test-private-channels.json new file mode 100644 index 0000000000..1e5adf991d --- /dev/null +++ b/tests/slack-import-test-private-channels.json @@ -0,0 +1,25 @@ +[ + { + "id": "G1T7B1T9N", + "name": "csi-team-chat", + "created": 1468959839, + "creator": "U17G57PB6", + "is_archived": false, + "members": [ + "U15CML9C1", + "U1934896U", + "U17G57PB6", + "U1T7A2J56" + ], + "topic": { + "value": "\"A constantly evolving landscape of hills\"", + "creator": "U1934896U", + "last_set": "1469017133" + }, + "purpose": { + "value": "CSI team chat", + "creator": "U17G57PB6", + "last_set": "1468972624" + } + } +] \ No newline at end of file