summaryrefslogtreecommitdiffstats
path: root/app/import.go
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-01-20 14:47:14 +0000
committerChristopher Speller <crspeller@gmail.com>2017-01-20 09:47:14 -0500
commit2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8 (patch)
tree47dd540e415b75c0a61b8beeb1e7f6856cc8b361 /app/import.go
parent6097f93704862215791ce8855c31471e40ef0af1 (diff)
downloadchat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.tar.gz
chat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.tar.bz2
chat-2de6c5394ec3a1cd974aae46c41f61fb0e9f9bd8.zip
Move Slack Import to App Layer. (#5135)
Diffstat (limited to 'app/import.go')
-rw-r--r--app/import.go157
1 files changed, 157 insertions, 0 deletions
diff --git a/app/import.go b/app/import.go
new file mode 100644
index 000000000..8f2cf552e
--- /dev/null
+++ b/app/import.go
@@ -0,0 +1,157 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package app
+
+import (
+ "bytes"
+ "io"
+ "regexp"
+ "unicode/utf8"
+
+ l4g "github.com/alecthomas/log4go"
+ "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 := <-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 := <-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 := <-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 := <-Srv.Store.User().VerifyEmail(ruser.Id); cresult.Err != nil {
+ l4g.Error(utils.T("api.import.import_user.set_email.error"), cresult.Err)
+ }
+
+ if err := 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 := <-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)
+}