From af6e2c29eb0a8610fe218e8ec85e739433eac729 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 1 Oct 2015 14:07:20 -0400 Subject: Implement outgoing webhooks. --- api/post.go | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 131 insertions(+), 3 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 58fd3488a..3167395f1 100644 --- a/api/post.go +++ b/api/post.go @@ -55,11 +55,15 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) { return } else { + if result := <-Srv.Store.Channel().UpdateLastViewedAt(post.ChannelId, c.Session.UserId); result.Err != nil { + l4g.Error("Encountered error updating last viewed, channel_id=%s, user_id=%s, err=%v", post.ChannelId, c.Session.UserId, result.Err) + } + w.Write([]byte(rp.ToJson())) } } -func CreatePost(c *Context, post *model.Post, doUpdateLastViewed bool) (*model.Post, *model.AppError) { +func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) @@ -130,18 +134,142 @@ func CreatePost(c *Context, post *model.Post, doUpdateLastViewed bool) (*model.P var rpost *model.Post if result := <-Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err - } else if doUpdateLastViewed && (<-Srv.Store.Channel().UpdateLastViewedAt(post.ChannelId, c.Session.UserId)).Err != nil { - return nil, result.Err } else { rpost = result.Data.(*model.Post) fireAndForgetNotifications(rpost, c.Session.TeamId, c.GetSiteURL()) + if triggerWebhooks { + fireAndForgetWebhookEvent(c, rpost) + } + } return rpost, nil } +func fireAndForgetWebhookEvent(c *Context, post *model.Post) { + + go func() { + + chchan := Srv.Store.Webhook().GetOutgoingByChannel(post.ChannelId) + + firstWord := strings.Split(post.Message, " ")[0] + thchan := Srv.Store.Webhook().GetOutgoingByTriggerWord(c.Session.TeamId, post.ChannelId, firstWord) + + hooks := []*model.OutgoingWebhook{} + + if result := <-chchan; result.Err != nil { + l4g.Error("Encountered error getting webhook by channel, err=%v", result.Err) + return + } else { + hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) + } + + if result := <-thchan; result.Err != nil { + l4g.Error("Encountered error getting webhook by trigger word, err=%v", result.Err) + return + } else { + hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) + } + + cchan := Srv.Store.Channel().Get(post.ChannelId) + uchan := Srv.Store.User().Get(post.UserId) + + tchan := make(chan *model.Team) + isTeamBeingFetched := make(map[string]bool) + for _, hook := range hooks { + if !isTeamBeingFetched[hook.TeamId] { + isTeamBeingFetched[hook.TeamId] = true + go func() { + if result := <-Srv.Store.Team().Get(hook.TeamId); result.Err != nil { + l4g.Error("Encountered error getting team, team_id=%s, err=%v", hook.TeamId, result.Err) + tchan <- nil + } else { + tchan <- result.Data.(*model.Team) + } + }() + } + } + + teams := make(map[string]*model.Team) + for _, _ = range isTeamBeingFetched { + team := <-tchan + if team != nil { + teams[team.Id] = team + } + } + + var channel *model.Channel + if result := <-cchan; result.Err != nil { + l4g.Error("Encountered error getting channel, channel_id=%s, err=%v", post.ChannelId, result.Err) + } else { + channel = result.Data.(*model.Channel) + } + + var user *model.User + if result := <-uchan; result.Err != nil { + l4g.Error("Encountered error getting user, user_id=%s, err=%v", post.UserId, result.Err) + } else { + user = result.Data.(*model.User) + } + + for _, hook := range hooks { + go func() { + p := url.Values{} + p.Set("token", hook.Token) + p.Set("team_id", hook.TeamId) + + if team, ok := teams[hook.TeamId]; ok { + p.Set("team_domain", team.Name) + } + + p.Set("channel_id", post.ChannelId) + if channel != nil { + p.Set("channel_name", channel.Name) + } + + p.Set("timestamp", strconv.FormatInt(post.CreateAt/1000, 10)) + + p.Set("user_id", post.UserId) + if user != nil { + p.Set("user_name", user.Username) + } + + p.Set("text", post.Message) + if len(hook.TriggerWords) > 0 { + p.Set("trigger_word", firstWord) + } + + client := &http.Client{} + + for _, url := range hook.CallbackURLs { + go func() { + req, _ := http.NewRequest("POST", url, strings.NewReader(p.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Accept", "application/json") + if resp, err := client.Do(req); err != nil { + l4g.Error("Event POST failed, err=%s", err.Error()) + } else { + respProps := model.MapFromJson(resp.Body) + + if text, ok := respProps["text"]; ok { + respPost := &model.Post{Message: text, ChannelId: post.ChannelId} + if _, err := CreatePost(c, respPost, false); err != nil { + l4g.Error("Failed to create response post, err=%v", err) + } + } + } + }() + } + + }() + } + + }() + +} + func fireAndForgetNotifications(post *model.Post, teamId, siteURL string) { go func() { -- cgit v1.2.3-1-g7c22 From 5bb22ed4541cf0e5ea246ebdc89243239afa66ea Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 1 Oct 2015 15:35:39 -0400 Subject: Fix minor style for gofmt. --- api/post.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 3167395f1..8770fde38 100644 --- a/api/post.go +++ b/api/post.go @@ -193,7 +193,7 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { } teams := make(map[string]*model.Team) - for _, _ = range isTeamBeingFetched { + for range isTeamBeingFetched { team := <-tchan if team != nil { teams[team.Id] = team -- cgit v1.2.3-1-g7c22 From 9de8bc4727132f8ec5d67f9d3e8a9642774c296c Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Mon, 5 Oct 2015 13:04:45 -0400 Subject: Remove special team logic. --- api/post.go | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 8770fde38..98c394727 100644 --- a/api/post.go +++ b/api/post.go @@ -155,7 +155,11 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { chchan := Srv.Store.Webhook().GetOutgoingByChannel(post.ChannelId) firstWord := strings.Split(post.Message, " ")[0] - thchan := Srv.Store.Webhook().GetOutgoingByTriggerWord(c.Session.TeamId, post.ChannelId, firstWord) + + var thchan store.StoreChannel + if len(firstWord) != 0 { + thchan = Srv.Store.Webhook().GetOutgoingByTriggerWord(c.Session.TeamId, post.ChannelId, firstWord) + } hooks := []*model.OutgoingWebhook{} @@ -166,38 +170,24 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) } - if result := <-thchan; result.Err != nil { - l4g.Error("Encountered error getting webhook by trigger word, err=%v", result.Err) - return - } else { - hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) + if thchan != nil { + if result := <-thchan; result.Err != nil { + l4g.Error("Encountered error getting webhook by trigger word, err=%v", result.Err) + return + } else { + hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) + } } cchan := Srv.Store.Channel().Get(post.ChannelId) uchan := Srv.Store.User().Get(post.UserId) + tchan := Srv.Store.Team().Get(c.Session.TeamId) - tchan := make(chan *model.Team) - isTeamBeingFetched := make(map[string]bool) - for _, hook := range hooks { - if !isTeamBeingFetched[hook.TeamId] { - isTeamBeingFetched[hook.TeamId] = true - go func() { - if result := <-Srv.Store.Team().Get(hook.TeamId); result.Err != nil { - l4g.Error("Encountered error getting team, team_id=%s, err=%v", hook.TeamId, result.Err) - tchan <- nil - } else { - tchan <- result.Data.(*model.Team) - } - }() - } - } - - teams := make(map[string]*model.Team) - for range isTeamBeingFetched { - team := <-tchan - if team != nil { - teams[team.Id] = team - } + var team *model.Team + if result := <-tchan; result.Err != nil { + l4g.Error("Encountered error getting team, team_id=%s, err=%v", c.Session.TeamId, result.Err) + } else { + team = result.Data.(*model.Team) } var channel *model.Channel @@ -220,7 +210,7 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { p.Set("token", hook.Token) p.Set("team_id", hook.TeamId) - if team, ok := teams[hook.TeamId]; ok { + if team != nil { p.Set("team_domain", team.Name) } -- cgit v1.2.3-1-g7c22 From f24ea30a75ad52b695f5f275139af1c0495624ac Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Fri, 16 Oct 2015 11:17:24 -0400 Subject: Refactor to hit database less often. --- api/post.go | 146 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 65 insertions(+), 81 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 98c394727..e4e67dc66 100644 --- a/api/post.go +++ b/api/post.go @@ -137,55 +137,23 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post } else { rpost = result.Data.(*model.Post) - fireAndForgetNotifications(rpost, c.Session.TeamId, c.GetSiteURL()) - - if triggerWebhooks { - fireAndForgetWebhookEvent(c, rpost) - } + handlePostEventsAndForget(c, rpost, triggerWebhooks) } return rpost, nil } -func fireAndForgetWebhookEvent(c *Context, post *model.Post) { - +func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks bool) { go func() { - - chchan := Srv.Store.Webhook().GetOutgoingByChannel(post.ChannelId) - - firstWord := strings.Split(post.Message, " ")[0] - - var thchan store.StoreChannel - if len(firstWord) != 0 { - thchan = Srv.Store.Webhook().GetOutgoingByTriggerWord(c.Session.TeamId, post.ChannelId, firstWord) - } - - hooks := []*model.OutgoingWebhook{} - - if result := <-chchan; result.Err != nil { - l4g.Error("Encountered error getting webhook by channel, err=%v", result.Err) - return - } else { - hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) - } - - if thchan != nil { - if result := <-thchan; result.Err != nil { - l4g.Error("Encountered error getting webhook by trigger word, err=%v", result.Err) - return - } else { - hooks = append(hooks, result.Data.([]*model.OutgoingWebhook)...) - } - } - + tchan := Srv.Store.Team().Get(c.Session.TeamId) cchan := Srv.Store.Channel().Get(post.ChannelId) uchan := Srv.Store.User().Get(post.UserId) - tchan := Srv.Store.Team().Get(c.Session.TeamId) var team *model.Team if result := <-tchan; result.Err != nil { l4g.Error("Encountered error getting team, team_id=%s, err=%v", c.Session.TeamId, result.Err) + return } else { team = result.Data.(*model.Team) } @@ -193,43 +161,76 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { var channel *model.Channel if result := <-cchan; result.Err != nil { l4g.Error("Encountered error getting channel, channel_id=%s, err=%v", post.ChannelId, result.Err) + return } else { channel = result.Data.(*model.Channel) } + fireAndForgetNotifications(c, post, team, channel) + var user *model.User if result := <-uchan; result.Err != nil { l4g.Error("Encountered error getting user, user_id=%s, err=%v", post.UserId, result.Err) + return } else { user = result.Data.(*model.User) } + if triggerWebhooks { + handleWebhookEventsAndForget(c, post, team, channel, user) + } + }() +} + +func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) { + go func() { + hchan := Srv.Store.Webhook().GetOutgoingByTeam(c.Session.TeamId) + + hooks := []*model.OutgoingWebhook{} + + if result := <-hchan; result.Err != nil { + l4g.Error("Encountered error getting webhooks by team, err=%v", result.Err) + return + } else { + hooks = result.Data.([]*model.OutgoingWebhook) + } + + if len(hooks) == 0 { + return + } + + firstWord := strings.Split(post.Message, " ")[0] + + relevantHooks := []*model.OutgoingWebhook{} + for _, hook := range hooks { + if hook.ChannelId == post.ChannelId { + if len(hook.TriggerWords) == 0 || hook.HasTriggerWord(firstWord) { + relevantHooks = append(relevantHooks, hook) + } + } else if len(hook.ChannelId) == 0 && hook.HasTriggerWord(firstWord) { + relevantHooks = append(relevantHooks, hook) + } + } + + for _, hook := range relevantHooks { go func() { p := url.Values{} p.Set("token", hook.Token) - p.Set("team_id", hook.TeamId) - if team != nil { - p.Set("team_domain", team.Name) - } + p.Set("team_id", hook.TeamId) + p.Set("team_domain", team.Name) p.Set("channel_id", post.ChannelId) - if channel != nil { - p.Set("channel_name", channel.Name) - } + p.Set("channel_name", channel.Name) p.Set("timestamp", strconv.FormatInt(post.CreateAt/1000, 10)) p.Set("user_id", post.UserId) - if user != nil { - p.Set("user_name", user.Username) - } + p.Set("user_name", user.Username) p.Set("text", post.Message) - if len(hook.TriggerWords) > 0 { - p.Set("trigger_word", firstWord) - } + p.Set("trigger_word", firstWord) client := &http.Client{} @@ -260,38 +261,29 @@ func fireAndForgetWebhookEvent(c *Context, post *model.Post) { } -func fireAndForgetNotifications(post *model.Post, teamId, siteURL string) { +func fireAndForgetNotifications(c *Context, post *model.Post, team *model.Team, channel *model.Channel) { go func() { // Get a list of user names (to be used as keywords) and ids for the given team - uchan := Srv.Store.User().GetProfiles(teamId) + uchan := Srv.Store.User().GetProfiles(c.Session.TeamId) echan := Srv.Store.Channel().GetMembers(post.ChannelId) - cchan := Srv.Store.Channel().Get(post.ChannelId) - tchan := Srv.Store.Team().Get(teamId) - var channel *model.Channel var channelName string var bodyText string var subjectText string - if result := <-cchan; result.Err != nil { - l4g.Error("Failed to retrieve channel channel_id=%v, err=%v", post.ChannelId, result.Err) - return + if channel.Type == model.CHANNEL_DIRECT { + bodyText = "You have one new message." + subjectText = "New Direct Message" } else { - channel = result.Data.(*model.Channel) - if channel.Type == model.CHANNEL_DIRECT { - bodyText = "You have one new message." - subjectText = "New Direct Message" - } else { - bodyText = "You have one new mention." - subjectText = "New Mention" - channelName = channel.DisplayName - } + bodyText = "You have one new mention." + subjectText = "New Mention" + channelName = channel.DisplayName } var mentionedUsers []string if result := <-uchan; result.Err != nil { - l4g.Error("Failed to retrieve user profiles team_id=%v, err=%v", teamId, result.Err) + l4g.Error("Failed to retrieve user profiles team_id=%v, err=%v", c.Session.TeamId, result.Err) return } else { profileMap := result.Data.(map[string]*model.User) @@ -414,23 +406,15 @@ func fireAndForgetNotifications(post *model.Post, teamId, siteURL string) { mentionedUsers = append(mentionedUsers, k) } - var teamDisplayName string - var teamURL string - if result := <-tchan; result.Err != nil { - l4g.Error("Failed to retrieve team team_id=%v, err=%v", teamId, result.Err) - return - } else { - teamDisplayName = result.Data.(*model.Team).DisplayName - teamURL = siteURL + "/" + result.Data.(*model.Team).Name - } + teamURL := c.GetSiteURL() + "/" + team.Name // Build and send the emails location, _ := time.LoadLocation("UTC") tm := time.Unix(post.CreateAt/1000, 0).In(location) subjectPage := NewServerTemplatePage("post_subject") - subjectPage.Props["SiteURL"] = siteURL - subjectPage.Props["TeamDisplayName"] = teamDisplayName + subjectPage.Props["SiteURL"] = c.GetSiteURL() + subjectPage.Props["TeamDisplayName"] = team.DisplayName subjectPage.Props["SubjectText"] = subjectText subjectPage.Props["Month"] = tm.Month().String()[:3] subjectPage.Props["Day"] = fmt.Sprintf("%d", tm.Day()) @@ -448,9 +432,9 @@ func fireAndForgetNotifications(post *model.Post, teamId, siteURL string) { } bodyPage := NewServerTemplatePage("post_body") - bodyPage.Props["SiteURL"] = siteURL + bodyPage.Props["SiteURL"] = c.GetSiteURL() bodyPage.Props["Nickname"] = profileMap[id].FirstName - bodyPage.Props["TeamDisplayName"] = teamDisplayName + bodyPage.Props["TeamDisplayName"] = team.DisplayName bodyPage.Props["ChannelName"] = channelName bodyPage.Props["BodyText"] = bodyText bodyPage.Props["SenderName"] = senderName @@ -517,7 +501,7 @@ func fireAndForgetNotifications(post *model.Post, teamId, siteURL string) { } } - message := model.NewMessage(teamId, post.ChannelId, post.UserId, model.ACTION_POSTED) + message := model.NewMessage(c.Session.TeamId, post.ChannelId, post.UserId, model.ACTION_POSTED) message.Add("post", post.ToJson()) if len(post.Filenames) != 0 { -- cgit v1.2.3-1-g7c22 From a3ca10674056d4dfcfec5cfb404f3488f8bc4090 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Mon, 19 Oct 2015 10:19:48 -0400 Subject: Generalized webhook post code and added it to outgoing webhooks --- api/post.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index e4e67dc66..73a63cb72 100644 --- a/api/post.go +++ b/api/post.go @@ -13,6 +13,7 @@ import ( "net/http" "net/url" "path/filepath" + "regexp" "strconv" "strings" "time" @@ -144,6 +145,40 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post return rpost, nil } +func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIconUrl string) (*model.Post, *model.AppError) { + // 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}") + + post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text} + post.AddProp("from_webhook", "true") + + if utils.Cfg.ServiceSettings.EnablePostUsernameOverride { + if len(overrideUsername) != 0 { + post.AddProp("override_username", overrideUsername) + } else { + post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME) + } + } + + if utils.Cfg.ServiceSettings.EnablePostIconOverride { + if len(overrideIconUrl) != 0 { + post.AddProp("override_icon_url", overrideIconUrl) + } else { + post.AddProp("override_icon_url", model.DEFAULT_WEBHOOK_ICON) + } + } + + if _, err := CreatePost(c, post, false); err != nil { + return nil, model.NewAppError("CreateWebhookPost", "Error creating post", "err="+err.Message) + } + + return post, nil +} + func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks bool) { go func() { tchan := Srv.Store.Team().Get(c.Session.TeamId) @@ -244,9 +279,12 @@ func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team } else { respProps := model.MapFromJson(resp.Body) + // copy the context and create a mock session for posting the message + mockSession := model.Session{UserId: hook.CreatorId, TeamId: hook.TeamId, IsOAuth: false} + newContext := &Context{mockSession, model.NewId(), "", c.Path, nil, c.teamURLValid, c.teamURL, c.siteURL} + if text, ok := respProps["text"]; ok { - respPost := &model.Post{Message: text, ChannelId: post.ChannelId} - if _, err := CreatePost(c, respPost, false); err != nil { + if _, err := CreateWebhookPost(newContext, post.ChannelId, text, respProps["username"], respProps["icon_url"]); err != nil { l4g.Error("Failed to create response post, err=%v", err) } } -- cgit v1.2.3-1-g7c22