From e57cba15ea13bdd2eb7a23ba7c7853c6d9c3ef64 Mon Sep 17 00:00:00 2001 From: enahum Date: Tue, 6 Dec 2016 12:57:38 -0300 Subject: PLT-4762 Prevent turn off of (at)mentions (server) (#4714) --- api/post.go | 9 ++++++--- api/post_test.go | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/api/post.go b/api/post.go index ef9c0991d..2dc574e9b 100644 --- a/api/post.go +++ b/api/post.go @@ -459,6 +459,9 @@ func getMentionKeywordsInChannel(profiles map[string]*model.User) map[string][]s keywords := make(map[string][]string) for id, profile := range profiles { + userMention := "@" + strings.ToLower(profile.Username) + keywords[userMention] = append(keywords[userMention], id) + if len(profile.NotifyProps["mention_keys"]) > 0 { // Add all the user's mention keys splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",") @@ -698,7 +701,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * post.UserId, &model.Post{ ChannelId: post.ChannelId, - Message: utils.T("api.post.disabled_here", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), + Message: c.T("api.post.disabled_here", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), CreateAt: post.CreateAt + 1, }, ) @@ -711,7 +714,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * post.UserId, &model.Post{ ChannelId: post.ChannelId, - Message: utils.T("api.post.disabled_channel", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), + Message: c.T("api.post.disabled_channel", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), CreateAt: post.CreateAt + 1, }, ) @@ -724,7 +727,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * post.UserId, &model.Post{ ChannelId: post.ChannelId, - Message: utils.T("api.post.disabled_all", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), + Message: c.T("api.post.disabled_all", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}), CreateAt: post.CreateAt + 1, }, ) diff --git a/api/post_test.go b/api/post_test.go index 0e340561c..497a283bc 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -937,8 +937,8 @@ func TestGetMentionKeywords(t *testing.T) { profiles = map[string]*model.User{user2.Id: user2} mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 1 { - t.Fatal("should've returned one mention keyword") + if len(mentions) != 2 { + t.Fatal("should've returned two mention keyword") } else if ids, ok := mentions["First"]; !ok || ids[0] != user2.Id { t.Fatal("should've returned mention key of First") } @@ -955,8 +955,8 @@ func TestGetMentionKeywords(t *testing.T) { profiles = map[string]*model.User{user3.Id: user3} mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 2 { - t.Fatal("should've returned two mention keywords") + if len(mentions) != 3 { + t.Fatal("should've returned three mention keywords") } else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id { t.Fatal("should've returned mention key of @channel") } else if ids, ok := mentions["@all"]; !ok || ids[0] != user3.Id { @@ -993,6 +993,24 @@ func TestGetMentionKeywords(t *testing.T) { t.Fatal("should've returned mention key of @all") } + dup_count := func(list []string) map[string]int { + + duplicate_frequency := make(map[string]int) + + for _, item := range list { + // check if the item/element exist in the duplicate_frequency map + + _, exist := duplicate_frequency[item] + + if exist { + duplicate_frequency[item] += 1 // increase counter by 1 if already in the map + } else { + duplicate_frequency[item] = 1 // else start counting from 1 + } + } + return duplicate_frequency + } + // multiple users profiles = map[string]*model.User{ user1.Id: user1, @@ -1005,7 +1023,7 @@ func TestGetMentionKeywords(t *testing.T) { t.Fatal("should've returned six mention keywords") } else if ids, ok := mentions["user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { t.Fatal("should've mentioned user1 and user4 with user") - } else if ids, ok := mentions["@user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { + } else if ids := dup_count(mentions["@user"]); len(ids) != 4 || (ids[user1.Id] != 2) || (ids[user4.Id] != 2) { t.Fatal("should've mentioned user1 and user4 with @user") } else if ids, ok := mentions["mention"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { t.Fatal("should've mentioned user1 and user4 with mention") -- cgit v1.2.3-1-g7c22