From 748a416961923932e9b2969beb2730ee8c240ea7 Mon Sep 17 00:00:00 2001 From: enahum Date: Thu, 23 Feb 2017 11:08:48 -0300 Subject: =?UTF-8?q?PLT-3193=20Add=20channel=20notification=20preferences?= =?UTF-8?q?=20for=20push=20and=20email=20noti=E2=80=A6=20(#5500)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * PLT-3193 Add channel notification preferences for push and email notifications * unit tests, model validation and localization * Feedback review * Adding back allowFromCache check * Setting push and email to use default settings * Move props as constants * address feedback --- api/channel_test.go | 79 ++++++++++++++++++++++++++++++++-------------- app/channel.go | 17 +++++++--- app/import.go | 4 +-- app/import_test.go | 54 +++++++++++++++---------------- app/notification.go | 31 +++++++++++++++--- app/web_hub.go | 12 +++++++ einterfaces/cluster.go | 1 + i18n/en.json | 8 +++++ model/channel_member.go | 28 +++++++++++++--- model/user.go | 11 +++++-- store/sql_channel_store.go | 68 +++++++++++++++++++++++++++++++++++++++ store/store.go | 2 ++ 12 files changed, 247 insertions(+), 68 deletions(-) diff --git a/api/channel_test.go b/api/channel_test.go index 89b370791..2e6484dfa 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -1536,7 +1536,7 @@ func TestUpdateNotifyProps(t *testing.T) { data := make(map[string]string) data["channel_id"] = channel1.Id data["user_id"] = user.Id - data["desktop"] = model.CHANNEL_NOTIFY_MENTION + data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION //timeBeforeUpdate := model.GetMillis() time.Sleep(100 * time.Millisecond) @@ -1544,46 +1544,65 @@ func TestUpdateNotifyProps(t *testing.T) { // test updating desktop if result, err := Client.UpdateNotifyProps(data); err != nil { t.Fatal(err) - } else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { t.Fatal("NotifyProps[\"desktop\"] did not update properly") - } else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { - t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) + } else if notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_ALL { + t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps[model.MARK_UNREAD_NOTIFY_PROP]) } // test an empty update - delete(data, "desktop") + delete(data, model.DESKTOP_NOTIFY_PROP) if result, err := Client.UpdateNotifyProps(data); err != nil { t.Fatal(err) - } else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { - t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) - } else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { - t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_ALL { + t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps[model.MARK_UNREAD_NOTIFY_PROP]) + } else if notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { + t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps[model.DESKTOP_NOTIFY_PROP]) } // test updating mark unread - data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION if result, err := Client.UpdateNotifyProps(data); err != nil { t.Fatal(err) - } else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION { t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") - } else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { - t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) + } else if notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { + t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps[model.DESKTOP_NOTIFY_PROP]) } // test updating both - data["desktop"] = model.CHANNEL_NOTIFY_NONE - data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_NONE + data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION if result, err := Client.UpdateNotifyProps(data); err != nil { t.Fatal(err) - } else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_NONE { + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_NONE { t.Fatal("NotifyProps[\"desktop\"] did not update properly") - } else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { + } else if notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION { t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") } + // test updating push notification preferences + delete(data, model.DESKTOP_NOTIFY_PROP) + delete(data, model.MARK_UNREAD_NOTIFY_PROP) + data[model.PUSH_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION + if result, err := Client.UpdateNotifyProps(data); err != nil { + t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.PUSH_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { + t.Fatal("NotifyProps[\"push\"] did not update properly") + } + + // test updating email preferences + delete(data, model.PUSH_NOTIFY_PROP) + data[model.EMAIL_NOTIFY_PROP] = "true" + if result, err := Client.UpdateNotifyProps(data); err != nil { + t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps[model.EMAIL_NOTIFY_PROP] != "true" { + t.Fatal("NotifyProps[\"email\"] did not update properly") + } + // test error cases data["user_id"] = "junk" if _, err := Client.UpdateNotifyProps(data); err == nil { @@ -1606,24 +1625,38 @@ func TestUpdateNotifyProps(t *testing.T) { t.Fatal("Should have errored - bad channel id") } - data["desktop"] = "junk" - data["mark_unread"] = model.CHANNEL_MARK_UNREAD_ALL + data[model.DESKTOP_NOTIFY_PROP] = "junk" + data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_ALL if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad desktop notify level") } - data["desktop"] = model.CHANNEL_NOTIFY_ALL - data["mark_unread"] = "junk" + data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL + data[model.MARK_UNREAD_NOTIFY_PROP] = "junk" if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad mark unread level") } + data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL + data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_ALL + data[model.PUSH_NOTIFY_PROP] = "junk" + data[model.EMAIL_NOTIFY_PROP] = "true" + if _, err := Client.UpdateNotifyProps(data); err == nil { + t.Fatal("Should have errored - bad push level") + } + + data[model.PUSH_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL + data[model.EMAIL_NOTIFY_PROP] = "junk" + if _, err := Client.UpdateNotifyProps(data); err == nil { + t.Fatal("Should have errored - bad email notification option") + } + th.LoginBasic2() data["channel_id"] = channel1.Id data["user_id"] = user2.Id - data["desktop"] = model.CHANNEL_NOTIFY_MENTION - data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION + data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - user not in channel") } diff --git a/app/channel.go b/app/channel.go index db007dd3b..fb4198c37 100644 --- a/app/channel.go +++ b/app/channel.go @@ -226,18 +226,27 @@ func UpdateChannelMemberNotifyProps(data map[string]string, channelId string, us } // update whichever notify properties have been provided, but don't change the others - if markUnread, exists := data["mark_unread"]; exists { - member.NotifyProps["mark_unread"] = markUnread + if markUnread, exists := data[model.MARK_UNREAD_NOTIFY_PROP]; exists { + member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] = markUnread } - if desktop, exists := data["desktop"]; exists { - member.NotifyProps["desktop"] = desktop + if desktop, exists := data[model.DESKTOP_NOTIFY_PROP]; exists { + member.NotifyProps[model.DESKTOP_NOTIFY_PROP] = desktop + } + + if email, exists := data[model.EMAIL_NOTIFY_PROP]; exists { + member.NotifyProps[model.EMAIL_NOTIFY_PROP] = email + } + + if push, exists := data[model.PUSH_NOTIFY_PROP]; exists { + member.NotifyProps[model.PUSH_NOTIFY_PROP] = push } if result := <-Srv.Store.Channel().UpdateMember(member); result.Err != nil { return nil, result.Err } else { InvalidateCacheForUser(userId) + InvalidateCacheForChannelMembersNotifyProps(channelId) return member, nil } } diff --git a/app/import.go b/app/import.go index 10a915b2a..c50c6d62d 100644 --- a/app/import.go +++ b/app/import.go @@ -581,11 +581,11 @@ func ImportUserChannels(user *model.User, team *model.Team, data *[]UserChannelI notifyProps := member.NotifyProps if cdata.NotifyProps.Desktop != nil { - notifyProps["desktop"] = *cdata.NotifyProps.Desktop + notifyProps[model.DESKTOP_NOTIFY_PROP] = *cdata.NotifyProps.Desktop } if cdata.NotifyProps.MarkUnread != nil { - notifyProps["mark_unread"] = *cdata.NotifyProps.MarkUnread + notifyProps[model.MARK_UNREAD_NOTIFY_PROP] = *cdata.NotifyProps.MarkUnread } if _, err := UpdateChannelMemberNotifyProps(notifyProps, channel.Id, user.Id); err != nil { diff --git a/app/import_test.go b/app/import_test.go index ea71d0e0c..ddcc2d06a 100644 --- a/app/import_test.go +++ b/app/import_test.go @@ -1157,7 +1157,7 @@ func TestImportImportUser(t *testing.T) { // Check channel member properties. if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { t.Fatalf("Failed to get channel member from database.") - } else if channelMember.Roles != "channel_user" || channelMember.NotifyProps["desktop"] != "default" || channelMember.NotifyProps["mark_unread"] != "all" { + } else if channelMember.Roles != "channel_user" || channelMember.NotifyProps[model.DESKTOP_NOTIFY_PROP] != "default" || channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] != "all" { t.Fatalf("Channel member properties not as expected") } @@ -1171,8 +1171,8 @@ func TestImportImportUser(t *testing.T) { Name: &channelName, Roles: ptrStr("channel_user channel_admin"), NotifyProps: &UserChannelNotifyPropsImportData{ - Desktop: ptrStr("mention"), - MarkUnread: ptrStr("mention"), + Desktop: ptrStr(model.USER_NOTIFY_MENTION), + MarkUnread: ptrStr(model.USER_NOTIFY_MENTION), }, }, }, @@ -1191,7 +1191,7 @@ func TestImportImportUser(t *testing.T) { if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { t.Fatalf("Failed to get channel member Desktop from database.") - } else if channelMember.Roles != "channel_user channel_admin" && channelMember.NotifyProps["desktop"] == "mention" && channelMember.NotifyProps["mark_unread"] == "mention" { + } else if channelMember.Roles != "channel_user channel_admin" && channelMember.NotifyProps[model.DESKTOP_NOTIFY_PROP] == model.USER_NOTIFY_MENTION && channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.USER_NOTIFY_MENTION { t.Fatalf("Channel member properties not as expected") } @@ -1211,14 +1211,14 @@ func TestImportImportUser(t *testing.T) { // Add a user with some preferences. username = model.NewId() data = UserImportData{ - Username: &username, - Email: ptrStr(model.NewId() + "@example.com"), - Theme: ptrStr(`{"awayIndicator":"#DCBD4E","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`), - SelectedFont: ptrStr("Roboto Slab"), - UseMilitaryTime: ptrStr("true"), - NameFormat: ptrStr("nickname_full_name"), - CollapsePreviews: ptrStr("true"), - MessageDisplay: ptrStr("compact"), + Username: &username, + Email: ptrStr(model.NewId() + "@example.com"), + Theme: ptrStr(`{"awayIndicator":"#DCBD4E","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`), + SelectedFont: ptrStr("Roboto Slab"), + UseMilitaryTime: ptrStr("true"), + NameFormat: ptrStr("nickname_full_name"), + CollapsePreviews: ptrStr("true"), + MessageDisplay: ptrStr("compact"), ChannelDisplayMode: ptrStr("centered"), } if err := ImportUser(&data, false); err != nil { @@ -1226,12 +1226,12 @@ func TestImportImportUser(t *testing.T) { } // Check their values. - user, err = GetUserByUsername(username); + user, err = GetUserByUsername(username) if err != nil { t.Fatalf("Failed to get user from database.") } - if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil { + if res := <-Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil { t.Fatalf("Failed to get theme category preferences") } else { preferences := res.Data.(model.Preferences) @@ -1242,7 +1242,7 @@ func TestImportImportUser(t *testing.T) { } } - if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil { + if res := <-Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil { t.Fatalf("Failed to get display category preferences") } else { preferences := res.Data.(model.Preferences) @@ -1275,14 +1275,14 @@ func TestImportImportUser(t *testing.T) { // Change those preferences. data = UserImportData{ - Username: &username, - Email: ptrStr(model.NewId() + "@example.com"), - Theme: ptrStr(`{"awayIndicator":"#123456","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`), - SelectedFont: ptrStr("Lato"), - UseMilitaryTime: ptrStr("false"), - NameFormat: ptrStr("full_name"), - CollapsePreviews: ptrStr("false"), - MessageDisplay: ptrStr("clean"), + Username: &username, + Email: ptrStr(model.NewId() + "@example.com"), + Theme: ptrStr(`{"awayIndicator":"#123456","buttonBg":"#23A2FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBj":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`), + SelectedFont: ptrStr("Lato"), + UseMilitaryTime: ptrStr("false"), + NameFormat: ptrStr("full_name"), + CollapsePreviews: ptrStr("false"), + MessageDisplay: ptrStr("clean"), ChannelDisplayMode: ptrStr("full"), } if err := ImportUser(&data, false); err != nil { @@ -1290,7 +1290,7 @@ func TestImportImportUser(t *testing.T) { } // Check their values again. - if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil { + if res := <-Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_THEME); res.Err != nil { t.Fatalf("Failed to get theme category preferences") } else { preferences := res.Data.(model.Preferences) @@ -1301,7 +1301,7 @@ func TestImportImportUser(t *testing.T) { } } - if res := <- Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil { + if res := <-Srv.Store.Preference().GetCategory(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS); res.Err != nil { t.Fatalf("Failed to get display category preferences") } else { preferences := res.Data.(model.Preferences) @@ -1388,7 +1388,7 @@ func TestImportBulkImport(t *testing.T) { } // Run bulk import using valid JSON but missing version line at the start. - data3:= `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}} + data3 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}} {"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}} {"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}} {"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}` @@ -1401,7 +1401,7 @@ func TestImportProcessImportDataFileVersionLine(t *testing.T) { _ = Setup() data := LineImportData{ - Type: "version", + Type: "version", Version: ptrInt(1), } if version, err := processImportDataFileVersionLine(data); err != nil || version != 1 { diff --git a/app/notification.go b/app/notification.go index 1df194d2c..e9e76de45 100644 --- a/app/notification.go +++ b/app/notification.go @@ -26,6 +26,7 @@ import ( func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) { pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true) + cmnchan := Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true) var fchan store.StoreChannel if len(post.FileIds) != 0 { @@ -39,6 +40,13 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe profileMap = result.Data.(map[string]*model.User) } + var channelMemberNotifyPropsMap map[string]model.StringMap + if result := <-cmnchan; result.Err != nil { + return nil, result.Err + } else { + channelMemberNotifyPropsMap = result.Data.(map[string]model.StringMap) + } + mentionedUserIds := make(map[string]bool) allActivityPushUserIds := []string{} hereNotification := false @@ -94,7 +102,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe // find which users in the channel are set up to always receive mobile notifications for _, profile := range profileMap { - if profile.NotifyProps["push"] == model.USER_NOTIFY_ALL && + if profile.NotifyProps[model.PUSH_NOTIFY_PROP] == model.USER_NOTIFY_ALL && (post.UserId != profile.Id || post.Props["from_webhook"] == "true") && !post.IsSystemMessage() { allActivityPushUserIds = append(allActivityPushUserIds, profile.Id) @@ -137,7 +145,12 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe if utils.Cfg.EmailSettings.SendEmailNotifications { for _, id := range mentionedUsersList { - userAllowsEmails := profileMap[id].NotifyProps["email"] != "false" + userAllowsEmails := profileMap[id].NotifyProps[model.EMAIL_NOTIFY_PROP] != "false" + if channelEmail, ok := channelMemberNotifyPropsMap[id][model.EMAIL_NOTIFY_PROP]; ok { + if channelEmail != model.CHANNEL_NOTIFY_DEFAULT { + userAllowsEmails = channelEmail != "false" + } + } var status *model.Status var err *model.AppError @@ -245,7 +258,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { - sendPushNotification(post, profileMap[id], channel, senderName[id], true) + sendPushNotification(post, profileMap[id], channel, senderName[id], channelMemberNotifyPropsMap[id], true) } } @@ -258,7 +271,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { - sendPushNotification(post, profileMap[id], channel, senderName[id], false) + sendPushNotification(post, profileMap[id], channel, senderName[id], channelMemberNotifyPropsMap[id], false) } } } @@ -442,7 +455,7 @@ func GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFun } } -func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) *model.AppError { +func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, channelNotifyProps model.StringMap, wasMentioned bool) *model.AppError { sessions, err := getMobileAppSessions(user.Id) if err != nil { return err @@ -450,6 +463,14 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha var channelName string + if channelNotify, ok := channelNotifyProps[model.PUSH_NOTIFY_PROP]; ok { + if channelNotify == model.USER_NOTIFY_NONE { + return nil + } else if channelNotify == model.USER_NOTIFY_MENTION && !wasMentioned { + return nil + } + } + if channel.Type == model.CHANNEL_DIRECT { channelName = senderName } else { diff --git a/app/web_hub.go b/app/web_hub.go index b4a8a4140..a214ffb5e 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -134,6 +134,18 @@ func InvalidateCacheForChannelMembersSkipClusterSend(channelId string) { Srv.Store.Channel().InvalidateMemberCount(channelId) } +func InvalidateCacheForChannelMembersNotifyProps(channelId string) { + InvalidateCacheForChannelMembersNotifyPropsSkipClusterSend(channelId) + + if cluster := einterfaces.GetClusterInterface(); cluster != nil { + cluster.InvalidateCacheForChannelMembersNotifyProps(channelId) + } +} + +func InvalidateCacheForChannelMembersNotifyPropsSkipClusterSend(channelId string) { + Srv.Store.Channel().InvalidateCacheForChannelMembersNotifyProps(channelId) +} + func InvalidateCacheForChannelByNameSkipClusterSend(teamId, name string) { Srv.Store.Channel().InvalidateChannelByName(teamId, name) } diff --git a/einterfaces/cluster.go b/einterfaces/cluster.go index 5ecee0b16..389f1f139 100644 --- a/einterfaces/cluster.go +++ b/einterfaces/cluster.go @@ -17,6 +17,7 @@ type ClusterInterface interface { InvalidateCacheForChannel(channelId string) InvalidateCacheForChannelByName(teamId, name string) InvalidateCacheForChannelMembers(channelId string) + InvalidateCacheForChannelMembersNotifyProps(channelId string) InvalidateCacheForChannelPosts(channelId string) InvalidateCacheForWebhook(webhookId string) Publish(event *model.WebSocketEvent) diff --git a/i18n/en.json b/i18n/en.json index cea44ef0a..bfeb9bc89 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3519,10 +3519,18 @@ "id": "model.channel_member.is_valid.role.app_error", "translation": "Invalid role" }, + { + "id": "model.channel_member.is_valid.email_value.app_error", + "translation": "Invalid email notification value" + }, { "id": "model.channel_member.is_valid.unread_level.app_error", "translation": "Invalid mark unread level" }, + { + "id": "model.channel_member.is_valid.push_level.app_error", + "translation": "Invalid push notification level" + }, { "id": "model.channel_member.is_valid.user_id.app_error", "translation": "Invalid user id" diff --git a/model/channel_member.go b/model/channel_member.go index a607a5059..5de58bc4f 100644 --- a/model/channel_member.go +++ b/model/channel_member.go @@ -88,18 +88,32 @@ func (o *ChannelMember) IsValid() *AppError { return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "") } - notifyLevel := o.NotifyProps["desktop"] + notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP] if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", nil, "notify_level="+notifyLevel) } - markUnreadLevel := o.NotifyProps["mark_unread"] + markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", nil, "mark_unread_level="+markUnreadLevel) } + if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok { + if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) { + return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error", + nil, "push_notification_level="+pushLevel) + } + } + + if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok { + if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) { + return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error", + nil, "push_notification_level="+sendEmail) + } + } + return nil } @@ -126,9 +140,15 @@ func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION } +func IsSendEmailValid(sendEmail string) bool { + return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false" +} + func GetDefaultChannelNotifyProps() StringMap { return StringMap{ - "desktop": CHANNEL_NOTIFY_DEFAULT, - "mark_unread": CHANNEL_MARK_UNREAD_ALL, + DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, + MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL, + PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, + EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, } } diff --git a/model/user.go b/model/user.go index 7057ab101..2412c2ec7 100644 --- a/model/user.go +++ b/model/user.go @@ -16,9 +16,14 @@ import ( ) const ( - USER_NOTIFY_ALL = "all" - USER_NOTIFY_MENTION = "mention" - USER_NOTIFY_NONE = "none" + USER_NOTIFY_ALL = "all" + USER_NOTIFY_MENTION = "mention" + USER_NOTIFY_NONE = "none" + DESKTOP_NOTIFY_PROP = "desktop" + MARK_UNREAD_NOTIFY_PROP = "mark_unread" + PUSH_NOTIFY_PROP = "push" + EMAIL_NOTIFY_PROP = "email" + DEFAULT_LOCALE = "en" USER_AUTH_SERVICE_EMAIL = "email" USER_AUTH_SERVICE_USERNAME = "username" diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 03bc70c75..aac5bfed3 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -25,6 +25,9 @@ const ( ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE = model.SESSION_CACHE_SIZE ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC = 900 // 15 mins + ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SIZE = model.SESSION_CACHE_SIZE + ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SEC = 1800 // 30 mins + CHANNEL_MEMBERS_COUNTS_CACHE_SIZE = model.CHANNEL_CACHE_SIZE CHANNEL_MEMBERS_COUNTS_CACHE_SEC = 1800 // 30 mins @@ -37,12 +40,14 @@ type SqlChannelStore struct { var channelMemberCountsCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE) var allChannelMembersForUserCache = utils.NewLru(ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE) +var allChannelMembersNotifyPropsForChannelCache = utils.NewLru(ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SIZE) var channelCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) var channelByNameCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) func ClearChannelCaches() { channelMemberCountsCache.Purge() allChannelMembersForUserCache.Purge() + allChannelMembersNotifyPropsForChannelCache.Purge() channelCache.Purge() channelByNameCache.Purge() } @@ -919,6 +924,69 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac return storeChannel } +func (us SqlChannelStore) InvalidateCacheForChannelMembersNotifyProps(channelId string) { + allChannelMembersNotifyPropsForChannelCache.Remove(channelId) +} + +type allChannelMemberNotifyProps struct { + UserId string + NotifyProps model.StringMap +} + +func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + metrics := einterfaces.GetMetricsInterface() + + if allowFromCache { + if cacheItem, ok := allChannelMembersNotifyPropsForChannelCache.Get(channelId); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("All Channel Members Notify Props for Channel") + } + result.Data = cacheItem.(map[string]model.StringMap) + storeChannel <- result + close(storeChannel) + return + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("All Channel Members Notify Props for Channel") + } + } + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("All Channel Members Notify Props for Channel") + } + } + + var data []allChannelMemberNotifyProps + _, err := s.GetReplica().Select(&data, ` + SELECT ChannelMembers.UserId, ChannelMembers.NotifyProps + FROM Channels, ChannelMembers + WHERE Channels.Id = ChannelMembers.ChannelId AND ChannelMembers.ChannelId = :ChannelId`, map[string]interface{}{"ChannelId": channelId}) + + if err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetAllChannelMembersPropsForChannel", "store.sql_channel.get_members.app_error", nil, "channelId="+channelId+", err="+err.Error()) + } else { + + props := make(map[string]model.StringMap) + for i := range data { + props[data[i].UserId] = data[i].NotifyProps + } + + result.Data = props + + allChannelMembersNotifyPropsForChannelCache.AddWithExpiresInSecs(channelId, props, ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SEC) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (us SqlChannelStore) InvalidateMemberCount(channelId string) { channelMemberCountsCache.Remove(channelId) } diff --git a/store/store.go b/store/store.go index d0c403460..3da331209 100644 --- a/store/store.go +++ b/store/store.go @@ -110,6 +110,8 @@ type ChannelStore interface { GetAllChannelMembersForUser(userId string, allowFromCache bool) StoreChannel InvalidateAllChannelMembersForUser(userId string) IsUserInChannelUseCache(userId string, channelId string) bool + GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) StoreChannel + InvalidateCacheForChannelMembersNotifyProps(channelId string) GetMemberForPost(postId string, userId string) StoreChannel InvalidateMemberCount(channelId string) GetMemberCountFromCache(channelId string) int64 -- cgit v1.2.3-1-g7c22