Reworked the code for sending notifications and checking for out of channel mentions to share some data

Этот коммит содержится в:
hmhealey
2016-02-03 10:56:47 -05:00
родитель fd123a6e4a
Коммит 994358c31a
4 изменённых файлов: 280 добавлений и 282 удалений

Просмотреть файл

@@ -232,6 +232,8 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
tchan := Srv.Store.Team().Get(c.Session.TeamId) tchan := Srv.Store.Team().Get(c.Session.TeamId)
cchan := Srv.Store.Channel().Get(post.ChannelId) cchan := Srv.Store.Channel().Get(post.ChannelId)
uchan := Srv.Store.User().Get(post.UserId) uchan := Srv.Store.User().Get(post.UserId)
pchan := Srv.Store.User().GetProfiles(c.Session.TeamId)
mchan := Srv.Store.Channel().GetMembers(post.ChannelId)
var team *model.Team var team *model.Team
if result := <-tchan; result.Err != nil { if result := <-tchan; result.Err != nil {
@@ -249,8 +251,24 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
channel = result.Data.(*model.Channel) channel = result.Data.(*model.Channel)
} }
sendNotificationsAndForget(c, post, team, channel) var profiles map[string]*model.User
go checkForOutOfChannelMentions(c, post, channel) if result := <-pchan; result.Err != nil {
l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), c.Session.TeamId, result.Err)
return
} else {
profiles = result.Data.(map[string]*model.User)
}
var members []model.ChannelMember
if result := <-mchan; result.Err != nil {
l4g.Error(utils.T("api.post.handle_post_events_and_forget.members.error"), post.ChannelId, result.Err)
return
} else {
members = result.Data.([]model.ChannelMember)
}
sendNotificationsAndForget(c, post, team, channel, profiles, members)
go checkForOutOfChannelMentions(c, post, channel, profiles, members)
var user *model.User var user *model.User
if result := <-uchan; result.Err != nil { if result := <-uchan; result.Err != nil {
@@ -415,281 +433,263 @@ func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team
} }
func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel) { func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel, profileMap map[string]*model.User, members []model.ChannelMember) {
go func() { go func() {
// Get a list of user names (to be used as keywords) and ids for the given team
uchan := Srv.Store.User().GetProfiles(c.Session.TeamId)
echan := Srv.Store.Channel().GetMembers(post.ChannelId)
var channelName string var channelName string
var bodyText string var bodyText string
var subjectText string var subjectText string
var mentionedUsers []string var mentionedUsers []string
if result := <-uchan; result.Err != nil { if _, ok := profileMap[post.UserId]; !ok {
l4g.Error(utils.T("api.post.send_notifications_and_forget.retrive_profiles.error"), c.Session.TeamId, result.Err) l4g.Error(utils.T("api.post.send_notifications_and_forget.user_id.error"), post.UserId)
return return
} else { }
profileMap := result.Data.(map[string]*model.User) senderName := profileMap[post.UserId].Username
if _, ok := profileMap[post.UserId]; !ok { toEmailMap := make(map[string]bool)
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) if channel.Type == model.CHANNEL_DIRECT {
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 {
otherUserId = userIds[0]
channelName = profileMap[userIds[0]].Username
}
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
}
var otherUserId string
if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId {
otherUserId = userIds[1]
channelName = profileMap[userIds[1]].Username
} else { } else {
otherUserId = userIds[0]
channelName = profileMap[userIds[0]].Username
}
// Find out who is a member of the channel, only keep those profiles otherUser := profileMap[otherUserId]
if eResult := <-echan; eResult.Err != nil { sendEmail := true
l4g.Error(utils.T("api.post.send_notifications_and_forget.members.error"), post.ChannelId, eResult.Err.Message) if _, ok := otherUser.NotifyProps["email"]; ok && otherUser.NotifyProps["email"] == "false" {
return sendEmail = false
} else { }
tempProfileMap := make(map[string]*model.User) if sendEmail && (otherUser.IsOffline() || otherUser.IsAway()) {
members := eResult.Data.([]model.ChannelMember) toEmailMap[otherUserId] = true
for _, member := range members { }
tempProfileMap[member.UserId] = profileMap[member.UserId]
}
profileMap = tempProfileMap } 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]
}
// Build map for keywords profileMap = tempProfileMap
keywordMap := make(map[string][]string)
for _, profile := range profileMap {
if len(profile.NotifyProps["mention_keys"]) > 0 {
// Add all the user's mention keys // Build map for keywords
splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",") keywordMap := make(map[string][]string)
for _, k := range splitKeys { for _, profile := range profileMap {
keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id) if len(profile.NotifyProps["mention_keys"]) > 0 {
}
}
// If turned on, add the user's case sensitive first name // Add all the user's mention keys
if profile.NotifyProps["first_name"] == "true" { splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",")
keywordMap[profile.FirstName] = append(keywordMap[profile.FirstName], profile.Id) for _, k := range splitKeys {
} keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id)
// Add @all to keywords if user has them turned on
// if profile.NotifyProps["all"] == "true" {
// keywordMap["@all"] = append(keywordMap["@all"], profile.Id)
// }
// Add @channel to keywords if user has them turned on
if profile.NotifyProps["channel"] == "true" {
keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id)
} }
} }
// Build a map as a list of unique user_ids that are mentioned in this post // If turned on, add the user's case sensitive first name
splitF := func(c rune) bool { if profile.NotifyProps["first_name"] == "true" {
return model.SplitRunes[c] 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...)
}
// Case-sensitive check for first name
if ids, match := keywordMap[word]; match {
userIds = append(userIds, ids...)
}
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...)
}
// Case-sensitive check for first name
if ids, match := keywordMap[splitWord]; 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 sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) {
toEmailMap[userId] = true
} else {
toEmailMap[userId] = false
}
}
} }
for id := range toEmailMap { // Add @all to keywords if user has them turned on
updateMentionCountAndForget(post.ChannelId, id) // if profile.NotifyProps["all"] == "true" {
// keywordMap["@all"] = append(keywordMap["@all"], profile.Id)
// }
// Add @channel to keywords if user has them turned on
if profile.NotifyProps["channel"] == "true" {
keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id)
} }
} }
if len(toEmailMap) != 0 { // Build a map as a list of unique user_ids that are mentioned in this post
mentionedUsers = make([]string, 0, len(toEmailMap)) splitF := func(c rune) bool {
for k := range toEmailMap { return model.SplitRunes[c]
mentionedUsers = append(mentionedUsers, k) }
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...)
} }
teamURL := c.GetSiteURL() + "/" + team.Name // Case-sensitive check for first name
if ids, match := keywordMap[word]; match {
userIds = append(userIds, ids...)
}
// Build and send the emails if len(userIds) == 0 {
tm := time.Unix(post.CreateAt/1000, 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 id, doSend := range toEmailMap { for _, splitWord := range splitWords {
// Non-case-sensitive check for regular keys
if ids, match := keywordMap[strings.ToLower(splitWord)]; match {
userIds = append(userIds, ids...)
}
if !doSend { // Case-sensitive check for first name
if ids, match := keywordMap[splitWord]; match {
userIds = append(userIds, ids...)
}
}
}
for _, userId := range userIds {
if post.UserId == userId {
continue continue
} }
sendEmail := true
// skip if inactive if _, ok := profileMap[userId].NotifyProps["email"]; ok && profileMap[userId].NotifyProps["email"] == "false" {
if profileMap[id].DeleteAt > 0 { sendEmail = false
continue
} }
if sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) {
userLocale := utils.GetUserTranslations(profileMap[id].Locale) toEmailMap[userId] = true
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 { } else {
bodyText = userLocale("api.post.send_notifications_and_forget.mention_body") toEmailMap[userId] = false
subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
channelName = channel.DisplayName
} }
}
}
month := userLocale(tm.Month().String()) for id := range toEmailMap {
day := fmt.Sprintf("%d", tm.Day()) updateMentionCountAndForget(post.ChannelId, id)
year := fmt.Sprintf("%d", tm.Year()) }
zone, _ := tm.Zone() }
subjectPage := NewServerTemplatePage("post_subject", c.Locale) if len(toEmailMap) != 0 {
subjectPage.Props["Subject"] = userLocale("api.templates.post_subject", mentionedUsers = make([]string, 0, len(toEmailMap))
map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName, for k := range toEmailMap {
"Month": month[:3], "Day": day, "Year": year}) mentionedUsers = append(mentionedUsers, k)
}
bodyPage := NewServerTemplatePage("post_body", c.Locale) teamURL := c.GetSiteURL() + "/" + team.Name
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 // Build and send the emails
if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 { tm := time.Unix(post.CreateAt/1000, 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)
}
ext := filepath.Ext(filename) for id, doSend := range toEmailMap {
onlyImages = onlyImages && model.IsFileExtImage(ext)
}
filenamesString := strings.Join(filenames, ", ")
var attachmentPrefix string if !doSend {
if onlyImages { continue
attachmentPrefix = "Image" }
} else {
attachmentPrefix = "File" // skip if inactive
} if profileMap[id].DeleteAt > 0 {
if len(post.Filenames) > 1 { continue
attachmentPrefix += "s" }
userLocale := utils.GetUserTranslations(profileMap[id].Locale)
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
}
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", ext := filepath.Ext(filename)
map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString}) onlyImages = onlyImages && model.IsFileExtImage(ext)
}
filenamesString := strings.Join(filenames, ", ")
var attachmentPrefix string
if onlyImages {
attachmentPrefix = "Image"
} else {
attachmentPrefix = "File"
}
if len(post.Filenames) > 1 {
attachmentPrefix += "s"
} }
if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil { bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent",
l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err) map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString})
} }
if *utils.Cfg.EmailSettings.SendPushNotifications { if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil {
sessionChan := Srv.Store.Session().GetSessions(id) l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err)
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 *utils.Cfg.EmailSettings.SendPushNotifications {
if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" && sessionChan := Srv.Store.Session().GetSessions(id)
(strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) { if result := <-sessionChan; result.Err != nil {
alreadySeen[session.DeviceId] = session.DeviceId 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)
msg := model.PushNotification{} for _, session := range sessions {
msg.Badge = 1 if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" &&
msg.ServerId = utils.CfgDiagnosticId (strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) {
alreadySeen[session.DeviceId] = session.DeviceId
if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") { msg := model.PushNotification{}
msg.Platform = model.PUSH_NOTIFY_APPLE msg.Badge = 1
msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") msg.ServerId = utils.CfgDiagnosticId
} 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+":")
}
if channel.Type == model.CHANNEL_DIRECT { if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") {
msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message") msg.Platform = model.PUSH_NOTIFY_APPLE
} else { msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":")
msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName } 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+":")
}
httpClient := http.Client{} if channel.Type == model.CHANNEL_DIRECT {
request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson())) 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
}
l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message) httpClient := http.Client{}
if _, err := httpClient.Do(request); err != nil { request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson()))
l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), id, err)
} 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)
} }
} }
} }
@@ -730,13 +730,13 @@ func updateMentionCountAndForget(channelId, userId string) {
}() }()
} }
func checkForOutOfChannelMentions(c *Context, post *model.Post, channel *model.Channel) { func checkForOutOfChannelMentions(c *Context, post *model.Post, channel *model.Channel, allProfiles map[string]*model.User, members []model.ChannelMember) {
// don't check for out of channel mentions in direct channels // don't check for out of channel mentions in direct channels
if channel.Type == model.CHANNEL_DIRECT { if channel.Type == model.CHANNEL_DIRECT {
return return
} }
mentioned := getOutOfChannelMentions(post, channel.TeamId) mentioned := getOutOfChannelMentions(post, allProfiles, members)
if len(mentioned) == 0 { if len(mentioned) == 0 {
return return
} }
@@ -779,28 +779,16 @@ func checkForOutOfChannelMentions(c *Context, post *model.Post, channel *model.C
} }
// Gets a list of users that were mentioned in a given post that aren't in the channel that the post was made in // Gets a list of users that were mentioned in a given post that aren't in the channel that the post was made in
func getOutOfChannelMentions(post *model.Post, teamId string) []*model.User { func getOutOfChannelMentions(post *model.Post, allProfiles map[string]*model.User, members []model.ChannelMember) []*model.User {
pchan := Srv.Store.User().GetProfiles(teamId) // copy the profiles map since we'll be removing items from it
mchan := Srv.Store.Channel().GetMembers(post.ChannelId) profiles := make(map[string]*model.User)
for id, profile := range allProfiles {
var profiles map[string]*model.User profiles[id] = profile
if result := <-pchan; result.Err != nil {
l4g.Error(utils.T("api.post.get_out_of_channel_mentions.retrieve_profiles.error"), teamId, result.Err)
return []*model.User{}
} else {
profiles = result.Data.(map[string]*model.User)
} }
// only keep profiles which aren't in the current channel // only keep profiles which aren't in the current channel
if result := <-mchan; result.Err != nil { for _, member := range members {
l4g.Error(utils.T("api.post.get_out_of_channel_mentions.retrieve_members.error"), post.ChannelId, result.Err) delete(profiles, member.UserId)
return []*model.User{}
} else {
members := result.Data.([]model.ChannelMember)
for _, member := range members {
delete(profiles, member.UserId)
}
} }
var mentioned []*model.User var mentioned []*model.User

Просмотреть файл

@@ -882,27 +882,41 @@ func TestGetOutOfChannelMentions(t *testing.T) {
channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team1.Id} channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team1.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
var allProfiles map[string]*model.User
if result := <-Srv.Store.User().GetProfiles(team1.Id); result.Err != nil {
t.Fatal(result.Err)
} else {
allProfiles = result.Data.(map[string]*model.User)
}
var members []model.ChannelMember
if result := <-Srv.Store.Channel().GetMembers(channel1.Id); result.Err != nil {
t.Fatal(result.Err)
} else {
members = result.Data.([]model.ChannelMember)
}
// test a post that doesn't @mention anybody // test a post that doesn't @mention anybody
post1 := &model.Post{ChannelId: channel1.Id, Message: "user1 user2 user3"} post1 := &model.Post{ChannelId: channel1.Id, Message: "user1 user2 user3"}
if mentioned := getOutOfChannelMentions(post1, team1.Id); len(mentioned) != 0 { if mentioned := getOutOfChannelMentions(post1, allProfiles, members); len(mentioned) != 0 {
t.Fatalf("getOutOfChannelMentions returned %v when no users were mentioned", mentioned) t.Fatalf("getOutOfChannelMentions returned %v when no users were mentioned", mentioned)
} }
// test a post that @mentions someone in the channel // test a post that @mentions someone in the channel
post2 := &model.Post{ChannelId: channel1.Id, Message: "@user1 is user1"} post2 := &model.Post{ChannelId: channel1.Id, Message: "@user1 is user1"}
if mentioned := getOutOfChannelMentions(post2, team1.Id); len(mentioned) != 0 { if mentioned := getOutOfChannelMentions(post2, allProfiles, members); len(mentioned) != 0 {
t.Fatalf("getOutOfChannelMentions returned %v when only users in the channel were mentioned", mentioned) t.Fatalf("getOutOfChannelMentions returned %v when only users in the channel were mentioned", mentioned)
} }
// test a post that @mentions someone not in the channel // test a post that @mentions someone not in the channel
post3 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user3 aren't in the channel"} post3 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user3 aren't in the channel"}
if mentioned := getOutOfChannelMentions(post3, team1.Id); len(mentioned) != 2 || (mentioned[0].Id != user2.Id && mentioned[0].Id != user3.Id) || (mentioned[1].Id != user2.Id && mentioned[1].Id != user3.Id) { if mentioned := getOutOfChannelMentions(post3, allProfiles, members); len(mentioned) != 2 || (mentioned[0].Id != user2.Id && mentioned[0].Id != user3.Id) || (mentioned[1].Id != user2.Id && mentioned[1].Id != user3.Id) {
t.Fatalf("getOutOfChannelMentions returned %v when two users outside the channel were mentioned", mentioned) t.Fatalf("getOutOfChannelMentions returned %v when two users outside the channel were mentioned", mentioned)
} }
// test a post that @mentions someone not in the channel as well as someone in the channel // test a post that @mentions someone not in the channel as well as someone in the channel
post4 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user1 might be in the channel"} post4 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user1 might be in the channel"}
if mentioned := getOutOfChannelMentions(post4, team1.Id); len(mentioned) != 1 || mentioned[0].Id != user2.Id { if mentioned := getOutOfChannelMentions(post4, allProfiles, members); len(mentioned) != 1 || mentioned[0].Id != user2.Id {
t.Fatalf("getOutOfChannelMentions returned %v when someone in the channel and someone outside the channel were mentioned", mentioned) t.Fatalf("getOutOfChannelMentions returned %v when someone in the channel and someone outside the channel were mentioned", mentioned)
} }
@@ -920,9 +934,21 @@ func TestGetOutOfChannelMentions(t *testing.T) {
channel2 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team2.Id} channel2 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team2.Id}
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel) channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
if result := <-Srv.Store.User().GetProfiles(team2.Id); result.Err != nil {
t.Fatal(result.Err)
} else {
allProfiles = result.Data.(map[string]*model.User)
}
if result := <-Srv.Store.Channel().GetMembers(channel2.Id); result.Err != nil {
t.Fatal(result.Err)
} else {
members = result.Data.([]model.ChannelMember)
}
// test a post that @mentions someone on a different team // test a post that @mentions someone on a different team
post5 := &model.Post{ChannelId: channel2.Id, Message: "@user2 and @user3 might be in the channel"} post5 := &model.Post{ChannelId: channel2.Id, Message: "@user2 and @user3 might be in the channel"}
if mentioned := getOutOfChannelMentions(post5, team2.Id); len(mentioned) != 0 { if mentioned := getOutOfChannelMentions(post5, allProfiles, members); len(mentioned) != 0 {
t.Fatalf("getOutOfChannelMentions returned %v when two users on a different team were mentioned", mentioned) t.Fatalf("getOutOfChannelMentions returned %v when two users on a different team were mentioned", mentioned)
} }
} }

Просмотреть файл

@@ -691,14 +691,6 @@
"id": "api.post.delete_post.permissions.app_error", "id": "api.post.delete_post.permissions.app_error",
"translation": "You do not have the appropriate permissions" "translation": "You do not have the appropriate permissions"
}, },
{
"id": "api.post.get_out_of_channel_mentions.retrieve_members.error",
"translation": "Failed to get channel members channel_id=%v err=%v"
},
{
"id": "api.post.get_out_of_channel_mentions.retrieve_profiles.error",
"translation": "Failed to retrieve user profiles team_id=%v, err=%v"
},
{ {
"id": "api.post.get_out_of_channel_mentions.regex.error", "id": "api.post.get_out_of_channel_mentions.regex.error",
"translation": "Failed to compile @mention regex user_id=%v, err=%v" "translation": "Failed to compile @mention regex user_id=%v, err=%v"
@@ -711,6 +703,14 @@
"id": "api.post.handle_post_events_and_forget.channel.error", "id": "api.post.handle_post_events_and_forget.channel.error",
"translation": "Encountered error getting channel, channel_id=%s, err=%v" "translation": "Encountered error getting channel, channel_id=%s, err=%v"
}, },
{
"id": "api.post.handle_post_events_and_forget.members.error",
"translation": "Failed to get channel members channel_id=%v err=%v"
},
{
"id": "api.post.handle_post_events_and_forget.profiles.error",
"translation": "Failed to retrieve user profiles team_id=%v, err=%v"
},
{ {
"id": "api.post.handle_post_events_and_forget.team.error", "id": "api.post.handle_post_events_and_forget.team.error",
"translation": "Encountered error getting team, team_id=%s, err=%v" "translation": "Encountered error getting team, team_id=%s, err=%v"
@@ -751,10 +751,6 @@
"id": "api.post.make_direct_channel_visible.update_pref.error", "id": "api.post.make_direct_channel_visible.update_pref.error",
"translation": "Failed to update direct channel preference user_id=%v other_user_id=%v err=%v" "translation": "Failed to update direct channel preference user_id=%v other_user_id=%v err=%v"
}, },
{
"id": "api.post.send_notifications_and_forget.members.error",
"translation": "Failed to get channel members channel_id=%v err=%v"
},
{ {
"id": "api.post.send_notifications_and_forget.mention_body", "id": "api.post.send_notifications_and_forget.mention_body",
"translation": "You have one new mention." "translation": "You have one new mention."
@@ -787,10 +783,6 @@
"id": "api.post.send_notifications_and_forget.push_notification.error", "id": "api.post.send_notifications_and_forget.push_notification.error",
"translation": "Failed to send push notificationid=%v, err=%v" "translation": "Failed to send push notificationid=%v, err=%v"
}, },
{
"id": "api.post.send_notifications_and_forget.retrive_profiles.error",
"translation": "Failed to retrieve user profiles team_id=%v, err=%v"
},
{ {
"id": "api.post.send_notifications_and_forget.send.error", "id": "api.post.send_notifications_and_forget.send.error",
"translation": "Failed to send mention email successfully email=%v err=%v" "translation": "Failed to send mention email successfully email=%v err=%v"

Просмотреть файл

@@ -683,14 +683,6 @@
"id": "api.post.delete_post.permissions.app_error", "id": "api.post.delete_post.permissions.app_error",
"translation": "No tienes los permisos apropiados" "translation": "No tienes los permisos apropiados"
}, },
{
"id": "api.post.get_out_of_channel_mentions.retrieve_members.error",
"translation": "Falla al obtener los miembros del canal channel_id=%v err=%v"
},
{
"id": "api.post.get_out_of_channel_mentions.retrieve_profiles.error",
"translation": "Falla al recuperar los perfiles de usuario team_id=%v, err=%v"
},
{ {
"id": "api.post.get_post.permissions.app_error", "id": "api.post.get_post.permissions.app_error",
"translation": "No tienes los permisos apropiados" "translation": "No tienes los permisos apropiados"
@@ -699,6 +691,14 @@
"id": "api.post.handle_post_events_and_forget.channel.error", "id": "api.post.handle_post_events_and_forget.channel.error",
"translation": "Se encontró un error obteniendo el canal, channel_id=%s, err=%v" "translation": "Se encontró un error obteniendo el canal, channel_id=%s, err=%v"
}, },
{
"id": "api.post.handle_post_events_and_forget.members.error",
"translation": "Falla al obtener los miembros del canal channel_id=%v err=%v"
},
{
"id": "api.post.handle_post_events_and_forget.profiles.error",
"translation": "Falla al recuperar los perfiles de usuario team_id=%v, err=%v"
},
{ {
"id": "api.post.handle_post_events_and_forget.team.error", "id": "api.post.handle_post_events_and_forget.team.error",
"translation": "Se encontró un error obteniendo el equipo, team_id=%s, err=%v" "translation": "Se encontró un error obteniendo el equipo, team_id=%s, err=%v"
@@ -739,10 +739,6 @@
"id": "api.post.make_direct_channel_visible.update_pref.error", "id": "api.post.make_direct_channel_visible.update_pref.error",
"translation": "Falla al actualizar las preferencias del canal directo user_id=%v other_user_id=%v err=%v" "translation": "Falla al actualizar las preferencias del canal directo user_id=%v other_user_id=%v err=%v"
}, },
{
"id": "api.post.send_notifications_and_forget.members.error",
"translation": "Falla al obtener los miembros del canal channel_id=%v err=%v"
},
{ {
"id": "api.post.send_notifications_and_forget.mention_body", "id": "api.post.send_notifications_and_forget.mention_body",
"translation": "Tienes una mención nueva." "translation": "Tienes una mención nueva."
@@ -775,10 +771,6 @@
"id": "api.post.send_notifications_and_forget.push_notification.error", "id": "api.post.send_notifications_and_forget.push_notification.error",
"translation": "Falló el envio de la notificación push notificationid=%v, err=%v" "translation": "Falló el envio de la notificación push notificationid=%v, err=%v"
}, },
{
"id": "api.post.send_notifications_and_forget.retrive_profiles.error",
"translation": "Falla al recuperar los perfiles de usuario team_id=%v, err=%v"
},
{ {
"id": "api.post.send_notifications_and_forget.send.error", "id": "api.post.send_notifications_and_forget.send.error",
"translation": "Falla al enviar el correo con la mención satisfactoriamente email=%v err=%v" "translation": "Falla al enviar el correo con la mención satisfactoriamente email=%v err=%v"