summaryrefslogtreecommitdiffstats
path: root/web/web.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2015-09-22 15:47:57 -0400
committerJoram Wilander <jwawilander@gmail.com>2015-09-22 15:47:57 -0400
commitac7918c5540900ab0dbe43d61b8c1155e4279b55 (patch)
tree2e519e101e995d24d656d7cf1f7a9d404303a364 /web/web.go
parenta44e8f0cd904d386caea410398dcaf7dbfd9c138 (diff)
parent98186e5018bbc604796d4f9762c93f4f75e2913f (diff)
downloadchat-ac7918c5540900ab0dbe43d61b8c1155e4279b55.tar.gz
chat-ac7918c5540900ab0dbe43d61b8c1155e4279b55.tar.bz2
chat-ac7918c5540900ab0dbe43d61b8c1155e4279b55.zip
Merge pull request #715 from mattermost/plt-27
PLT-27 Implement incoming webhooks.
Diffstat (limited to 'web/web.go')
-rw-r--r--web/web.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/web/web.go b/web/web.go
index 305e4f199..95a5a5881 100644
--- a/web/web.go
+++ b/web/web.go
@@ -9,11 +9,13 @@ import (
"github.com/gorilla/mux"
"github.com/mattermost/platform/api"
"github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"github.com/mssola/user_agent"
"gopkg.in/fsnotify.v1"
"html/template"
"net/http"
+ "regexp"
"strconv"
"strings"
)
@@ -63,6 +65,8 @@ func InitWeb() {
mainrouter.Handle("/admin_console", api.UserRequired(adminConsole)).Methods("GET")
+ mainrouter.Handle("/hooks/{id:[A-Za-z0-9]+}", api.ApiAppHandler(incomingWebhook)).Methods("POST")
+
// ----------------------------------------------------------------------------------------------
// *ANYTHING* team specific should go below this line
// ----------------------------------------------------------------------------------------------
@@ -838,3 +842,84 @@ func getAccessToken(c *api.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(accessRsp.ToJson()))
}
+
+func incomingWebhook(c *api.Context, w http.ResponseWriter, r *http.Request) {
+ params := mux.Vars(r)
+ id := params["id"]
+
+ hchan := api.Srv.Store.Webhook().GetIncoming(id)
+
+ r.ParseForm()
+
+ props := model.MapFromJson(strings.NewReader(r.FormValue("payload")))
+
+ text := props["text"]
+ if len(text) == 0 {
+ c.Err = model.NewAppError("incomingWebhook", "No text specified", "")
+ return
+ }
+
+ channelName := props["channel"]
+
+ var hook *model.IncomingWebhook
+ if result := <-hchan; result.Err != nil {
+ c.Err = model.NewAppError("incomingWebhook", "Invalid webhook", "err="+result.Err.Message)
+ return
+ } else {
+ hook = result.Data.(*model.IncomingWebhook)
+ }
+
+ var channel *model.Channel
+ var cchan store.StoreChannel
+
+ if len(channelName) != 0 {
+ if channelName[0] == '@' {
+ if result := <-api.Srv.Store.User().GetByUsername(hook.TeamId, channelName[1:]); result.Err != nil {
+ c.Err = model.NewAppError("incomingWebhook", "Couldn't find the user", "err="+result.Err.Message)
+ return
+ } else {
+ channelName = model.GetDMNameFromIds(result.Data.(*model.User).Id, hook.UserId)
+ }
+ } else if channelName[0] == '#' {
+ channelName = channelName[1:]
+ }
+
+ cchan = api.Srv.Store.Channel().GetByName(hook.TeamId, channelName)
+ } else {
+ cchan = api.Srv.Store.Channel().Get(hook.ChannelId)
+ }
+
+ // parse links into Markdown format
+ linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
+ text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
+
+ linkRegex := regexp.MustCompile(`<\s*(\S*)\s*>`)
+ text = linkRegex.ReplaceAllString(text, "${1}")
+
+ if result := <-cchan; result.Err != nil {
+ c.Err = model.NewAppError("incomingWebhook", "Couldn't find the channel", "err="+result.Err.Message)
+ return
+ } else {
+ channel = result.Data.(*model.Channel)
+ }
+
+ pchan := api.Srv.Store.Channel().CheckPermissionsTo(hook.TeamId, channel.Id, hook.UserId)
+
+ post := &model.Post{UserId: hook.UserId, ChannelId: channel.Id, Message: text}
+
+ if !c.HasPermissionsToChannel(pchan, "createIncomingHook") && channel.Type != model.CHANNEL_OPEN {
+ c.Err = model.NewAppError("incomingWebhook", "Inappropriate channel permissions", "")
+ return
+ }
+
+ // create a mock session
+ c.Session = model.Session{UserId: hook.UserId, TeamId: hook.TeamId, IsOAuth: false}
+
+ if _, err := api.CreatePost(c, post, false); err != nil {
+ c.Err = model.NewAppError("incomingWebhook", "Error creating post", "err="+err.Message)
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/plain")
+ w.Write([]byte("ok"))
+}