2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-07-07 09:16:13 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"archive/zip"
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2016-01-20 13:36:16 -06:00
|
|
|
l4g "github.com/alecthomas/log4go"
|
|
|
|
|
"github.com/mattermost/platform/model"
|
2016-01-22 01:37:11 -03:00
|
|
|
"github.com/mattermost/platform/utils"
|
2015-07-07 09:16:13 -04:00
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SlackChannel struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Members []string `json:"members"`
|
|
|
|
|
Topic map[string]string `json:"topic"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SlackUser struct {
|
|
|
|
|
Id string `json:"id"`
|
2015-08-18 11:53:18 -04:00
|
|
|
Username string `json:"name"`
|
2015-07-07 09:16:13 -04:00
|
|
|
Profile map[string]string `json:"profile"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SlackPost struct {
|
|
|
|
|
User string `json:"user"`
|
|
|
|
|
BotId string `json:"bot_id"`
|
|
|
|
|
BotUsername string `json:"username"`
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
TimeStamp string `json:"ts"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
SubType string `json:"subtype"`
|
|
|
|
|
Comment map[string]string `json:"comment"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SlackConvertTimeStamp(ts string) int64 {
|
|
|
|
|
timeString := strings.SplitN(ts, ".", 2)[0]
|
|
|
|
|
|
|
|
|
|
timeStamp, err := strconv.ParseInt(timeString, 10, 64)
|
|
|
|
|
if err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Warn(utils.T("api.slackimport.slack_convert_timestamp.bad.warn"))
|
2015-07-07 09:16:13 -04:00
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return timeStamp * 1000 // Convert to milliseconds
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-14 12:04:57 -04:00
|
|
|
func SlackConvertChannelName(channelName string) string {
|
|
|
|
|
newName := strings.Trim(channelName, "_-")
|
|
|
|
|
if len(newName) == 1 {
|
|
|
|
|
return "slack-channel-" + newName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newName
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 09:16:13 -04:00
|
|
|
func SlackParseChannels(data io.Reader) []SlackChannel {
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
|
|
|
|
|
|
var channels []SlackChannel
|
|
|
|
|
if err := decoder.Decode(&channels); err != nil {
|
|
|
|
|
return make([]SlackChannel, 0)
|
|
|
|
|
}
|
|
|
|
|
return channels
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SlackParseUsers(data io.Reader) []SlackUser {
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
|
|
|
|
|
|
var users []SlackUser
|
|
|
|
|
if err := decoder.Decode(&users); err != nil {
|
2016-02-08 09:41:56 -05:00
|
|
|
// This actually returns errors that are ignored.
|
|
|
|
|
// In this case it is erroring because of a null that Slack
|
|
|
|
|
// introduced. So we just return the users here.
|
|
|
|
|
return users
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
return users
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SlackParsePosts(data io.Reader) []SlackPost {
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
|
|
|
|
|
|
var posts []SlackPost
|
|
|
|
|
if err := decoder.Decode(&posts); err != nil {
|
|
|
|
|
return make([]SlackPost, 0)
|
|
|
|
|
}
|
|
|
|
|
return posts
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
func SlackAddUsers(teamId string, slackusers []SlackUser, log *bytes.Buffer) map[string]*model.User {
|
2015-07-07 09:16:13 -04:00
|
|
|
// Log header
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_users.created"))
|
2015-08-26 15:24:17 -04:00
|
|
|
log.WriteString("===============\r\n\r\n")
|
2015-07-07 09:16:13 -04:00
|
|
|
|
|
|
|
|
addedUsers := make(map[string]*model.User)
|
|
|
|
|
for _, sUser := range slackusers {
|
|
|
|
|
firstName := ""
|
|
|
|
|
lastName := ""
|
|
|
|
|
if name, ok := sUser.Profile["first_name"]; ok {
|
|
|
|
|
firstName = name
|
|
|
|
|
}
|
|
|
|
|
if name, ok := sUser.Profile["last_name"]; ok {
|
|
|
|
|
lastName = name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
password := model.NewId()
|
|
|
|
|
|
|
|
|
|
newUser := model.User{
|
2015-08-18 11:53:18 -04:00
|
|
|
Username: sUser.Username,
|
2015-07-07 09:16:13 -04:00
|
|
|
FirstName: firstName,
|
|
|
|
|
LastName: lastName,
|
|
|
|
|
Email: sUser.Profile["email"],
|
|
|
|
|
Password: password,
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
if mUser := ImportUser(teamId, &newUser); mUser != nil {
|
2015-07-07 09:16:13 -04:00
|
|
|
addedUsers[sUser.Id] = mUser
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_users.email_pwd", map[string]interface{}{"Email": newUser.Email, "Password": password}))
|
2015-07-07 09:16:13 -04:00
|
|
|
} else {
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_users.unable_import", map[string]interface{}{"Username": sUser.Username}))
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return addedUsers
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
func SlackAddPosts(channel *model.Channel, posts []SlackPost, users map[string]*model.User) {
|
2015-07-07 09:16:13 -04:00
|
|
|
for _, sPost := range posts {
|
|
|
|
|
switch {
|
|
|
|
|
case sPost.Type == "message" && (sPost.SubType == "" || sPost.SubType == "file_share"):
|
|
|
|
|
if sPost.User == "" {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.slackimport.slack_add_posts.without_user.debug"))
|
2015-07-07 09:16:13 -04:00
|
|
|
continue
|
|
|
|
|
} else if users[sPost.User] == nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.slackimport.slack_add_posts.user_no_exists.debug"), sPost.User)
|
2015-07-07 09:16:13 -04:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
newPost := model.Post{
|
|
|
|
|
UserId: users[sPost.User].Id,
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
Message: sPost.Text,
|
|
|
|
|
CreateAt: SlackConvertTimeStamp(sPost.TimeStamp),
|
|
|
|
|
}
|
2016-01-20 13:36:16 -06:00
|
|
|
ImportPost(&newPost)
|
2015-07-07 09:16:13 -04:00
|
|
|
case sPost.Type == "message" && sPost.SubType == "file_comment":
|
|
|
|
|
if sPost.Comment["user"] == "" {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.slackimport.slack_add_posts.msg_no_usr.debug"))
|
2015-07-07 09:16:13 -04:00
|
|
|
continue
|
|
|
|
|
} else if users[sPost.Comment["user"]] == nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.slackimport.slack_add_posts.user_no_exists.debug"), sPost.User)
|
2015-07-07 09:16:13 -04:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
newPost := model.Post{
|
|
|
|
|
UserId: users[sPost.Comment["user"]].Id,
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
Message: sPost.Comment["comment"],
|
|
|
|
|
CreateAt: SlackConvertTimeStamp(sPost.TimeStamp),
|
|
|
|
|
}
|
2016-01-20 13:36:16 -06:00
|
|
|
ImportPost(&newPost)
|
2015-07-07 09:16:13 -04:00
|
|
|
case sPost.Type == "message" && sPost.SubType == "bot_message":
|
|
|
|
|
// In the future this will use the "Action Post" spec to post
|
|
|
|
|
// a message without using a username. For now we just warn that we don't handle this case
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Warn(utils.T("api.slackimport.slack_add_posts.bot.warn"))
|
2015-07-07 09:16:13 -04:00
|
|
|
default:
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Warn(utils.T("api.slackimport.slack_add_posts.unsupported.warn"), sPost.Type, sPost.SubType)
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
func SlackAddChannels(teamId string, slackchannels []SlackChannel, posts map[string][]SlackPost, users map[string]*model.User, log *bytes.Buffer) map[string]*model.Channel {
|
2015-07-07 09:16:13 -04:00
|
|
|
// Write Header
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_channels.added"))
|
2015-08-26 15:24:17 -04:00
|
|
|
log.WriteString("=================\r\n\r\n")
|
2015-07-07 09:16:13 -04:00
|
|
|
|
|
|
|
|
addedChannels := make(map[string]*model.Channel)
|
|
|
|
|
for _, sChannel := range slackchannels {
|
|
|
|
|
newChannel := model.Channel{
|
|
|
|
|
TeamId: teamId,
|
|
|
|
|
Type: model.CHANNEL_OPEN,
|
|
|
|
|
DisplayName: sChannel.Name,
|
2015-09-14 12:04:57 -04:00
|
|
|
Name: SlackConvertChannelName(sChannel.Name),
|
2015-10-27 11:04:23 -04:00
|
|
|
Purpose: sChannel.Topic["value"],
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
2016-01-20 13:36:16 -06:00
|
|
|
mChannel := ImportChannel(&newChannel)
|
2015-07-07 09:16:13 -04:00
|
|
|
if mChannel == nil {
|
|
|
|
|
// Maybe it already exists?
|
2016-01-20 13:36:16 -06:00
|
|
|
if result := <-Srv.Store.Channel().GetByName(teamId, sChannel.Name); result.Err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.slackimport.slack_add_channels.import_failed.debug"), newChannel.DisplayName)
|
|
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_channels.import_failed", map[string]interface{}{"DisplayName": newChannel.DisplayName}))
|
2015-07-07 09:16:13 -04:00
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
mChannel = result.Data.(*model.Channel)
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_add_channels.merge", map[string]interface{}{"DisplayName": newChannel.DisplayName}))
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-26 15:24:17 -04:00
|
|
|
log.WriteString(newChannel.DisplayName + "\r\n")
|
2015-07-07 09:16:13 -04:00
|
|
|
addedChannels[sChannel.Id] = mChannel
|
2016-01-20 13:36:16 -06:00
|
|
|
SlackAddPosts(mChannel, posts[sChannel.Name], users)
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return addedChannels
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
func SlackImport(fileData multipart.File, fileSize int64, teamID string) (*model.AppError, *bytes.Buffer) {
|
2015-07-07 09:16:13 -04:00
|
|
|
zipreader, err := zip.NewReader(fileData, fileSize)
|
|
|
|
|
if err != nil || zipreader.File == nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
return model.NewLocAppError("SlackImport", "api.slackimport.slack_import.zip.app_error", nil, err.Error()), nil
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create log file
|
2016-01-22 01:37:11 -03:00
|
|
|
log := bytes.NewBufferString(utils.T("api.slackimport.slack_import.log"))
|
2015-07-07 09:16:13 -04:00
|
|
|
|
|
|
|
|
var channels []SlackChannel
|
|
|
|
|
var users []SlackUser
|
|
|
|
|
posts := make(map[string][]SlackPost)
|
|
|
|
|
for _, file := range zipreader.File {
|
|
|
|
|
reader, err := file.Open()
|
|
|
|
|
if err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
return model.NewLocAppError("SlackImport", "api.slackimport.slack_import.open.app_error", map[string]interface{}{"Filename": file.Name}, err.Error()), log
|
2015-07-07 09:16:13 -04:00
|
|
|
}
|
|
|
|
|
if file.Name == "channels.json" {
|
|
|
|
|
channels = SlackParseChannels(reader)
|
|
|
|
|
} else if file.Name == "users.json" {
|
|
|
|
|
users = SlackParseUsers(reader)
|
|
|
|
|
} else {
|
|
|
|
|
spl := strings.Split(file.Name, "/")
|
|
|
|
|
if len(spl) == 2 && strings.HasSuffix(spl[1], ".json") {
|
|
|
|
|
newposts := SlackParsePosts(reader)
|
|
|
|
|
channel := spl[0]
|
|
|
|
|
if _, ok := posts[channel]; ok == false {
|
|
|
|
|
posts[channel] = newposts
|
|
|
|
|
} else {
|
|
|
|
|
posts[channel] = append(posts[channel], newposts...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
addedUsers := SlackAddUsers(teamID, users, log)
|
|
|
|
|
SlackAddChannels(teamID, channels, posts, addedUsers, log)
|
2015-07-07 09:16:13 -04:00
|
|
|
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_import.notes"))
|
2015-08-26 15:24:17 -04:00
|
|
|
log.WriteString("=======\r\n\r\n")
|
2015-07-07 09:16:13 -04:00
|
|
|
|
2016-01-22 01:37:11 -03:00
|
|
|
log.WriteString(utils.T("api.slackimport.slack_import.note1"))
|
|
|
|
|
log.WriteString(utils.T("api.slackimport.slack_import.note2"))
|
2015-07-07 09:16:13 -04:00
|
|
|
|
|
|
|
|
return nil, log
|
|
|
|
|
}
|