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