Increase Channel Purpose length to 250, and add channel field length handling code to the slack importer (#4458)

* Increase Channel Purpose length to 250.

This commit increases the maxmimum length of the channel purpose field
to 250, including the database migration necessary.

It also adds a method to the Slack importer to check the lengths of
channel properties before importing, and truncate them if necessary.

Fixes #4168

* Fix database migration.
This commit is contained in:
Christopher Speller
2016-11-04 17:20:21 -04:00
committed by Harrison Healey
parent 20254073cb
commit 18745b2d5c
10 changed files with 121 additions and 16 deletions

View File

@@ -546,7 +546,7 @@ func TestUpdateChannelPurpose(t *testing.T) {
}
data["channel_id"] = channel1.Id
data["channel_purpose"] = strings.Repeat("a", 150)
data["channel_purpose"] = strings.Repeat("a", 350)
if _, err := Client.UpdateChannelPurpose(data); err == nil {
t.Fatal("should have errored on bad channel purpose")
}

View File

@@ -7,15 +7,17 @@ import (
"archive/zip"
"bytes"
"encoding/json"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"io"
"mime/multipart"
"path/filepath"
"regexp"
"strconv"
"strings"
"unicode/utf8"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
)
type SlackChannel struct {
@@ -63,6 +65,14 @@ type SlackAttachment struct {
Fields []map[string]interface{} `json:"fields"`
}
func truncateRunes(s string, i int) string {
runes := []rune(s)
if len(runes) > i {
return string(runes[:i])
}
return s
}
func SlackConvertTimeStamp(ts string) int64 {
timeString := strings.SplitN(ts, ".", 2)[0]
@@ -368,6 +378,30 @@ func addSlackUsersToChannel(members []string, users map[string]*model.User, chan
}
}
func SlackSanitiseChannelProperties(channel model.Channel) model.Channel {
if utf8.RuneCountInString(channel.DisplayName) > model.CHANNEL_DISPLAY_NAME_MAX_RUNES {
l4g.Warn("api.slackimport.slack_sanitise_channel_properties.display_name_too_long.warn", map[string]interface{}{"ChannelName": channel.DisplayName})
channel.DisplayName = truncateRunes(channel.DisplayName, model.CHANNEL_DISPLAY_NAME_MAX_RUNES)
}
if len(channel.Name) > model.CHANNEL_NAME_MAX_LENGTH {
l4g.Warn("api.slackimport.slack_sanitise_channel_properties.name_too_long.warn", map[string]interface{}{"ChannelName": channel.DisplayName})
channel.Name = channel.Name[0:model.CHANNEL_NAME_MAX_LENGTH]
}
if utf8.RuneCountInString(channel.Purpose) > model.CHANNEL_PURPOSE_MAX_RUNES {
l4g.Warn("api.slackimport.slack_sanitise_channel_properties.purpose_too_long.warn", map[string]interface{}{"ChannelName": channel.DisplayName})
channel.Purpose = truncateRunes(channel.Purpose, model.CHANNEL_PURPOSE_MAX_RUNES)
}
if utf8.RuneCountInString(channel.Header) > model.CHANNEL_HEADER_MAX_RUNES {
l4g.Warn("api.slackimport.slack_sanitise_channel_properties.header_too_long.warn", map[string]interface{}{"ChannelName": channel.DisplayName})
channel.Header = truncateRunes(channel.Header, model.CHANNEL_HEADER_MAX_RUNES)
}
return channel
}
func SlackAddChannels(teamId string, slackchannels []SlackChannel, posts map[string][]SlackPost, users map[string]*model.User, uploads map[string]*zip.File, botUser *model.User, log *bytes.Buffer) map[string]*model.Channel {
// Write Header
log.WriteString(utils.T("api.slackimport.slack_add_channels.added"))
@@ -383,6 +417,7 @@ func SlackAddChannels(teamId string, slackchannels []SlackChannel, posts map[str
Purpose: sChannel.Purpose["value"],
Header: sChannel.Topic["value"],
}
newChannel = SlackSanitiseChannelProperties(newChannel)
mChannel := ImportChannel(&newChannel)
if mChannel == nil {
// Maybe it already exists?

View File

@@ -4,7 +4,9 @@
package api
import (
"github.com/mattermost/platform/model"
"os"
"strings"
"testing"
)
@@ -177,3 +179,41 @@ func TestSlackParsePosts(t *testing.T) {
t.Fatalf("Unexpected number of posts: %v", len(posts))
}
}
func TestSlackSanitiseChannelProperties(t *testing.T) {
c1 := model.Channel{
DisplayName: "display-name",
Name: "name",
Purpose: "The channel purpose",
Header: "The channel header",
}
c1s := SlackSanitiseChannelProperties(c1)
if c1.DisplayName != c1s.DisplayName || c1.Name != c1s.Name || c1.Purpose != c1s.Purpose || c1.Header != c1s.Header {
t.Fatalf("Unexpected alterations to the channel properties.")
}
c2 := model.Channel{
DisplayName: strings.Repeat("abcdefghij", 7),
Name: strings.Repeat("abcdefghij", 7),
Purpose: strings.Repeat("0123456789", 30),
Header: strings.Repeat("0123456789", 120),
}
c2s := SlackSanitiseChannelProperties(c2)
if c2s.DisplayName != strings.Repeat("abcdefghij", 6)+"abcd" {
t.Fatalf("Unexpected alterations to the channel properties: %v", c2s.DisplayName)
}
if c2s.Name != strings.Repeat("abcdefghij", 6)+"abcd" {
t.Fatalf("Unexpected alterations to the channel properties: %v", c2s.Name)
}
if c2s.Purpose != strings.Repeat("0123456789", 25) {
t.Fatalf("Unexpected alterations to the channel properties: %v", c2s.Purpose)
}
if c2s.Header != strings.Repeat("0123456789", 102)+"0123" {
t.Fatalf("Unexpected alterations to the channel properties: %v", c2s.Header)
}
}