github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/app/webhook.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  	"regexp"
    10  	"strings"
    11  	"unicode/utf8"
    12  
    13  	l4g "github.com/alecthomas/log4go"
    14  	"github.com/mattermost/mattermost-server/model"
    15  	"github.com/mattermost/mattermost-server/store"
    16  	"github.com/mattermost/mattermost-server/utils"
    17  )
    18  
    19  const (
    20  	TRIGGERWORDS_EXACT_MATCH = 0
    21  	TRIGGERWORDS_STARTS_WITH = 1
    22  )
    23  
    24  func (a *App) handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User) *model.AppError {
    25  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
    26  		return nil
    27  	}
    28  
    29  	if channel.Type != model.CHANNEL_OPEN {
    30  		return nil
    31  	}
    32  
    33  	hchan := a.Srv.Store.Webhook().GetOutgoingByTeam(team.Id, -1, -1)
    34  	result := <-hchan
    35  	if result.Err != nil {
    36  		return result.Err
    37  	}
    38  
    39  	hooks := result.Data.([]*model.OutgoingWebhook)
    40  	if len(hooks) == 0 {
    41  		return nil
    42  	}
    43  
    44  	var firstWord, triggerWord string
    45  
    46  	splitWords := strings.Fields(post.Message)
    47  	if len(splitWords) > 0 {
    48  		firstWord = splitWords[0]
    49  	}
    50  
    51  	relevantHooks := []*model.OutgoingWebhook{}
    52  	for _, hook := range hooks {
    53  		if hook.ChannelId == post.ChannelId || len(hook.ChannelId) == 0 {
    54  			if hook.ChannelId == post.ChannelId && len(hook.TriggerWords) == 0 {
    55  				relevantHooks = append(relevantHooks, hook)
    56  				triggerWord = ""
    57  			} else if hook.TriggerWhen == TRIGGERWORDS_EXACT_MATCH && hook.TriggerWordExactMatch(firstWord) {
    58  				relevantHooks = append(relevantHooks, hook)
    59  				triggerWord = hook.GetTriggerWord(firstWord, true)
    60  			} else if hook.TriggerWhen == TRIGGERWORDS_STARTS_WITH && hook.TriggerWordStartsWith(firstWord) {
    61  				relevantHooks = append(relevantHooks, hook)
    62  				triggerWord = hook.GetTriggerWord(firstWord, false)
    63  			}
    64  		}
    65  	}
    66  
    67  	for _, hook := range relevantHooks {
    68  		payload := &model.OutgoingWebhookPayload{
    69  			Token:       hook.Token,
    70  			TeamId:      hook.TeamId,
    71  			TeamDomain:  team.Name,
    72  			ChannelId:   post.ChannelId,
    73  			ChannelName: channel.Name,
    74  			Timestamp:   post.CreateAt,
    75  			UserId:      post.UserId,
    76  			UserName:    user.Username,
    77  			PostId:      post.Id,
    78  			Text:        post.Message,
    79  			TriggerWord: triggerWord,
    80  			FileIds:     strings.Join(post.FileIds, ","),
    81  		}
    82  		a.Go(func(hook *model.OutgoingWebhook) func() {
    83  			return func() {
    84  				a.TriggerWebhook(payload, hook, post, channel)
    85  			}
    86  		}(hook))
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func (a *App) TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingWebhook, post *model.Post, channel *model.Channel) {
    93  	var body io.Reader
    94  	var contentType string
    95  	if hook.ContentType == "application/json" {
    96  		body = strings.NewReader(payload.ToJSON())
    97  		contentType = "application/json"
    98  	} else {
    99  		body = strings.NewReader(payload.ToFormValues())
   100  		contentType = "application/x-www-form-urlencoded"
   101  	}
   102  
   103  	for _, url := range hook.CallbackURLs {
   104  		a.Go(func(url string) func() {
   105  			return func() {
   106  				req, _ := http.NewRequest("POST", url, body)
   107  				req.Header.Set("Content-Type", contentType)
   108  				req.Header.Set("Accept", "application/json")
   109  				if resp, err := a.HTTPClient(false).Do(req); err != nil {
   110  					l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.event_post.error"), err.Error())
   111  				} else {
   112  					defer consumeAndClose(resp)
   113  
   114  					webhookResp := model.OutgoingWebhookResponseFromJson(resp.Body)
   115  
   116  					if webhookResp != nil && (webhookResp.Text != nil || len(webhookResp.Attachments) > 0) {
   117  						postRootId := ""
   118  						if webhookResp.ResponseType == model.OUTGOING_HOOK_RESPONSE_TYPE_COMMENT {
   119  							postRootId = post.Id
   120  						}
   121  						if len(webhookResp.Props) == 0 {
   122  							webhookResp.Props = make(model.StringInterface)
   123  						}
   124  						webhookResp.Props["webhook_display_name"] = hook.DisplayName
   125  
   126  						text := ""
   127  						if webhookResp.Text != nil {
   128  							text = a.ProcessSlackText(*webhookResp.Text)
   129  						}
   130  						webhookResp.Attachments = a.ProcessSlackAttachments(webhookResp.Attachments)
   131  						// attachments is in here for slack compatibility
   132  						if len(webhookResp.Attachments) > 0 {
   133  							webhookResp.Props["attachments"] = webhookResp.Attachments
   134  						}
   135  
   136  						if _, err := a.CreateWebhookPost(hook.CreatorId, channel, text, webhookResp.Username, webhookResp.IconURL, webhookResp.Props, webhookResp.Type, postRootId); err != nil {
   137  							l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.create_post.error"), err)
   138  						}
   139  					}
   140  				}
   141  			}
   142  		}(url))
   143  	}
   144  }
   145  
   146  func SplitWebhookPost(post *model.Post) ([]*model.Post, *model.AppError) {
   147  	splits := make([]*model.Post, 0)
   148  	remainingText := post.Message
   149  
   150  	base := *post
   151  	base.Message = ""
   152  	base.Props = make(map[string]interface{})
   153  	for k, v := range post.Props {
   154  		if k != "attachments" {
   155  			base.Props[k] = v
   156  		}
   157  	}
   158  	if utf8.RuneCountInString(model.StringInterfaceToJson(base.Props)) > model.POST_PROPS_MAX_USER_RUNES {
   159  		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)
   160  	}
   161  
   162  	for utf8.RuneCountInString(remainingText) > model.POST_MESSAGE_MAX_RUNES {
   163  		split := base
   164  		x := 0
   165  		for index := range remainingText {
   166  			x++
   167  			if x > model.POST_MESSAGE_MAX_RUNES {
   168  				split.Message = remainingText[:index]
   169  				remainingText = remainingText[index:]
   170  				break
   171  			}
   172  		}
   173  		splits = append(splits, &split)
   174  	}
   175  
   176  	split := base
   177  	split.Message = remainingText
   178  	splits = append(splits, &split)
   179  
   180  	attachments, _ := post.Props["attachments"].([]*model.SlackAttachment)
   181  	for _, attachment := range attachments {
   182  		newAttachment := *attachment
   183  		for {
   184  			lastSplit := splits[len(splits)-1]
   185  			newProps := make(map[string]interface{})
   186  			for k, v := range lastSplit.Props {
   187  				newProps[k] = v
   188  			}
   189  			origAttachments, _ := newProps["attachments"].([]*model.SlackAttachment)
   190  			newProps["attachments"] = append(origAttachments, &newAttachment)
   191  			newPropsString := model.StringInterfaceToJson(newProps)
   192  			runeCount := utf8.RuneCountInString(newPropsString)
   193  
   194  			if runeCount <= model.POST_PROPS_MAX_USER_RUNES {
   195  				lastSplit.Props = newProps
   196  				break
   197  			}
   198  
   199  			if len(origAttachments) > 0 {
   200  				newSplit := base
   201  				splits = append(splits, &newSplit)
   202  				continue
   203  			}
   204  
   205  			truncationNeeded := runeCount - model.POST_PROPS_MAX_USER_RUNES
   206  			textRuneCount := utf8.RuneCountInString(attachment.Text)
   207  			if textRuneCount < truncationNeeded {
   208  				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)
   209  			}
   210  			x := 0
   211  			for index := range attachment.Text {
   212  				x++
   213  				if x > textRuneCount-truncationNeeded {
   214  					newAttachment.Text = newAttachment.Text[:index]
   215  					break
   216  				}
   217  			}
   218  			lastSplit.Props = newProps
   219  			break
   220  		}
   221  	}
   222  
   223  	return splits, nil
   224  }
   225  
   226  func (a *App) CreateWebhookPost(userId string, channel *model.Channel, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string, postRootId string) (*model.Post, *model.AppError) {
   227  	// parse links into Markdown format
   228  	linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\n>]+)>`)
   229  	text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
   230  
   231  	post := &model.Post{UserId: userId, ChannelId: channel.Id, Message: text, Type: postType, RootId: postRootId}
   232  	post.AddProp("from_webhook", "true")
   233  
   234  	if strings.HasPrefix(post.Type, model.POST_SYSTEM_MESSAGE_PREFIX) {
   235  		err := model.NewAppError("CreateWebhookPost", "api.context.invalid_param.app_error", map[string]interface{}{"Name": "post.type"}, "", http.StatusBadRequest)
   236  		return nil, err
   237  	}
   238  
   239  	if metrics := a.Metrics; metrics != nil {
   240  		metrics.IncrementWebhookPost()
   241  	}
   242  
   243  	if a.Config().ServiceSettings.EnablePostUsernameOverride {
   244  		if len(overrideUsername) != 0 {
   245  			post.AddProp("override_username", overrideUsername)
   246  		} else {
   247  			post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME)
   248  		}
   249  	}
   250  
   251  	if a.Config().ServiceSettings.EnablePostIconOverride {
   252  		if len(overrideIconUrl) != 0 {
   253  			post.AddProp("override_icon_url", overrideIconUrl)
   254  		}
   255  	}
   256  
   257  	if len(props) > 0 {
   258  		for key, val := range props {
   259  			if key == "attachments" {
   260  				if attachments, success := val.([]*model.SlackAttachment); success {
   261  					parseSlackAttachment(post, attachments)
   262  				}
   263  			} else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" {
   264  				post.AddProp(key, val)
   265  			}
   266  		}
   267  	}
   268  
   269  	splits, err := SplitWebhookPost(post)
   270  	if err != nil {
   271  		return nil, err
   272  	}
   273  
   274  	for _, split := range splits {
   275  		if _, err := a.CreatePostMissingChannel(split, false); err != nil {
   276  			return nil, model.NewAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message, http.StatusInternalServerError)
   277  		}
   278  	}
   279  
   280  	return splits[0], nil
   281  }
   282  
   283  func (a *App) CreateIncomingWebhookForChannel(creatorId string, channel *model.Channel, hook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) {
   284  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   285  		return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   286  	}
   287  
   288  	hook.UserId = creatorId
   289  	hook.TeamId = channel.TeamId
   290  
   291  	if !a.Config().ServiceSettings.EnablePostUsernameOverride {
   292  		hook.Username = ""
   293  	}
   294  	if !a.Config().ServiceSettings.EnablePostIconOverride {
   295  		hook.IconURL = ""
   296  	}
   297  
   298  	if hook.Username != "" && !model.IsValidUsername(hook.Username) {
   299  		return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.incoming_webhook.invalid_username.app_error", nil, "", http.StatusBadRequest)
   300  	}
   301  
   302  	if result := <-a.Srv.Store.Webhook().SaveIncoming(hook); result.Err != nil {
   303  		return nil, result.Err
   304  	} else {
   305  		return result.Data.(*model.IncomingWebhook), nil
   306  	}
   307  }
   308  
   309  func (a *App) UpdateIncomingWebhook(oldHook, updatedHook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) {
   310  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   311  		return nil, model.NewAppError("UpdateIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   312  	}
   313  
   314  	if !a.Config().ServiceSettings.EnablePostUsernameOverride {
   315  		updatedHook.Username = oldHook.Username
   316  	}
   317  	if !a.Config().ServiceSettings.EnablePostIconOverride {
   318  		updatedHook.IconURL = oldHook.IconURL
   319  	}
   320  
   321  	if updatedHook.Username != "" && !model.IsValidUsername(updatedHook.Username) {
   322  		return nil, model.NewAppError("UpdateIncomingWebhook", "api.incoming_webhook.invalid_username.app_error", nil, "", http.StatusBadRequest)
   323  	}
   324  
   325  	updatedHook.Id = oldHook.Id
   326  	updatedHook.UserId = oldHook.UserId
   327  	updatedHook.CreateAt = oldHook.CreateAt
   328  	updatedHook.UpdateAt = model.GetMillis()
   329  	updatedHook.TeamId = oldHook.TeamId
   330  	updatedHook.DeleteAt = oldHook.DeleteAt
   331  
   332  	if result := <-a.Srv.Store.Webhook().UpdateIncoming(updatedHook); result.Err != nil {
   333  		return nil, result.Err
   334  	} else {
   335  		a.InvalidateCacheForWebhook(oldHook.Id)
   336  		return result.Data.(*model.IncomingWebhook), nil
   337  	}
   338  }
   339  
   340  func (a *App) DeleteIncomingWebhook(hookId string) *model.AppError {
   341  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   342  		return model.NewAppError("DeleteIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   343  	}
   344  
   345  	if result := <-a.Srv.Store.Webhook().DeleteIncoming(hookId, model.GetMillis()); result.Err != nil {
   346  		return result.Err
   347  	}
   348  
   349  	a.InvalidateCacheForWebhook(hookId)
   350  
   351  	return nil
   352  }
   353  
   354  func (a *App) GetIncomingWebhook(hookId string) (*model.IncomingWebhook, *model.AppError) {
   355  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   356  		return nil, model.NewAppError("GetIncomingWebhook", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   357  	}
   358  
   359  	if result := <-a.Srv.Store.Webhook().GetIncoming(hookId, true); result.Err != nil {
   360  		return nil, result.Err
   361  	} else {
   362  		return result.Data.(*model.IncomingWebhook), nil
   363  	}
   364  }
   365  
   366  func (a *App) GetIncomingWebhooksForTeamPage(teamId string, page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
   367  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   368  		return nil, model.NewAppError("GetIncomingWebhooksForTeamPage", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   369  	}
   370  
   371  	if result := <-a.Srv.Store.Webhook().GetIncomingByTeam(teamId, page*perPage, perPage); result.Err != nil {
   372  		return nil, result.Err
   373  	} else {
   374  		return result.Data.([]*model.IncomingWebhook), nil
   375  	}
   376  }
   377  
   378  func (a *App) GetIncomingWebhooksPage(page, perPage int) ([]*model.IncomingWebhook, *model.AppError) {
   379  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   380  		return nil, model.NewAppError("GetIncomingWebhooksPage", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   381  	}
   382  
   383  	if result := <-a.Srv.Store.Webhook().GetIncomingList(page*perPage, perPage); result.Err != nil {
   384  		return nil, result.Err
   385  	} else {
   386  		return result.Data.([]*model.IncomingWebhook), nil
   387  	}
   388  }
   389  
   390  func (a *App) CreateOutgoingWebhook(hook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
   391  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   392  		return nil, model.NewAppError("CreateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   393  	}
   394  
   395  	if len(hook.ChannelId) != 0 {
   396  		cchan := a.Srv.Store.Channel().Get(hook.ChannelId, true)
   397  
   398  		var channel *model.Channel
   399  		if result := <-cchan; result.Err != nil {
   400  			return nil, result.Err
   401  		} else {
   402  			channel = result.Data.(*model.Channel)
   403  		}
   404  
   405  		if channel.Type != model.CHANNEL_OPEN {
   406  			return nil, model.NewAppError("CreateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusForbidden)
   407  		}
   408  
   409  		if channel.Type != model.CHANNEL_OPEN || channel.TeamId != hook.TeamId {
   410  			return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.permissions.app_error", nil, "", http.StatusForbidden)
   411  		}
   412  	} else if len(hook.TriggerWords) == 0 {
   413  		return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.triggers.app_error", nil, "", http.StatusBadRequest)
   414  	}
   415  
   416  	if result := <-a.Srv.Store.Webhook().GetOutgoingByTeam(hook.TeamId, -1, -1); result.Err != nil {
   417  		return nil, result.Err
   418  	} else {
   419  		allHooks := result.Data.([]*model.OutgoingWebhook)
   420  
   421  		for _, existingOutHook := range allHooks {
   422  			urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, hook.CallbackURLs)
   423  			triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, hook.TriggerWords)
   424  
   425  			if existingOutHook.ChannelId == hook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 {
   426  				return nil, model.NewAppError("CreateOutgoingWebhook", "api.webhook.create_outgoing.intersect.app_error", nil, "", http.StatusInternalServerError)
   427  			}
   428  		}
   429  	}
   430  
   431  	if result := <-a.Srv.Store.Webhook().SaveOutgoing(hook); result.Err != nil {
   432  		return nil, result.Err
   433  	} else {
   434  		return result.Data.(*model.OutgoingWebhook), nil
   435  	}
   436  }
   437  
   438  func (a *App) UpdateOutgoingWebhook(oldHook, updatedHook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
   439  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   440  		return nil, model.NewAppError("UpdateOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   441  	}
   442  
   443  	if len(updatedHook.ChannelId) > 0 {
   444  		channel, err := a.GetChannel(updatedHook.ChannelId)
   445  		if err != nil {
   446  			return nil, err
   447  		}
   448  
   449  		if channel.Type != model.CHANNEL_OPEN {
   450  			return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.not_open.app_error", nil, "", http.StatusForbidden)
   451  		}
   452  
   453  		if channel.TeamId != oldHook.TeamId {
   454  			return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.permissions.app_error", nil, "", http.StatusForbidden)
   455  		}
   456  	} else if len(updatedHook.TriggerWords) == 0 {
   457  		return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.create_outgoing.triggers.app_error", nil, "", http.StatusInternalServerError)
   458  	}
   459  
   460  	var result store.StoreResult
   461  	if result = <-a.Srv.Store.Webhook().GetOutgoingByTeam(oldHook.TeamId, -1, -1); result.Err != nil {
   462  		return nil, result.Err
   463  	}
   464  
   465  	allHooks := result.Data.([]*model.OutgoingWebhook)
   466  
   467  	for _, existingOutHook := range allHooks {
   468  		urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, updatedHook.CallbackURLs)
   469  		triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, updatedHook.TriggerWords)
   470  
   471  		if existingOutHook.ChannelId == updatedHook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 && existingOutHook.Id != updatedHook.Id {
   472  			return nil, model.NewAppError("UpdateOutgoingWebhook", "api.webhook.update_outgoing.intersect.app_error", nil, "", http.StatusBadRequest)
   473  		}
   474  	}
   475  
   476  	updatedHook.CreatorId = oldHook.CreatorId
   477  	updatedHook.CreateAt = oldHook.CreateAt
   478  	updatedHook.DeleteAt = oldHook.DeleteAt
   479  	updatedHook.TeamId = oldHook.TeamId
   480  	updatedHook.UpdateAt = model.GetMillis()
   481  
   482  	if result = <-a.Srv.Store.Webhook().UpdateOutgoing(updatedHook); result.Err != nil {
   483  		return nil, result.Err
   484  	} else {
   485  		return result.Data.(*model.OutgoingWebhook), nil
   486  	}
   487  }
   488  
   489  func (a *App) GetOutgoingWebhook(hookId string) (*model.OutgoingWebhook, *model.AppError) {
   490  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   491  		return nil, model.NewAppError("GetOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   492  	}
   493  
   494  	if result := <-a.Srv.Store.Webhook().GetOutgoing(hookId); result.Err != nil {
   495  		return nil, result.Err
   496  	} else {
   497  		return result.Data.(*model.OutgoingWebhook), nil
   498  	}
   499  }
   500  
   501  func (a *App) GetOutgoingWebhooksPage(page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
   502  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   503  		return nil, model.NewAppError("GetOutgoingWebhooksPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   504  	}
   505  
   506  	if result := <-a.Srv.Store.Webhook().GetOutgoingList(page*perPage, perPage); result.Err != nil {
   507  		return nil, result.Err
   508  	} else {
   509  		return result.Data.([]*model.OutgoingWebhook), nil
   510  	}
   511  }
   512  
   513  func (a *App) GetOutgoingWebhooksForChannelPage(channelId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
   514  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   515  		return nil, model.NewAppError("GetOutgoingWebhooksForChannelPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   516  	}
   517  
   518  	if result := <-a.Srv.Store.Webhook().GetOutgoingByChannel(channelId, page*perPage, perPage); result.Err != nil {
   519  		return nil, result.Err
   520  	} else {
   521  		return result.Data.([]*model.OutgoingWebhook), nil
   522  	}
   523  }
   524  
   525  func (a *App) GetOutgoingWebhooksForTeamPage(teamId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) {
   526  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   527  		return nil, model.NewAppError("GetOutgoingWebhooksForTeamPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   528  	}
   529  
   530  	if result := <-a.Srv.Store.Webhook().GetOutgoingByTeam(teamId, page*perPage, perPage); result.Err != nil {
   531  		return nil, result.Err
   532  	} else {
   533  		return result.Data.([]*model.OutgoingWebhook), nil
   534  	}
   535  }
   536  
   537  func (a *App) DeleteOutgoingWebhook(hookId string) *model.AppError {
   538  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   539  		return model.NewAppError("DeleteOutgoingWebhook", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   540  	}
   541  
   542  	if result := <-a.Srv.Store.Webhook().DeleteOutgoing(hookId, model.GetMillis()); result.Err != nil {
   543  		return result.Err
   544  	}
   545  
   546  	return nil
   547  }
   548  
   549  func (a *App) RegenOutgoingWebhookToken(hook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) {
   550  	if !a.Config().ServiceSettings.EnableOutgoingWebhooks {
   551  		return nil, model.NewAppError("RegenOutgoingWebhookToken", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   552  	}
   553  
   554  	hook.Token = model.NewId()
   555  
   556  	if result := <-a.Srv.Store.Webhook().UpdateOutgoing(hook); result.Err != nil {
   557  		return nil, result.Err
   558  	} else {
   559  		return result.Data.(*model.OutgoingWebhook), nil
   560  	}
   561  }
   562  
   563  func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *model.AppError {
   564  	if !a.Config().ServiceSettings.EnableIncomingWebhooks {
   565  		return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented)
   566  	}
   567  
   568  	hchan := a.Srv.Store.Webhook().GetIncoming(hookId, true)
   569  
   570  	if req == nil {
   571  		return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.parse.app_error", nil, "", http.StatusBadRequest)
   572  	}
   573  
   574  	text := req.Text
   575  	if len(text) == 0 && req.Attachments == nil {
   576  		return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.text.app_error", nil, "", http.StatusBadRequest)
   577  	}
   578  
   579  	channelName := req.ChannelName
   580  	webhookType := req.Type
   581  
   582  	var hook *model.IncomingWebhook
   583  	if result := <-hchan; result.Err != nil {
   584  		return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.invalid.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest)
   585  	} else {
   586  		hook = result.Data.(*model.IncomingWebhook)
   587  	}
   588  
   589  	if len(req.Props) == 0 {
   590  		req.Props = make(model.StringInterface)
   591  	}
   592  
   593  	req.Props["webhook_display_name"] = hook.DisplayName
   594  
   595  	text = a.ProcessSlackText(text)
   596  	req.Attachments = a.ProcessSlackAttachments(req.Attachments)
   597  	// attachments is in here for slack compatibility
   598  	if len(req.Attachments) > 0 {
   599  		req.Props["attachments"] = req.Attachments
   600  		webhookType = model.POST_SLACK_ATTACHMENT
   601  	}
   602  
   603  	var channel *model.Channel
   604  	var cchan store.StoreChannel
   605  
   606  	if len(channelName) != 0 {
   607  		if channelName[0] == '@' {
   608  			if result := <-a.Srv.Store.User().GetByUsername(channelName[1:]); result.Err != nil {
   609  				return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest)
   610  			} else {
   611  				if ch, err := a.GetDirectChannel(hook.UserId, result.Data.(*model.User).Id); err != nil {
   612  					return err
   613  				} else {
   614  					channel = ch
   615  				}
   616  			}
   617  		} else if channelName[0] == '#' {
   618  			cchan = a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true)
   619  		} else {
   620  			cchan = a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true)
   621  		}
   622  	} else {
   623  		cchan = a.Srv.Store.Channel().Get(hook.ChannelId, true)
   624  	}
   625  
   626  	if channel == nil {
   627  		result := <-cchan
   628  		if result.Err != nil {
   629  			return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.channel.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
   630  		} else {
   631  			channel = result.Data.(*model.Channel)
   632  		}
   633  	}
   634  
   635  	if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly &&
   636  		channel.Name == model.DEFAULT_CHANNEL {
   637  		return model.NewAppError("HandleIncomingWebhook", "api.post.create_post.town_square_read_only", nil, "", http.StatusForbidden)
   638  	}
   639  
   640  	if channel.Type != model.CHANNEL_OPEN && !a.HasPermissionToChannel(hook.UserId, channel.Id, model.PERMISSION_READ_CHANNEL) {
   641  		return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.permissions.app_error", nil, "", http.StatusForbidden)
   642  	}
   643  
   644  	overrideUsername := hook.Username
   645  	if req.Username != "" {
   646  		overrideUsername = req.Username
   647  	}
   648  
   649  	overrideIconUrl := hook.IconURL
   650  	if req.IconURL != "" {
   651  		overrideIconUrl = req.IconURL
   652  	}
   653  
   654  	_, err := a.CreateWebhookPost(hook.UserId, channel, text, overrideUsername, overrideIconUrl, req.Props, webhookType, "")
   655  	return err
   656  }
   657  
   658  func (a *App) CreateCommandWebhook(commandId string, args *model.CommandArgs) (*model.CommandWebhook, *model.AppError) {
   659  	hook := &model.CommandWebhook{
   660  		CommandId: commandId,
   661  		UserId:    args.UserId,
   662  		ChannelId: args.ChannelId,
   663  		RootId:    args.RootId,
   664  		ParentId:  args.ParentId,
   665  	}
   666  
   667  	if result := <-a.Srv.Store.CommandWebhook().Save(hook); result.Err != nil {
   668  		return nil, result.Err
   669  	} else {
   670  		return result.Data.(*model.CommandWebhook), nil
   671  	}
   672  }
   673  
   674  func (a *App) HandleCommandWebhook(hookId string, response *model.CommandResponse) *model.AppError {
   675  	if response == nil {
   676  		return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest)
   677  	}
   678  
   679  	var hook *model.CommandWebhook
   680  	if result := <-a.Srv.Store.CommandWebhook().Get(hookId); result.Err != nil {
   681  		return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
   682  	} else {
   683  		hook = result.Data.(*model.CommandWebhook)
   684  	}
   685  
   686  	var cmd *model.Command
   687  	if result := <-a.Srv.Store.Command().Get(hook.CommandId); result.Err != nil {
   688  		return model.NewAppError("HandleCommandWebhook", "web.command_webhook.command.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest)
   689  	} else {
   690  		cmd = result.Data.(*model.Command)
   691  	}
   692  
   693  	args := &model.CommandArgs{
   694  		UserId:    hook.UserId,
   695  		ChannelId: hook.ChannelId,
   696  		TeamId:    cmd.TeamId,
   697  		RootId:    hook.RootId,
   698  		ParentId:  hook.ParentId,
   699  	}
   700  
   701  	if result := <-a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); result.Err != nil {
   702  		return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
   703  	}
   704  
   705  	_, err := a.HandleCommandResponse(cmd, args, response, false)
   706  	return err
   707  }