PLT-6581 breakup webhooks greater than 4K into multiple posts (#6530)

Этот коммит содержится в:
Corey Hulen
2017-06-02 09:08:04 -07:00
коммит произвёл Christopher Speller
родитель 1c9ec861fd
Коммит 6a54f3c489
2 изменённых файлов: 79 добавлений и 58 удалений

Просмотреть файл

@@ -1005,37 +1005,37 @@ func TestIncomingWebhooks(t *testing.T) {
} }
attachmentPayload := `{ attachmentPayload := `{
"text": "this is a test", "text": "this is a test",
"attachments": [ "attachments": [
{ {
"fallback": "Required plain-text summary of the attachment.", "fallback": "Required plain-text summary of the attachment.",
"color": "#36a64f", "color": "#36a64f",
"pretext": "Optional text that appears above the attachment block", "pretext": "Optional text that appears above the attachment block",
"author_name": "Bobby Tables", "author_name": "Bobby Tables",
"author_link": "http://flickr.com/bobby/", "author_link": "http://flickr.com/bobby/",
"author_icon": "http://flickr.com/icons/bobby.jpg", "author_icon": "http://flickr.com/icons/bobby.jpg",
"title": "Slack API Documentation", "title": "Slack API Documentation",
"title_link": "https://api.slack.com/", "title_link": "https://api.slack.com/",
"text": "Optional text that appears within the attachment", "text": "Optional text that appears within the attachment",
"fields": [ "fields": [
{ {
"title": "Priority", "title": "Priority",
"value": "High", "value": "High",
"short": false "short": false
} }
], ],
"image_url": "http://my-website.com/path/to/image.jpg", "image_url": "http://my-website.com/path/to/image.jpg",
"thumb_url": "http://example.com/path/to/thumb.png" "thumb_url": "http://example.com/path/to/thumb.png"
} }
] ]
}` }`
if _, err := Client.DoPost(url, attachmentPayload, "application/json"); err != nil { if _, err := Client.DoPost(url, attachmentPayload, "application/json"); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -1050,42 +1050,42 @@ func TestIncomingWebhooks(t *testing.T) {
tooLongText += "a" tooLongText += "a"
} }
if _, err := Client.DoPost(url, "{\"text\":\""+tooLongText+"\"}", "application/json"); err == nil || err.StatusCode != http.StatusBadRequest { if _, err := Client.DoPost(url, "{\"text\":\""+tooLongText+"\"}", "application/json"); err != nil {
t.Fatal("should have failed - text too long") t.Fatal(err)
} }
attachmentPayload = `{ attachmentPayload = `{
"text": "this is a test", "text": "this is a test",
"attachments": [ "attachments": [
{ {
"fallback": "Required plain-text summary of the attachment.", "fallback": "Required plain-text summary of the attachment.",
"color": "#36a64f", "color": "#36a64f",
"pretext": "Optional text that appears above the attachment block", "pretext": "Optional text that appears above the attachment block",
"author_name": "Bobby Tables", "author_name": "Bobby Tables",
"author_link": "http://flickr.com/bobby/", "author_link": "http://flickr.com/bobby/",
"author_icon": "http://flickr.com/icons/bobby.jpg", "author_icon": "http://flickr.com/icons/bobby.jpg",
"title": "Slack API Documentation", "title": "Slack API Documentation",
"title_link": "https://api.slack.com/", "title_link": "https://api.slack.com/",
"text": "` + tooLongText + `", "text": "` + tooLongText + `",
"fields": [ "fields": [
{ {
"title": "Priority", "title": "Priority",
"value": "High", "value": "High",
"short": false "short": false
} }
], ],
"image_url": "http://my-website.com/path/to/image.jpg", "image_url": "http://my-website.com/path/to/image.jpg",
"thumb_url": "http://example.com/path/to/thumb.png" "thumb_url": "http://example.com/path/to/thumb.png"
} }
] ]
}` }`
if _, err := Client.DoPost(url, attachmentPayload, "application/json"); err == nil || err.StatusCode != http.StatusBadRequest { if _, err := Client.DoPost(url, attachmentPayload, "application/json"); err == nil || err.StatusCode != http.StatusBadRequest {
t.Fatal("should have failed with bad request - attachment too long") t.Fatal("should have failed with bad request - attachment too long")

Просмотреть файл

@@ -150,11 +150,37 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri
} }
} }
if _, err := CreatePost(post, teamId, false); err != nil { splits := make([]string, 0)
return nil, model.NewLocAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message) remainingText := post.Message
for len(remainingText) > model.POST_MESSAGE_MAX_RUNES {
splits = append(splits, remainingText[:model.POST_MESSAGE_MAX_RUNES])
remainingText = remainingText[model.POST_MESSAGE_MAX_RUNES:]
} }
return post, nil splits = append(splits, remainingText)
var firstPost *model.Post = nil
for _, txt := range splits {
post.Id = ""
post.UpdateAt = 0
post.CreateAt = 0
post.Message = txt
if _, err := CreatePost(post, teamId, false); err != nil {
return nil, model.NewLocAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message)
}
if firstPost == nil {
if len(splits) > 1 {
firstPost = model.PostFromJson(strings.NewReader(post.ToJson()))
} else {
firstPost = post
}
}
}
return firstPost, nil
} }
func CreateIncomingWebhookForChannel(creatorId string, channel *model.Channel, hook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) { func CreateIncomingWebhookForChannel(creatorId string, channel *model.Channel, hook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) {
@@ -430,11 +456,6 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *mo
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.text.app_error", nil, "", http.StatusBadRequest) return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.text.app_error", nil, "", http.StatusBadRequest)
} }
textSize := utf8.RuneCountInString(text)
if textSize > model.POST_MESSAGE_MAX_RUNES {
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.text.length.app_error", map[string]interface{}{"Max": model.POST_MESSAGE_MAX_RUNES, "Actual": textSize}, "", http.StatusBadRequest)
}
channelName := req.ChannelName channelName := req.ChannelName
webhookType := req.Type webhookType := req.Type