From 21318b7505babaebbb5174c62b75595b2936ebe0 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Thu, 4 Feb 2016 11:45:19 -0500 Subject: Changed sendNotificationsAndForget to be synchronous and called as a go func --- api/post.go | 453 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 225 insertions(+), 228 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index a2018bdbd..c17da262f 100644 --- a/api/post.go +++ b/api/post.go @@ -267,7 +267,7 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo members = result.Data.([]model.ChannelMember) } - sendNotificationsAndForget(c, post, team, channel, profiles, members) + go sendNotifications(c, post, team, channel, profiles, members) go checkForOutOfChannelMentions(c, post, channel, profiles, members) var user *model.User @@ -433,293 +433,290 @@ func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team } -func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel, profileMap map[string]*model.User, members []model.ChannelMember) { +func sendNotifications(c *Context, post *model.Post, team *model.Team, channel *model.Channel, profileMap map[string]*model.User, members []model.ChannelMember) { + var channelName string + var bodyText string + var subjectText string - go func() { - var channelName string - var bodyText string - var subjectText string - - var mentionedUsers []string - - if _, ok := profileMap[post.UserId]; !ok { - l4g.Error(utils.T("api.post.send_notifications_and_forget.user_id.error"), post.UserId) - return - } - senderName := profileMap[post.UserId].Username - - toEmailMap := make(map[string]bool) + var mentionedUsers []string - if channel.Type == model.CHANNEL_DIRECT { + if _, ok := profileMap[post.UserId]; !ok { + l4g.Error(utils.T("api.post.send_notifications_and_forget.user_id.error"), post.UserId) + return + } + senderName := profileMap[post.UserId].Username - var otherUserId string - if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId { - otherUserId = userIds[1] - channelName = profileMap[userIds[1]].Username - } else { - otherUserId = userIds[0] - channelName = profileMap[userIds[0]].Username - } + toEmailMap := make(map[string]bool) - otherUser := profileMap[otherUserId] - sendEmail := true - if _, ok := otherUser.NotifyProps["email"]; ok && otherUser.NotifyProps["email"] == "false" { - sendEmail = false - } - if sendEmail && (otherUser.IsOffline() || otherUser.IsAway()) { - toEmailMap[otherUserId] = true - } + if channel.Type == model.CHANNEL_DIRECT { + var otherUserId string + if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId { + otherUserId = userIds[1] + channelName = profileMap[userIds[1]].Username } else { - // Find out who is a member of the channel, only keep those profiles - tempProfileMap := make(map[string]*model.User) - for _, member := range members { - tempProfileMap[member.UserId] = profileMap[member.UserId] - } - - profileMap = tempProfileMap + otherUserId = userIds[0] + channelName = profileMap[userIds[0]].Username + } - // Build map for keywords - keywordMap := make(map[string][]string) - for _, profile := range profileMap { - if len(profile.NotifyProps["mention_keys"]) > 0 { + otherUser := profileMap[otherUserId] + sendEmail := true + if _, ok := otherUser.NotifyProps["email"]; ok && otherUser.NotifyProps["email"] == "false" { + sendEmail = false + } + if sendEmail && (otherUser.IsOffline() || otherUser.IsAway()) { + toEmailMap[otherUserId] = true + } - // Add all the user's mention keys - splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",") - for _, k := range splitKeys { - keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id) - } - } + } else { + // Find out who is a member of the channel, only keep those profiles + tempProfileMap := make(map[string]*model.User) + for _, member := range members { + tempProfileMap[member.UserId] = profileMap[member.UserId] + } - // If turned on, add the user's case sensitive first name - if profile.NotifyProps["first_name"] == "true" { - keywordMap[profile.FirstName] = append(keywordMap[profile.FirstName], profile.Id) - } + profileMap = tempProfileMap - // Add @all to keywords if user has them turned on - // if profile.NotifyProps["all"] == "true" { - // keywordMap["@all"] = append(keywordMap["@all"], profile.Id) - // } + // Build map for keywords + keywordMap := make(map[string][]string) + for _, profile := range profileMap { + if len(profile.NotifyProps["mention_keys"]) > 0 { - // Add @channel to keywords if user has them turned on - if profile.NotifyProps["channel"] == "true" { - keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id) + // Add all the user's mention keys + splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",") + for _, k := range splitKeys { + keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id) } } - // Build a map as a list of unique user_ids that are mentioned in this post - splitF := func(c rune) bool { - return model.SplitRunes[c] + // If turned on, add the user's case sensitive first name + if profile.NotifyProps["first_name"] == "true" { + keywordMap[profile.FirstName] = append(keywordMap[profile.FirstName], profile.Id) } - splitMessage := strings.Fields(post.Message) - for _, word := range splitMessage { - var userIds []string - // Non-case-sensitive check for regular keys - if ids, match := keywordMap[strings.ToLower(word)]; match { - userIds = append(userIds, ids...) - } + // Add @all to keywords if user has them turned on + // if profile.NotifyProps["all"] == "true" { + // keywordMap["@all"] = append(keywordMap["@all"], profile.Id) + // } - // Case-sensitive check for first name - if ids, match := keywordMap[word]; match { - userIds = append(userIds, ids...) - } + // Add @channel to keywords if user has them turned on + if profile.NotifyProps["channel"] == "true" { + keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id) + } + } - if len(userIds) == 0 { - // No matches were found with the string split just on whitespace so try further splitting - // the message on punctuation - splitWords := strings.FieldsFunc(word, splitF) + // Build a map as a list of unique user_ids that are mentioned in this post + splitF := func(c rune) bool { + return model.SplitRunes[c] + } + splitMessage := strings.Fields(post.Message) + for _, word := range splitMessage { + var userIds []string - for _, splitWord := range splitWords { - // Non-case-sensitive check for regular keys - if ids, match := keywordMap[strings.ToLower(splitWord)]; match { - userIds = append(userIds, ids...) - } + // Non-case-sensitive check for regular keys + if ids, match := keywordMap[strings.ToLower(word)]; match { + userIds = append(userIds, ids...) + } - // Case-sensitive check for first name - if ids, match := keywordMap[splitWord]; match { - userIds = append(userIds, ids...) - } - } - } + // Case-sensitive check for first name + if ids, match := keywordMap[word]; match { + userIds = append(userIds, ids...) + } - for _, userId := range userIds { - if post.UserId == userId { - continue - } - sendEmail := true - if _, ok := profileMap[userId].NotifyProps["email"]; ok && profileMap[userId].NotifyProps["email"] == "false" { - sendEmail = false + if len(userIds) == 0 { + // No matches were found with the string split just on whitespace so try further splitting + // the message on punctuation + splitWords := strings.FieldsFunc(word, splitF) + + for _, splitWord := range splitWords { + // Non-case-sensitive check for regular keys + if ids, match := keywordMap[strings.ToLower(splitWord)]; match { + userIds = append(userIds, ids...) } - if sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) { - toEmailMap[userId] = true - } else { - toEmailMap[userId] = false + + // Case-sensitive check for first name + if ids, match := keywordMap[splitWord]; match { + userIds = append(userIds, ids...) } } } - for id := range toEmailMap { - updateMentionCountAndForget(post.ChannelId, id) + for _, userId := range userIds { + if post.UserId == userId { + continue + } + sendEmail := true + if _, ok := profileMap[userId].NotifyProps["email"]; ok && profileMap[userId].NotifyProps["email"] == "false" { + sendEmail = false + } + if sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) { + toEmailMap[userId] = true + } else { + toEmailMap[userId] = false + } } } - if len(toEmailMap) != 0 { - mentionedUsers = make([]string, 0, len(toEmailMap)) - for k := range toEmailMap { - mentionedUsers = append(mentionedUsers, k) - } + for id := range toEmailMap { + updateMentionCountAndForget(post.ChannelId, id) + } + } - teamURL := c.GetSiteURL() + "/" + team.Name + if len(toEmailMap) != 0 { + mentionedUsers = make([]string, 0, len(toEmailMap)) + for k := range toEmailMap { + mentionedUsers = append(mentionedUsers, k) + } - // Build and send the emails - tm := time.Unix(post.CreateAt/1000, 0) + teamURL := c.GetSiteURL() + "/" + team.Name - for id, doSend := range toEmailMap { + // Build and send the emails + tm := time.Unix(post.CreateAt/1000, 0) - if !doSend { - continue - } + for id, doSend := range toEmailMap { - // skip if inactive - if profileMap[id].DeleteAt > 0 { - continue - } - - userLocale := utils.GetUserTranslations(profileMap[id].Locale) + if !doSend { + continue + } - if channel.Type == model.CHANNEL_DIRECT { - bodyText = userLocale("api.post.send_notifications_and_forget.message_body") - subjectText = userLocale("api.post.send_notifications_and_forget.message_subject") - } else { - bodyText = userLocale("api.post.send_notifications_and_forget.mention_body") - subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject") - channelName = channel.DisplayName - } + // skip if inactive + if profileMap[id].DeleteAt > 0 { + continue + } - month := userLocale(tm.Month().String()) - day := fmt.Sprintf("%d", tm.Day()) - year := fmt.Sprintf("%d", tm.Year()) - zone, _ := tm.Zone() - - subjectPage := NewServerTemplatePage("post_subject", c.Locale) - subjectPage.Props["Subject"] = userLocale("api.templates.post_subject", - map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName, - "Month": month[:3], "Day": day, "Year": year}) - - bodyPage := NewServerTemplatePage("post_body", c.Locale) - bodyPage.Props["SiteURL"] = c.GetSiteURL() - bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message) - bodyPage.Props["TeamLink"] = teamURL + "/channels/" + channel.Name - bodyPage.Props["BodyText"] = bodyText - bodyPage.Props["Button"] = userLocale("api.templates.post_body.button") - bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info", - map[string]interface{}{"ChannelName": channelName, "SenderName": senderName, - "Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()), - "TimeZone": zone, "Month": month, "Day": day})) - - // attempt to fill in a message body if the post doesn't have any text - if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 { - // extract the filenames from their paths and determine what type of files are attached - filenames := make([]string, len(post.Filenames)) - onlyImages := true - for i, filename := range post.Filenames { - var err error - if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil { - // this should never error since filepath was escaped using url.QueryEscape - filenames[i] = filepath.Base(filename) - } + userLocale := utils.GetUserTranslations(profileMap[id].Locale) - ext := filepath.Ext(filename) - onlyImages = onlyImages && model.IsFileExtImage(ext) - } - filenamesString := strings.Join(filenames, ", ") + if channel.Type == model.CHANNEL_DIRECT { + bodyText = userLocale("api.post.send_notifications_and_forget.message_body") + subjectText = userLocale("api.post.send_notifications_and_forget.message_subject") + } else { + bodyText = userLocale("api.post.send_notifications_and_forget.mention_body") + subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject") + channelName = channel.DisplayName + } - var attachmentPrefix string - if onlyImages { - attachmentPrefix = "Image" - } else { - attachmentPrefix = "File" - } - if len(post.Filenames) > 1 { - attachmentPrefix += "s" + month := userLocale(tm.Month().String()) + day := fmt.Sprintf("%d", tm.Day()) + year := fmt.Sprintf("%d", tm.Year()) + zone, _ := tm.Zone() + + subjectPage := NewServerTemplatePage("post_subject", c.Locale) + subjectPage.Props["Subject"] = userLocale("api.templates.post_subject", + map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName, + "Month": month[:3], "Day": day, "Year": year}) + + bodyPage := NewServerTemplatePage("post_body", c.Locale) + bodyPage.Props["SiteURL"] = c.GetSiteURL() + bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message) + bodyPage.Props["TeamLink"] = teamURL + "/channels/" + channel.Name + bodyPage.Props["BodyText"] = bodyText + bodyPage.Props["Button"] = userLocale("api.templates.post_body.button") + bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info", + map[string]interface{}{"ChannelName": channelName, "SenderName": senderName, + "Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()), + "TimeZone": zone, "Month": month, "Day": day})) + + // attempt to fill in a message body if the post doesn't have any text + if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 { + // extract the filenames from their paths and determine what type of files are attached + filenames := make([]string, len(post.Filenames)) + onlyImages := true + for i, filename := range post.Filenames { + var err error + if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil { + // this should never error since filepath was escaped using url.QueryEscape + filenames[i] = filepath.Base(filename) } - bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent", - map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString}) + ext := filepath.Ext(filename) + onlyImages = onlyImages && model.IsFileExtImage(ext) } + filenamesString := strings.Join(filenames, ", ") - if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil { - l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err) + var attachmentPrefix string + if onlyImages { + attachmentPrefix = "Image" + } else { + attachmentPrefix = "File" + } + if len(post.Filenames) > 1 { + attachmentPrefix += "s" } - if *utils.Cfg.EmailSettings.SendPushNotifications { - sessionChan := Srv.Store.Session().GetSessions(id) - if result := <-sessionChan; result.Err != nil { - l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), id, result.Err) - } else { - sessions := result.Data.([]*model.Session) - alreadySeen := make(map[string]string) - - for _, session := range sessions { - if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" && - (strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) { - alreadySeen[session.DeviceId] = session.DeviceId - - msg := model.PushNotification{} - msg.Badge = 1 - msg.ServerId = utils.CfgDiagnosticId - - if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") { - msg.Platform = model.PUSH_NOTIFY_APPLE - msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") - } else if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") { - msg.Platform = model.PUSH_NOTIFY_ANDROID - msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") - } + bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent", + map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString}) + } - if channel.Type == model.CHANNEL_DIRECT { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") - } else { - msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName - } + if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil { + l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err) + } - httpClient := http.Client{} - request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson())) + if *utils.Cfg.EmailSettings.SendPushNotifications { + sessionChan := Srv.Store.Session().GetSessions(id) + if result := <-sessionChan; result.Err != nil { + l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), id, result.Err) + } else { + sessions := result.Data.([]*model.Session) + alreadySeen := make(map[string]string) + + for _, session := range sessions { + if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" && + (strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) { + alreadySeen[session.DeviceId] = session.DeviceId + + msg := model.PushNotification{} + msg.Badge = 1 + msg.ServerId = utils.CfgDiagnosticId + + if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") { + msg.Platform = model.PUSH_NOTIFY_APPLE + msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") + } else if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") { + msg.Platform = model.PUSH_NOTIFY_ANDROID + msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") + } - l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message) - if _, err := httpClient.Do(request); err != nil { - l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), id, err) - } + if channel.Type == model.CHANNEL_DIRECT { + msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") + } else { + msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName + } + + httpClient := http.Client{} + request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson())) + + l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message) + if _, err := httpClient.Do(request); err != nil { + l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), id, err) } } } } } } + } - message := model.NewMessage(c.Session.TeamId, post.ChannelId, post.UserId, model.ACTION_POSTED) - message.Add("post", post.ToJson()) - message.Add("channel_type", channel.Type) + message := model.NewMessage(c.Session.TeamId, post.ChannelId, post.UserId, model.ACTION_POSTED) + message.Add("post", post.ToJson()) + message.Add("channel_type", channel.Type) - if len(post.Filenames) != 0 { - message.Add("otherFile", "true") + if len(post.Filenames) != 0 { + message.Add("otherFile", "true") - for _, filename := range post.Filenames { - ext := filepath.Ext(filename) - if model.IsFileExtImage(ext) { - message.Add("image", "true") - break - } + for _, filename := range post.Filenames { + ext := filepath.Ext(filename) + if model.IsFileExtImage(ext) { + message.Add("image", "true") + break } } + } - if len(mentionedUsers) != 0 { - message.Add("mentions", model.ArrayToJson(mentionedUsers)) - } + if len(mentionedUsers) != 0 { + message.Add("mentions", model.ArrayToJson(mentionedUsers)) + } - PublishAndForget(message) - }() + PublishAndForget(message) } func updateMentionCountAndForget(channelId, userId string) { -- cgit v1.2.3-1-g7c22