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 --- app/channel.go | 17 +++++++++++++---- app/import.go | 4 ++-- app/import_test.go | 54 ++++++++++++++++++++++++++--------------------------- app/notification.go | 31 +++++++++++++++++++++++++----- app/web_hub.go | 12 ++++++++++++ 5 files changed, 80 insertions(+), 38 deletions(-) (limited to 'app') 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) } -- cgit v1.2.3-1-g7c22