diff options
author | George Goldberg <george@gberg.me> | 2017-01-20 14:47:14 +0000 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2017-01-20 09:47:14 -0500 |
commit | 2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8 (patch) | |
tree | 47dd540e415b75c0a61b8beeb1e7f6856cc8b361 /api/import.go | |
parent | 6097f93704862215791ce8855c31471e40ef0af1 (diff) | |
download | chat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.tar.gz chat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.tar.bz2 chat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.zip |
Move Slack Import to App Layer. (#5135)
Diffstat (limited to 'api/import.go')
-rw-r--r-- | api/import.go | 158 |
1 files changed, 0 insertions, 158 deletions
diff --git a/api/import.go b/api/import.go deleted file mode 100644 index b93a061fe..000000000 --- a/api/import.go +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -package api - -import ( - "bytes" - "io" - "regexp" - "unicode/utf8" - - l4g "github.com/alecthomas/log4go" - "github.com/mattermost/platform/app" - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/utils" -) - -// -// Import functions are sutible for entering posts and users into the database without -// some of the usual checks. (IsValid is still run) -// - -func ImportPost(post *model.Post) { - // Workaround for empty messages, which may be the case if they are webhook posts. - firstIteration := true - for messageRuneCount := utf8.RuneCountInString(post.Message); messageRuneCount > 0 || firstIteration; messageRuneCount = utf8.RuneCountInString(post.Message) { - firstIteration = false - var remainder string - if messageRuneCount > model.POST_MESSAGE_MAX_RUNES { - remainder = string(([]rune(post.Message))[model.POST_MESSAGE_MAX_RUNES:]) - post.Message = truncateRunes(post.Message, model.POST_MESSAGE_MAX_RUNES) - } else { - remainder = "" - } - - post.Hashtags, _ = model.ParseHashtags(post.Message) - - if result := <-app.Srv.Store.Post().Save(post); result.Err != nil { - l4g.Debug(utils.T("api.import.import_post.saving.debug"), post.UserId, post.Message) - } - - for _, fileId := range post.FileIds { - if result := <-app.Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil { - l4g.Error(utils.T("api.import.import_post.attach_files.error"), post.Id, post.FileIds, result.Err) - } - } - - post.Id = "" - post.CreateAt++ - post.Message = remainder - } -} - -func ImportUser(team *model.Team, user *model.User) *model.User { - user.MakeNonNil() - - user.Roles = model.ROLE_SYSTEM_USER.Id - - if result := <-app.Srv.Store.User().Save(user); result.Err != nil { - l4g.Error(utils.T("api.import.import_user.saving.error"), result.Err) - return nil - } else { - ruser := result.Data.(*model.User) - - if cresult := <-app.Srv.Store.User().VerifyEmail(ruser.Id); cresult.Err != nil { - l4g.Error(utils.T("api.import.import_user.set_email.error"), cresult.Err) - } - - if err := app.JoinUserToTeam(team, user); err != nil { - l4g.Error(utils.T("api.import.import_user.join_team.error"), err) - } - - return ruser - } -} - -func ImportChannel(channel *model.Channel) *model.Channel { - if result := <-app.Srv.Store.Channel().Save(channel); result.Err != nil { - return nil - } else { - sc := result.Data.(*model.Channel) - - return sc - } -} - -func ImportFile(file io.Reader, teamId string, channelId string, userId string, fileName string) (*model.FileInfo, error) { - buf := bytes.NewBuffer(nil) - io.Copy(buf, file) - data := buf.Bytes() - - fileInfo, err := doUploadFile(teamId, channelId, userId, fileName, data) - if err != nil { - return nil, err - } - - img, width, height := prepareImage(data) - if img != nil { - generateThumbnailImage(*img, fileInfo.ThumbnailPath, width, height) - generatePreviewImage(*img, fileInfo.PreviewPath, width) - } - - return fileInfo, nil -} - -func ImportIncomingWebhookPost(post *model.Post, props model.StringInterface) { - linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) - post.Message = linkWithTextRegex.ReplaceAllString(post.Message, "[${2}](${1})") - - post.AddProp("from_webhook", "true") - - if _, ok := props["override_username"]; !ok { - post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME) - } - - if len(props) > 0 { - for key, val := range props { - if key == "attachments" { - if list, success := val.([]interface{}); success { - // parse attachment links into Markdown format - for i, aInt := range list { - attachment := aInt.(map[string]interface{}) - if aText, ok := attachment["text"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["text"] = aText - list[i] = attachment - } - if aText, ok := attachment["pretext"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["pretext"] = aText - list[i] = attachment - } - if fVal, ok := attachment["fields"]; ok { - if fields, ok := fVal.([]interface{}); ok { - // parse attachment field links into Markdown format - for j, fInt := range fields { - field := fInt.(map[string]interface{}) - if fValue, ok := field["value"].(string); ok { - fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") - field["value"] = fValue - fields[j] = field - } - } - attachment["fields"] = fields - list[i] = attachment - } - } - } - post.AddProp(key, list) - } - } else if key != "from_webhook" { - post.AddProp(key, val) - } - } - } - - ImportPost(post) -} |