From 275188ad040864823764deb8a71c1e85c9ef00b2 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Fri, 2 Dec 2016 13:13:15 +0000 Subject: [PATCH] PLT-4839 Split too-long Slack messages on import. (#4679) * PLT-4839 Split too-long Slack messages on import. This PR also takes the opportunity to make the max values for Post properties into constants for easier use elsewhere, as has previously been done for Channel properties. * Only count runes once. --- api/import.go | 21 ++++++++++++++++++--- model/post.go | 15 ++++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/api/import.go b/api/import.go index 5704444641..63dfb033d8 100644 --- a/api/import.go +++ b/api/import.go @@ -7,6 +7,7 @@ import ( "bytes" "io" "regexp" + "unicode/utf8" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" @@ -19,10 +20,24 @@ import ( // func ImportPost(post *model.Post) { - post.Hashtags, _ = model.ParseHashtags(post.Message) + for messageRuneCount := utf8.RuneCountInString(post.Message); messageRuneCount > 0; messageRuneCount = utf8.RuneCountInString(post.Message) { + 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 = "" + } - if result := <-Srv.Store.Post().Save(post); result.Err != nil { - l4g.Debug(utils.T("api.import.import_post.saving.debug"), post.UserId, post.Message) + 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) + } + + post.Id = "" + post.CreateAt++ + post.Message = remainder } } diff --git a/model/post.go b/model/post.go index b5dcc45397..cffe8e3429 100644 --- a/model/post.go +++ b/model/post.go @@ -19,6 +19,11 @@ const ( POST_HEADER_CHANGE = "system_header_change" POST_CHANNEL_DELETED = "system_channel_deleted" POST_EPHEMERAL = "system_ephemeral" + POST_FILEIDS_MAX_RUNES = 150 + POST_FILENAMES_MAX_RUNES = 4000 + POST_HASHTAGS_MAX_RUNES = 1000 + POST_MESSAGE_MAX_RUNES = 4000 + POST_PROPS_MAX_RUNES = 8000 ) type Post struct { @@ -103,11 +108,11 @@ func (o *Post) IsValid() *AppError { return NewLocAppError("Post.IsValid", "model.post.is_valid.original_id.app_error", nil, "") } - if utf8.RuneCountInString(o.Message) > 4000 { + if utf8.RuneCountInString(o.Message) > POST_MESSAGE_MAX_RUNES { return NewLocAppError("Post.IsValid", "model.post.is_valid.msg.app_error", nil, "id="+o.Id) } - if utf8.RuneCountInString(o.Hashtags) > 1000 { + if utf8.RuneCountInString(o.Hashtags) > POST_HASHTAGS_MAX_RUNES { return NewLocAppError("Post.IsValid", "model.post.is_valid.hashtags.app_error", nil, "id="+o.Id) } @@ -116,15 +121,15 @@ func (o *Post) IsValid() *AppError { return NewLocAppError("Post.IsValid", "model.post.is_valid.type.app_error", nil, "id="+o.Type) } - if utf8.RuneCountInString(ArrayToJson(o.Filenames)) > 4000 { + if utf8.RuneCountInString(ArrayToJson(o.Filenames)) > POST_FILENAMES_MAX_RUNES { return NewLocAppError("Post.IsValid", "model.post.is_valid.filenames.app_error", nil, "id="+o.Id) } - if utf8.RuneCountInString(ArrayToJson(o.FileIds)) > 150 { + if utf8.RuneCountInString(ArrayToJson(o.FileIds)) > POST_FILEIDS_MAX_RUNES { return NewLocAppError("Post.IsValid", "model.post.is_valid.file_ids.app_error", nil, "id="+o.Id) } - if utf8.RuneCountInString(StringInterfaceToJson(o.Props)) > 8000 { + if utf8.RuneCountInString(StringInterfaceToJson(o.Props)) > POST_PROPS_MAX_RUNES { return NewLocAppError("Post.IsValid", "model.post.is_valid.props.app_error", nil, "id="+o.Id) }