add support for new slack post format and multiple attachments in slack importer (#11415)

* add support for new slack post format and multiple attachments

* add test for multiple attachments in imported slack posts
This commit is contained in:
Sven Hüster 2019-07-11 11:47:51 +02:00 committed by George Goldberg
parent 859c571558
commit 5690df9d95
3 changed files with 138 additions and 11 deletions

View file

@ -62,6 +62,7 @@ type SlackPost struct {
Comment *SlackComment `json:"comment"`
Upload bool `json:"upload"`
File *SlackFile `json:"file"`
Files []*SlackFile `json:"files"`
Attachments []*model.SlackAttachment `json:"attachments"`
}
@ -250,8 +251,16 @@ func (a *App) SlackAddPosts(teamId string, channel *model.Channel, posts []Slack
CreateAt: SlackConvertTimeStamp(sPost.TimeStamp),
}
if sPost.Upload {
if fileInfo, ok := a.SlackUploadFile(sPost, uploads, teamId, newPost.ChannelId, newPost.UserId); ok {
newPost.FileIds = append(newPost.FileIds, fileInfo.Id)
if sPost.File != nil {
if fileInfo, ok := a.SlackUploadFile(sPost.File, uploads, teamId, newPost.ChannelId, newPost.UserId, sPost.TimeStamp); ok {
newPost.FileIds = append(newPost.FileIds, fileInfo.Id)
}
} else if sPost.Files != nil {
for _, file := range sPost.Files {
if fileInfo, ok := a.SlackUploadFile(file, uploads, teamId, newPost.ChannelId, newPost.UserId, sPost.TimeStamp); ok {
newPost.FileIds = append(newPost.FileIds, fileInfo.Id)
}
}
}
}
// If post in thread
@ -418,27 +427,27 @@ func (a *App) SlackAddPosts(teamId string, channel *model.Channel, posts []Slack
}
}
func (a *App) SlackUploadFile(sPost SlackPost, uploads map[string]*zip.File, teamId string, channelId string, userId string) (*model.FileInfo, bool) {
if sPost.File == nil {
func (a *App) SlackUploadFile(slackPostFile *SlackFile, uploads map[string]*zip.File, teamId string, channelId string, userId string, slackTimestamp string) (*model.FileInfo, bool) {
if slackPostFile == nil {
mlog.Warn("Slack Import: Unable to attach the file to the post as the latter has no file section present in Slack export.")
return nil, false
}
file, ok := uploads[sPost.File.Id]
file, ok := uploads[slackPostFile.Id]
if !ok {
mlog.Warn(fmt.Sprintf("Slack Import: Unable to import file %v as the file is missing from the Slack export zip file.", sPost.File.Id))
mlog.Warn(fmt.Sprintf("Slack Import: Unable to import file %v as the file is missing from the Slack export zip file.", slackPostFile.Id))
return nil, false
}
openFile, err := file.Open()
if err != nil {
mlog.Warn(fmt.Sprintf("Slack Import: Unable to open the file %v from the Slack export: %v.", sPost.File.Id, err.Error()))
mlog.Warn(fmt.Sprintf("Slack Import: Unable to open the file %v from the Slack export: %v.", slackPostFile.Id, err.Error()))
return nil, false
}
defer openFile.Close()
timestamp := utils.TimeFromMillis(SlackConvertTimeStamp(sPost.TimeStamp))
timestamp := utils.TimeFromMillis(SlackConvertTimeStamp(slackTimestamp))
uploadedFile, err := a.OldImportFile(timestamp, openFile, teamId, channelId, userId, filepath.Base(file.Name))
if err != nil {
mlog.Warn(fmt.Sprintf("Slack Import: An error occurred when uploading file %v: %v.", sPost.File.Id, err.Error()))
mlog.Warn(fmt.Sprintf("Slack Import: An error occurred when uploading file %v: %v.", slackPostFile.Id, err.Error()))
return nil, false
}

View file

@ -168,7 +168,17 @@ func TestSlackParsePosts(t *testing.T) {
posts, err := SlackParsePosts(file)
require.NoError(t, err)
assert.Equal(t, 8, len(posts))
assert.Equal(t, 9, len(posts))
}
func TestSlackParseMultipleAttachments(t *testing.T) {
file, err := os.Open("tests/slack-import-test-posts.json")
require.NoError(t, err)
defer file.Close()
posts, err := SlackParsePosts(file)
require.NoError(t, err)
assert.Equal(t, 2, len(posts[8].Files))
}
func TestSlackSanitiseChannelProperties(t *testing.T) {

View file

@ -176,5 +176,113 @@
"username": "<@U07Q4MHCP|lindy>",
"bot_id": null,
"ts": "1469782077.000010"
},
{
"type": "message",
"text": "multiple_attachments",
"files": [
{
"id": "FL9GSN60L",
"created": 1562758810,
"timestamp": 1562758810,
"name": "Screenshot from 2019-07-09 17-08-24.png",
"title": "Screenshot from 2019-07-09 17-08-24.png",
"mimetype": "image\/png",
"filetype": "png",
"pretty_type": "PNG",
"user": "UKW2XKH7E",
"editable": false,
"size": 79513,
"mode": "hosted",
"is_external": false,
"external_type": "",
"is_public": true,
"public_url_shared": false,
"display_as_bot": false,
"username": "",
"url_private": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL9GSN60L\/screenshot_from_2019-07-09_17-08-24.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"url_private_download": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL9GSN60L\/download\/screenshot_from_2019-07-09_17-08-24.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_64": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_64.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_80": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_80.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_360": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_360.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_360_w": 197,
"thumb_360_h": 360,
"thumb_480": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_480.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_480_w": 263,
"thumb_480_h": 480,
"thumb_160": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_160.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_720": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_720.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_720_w": 395,
"thumb_720_h": 720,
"thumb_800": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_800.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_800_w": 439,
"thumb_800_h": 800,
"thumb_960": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_960.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_960_w": 526,
"thumb_960_h": 960,
"thumb_1024": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL9GSN60L-e70b98a77f\/screenshot_from_2019-07-09_17-08-24_1024.png?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_1024_w": 561,
"thumb_1024_h": 1024,
"image_exif_rotation": 1,
"original_w": 632,
"original_h": 1153,
"permalink": "https:\/\/svenstestteam.slack.com\/files\/UKW2XKH7E\/FL9GSN60L\/screenshot_from_2019-07-09_17-08-24.png",
"permalink_public": "https:\/\/slack-files.com\/TKK4L637U-FL9GSN60L-19fdfd655a",
"has_rich_preview": false
},
{
"id": "FL3LBGRT3",
"created": 1562758810,
"timestamp": 1562758810,
"name": "vscode.jpg",
"title": "vscode.jpg",
"mimetype": "image\/jpeg",
"filetype": "jpg",
"pretty_type": "JPEG",
"user": "UKW2XKH7E",
"editable": false,
"size": 549439,
"mode": "hosted",
"is_external": false,
"external_type": "",
"is_public": true,
"public_url_shared": false,
"display_as_bot": false,
"username": "",
"url_private": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL3LBGRT3\/vscode.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"url_private_download": "https:\/\/files.slack.com\/files-pri\/TKK4L637U-FL3LBGRT3\/download\/vscode.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_64": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_64.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_80": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_80.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_360": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_360.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_360_w": 360,
"thumb_360_h": 203,
"thumb_480": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_480.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_480_w": 480,
"thumb_480_h": 270,
"thumb_160": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_160.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_720": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_720.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_720_w": 720,
"thumb_720_h": 405,
"thumb_800": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_800.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_800_w": 800,
"thumb_800_h": 450,
"thumb_960": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_960.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_960_w": 960,
"thumb_960_h": 540,
"thumb_1024": "https:\/\/files.slack.com\/files-tmb\/TKK4L637U-FL3LBGRT3-ece28edc37\/vscode_1024.jpg?t=xoxe-665156207266-683692183393-689571362948-acb8000145d9929cba3d5c38249a6673",
"thumb_1024_w": 1024,
"thumb_1024_h": 576,
"image_exif_rotation": 1,
"original_w": 1920,
"original_h": 1080,
"permalink": "https:\/\/svenstestteam.slack.com\/files\/UKW2XKH7E\/FL3LBGRT3\/vscode.jpg",
"permalink_public": "https:\/\/slack-files.com\/TKK4L637U-FL3LBGRT3-ffd5da7911",
"has_rich_preview": false
}
],
"upload": true,
"user": "UKW2XKH7E",
"ts": "1562758814.000100",
"client_msg_id": "bf2a1192-6b57-4dd3-90f9-8e833fcad758"
}
]
]