From 50eaec6695f981489e66f82bdf895c6f809e6dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Tue, 10 May 2022 12:37:47 +0200 Subject: [PATCH] Respect send push proxy notification configuration on test notifications (#20063) * Respect send push proxy notification configuration on test notifications * Unify notifications check Co-authored-by: Mattermod --- app/channel.go | 2 +- app/expirynotify.go | 8 ++------ app/notification.go | 27 +++++++++++++++------------ app/notification_push.go | 4 ++++ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/app/channel.go b/app/channel.go index 5394739f42..888ee52bc1 100644 --- a/app/channel.go +++ b/app/channel.go @@ -2904,7 +2904,7 @@ func (a *App) SearchChannelsUserNotIn(teamID string, userID string, term string) func (a *App) MarkChannelsAsViewed(channelIDs []string, userID string, currentSessionId string, collapsedThreadsSupported bool) (map[string]int64, *model.AppError) { // I start looking for channels with notifications before I mark it as read, to clear the push notifications if needed channelsToClearPushNotifications := []string{} - if *a.Config().EmailSettings.SendPushNotifications { + if a.canSendPushNotifications() { for _, channelID := range channelIDs { channel, errCh := a.Srv().Store.Channel().Get(channelID, true) if errCh != nil { diff --git a/app/expirynotify.go b/app/expirynotify.go index 8d1dbdbea0..4034718245 100644 --- a/app/expirynotify.go +++ b/app/expirynotify.go @@ -17,12 +17,8 @@ const ( // NotifySessionsExpired is called periodically from the job server to notify any mobile sessions that have expired. func (a *App) NotifySessionsExpired() error { - if *a.Config().EmailSettings.SendPushNotifications { - pushServer := *a.Config().EmailSettings.PushNotificationServer - if license := a.ch.srv.License(); pushServer == model.MHPNS && (license == nil || !*license.Features.MHPNS) { - mlog.Warn("Push notifications are disabled. Go to System Console > Notifications > Mobile Push to enable them.") - return nil - } + if !a.canSendPushNotifications() { + return nil } // Get all mobile sessions that expired within the last hour. diff --git a/app/notification.go b/app/notification.go index 7e46290e62..972bab9f57 100644 --- a/app/notification.go +++ b/app/notification.go @@ -22,6 +22,20 @@ import ( "github.com/mattermost/mattermost-server/v6/store" ) +func (a *App) canSendPushNotifications() bool { + if !*a.Config().EmailSettings.SendPushNotifications { + return false + } + + pushServer := *a.Config().EmailSettings.PushNotificationServer + if license := a.Srv().License(); pushServer == model.MHPNS && (license == nil || !*license.Features.MHPNS) { + mlog.Warn("Push notifications have been disabled. Update your license or go to System Console > Environment > Push Notification Server to use a different server") + return false + } + + return true +} + func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList, setOnline bool) ([]string, error) { // Do not send notifications in archived channels if channel.DeleteAt > 0 { @@ -401,18 +415,7 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod } } - sendPushNotifications := false - if *a.Config().EmailSettings.SendPushNotifications { - pushServer := *a.Config().EmailSettings.PushNotificationServer - if license := a.Srv().License(); pushServer == model.MHPNS && (license == nil || !*license.Features.MHPNS) { - mlog.Warn("Push notifications are disabled. Go to System Console > Notifications > Mobile Push to enable them.") - sendPushNotifications = false - } else { - sendPushNotifications = true - } - } - - if sendPushNotifications { + if a.canSendPushNotifications() { for _, id := range mentionedUsersList { if profileMap[id] == nil || notificationsForCRT.Push.Contains(id) { continue diff --git a/app/notification_push.go b/app/notification_push.go index db349209e5..c79987cac2 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -578,6 +578,10 @@ func (a *App) BuildPushNotificationMessage(contentsConfig string, post *model.Po } func (a *App) SendTestPushNotification(deviceID string) string { + if !a.canSendPushNotifications() { + return "false" + } + msg := &model.PushNotification{ Version: "2", Type: model.PushTypeTest,