2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2017-01-13 13:53:37 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2017-01-13 15:17:50 -05:00
|
|
|
"regexp"
|
2017-01-13 13:53:37 -05:00
|
|
|
"strings"
|
2017-02-28 04:31:53 -05:00
|
|
|
"unicode/utf8"
|
2017-01-13 13:53:37 -05:00
|
|
|
|
2018-04-27 12:49:45 -07:00
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/store"
|
|
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2017-01-13 13:53:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2017-07-19 03:43:31 +08:00
|
|
|
TRIGGERWORDS_EXACT_MATCH = 0
|
|
|
|
|
TRIGGERWORDS_STARTS_WITH = 1
|
2019-01-09 17:07:08 -05:00
|
|
|
|
|
|
|
|
MaxIntegrationResponseSize = 1024 * 1024 // Posts can be <100KB at most, so this is likely more than enough
|
2017-01-13 13:53:37 -05:00
|
|
|
)
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User) *model.AppError {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-01-13 13:53:37 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.Type != model.CHANNEL_OPEN {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 12:20:36 -07:00
|
|
|
hooks, err := a.Srv.Store.Webhook().GetOutgoingByTeam(team.Id, -1, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(hooks) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 03:43:31 +08:00
|
|
|
var firstWord, triggerWord string
|
|
|
|
|
|
2017-01-13 13:53:37 -05:00
|
|
|
splitWords := strings.Fields(post.Message)
|
2017-07-19 03:43:31 +08:00
|
|
|
if len(splitWords) > 0 {
|
|
|
|
|
firstWord = splitWords[0]
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
relevantHooks := []*model.OutgoingWebhook{}
|
|
|
|
|
for _, hook := range hooks {
|
|
|
|
|
if hook.ChannelId == post.ChannelId || len(hook.ChannelId) == 0 {
|
|
|
|
|
if hook.ChannelId == post.ChannelId && len(hook.TriggerWords) == 0 {
|
|
|
|
|
relevantHooks = append(relevantHooks, hook)
|
2017-07-19 03:43:31 +08:00
|
|
|
triggerWord = ""
|
|
|
|
|
} else if hook.TriggerWhen == TRIGGERWORDS_EXACT_MATCH && hook.TriggerWordExactMatch(firstWord) {
|
2017-01-13 13:53:37 -05:00
|
|
|
relevantHooks = append(relevantHooks, hook)
|
2017-07-19 03:43:31 +08:00
|
|
|
triggerWord = hook.GetTriggerWord(firstWord, true)
|
|
|
|
|
} else if hook.TriggerWhen == TRIGGERWORDS_STARTS_WITH && hook.TriggerWordStartsWith(firstWord) {
|
2017-01-13 13:53:37 -05:00
|
|
|
relevantHooks = append(relevantHooks, hook)
|
2017-07-19 03:43:31 +08:00
|
|
|
triggerWord = hook.GetTriggerWord(firstWord, false)
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, hook := range relevantHooks {
|
2017-07-19 03:43:31 +08:00
|
|
|
payload := &model.OutgoingWebhookPayload{
|
|
|
|
|
Token: hook.Token,
|
|
|
|
|
TeamId: hook.TeamId,
|
|
|
|
|
TeamDomain: team.Name,
|
|
|
|
|
ChannelId: post.ChannelId,
|
|
|
|
|
ChannelName: channel.Name,
|
|
|
|
|
Timestamp: post.CreateAt,
|
|
|
|
|
UserId: post.UserId,
|
|
|
|
|
UserName: user.Username,
|
|
|
|
|
PostId: post.Id,
|
|
|
|
|
Text: post.Message,
|
|
|
|
|
TriggerWord: triggerWord,
|
|
|
|
|
FileIds: strings.Join(post.FileIds, ","),
|
|
|
|
|
}
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.Go(func(hook *model.OutgoingWebhook) func() {
|
2017-10-03 10:53:53 -05:00
|
|
|
return func() {
|
|
|
|
|
a.TriggerWebhook(payload, hook, post, channel)
|
|
|
|
|
}
|
|
|
|
|
}(hook))
|
2017-07-19 03:43:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingWebhook, post *model.Post, channel *model.Channel) {
|
2017-07-19 03:43:31 +08:00
|
|
|
var body io.Reader
|
|
|
|
|
var contentType string
|
|
|
|
|
if hook.ContentType == "application/json" {
|
|
|
|
|
body = strings.NewReader(payload.ToJSON())
|
|
|
|
|
contentType = "application/json"
|
|
|
|
|
} else {
|
|
|
|
|
body = strings.NewReader(payload.ToFormValues())
|
|
|
|
|
contentType = "application/x-www-form-urlencoded"
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 17:07:08 -05:00
|
|
|
for i := range hook.CallbackURLs {
|
|
|
|
|
// Get the callback URL by index to properly capture it for the go func
|
|
|
|
|
url := hook.CallbackURLs[i]
|
|
|
|
|
|
|
|
|
|
a.Srv.Go(func() {
|
|
|
|
|
webhookResp, err := a.doOutgoingWebhookRequest(url, body, contentType)
|
|
|
|
|
if err != nil {
|
2019-09-13 07:31:59 +02:00
|
|
|
mlog.Error("Event POST failed.", mlog.Err(err))
|
2019-01-09 17:07:08 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if webhookResp != nil && (webhookResp.Text != nil || len(webhookResp.Attachments) > 0) {
|
|
|
|
|
postRootId := ""
|
|
|
|
|
if webhookResp.ResponseType == model.OUTGOING_HOOK_RESPONSE_TYPE_COMMENT {
|
|
|
|
|
postRootId = post.Id
|
|
|
|
|
}
|
|
|
|
|
if len(webhookResp.Props) == 0 {
|
|
|
|
|
webhookResp.Props = make(model.StringInterface)
|
|
|
|
|
}
|
|
|
|
|
webhookResp.Props["webhook_display_name"] = hook.DisplayName
|
|
|
|
|
|
|
|
|
|
text := ""
|
|
|
|
|
if webhookResp.Text != nil {
|
|
|
|
|
text = a.ProcessSlackText(*webhookResp.Text)
|
|
|
|
|
}
|
|
|
|
|
webhookResp.Attachments = a.ProcessSlackAttachments(webhookResp.Attachments)
|
|
|
|
|
// attachments is in here for slack compatibility
|
|
|
|
|
if len(webhookResp.Attachments) > 0 {
|
|
|
|
|
webhookResp.Props["attachments"] = webhookResp.Attachments
|
|
|
|
|
}
|
2019-01-31 08:12:01 -05:00
|
|
|
if *a.Config().ServiceSettings.EnablePostUsernameOverride && hook.Username != "" && webhookResp.Username == "" {
|
2019-01-09 17:07:08 -05:00
|
|
|
webhookResp.Username = hook.Username
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 08:12:01 -05:00
|
|
|
if *a.Config().ServiceSettings.EnablePostIconOverride && hook.IconURL != "" && webhookResp.IconURL == "" {
|
2019-01-09 17:07:08 -05:00
|
|
|
webhookResp.IconURL = hook.IconURL
|
|
|
|
|
}
|
2019-07-17 09:01:18 +00:00
|
|
|
if _, err := a.CreateWebhookPost(hook.CreatorId, channel, text, webhookResp.Username, webhookResp.IconURL, "", webhookResp.Props, webhookResp.Type, postRootId); err != nil {
|
2019-09-13 07:31:59 +02:00
|
|
|
mlog.Error("Failed to create response post.", mlog.Err(err))
|
2017-07-19 03:43:31 +08:00
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
2019-01-09 17:07:08 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) doOutgoingWebhookRequest(url string, body io.Reader, contentType string) (*model.OutgoingWebhookResponse, error) {
|
|
|
|
|
req, err := http.NewRequest("POST", url, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
2019-01-09 17:07:08 -05:00
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
|
|
resp, err := a.HTTPService.MakeClient(false).Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
return model.OutgoingWebhookResponseFromJson(io.LimitReader(resp.Body, MaxIntegrationResponseSize))
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
Relax 4k post message limit (#8478)
* MM-9661: rename POST_MESSAGE_MAX_RUNES to \0_v1
* MM-9661: s/4000/POST_MESSAGE_MAX_RUNES_V1/ in tests
* MM-9661: introduce POST_MESSAGE_MAX_RUNES_V2
* MM-9661: migrate Postgres Posts.Message column to TEXT from VARCHAR(4000)
This is safe to do in a production instance since the underyling type is
not changing. We explicitly don't do this automatically for MySQL, but
also don't need to since the ORM would have already created a TEXT column
for MySQL in that case.
* MM-9661: emit MaxPostSize in client config
This value remains unconfigurable at this time, but exposes the current
limit to the client. The limit remains at 4k in this commit.
* MM-9661: introduce and use SqlPostStore.GetMaxPostSize
Enforce a byte limitation in the database, and use 1/4 of that value as
the rune count limitation (assuming a worst case UTF-8 representation).
* move maxPostSizeCached, lastPostsCache and lastPostTimeCache out of the global context and onto the SqlPostStore
* address feedback from code review:
* ensure sqlstore unit tests are actually being run
* move global caches into SqlPostStore
* leverage sync.Once to address a race condition
* modify upgrade semantics to match new db semantics
gorp's behaviour on creating columns with a maximum length on Postgres
differs from MySQL:
* Postgres
* gorp uses TEXT for string columns without a maximum length
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* MySQL
* gorp uses TEXT for string columns with a maximum length >= 256
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* gorp defaults to a maximum length of 255, implying VARCHAR(255)
So the Message column has been TEXT on MySQL but VARCHAR(4000) on
Postgres. With the new, longer limits of 65535, and without changes to
gorp, the expected behaviour is TEXT on MySQL and VARCHAR(65535) on
Postgres. This commit makes the upgrade semantics match the new database
semantics.
Ideally, we'd revisit the gorp behaviour at a later time.
* allow TestMaxPostSize test cases to actually run in parallel
* default maxPostSizeCached to POST_MESSAGE_MAX_RUNES_V1 in case the once initializer panics
* fix casting error
* MM-9661: skip the schema migration for Postgres
It turns out resizing VARCHAR requires a rewrite in some versions of
Postgres, but migrating VARCHAR to TEXT does not. Given the increasing
complexity, let's defer the migration to the enduser instead.
2018-03-26 17:55:35 -04:00
|
|
|
func SplitWebhookPost(post *model.Post, maxPostSize int) ([]*model.Post, *model.AppError) {
|
2017-10-24 18:36:31 -05:00
|
|
|
splits := make([]*model.Post, 0)
|
|
|
|
|
remainingText := post.Message
|
|
|
|
|
|
|
|
|
|
base := *post
|
|
|
|
|
base.Message = ""
|
|
|
|
|
base.Props = make(map[string]interface{})
|
|
|
|
|
for k, v := range post.Props {
|
|
|
|
|
if k != "attachments" {
|
|
|
|
|
base.Props[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if utf8.RuneCountInString(model.StringInterfaceToJson(base.Props)) > model.POST_PROPS_MAX_USER_RUNES {
|
|
|
|
|
return nil, model.NewAppError("SplitWebhookPost", "web.incoming_webhook.split_props_length.app_error", map[string]interface{}{"Max": model.POST_PROPS_MAX_USER_RUNES}, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
Relax 4k post message limit (#8478)
* MM-9661: rename POST_MESSAGE_MAX_RUNES to \0_v1
* MM-9661: s/4000/POST_MESSAGE_MAX_RUNES_V1/ in tests
* MM-9661: introduce POST_MESSAGE_MAX_RUNES_V2
* MM-9661: migrate Postgres Posts.Message column to TEXT from VARCHAR(4000)
This is safe to do in a production instance since the underyling type is
not changing. We explicitly don't do this automatically for MySQL, but
also don't need to since the ORM would have already created a TEXT column
for MySQL in that case.
* MM-9661: emit MaxPostSize in client config
This value remains unconfigurable at this time, but exposes the current
limit to the client. The limit remains at 4k in this commit.
* MM-9661: introduce and use SqlPostStore.GetMaxPostSize
Enforce a byte limitation in the database, and use 1/4 of that value as
the rune count limitation (assuming a worst case UTF-8 representation).
* move maxPostSizeCached, lastPostsCache and lastPostTimeCache out of the global context and onto the SqlPostStore
* address feedback from code review:
* ensure sqlstore unit tests are actually being run
* move global caches into SqlPostStore
* leverage sync.Once to address a race condition
* modify upgrade semantics to match new db semantics
gorp's behaviour on creating columns with a maximum length on Postgres
differs from MySQL:
* Postgres
* gorp uses TEXT for string columns without a maximum length
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* MySQL
* gorp uses TEXT for string columns with a maximum length >= 256
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* gorp defaults to a maximum length of 255, implying VARCHAR(255)
So the Message column has been TEXT on MySQL but VARCHAR(4000) on
Postgres. With the new, longer limits of 65535, and without changes to
gorp, the expected behaviour is TEXT on MySQL and VARCHAR(65535) on
Postgres. This commit makes the upgrade semantics match the new database
semantics.
Ideally, we'd revisit the gorp behaviour at a later time.
* allow TestMaxPostSize test cases to actually run in parallel
* default maxPostSizeCached to POST_MESSAGE_MAX_RUNES_V1 in case the once initializer panics
* fix casting error
* MM-9661: skip the schema migration for Postgres
It turns out resizing VARCHAR requires a rewrite in some versions of
Postgres, but migrating VARCHAR to TEXT does not. Given the increasing
complexity, let's defer the migration to the enduser instead.
2018-03-26 17:55:35 -04:00
|
|
|
for utf8.RuneCountInString(remainingText) > maxPostSize {
|
2017-10-24 18:36:31 -05:00
|
|
|
split := base
|
|
|
|
|
x := 0
|
|
|
|
|
for index := range remainingText {
|
|
|
|
|
x++
|
Relax 4k post message limit (#8478)
* MM-9661: rename POST_MESSAGE_MAX_RUNES to \0_v1
* MM-9661: s/4000/POST_MESSAGE_MAX_RUNES_V1/ in tests
* MM-9661: introduce POST_MESSAGE_MAX_RUNES_V2
* MM-9661: migrate Postgres Posts.Message column to TEXT from VARCHAR(4000)
This is safe to do in a production instance since the underyling type is
not changing. We explicitly don't do this automatically for MySQL, but
also don't need to since the ORM would have already created a TEXT column
for MySQL in that case.
* MM-9661: emit MaxPostSize in client config
This value remains unconfigurable at this time, but exposes the current
limit to the client. The limit remains at 4k in this commit.
* MM-9661: introduce and use SqlPostStore.GetMaxPostSize
Enforce a byte limitation in the database, and use 1/4 of that value as
the rune count limitation (assuming a worst case UTF-8 representation).
* move maxPostSizeCached, lastPostsCache and lastPostTimeCache out of the global context and onto the SqlPostStore
* address feedback from code review:
* ensure sqlstore unit tests are actually being run
* move global caches into SqlPostStore
* leverage sync.Once to address a race condition
* modify upgrade semantics to match new db semantics
gorp's behaviour on creating columns with a maximum length on Postgres
differs from MySQL:
* Postgres
* gorp uses TEXT for string columns without a maximum length
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* MySQL
* gorp uses TEXT for string columns with a maximum length >= 256
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* gorp defaults to a maximum length of 255, implying VARCHAR(255)
So the Message column has been TEXT on MySQL but VARCHAR(4000) on
Postgres. With the new, longer limits of 65535, and without changes to
gorp, the expected behaviour is TEXT on MySQL and VARCHAR(65535) on
Postgres. This commit makes the upgrade semantics match the new database
semantics.
Ideally, we'd revisit the gorp behaviour at a later time.
* allow TestMaxPostSize test cases to actually run in parallel
* default maxPostSizeCached to POST_MESSAGE_MAX_RUNES_V1 in case the once initializer panics
* fix casting error
* MM-9661: skip the schema migration for Postgres
It turns out resizing VARCHAR requires a rewrite in some versions of
Postgres, but migrating VARCHAR to TEXT does not. Given the increasing
complexity, let's defer the migration to the enduser instead.
2018-03-26 17:55:35 -04:00
|
|
|
if x > maxPostSize {
|
2017-10-24 18:36:31 -05:00
|
|
|
split.Message = remainingText[:index]
|
|
|
|
|
remainingText = remainingText[index:]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
splits = append(splits, &split)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
split := base
|
|
|
|
|
split.Message = remainingText
|
|
|
|
|
splits = append(splits, &split)
|
|
|
|
|
|
|
|
|
|
attachments, _ := post.Props["attachments"].([]*model.SlackAttachment)
|
|
|
|
|
for _, attachment := range attachments {
|
|
|
|
|
newAttachment := *attachment
|
|
|
|
|
for {
|
|
|
|
|
lastSplit := splits[len(splits)-1]
|
|
|
|
|
newProps := make(map[string]interface{})
|
|
|
|
|
for k, v := range lastSplit.Props {
|
|
|
|
|
newProps[k] = v
|
|
|
|
|
}
|
|
|
|
|
origAttachments, _ := newProps["attachments"].([]*model.SlackAttachment)
|
|
|
|
|
newProps["attachments"] = append(origAttachments, &newAttachment)
|
|
|
|
|
newPropsString := model.StringInterfaceToJson(newProps)
|
|
|
|
|
runeCount := utf8.RuneCountInString(newPropsString)
|
|
|
|
|
|
|
|
|
|
if runeCount <= model.POST_PROPS_MAX_USER_RUNES {
|
|
|
|
|
lastSplit.Props = newProps
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(origAttachments) > 0 {
|
|
|
|
|
newSplit := base
|
|
|
|
|
splits = append(splits, &newSplit)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
truncationNeeded := runeCount - model.POST_PROPS_MAX_USER_RUNES
|
|
|
|
|
textRuneCount := utf8.RuneCountInString(attachment.Text)
|
|
|
|
|
if textRuneCount < truncationNeeded {
|
|
|
|
|
return nil, model.NewAppError("SplitWebhookPost", "web.incoming_webhook.split_props_length.app_error", map[string]interface{}{"Max": model.POST_PROPS_MAX_USER_RUNES}, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
x := 0
|
|
|
|
|
for index := range attachment.Text {
|
|
|
|
|
x++
|
|
|
|
|
if x > textRuneCount-truncationNeeded {
|
|
|
|
|
newAttachment.Text = newAttachment.Text[:index]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lastSplit.Props = newProps
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return splits, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 09:01:18 +00:00
|
|
|
func (a *App) CreateWebhookPost(userId string, channel *model.Channel, text, overrideUsername, overrideIconUrl, overrideIconEmoji string, props model.StringInterface, postType string, postRootId string) (*model.Post, *model.AppError) {
|
2017-01-13 15:17:50 -05:00
|
|
|
// parse links into Markdown format
|
2018-03-12 12:40:11 +01:00
|
|
|
linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\n>]+)>`)
|
2017-01-13 15:17:50 -05:00
|
|
|
text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
|
|
|
|
|
|
2017-11-21 00:57:35 +01:00
|
|
|
post := &model.Post{UserId: userId, ChannelId: channel.Id, Message: text, Type: postType, RootId: postRootId}
|
2017-01-13 13:53:37 -05:00
|
|
|
post.AddProp("from_webhook", "true")
|
|
|
|
|
|
2017-10-09 13:30:48 -04:00
|
|
|
if strings.HasPrefix(post.Type, model.POST_SYSTEM_MESSAGE_PREFIX) {
|
|
|
|
|
err := model.NewAppError("CreateWebhookPost", "api.context.invalid_param.app_error", map[string]interface{}{"Name": "post.type"}, "", http.StatusBadRequest)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
if metrics := a.Metrics; metrics != nil {
|
2017-02-10 10:05:12 -05:00
|
|
|
metrics.IncrementWebhookPost()
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 08:12:01 -05:00
|
|
|
if *a.Config().ServiceSettings.EnablePostUsernameOverride {
|
2017-01-13 13:53:37 -05:00
|
|
|
if len(overrideUsername) != 0 {
|
|
|
|
|
post.AddProp("override_username", overrideUsername)
|
|
|
|
|
} else {
|
|
|
|
|
post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 08:12:01 -05:00
|
|
|
if *a.Config().ServiceSettings.EnablePostIconOverride {
|
2017-01-13 13:53:37 -05:00
|
|
|
if len(overrideIconUrl) != 0 {
|
|
|
|
|
post.AddProp("override_icon_url", overrideIconUrl)
|
|
|
|
|
}
|
2019-07-17 09:01:18 +00:00
|
|
|
if len(overrideIconEmoji) != 0 {
|
|
|
|
|
post.AddProp("override_icon_emoji", overrideIconEmoji)
|
|
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(props) > 0 {
|
|
|
|
|
for key, val := range props {
|
|
|
|
|
if key == "attachments" {
|
2017-03-10 05:22:14 -05:00
|
|
|
if attachments, success := val.([]*model.SlackAttachment); success {
|
2018-09-17 10:15:28 -04:00
|
|
|
model.ParseSlackAttachment(post, attachments)
|
2017-01-13 15:17:50 -05:00
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
} else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" {
|
|
|
|
|
post.AddProp(key, val)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Relax 4k post message limit (#8478)
* MM-9661: rename POST_MESSAGE_MAX_RUNES to \0_v1
* MM-9661: s/4000/POST_MESSAGE_MAX_RUNES_V1/ in tests
* MM-9661: introduce POST_MESSAGE_MAX_RUNES_V2
* MM-9661: migrate Postgres Posts.Message column to TEXT from VARCHAR(4000)
This is safe to do in a production instance since the underyling type is
not changing. We explicitly don't do this automatically for MySQL, but
also don't need to since the ORM would have already created a TEXT column
for MySQL in that case.
* MM-9661: emit MaxPostSize in client config
This value remains unconfigurable at this time, but exposes the current
limit to the client. The limit remains at 4k in this commit.
* MM-9661: introduce and use SqlPostStore.GetMaxPostSize
Enforce a byte limitation in the database, and use 1/4 of that value as
the rune count limitation (assuming a worst case UTF-8 representation).
* move maxPostSizeCached, lastPostsCache and lastPostTimeCache out of the global context and onto the SqlPostStore
* address feedback from code review:
* ensure sqlstore unit tests are actually being run
* move global caches into SqlPostStore
* leverage sync.Once to address a race condition
* modify upgrade semantics to match new db semantics
gorp's behaviour on creating columns with a maximum length on Postgres
differs from MySQL:
* Postgres
* gorp uses TEXT for string columns without a maximum length
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* MySQL
* gorp uses TEXT for string columns with a maximum length >= 256
* gorp uses VARCHAR(N) for string columns with a maximum length of N
* gorp defaults to a maximum length of 255, implying VARCHAR(255)
So the Message column has been TEXT on MySQL but VARCHAR(4000) on
Postgres. With the new, longer limits of 65535, and without changes to
gorp, the expected behaviour is TEXT on MySQL and VARCHAR(65535) on
Postgres. This commit makes the upgrade semantics match the new database
semantics.
Ideally, we'd revisit the gorp behaviour at a later time.
* allow TestMaxPostSize test cases to actually run in parallel
* default maxPostSizeCached to POST_MESSAGE_MAX_RUNES_V1 in case the once initializer panics
* fix casting error
* MM-9661: skip the schema migration for Postgres
It turns out resizing VARCHAR requires a rewrite in some versions of
Postgres, but migrating VARCHAR to TEXT does not. Given the increasing
complexity, let's defer the migration to the enduser instead.
2018-03-26 17:55:35 -04:00
|
|
|
splits, err := SplitWebhookPost(post, a.MaxPostSize())
|
2017-10-24 18:36:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-06-02 09:08:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 18:36:31 -05:00
|
|
|
for _, split := range splits {
|
|
|
|
|
if _, err := a.CreatePostMissingChannel(split, false); err != nil {
|
2017-09-01 16:42:02 +01:00
|
|
|
return nil, model.NewAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message, http.StatusInternalServerError)
|
2017-06-02 09:08:04 -07:00
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 18:36:31 -05:00
|
|
|
return splits[0], nil
|
2017-01-13 13:53:37 -05:00
|
|
|
}
|
2017-02-21 19:42:34 -05:00
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) CreateIncomingWebhookForChannel(creatorId string, channel *model.Channel, hook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-28 04:31:53 -05:00
|
|
|
hook.UserId = creatorId
|
2017-02-21 19:42:34 -05:00
|
|
|
hook.TeamId = channel.TeamId
|
|
|
|
|
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnablePostUsernameOverride {
|
2018-01-03 10:35:36 -05:00
|
|
|
hook.Username = ""
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnablePostIconOverride {
|
2018-01-03 10:35:36 -05:00
|
|
|
hook.IconURL = ""
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 10:35:36 -05:00
|
|
|
if hook.Username != "" && !model.IsValidUsername(hook.Username) {
|
|
|
|
|
return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.incoming_webhook.invalid_username.app_error", nil, "", http.StatusBadRequest)
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 04:30:41 -04:00
|
|
|
return a.Srv.Store.Webhook().SaveIncoming(hook)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) UpdateIncomingWebhook(oldHook, updatedHook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("UpdateIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnablePostUsernameOverride {
|
2018-01-03 10:35:36 -05:00
|
|
|
updatedHook.Username = oldHook.Username
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnablePostIconOverride {
|
2018-01-03 10:35:36 -05:00
|
|
|
updatedHook.IconURL = oldHook.IconURL
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 10:35:36 -05:00
|
|
|
if updatedHook.Username != "" && !model.IsValidUsername(updatedHook.Username) {
|
|
|
|
|
return nil, model.NewAppError("UpdateIncomingWebhook", "api.incoming_webhook.invalid_username.app_error", nil, "", http.StatusBadRequest)
|
2018-01-02 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-16 17:26:15 +01:00
|
|
|
updatedHook.Id = oldHook.Id
|
2017-02-28 04:31:53 -05:00
|
|
|
updatedHook.UserId = oldHook.UserId
|
|
|
|
|
updatedHook.CreateAt = oldHook.CreateAt
|
|
|
|
|
updatedHook.UpdateAt = model.GetMillis()
|
|
|
|
|
updatedHook.TeamId = oldHook.TeamId
|
|
|
|
|
updatedHook.DeleteAt = oldHook.DeleteAt
|
|
|
|
|
|
2019-04-18 14:03:59 +02:00
|
|
|
newWebhook, err := a.Srv.Store.Webhook().UpdateIncoming(updatedHook)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
2019-04-18 14:03:59 +02:00
|
|
|
a.InvalidateCacheForWebhook(oldHook.Id)
|
|
|
|
|
return newWebhook, nil
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) DeleteIncomingWebhook(hookId string) *model.AppError {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return model.NewAppError("DeleteIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 22:34:08 -07:00
|
|
|
if err := a.Srv.Store.Webhook().DeleteIncoming(hookId, model.GetMillis()); err != nil {
|
|
|
|
|
return err
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
a.InvalidateCacheForWebhook(hookId)
|
2017-02-28 04:31:53 -05:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) GetIncomingWebhook(hookId string) (*model.IncomingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("GetIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 19:16:30 +02:00
|
|
|
return a.Srv.Store.Webhook().GetIncoming(hookId, true)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) GetIncomingWebhooksForTeamPage(teamId string, page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.GetIncomingWebhooksForTeamPageByUser(teamId, "", page, perPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) GetIncomingWebhooksForTeamPageByUser(teamId string, userId string, page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("GetIncomingWebhooksForTeamPage", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.Srv.Store.Webhook().GetIncomingByTeamByUser(teamId, userId, page*perPage, perPage)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
func (a *App) GetIncomingWebhooksPageByUser(userId string, page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("GetIncomingWebhooksPage", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.Srv.Store.Webhook().GetIncomingListByUser(userId, page*perPage, perPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) GetIncomingWebhooksPage(page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
|
|
|
|
|
return a.GetIncomingWebhooksPageByUser("", page, perPage)
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
2017-02-28 04:31:53 -05:00
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) CreateOutgoingWebhook(hook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("CreateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(hook.ChannelId) != 0 {
|
2019-04-24 15:28:06 -04:00
|
|
|
channel, errCh := a.Srv.Store.Channel().Get(hook.ChannelId, true)
|
|
|
|
|
if errCh != nil {
|
|
|
|
|
return nil, errCh
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.Type != model.CHANNEL_OPEN {
|
|
|
|
|
return nil, model.NewAppError("CreateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.Type != model.CHANNEL_OPEN || channel.TeamId != hook.TeamId {
|
|
|
|
|
return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.permissions.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
} else if len(hook.TriggerWords) == 0 {
|
|
|
|
|
return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.triggers.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 12:20:36 -07:00
|
|
|
if allHooks, err := a.Srv.Store.Webhook().GetOutgoingByTeam(hook.TeamId, -1, -1); err != nil {
|
|
|
|
|
return nil, err
|
2017-02-28 04:31:53 -05:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
for _, existingOutHook := range allHooks {
|
|
|
|
|
urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, hook.CallbackURLs)
|
|
|
|
|
triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, hook.TriggerWords)
|
|
|
|
|
|
|
|
|
|
if existingOutHook.ChannelId == hook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 {
|
2017-09-01 16:42:02 +01:00
|
|
|
return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.intersect.app_error", nil, "", http.StatusInternalServerError)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 01:39:59 -07:00
|
|
|
webhook, err := a.Srv.Store.Webhook().SaveOutgoing(hook)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
2019-04-23 01:39:59 -07:00
|
|
|
|
|
|
|
|
return webhook, nil
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) UpdateOutgoingWebhook(oldHook, updatedHook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("UpdateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(updatedHook.ChannelId) > 0 {
|
2017-09-06 17:12:54 -05:00
|
|
|
channel, err := a.GetChannel(updatedHook.ChannelId)
|
2017-02-28 04:31:53 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.Type != model.CHANNEL_OPEN {
|
|
|
|
|
return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.not_open.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if channel.TeamId != oldHook.TeamId {
|
|
|
|
|
return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.permissions.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
} else if len(updatedHook.TriggerWords) == 0 {
|
2017-09-01 16:42:02 +01:00
|
|
|
return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.triggers.app_error", nil, "", http.StatusInternalServerError)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2019-04-26 12:20:36 -07:00
|
|
|
allHooks, err := a.Srv.Store.Webhook().GetOutgoingByTeam(oldHook.TeamId, -1, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, existingOutHook := range allHooks {
|
|
|
|
|
urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, updatedHook.CallbackURLs)
|
|
|
|
|
triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, updatedHook.TriggerWords)
|
|
|
|
|
|
|
|
|
|
if existingOutHook.ChannelId == updatedHook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 && existingOutHook.Id != updatedHook.Id {
|
|
|
|
|
return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.update_outgoing.intersect.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updatedHook.CreatorId = oldHook.CreatorId
|
|
|
|
|
updatedHook.CreateAt = oldHook.CreateAt
|
|
|
|
|
updatedHook.DeleteAt = oldHook.DeleteAt
|
|
|
|
|
updatedHook.TeamId = oldHook.TeamId
|
|
|
|
|
updatedHook.UpdateAt = model.GetMillis()
|
|
|
|
|
|
2019-04-26 00:28:04 -07:00
|
|
|
return a.Srv.Store.Webhook().UpdateOutgoing(updatedHook)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) GetOutgoingWebhook(hookId string) (*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("GetOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 14:19:38 +08:00
|
|
|
return a.Srv.Store.Webhook().GetOutgoing(hookId)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) GetOutgoingWebhooksPage(page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.GetOutgoingWebhooksPageByUser("", page, perPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) GetOutgoingWebhooksPageByUser(userId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-03-13 10:40:43 -04:00
|
|
|
return nil, model.NewAppError("GetOutgoingWebhooksPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.Srv.Store.Webhook().GetOutgoingListByUser(userId, page*perPage, perPage)
|
2017-03-13 10:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
func (a *App) GetOutgoingWebhooksForChannelPageByUser(channelId string, userId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-03-13 10:40:43 -04:00
|
|
|
return nil, model.NewAppError("GetOutgoingWebhooksForChannelPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.Srv.Store.Webhook().GetOutgoingByChannelByUser(channelId, userId, page*perPage, perPage)
|
2017-03-13 10:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) GetOutgoingWebhooksForTeamPage(teamId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.GetOutgoingWebhooksForTeamPageByUser(teamId, "", page, perPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) GetOutgoingWebhooksForTeamPageByUser(teamId string, userId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("GetOutgoingWebhooksForTeamPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
return a.Srv.Store.Webhook().GetOutgoingByTeamByUser(teamId, userId, page*perPage, perPage)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) DeleteOutgoingWebhook(hookId string) *model.AppError {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return model.NewAppError("DeleteOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 22:44:00 -07:00
|
|
|
return a.Srv.Store.Webhook().DeleteOutgoing(hookId, model.GetMillis())
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) RegenOutgoingWebhookToken(hook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableOutgoingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return nil, model.NewAppError("RegenOutgoingWebhookToken", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hook.Token = model.NewId()
|
|
|
|
|
|
2019-04-26 00:28:04 -07:00
|
|
|
return a.Srv.Store.Webhook().UpdateOutgoing(hook)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *model.AppError {
|
2019-01-31 08:12:01 -05:00
|
|
|
if !*a.Config().ServiceSettings.EnableIncomingWebhooks {
|
2017-02-28 04:31:53 -05:00
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 19:16:30 +02:00
|
|
|
hchan := make(chan store.StoreResult, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
webhook, err := a.Srv.Store.Webhook().GetIncoming(hookId, true)
|
|
|
|
|
hchan <- store.StoreResult{Data: webhook, Err: err}
|
|
|
|
|
close(hchan)
|
|
|
|
|
}()
|
2017-02-28 04:31:53 -05:00
|
|
|
|
|
|
|
|
if req == nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.parse.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text := req.Text
|
|
|
|
|
if len(text) == 0 && req.Attachments == nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.text.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channelName := req.ChannelName
|
|
|
|
|
webhookType := req.Type
|
|
|
|
|
|
2017-12-22 16:20:18 +01:00
|
|
|
var hook *model.IncomingWebhook
|
|
|
|
|
if result := <-hchan; result.Err != nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.invalid.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest)
|
|
|
|
|
} else {
|
|
|
|
|
hook = result.Data.(*model.IncomingWebhook)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 22:53:52 +02:00
|
|
|
uchan := make(chan store.StoreResult, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
user, err := a.Srv.Store.User().Get(hook.UserId)
|
|
|
|
|
uchan <- store.StoreResult{Data: user, Err: err}
|
|
|
|
|
close(uchan)
|
|
|
|
|
}()
|
2018-07-06 09:07:36 +01:00
|
|
|
|
2017-12-22 16:20:18 +01:00
|
|
|
if len(req.Props) == 0 {
|
|
|
|
|
req.Props = make(model.StringInterface)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Props["webhook_display_name"] = hook.DisplayName
|
|
|
|
|
|
2017-11-18 03:17:59 +11:00
|
|
|
text = a.ProcessSlackText(text)
|
|
|
|
|
req.Attachments = a.ProcessSlackAttachments(req.Attachments)
|
2017-02-28 04:31:53 -05:00
|
|
|
// attachments is in here for slack compatibility
|
2017-03-10 05:22:14 -05:00
|
|
|
if len(req.Attachments) > 0 {
|
2017-02-28 04:31:53 -05:00
|
|
|
req.Props["attachments"] = req.Attachments
|
|
|
|
|
webhookType = model.POST_SLACK_ATTACHMENT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channel *model.Channel
|
2019-07-29 12:38:46 +02:00
|
|
|
var cchan chan store.StoreResult
|
2017-02-28 04:31:53 -05:00
|
|
|
|
|
|
|
|
if len(channelName) != 0 {
|
|
|
|
|
if channelName[0] == '@' {
|
2019-07-10 14:46:03 +06:00
|
|
|
if result, err := a.Srv.Store.User().GetByUsername(channelName[1:]); err != nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+err.Message, http.StatusBadRequest)
|
2017-02-28 04:31:53 -05:00
|
|
|
} else {
|
2019-07-10 14:46:03 +06:00
|
|
|
if ch, err := a.GetOrCreateDirectChannel(hook.UserId, result.Id); err != nil {
|
2017-08-02 01:36:54 -07:00
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
channel = ch
|
|
|
|
|
}
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
} else if channelName[0] == '#' {
|
2019-07-29 12:38:46 +02:00
|
|
|
cchan = make(chan store.StoreResult, 1)
|
2019-06-20 09:21:36 -04:00
|
|
|
go func() {
|
|
|
|
|
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true)
|
|
|
|
|
cchan <- store.StoreResult{Data: chnn, Err: chnnErr}
|
|
|
|
|
close(cchan)
|
|
|
|
|
}()
|
2017-08-02 01:36:54 -07:00
|
|
|
} else {
|
2019-07-29 12:38:46 +02:00
|
|
|
cchan = make(chan store.StoreResult, 1)
|
2019-06-20 09:21:36 -04:00
|
|
|
go func() {
|
|
|
|
|
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true)
|
|
|
|
|
cchan <- store.StoreResult{Data: chnn, Err: chnnErr}
|
|
|
|
|
close(cchan)
|
|
|
|
|
}()
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-04-24 15:28:06 -04:00
|
|
|
var err *model.AppError
|
|
|
|
|
channel, err = a.Srv.Store.Channel().Get(hook.ChannelId, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+err.Message, err.StatusCode)
|
|
|
|
|
}
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-08-02 01:36:54 -07:00
|
|
|
if channel == nil {
|
|
|
|
|
result := <-cchan
|
|
|
|
|
if result.Err != nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
|
2017-02-28 04:31:53 -05:00
|
|
|
} else {
|
2017-08-02 01:36:54 -07:00
|
|
|
channel = result.Data.(*model.Channel)
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 19:06:14 +01:00
|
|
|
if hook.ChannelLocked && hook.ChannelId != channel.Id {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.channel_locked.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 09:07:36 +01:00
|
|
|
var user *model.User
|
|
|
|
|
if result := <-uchan; result.Err != nil {
|
|
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+result.Err.Message, http.StatusForbidden)
|
|
|
|
|
} else {
|
|
|
|
|
user = result.Data.(*model.User)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 17:25:49 -06:00
|
|
|
if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly &&
|
2018-07-06 09:07:36 +01:00
|
|
|
channel.Name == model.DEFAULT_CHANNEL && !a.RolesGrantPermission(user.GetRoles(), model.PERMISSION_MANAGE_SYSTEM.Id) {
|
2017-09-25 13:25:43 +01:00
|
|
|
return model.NewAppError("HandleIncomingWebhook", "api.post.create_post.town_square_read_only", nil, "", http.StatusForbidden)
|
2017-09-01 08:53:55 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
if channel.Type != model.CHANNEL_OPEN && !a.HasPermissionToChannel(hook.UserId, channel.Id, model.PERMISSION_READ_CHANNEL) {
|
2017-02-28 04:31:53 -05:00
|
|
|
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.permissions.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 10:35:36 -05:00
|
|
|
overrideUsername := hook.Username
|
2018-01-02 11:41:23 -05:00
|
|
|
if req.Username != "" {
|
|
|
|
|
overrideUsername = req.Username
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 10:35:36 -05:00
|
|
|
overrideIconUrl := hook.IconURL
|
2018-01-02 11:41:23 -05:00
|
|
|
if req.IconURL != "" {
|
|
|
|
|
overrideIconUrl = req.IconURL
|
|
|
|
|
}
|
2017-08-02 01:36:54 -07:00
|
|
|
|
2019-07-17 09:01:18 +00:00
|
|
|
_, err := a.CreateWebhookPost(hook.UserId, channel, text, overrideUsername, overrideIconUrl, req.IconEmoji, req.Props, webhookType, "")
|
2017-10-30 11:57:24 -05:00
|
|
|
return err
|
2017-02-28 04:31:53 -05:00
|
|
|
}
|
2017-08-16 07:17:57 -05:00
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) CreateCommandWebhook(commandId string, args *model.CommandArgs) (*model.CommandWebhook, *model.AppError) {
|
2017-08-16 07:17:57 -05:00
|
|
|
hook := &model.CommandWebhook{
|
|
|
|
|
CommandId: commandId,
|
|
|
|
|
UserId: args.UserId,
|
|
|
|
|
ChannelId: args.ChannelId,
|
|
|
|
|
RootId: args.RootId,
|
|
|
|
|
ParentId: args.ParentId,
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 10:42:10 -04:00
|
|
|
return a.Srv.Store.CommandWebhook().Save(hook)
|
2017-08-16 07:17:57 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) HandleCommandWebhook(hookId string, response *model.CommandResponse) *model.AppError {
|
2017-08-16 07:17:57 -05:00
|
|
|
if response == nil {
|
|
|
|
|
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 11:54:25 -04:00
|
|
|
hook, err := a.Srv.Store.CommandWebhook().Get(hookId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+err.Message, err.StatusCode)
|
2017-08-16 07:17:57 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-06 09:12:41 -07:00
|
|
|
cmd, err := a.Srv.Store.Command().Get(hook.CommandId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.command.app_error", nil, "err="+err.Message, http.StatusBadRequest)
|
2017-08-16 07:17:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := &model.CommandArgs{
|
|
|
|
|
UserId: hook.UserId,
|
|
|
|
|
ChannelId: hook.ChannelId,
|
|
|
|
|
TeamId: cmd.TeamId,
|
|
|
|
|
RootId: hook.RootId,
|
|
|
|
|
ParentId: hook.ParentId,
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 16:57:05 -04:00
|
|
|
if err = a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); err != nil {
|
|
|
|
|
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+err.Message, err.StatusCode)
|
2017-08-16 07:17:57 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-06 09:12:41 -07:00
|
|
|
_, err = a.HandleCommandResponse(cmd, args, response, false)
|
2017-08-16 07:17:57 -05:00
|
|
|
return err
|
|
|
|
|
}
|