github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+incompatible/app/notification_push.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"hash/fnv"
     9  	"net/http"
    10  	"strings"
    11  
    12  	"github.com/mattermost/mattermost-server/mlog"
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/utils"
    15  	"github.com/nicksnyder/go-i18n/i18n"
    16  )
    17  
    18  type NotificationType string
    19  
    20  const NOTIFICATION_TYPE_CLEAR NotificationType = "clear"
    21  const NOTIFICATION_TYPE_MESSAGE NotificationType = "message"
    22  
    23  const PUSH_NOTIFICATION_HUB_WORKERS = 1000
    24  const PUSH_NOTIFICATIONS_HUB_BUFFER_PER_WORKER = 50
    25  
    26  type PushNotificationsHub struct {
    27  	Channels []chan PushNotification
    28  }
    29  
    30  type PushNotification struct {
    31  	notificationType   NotificationType
    32  	userId             string
    33  	channelId          string
    34  	post               *model.Post
    35  	user               *model.User
    36  	channel            *model.Channel
    37  	senderName         string
    38  	channelName        string
    39  	explicitMention    bool
    40  	channelWideMention bool
    41  	replyToThreadType  string
    42  }
    43  
    44  func (hub *PushNotificationsHub) GetGoChannelFromUserId(userId string) chan PushNotification {
    45  	h := fnv.New32a()
    46  	h.Write([]byte(userId))
    47  	chanIdx := h.Sum32() % PUSH_NOTIFICATION_HUB_WORKERS
    48  	return hub.Channels[chanIdx]
    49  }
    50  
    51  func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, channel *model.Channel, channelName string, senderName string,
    52  	explicitMention, channelWideMention bool, replyToThreadType string) *model.AppError {
    53  	cfg := a.Config()
    54  
    55  	sessions, err := a.getMobileAppSessions(user.Id)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	msg := model.PushNotification{}
    61  	if badge := <-a.Srv.Store.User().GetUnreadCount(user.Id); badge.Err != nil {
    62  		msg.Badge = 1
    63  		mlog.Error(fmt.Sprint("We could not get the unread message count for the user", user.Id, badge.Err), mlog.String("user_id", user.Id))
    64  	} else {
    65  		msg.Badge = int(badge.Data.(int64))
    66  	}
    67  
    68  	msg.Category = model.CATEGORY_CAN_REPLY
    69  	msg.Version = model.PUSH_MESSAGE_V2
    70  	msg.Type = model.PUSH_TYPE_MESSAGE
    71  	msg.TeamId = channel.TeamId
    72  	msg.ChannelId = channel.Id
    73  	msg.PostId = post.Id
    74  	msg.RootId = post.RootId
    75  	msg.SenderId = post.UserId
    76  
    77  	contentsConfig := *cfg.EmailSettings.PushNotificationContents
    78  	if contentsConfig != model.GENERIC_NO_CHANNEL_NOTIFICATION || channel.Type == model.CHANNEL_DIRECT {
    79  		msg.ChannelName = channelName
    80  	}
    81  
    82  	if ou, ok := post.Props["override_username"].(string); ok && cfg.ServiceSettings.EnablePostUsernameOverride {
    83  		msg.OverrideUsername = ou
    84  	}
    85  
    86  	if oi, ok := post.Props["override_icon_url"].(string); ok && cfg.ServiceSettings.EnablePostIconOverride {
    87  		msg.OverrideIconUrl = oi
    88  	}
    89  
    90  	if fw, ok := post.Props["from_webhook"].(string); ok {
    91  		msg.FromWebhook = fw
    92  	}
    93  
    94  	userLocale := utils.GetUserTranslations(user.Locale)
    95  	hasFiles := post.FileIds != nil && len(post.FileIds) > 0
    96  
    97  	msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, senderName, channelName, channel.Type, replyToThreadType, userLocale)
    98  
    99  	for _, session := range sessions {
   100  
   101  		if session.IsExpired() {
   102  			continue
   103  		}
   104  
   105  		tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
   106  		tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
   107  
   108  		mlog.Debug(fmt.Sprintf("Sending push notification to device %v for user %v with msg of '%v'", tmpMessage.DeviceId, user.Id, msg.Message), mlog.String("user_id", user.Id))
   109  
   110  		a.sendToPushProxy(tmpMessage, session)
   111  
   112  		if a.Metrics != nil {
   113  			a.Metrics.IncrementPostSentPush()
   114  		}
   115  	}
   116  
   117  	return nil
   118  }
   119  
   120  func (a *App) sendPushNotification(notification *postNotification, user *model.User, explicitMention, channelWideMention bool, replyToThreadType string) {
   121  	cfg := a.Config()
   122  	channel := notification.channel
   123  	post := notification.post
   124  
   125  	var nameFormat string
   126  	if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_NAME_FORMAT); result.Err != nil {
   127  		nameFormat = *a.Config().TeamSettings.TeammateNameDisplay
   128  	} else {
   129  		nameFormat = result.Data.(model.Preference).Value
   130  	}
   131  
   132  	channelName := notification.GetChannelName(nameFormat, user.Id)
   133  	senderName := notification.GetSenderName(nameFormat, cfg.ServiceSettings.EnablePostUsernameOverride)
   134  
   135  	c := a.PushNotificationsHub.GetGoChannelFromUserId(user.Id)
   136  	c <- PushNotification{
   137  		notificationType:   NOTIFICATION_TYPE_MESSAGE,
   138  		post:               post,
   139  		user:               user,
   140  		channel:            channel,
   141  		senderName:         senderName,
   142  		channelName:        channelName,
   143  		explicitMention:    explicitMention,
   144  		channelWideMention: channelWideMention,
   145  		replyToThreadType:  replyToThreadType,
   146  	}
   147  }
   148  
   149  func (a *App) getPushNotificationMessage(postMessage string, explicitMention, channelWideMention, hasFiles bool,
   150  	senderName, channelName, channelType, replyToThreadType string, userLocale i18n.TranslateFunc) string {
   151  	message := ""
   152  
   153  	contentsConfig := *a.Config().EmailSettings.PushNotificationContents
   154  
   155  	if contentsConfig == model.FULL_NOTIFICATION {
   156  		if channelType == model.CHANNEL_DIRECT {
   157  			message = model.ClearMentionTags(postMessage)
   158  		} else {
   159  			message = "@" + senderName + ": " + model.ClearMentionTags(postMessage)
   160  		}
   161  	} else {
   162  		if channelType == model.CHANNEL_DIRECT {
   163  			message = userLocale("api.post.send_notifications_and_forget.push_message")
   164  		} else if channelWideMention {
   165  			message = "@" + senderName + userLocale("api.post.send_notification_and_forget.push_channel_mention")
   166  		} else if explicitMention {
   167  			message = "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_explicit_mention")
   168  		} else if replyToThreadType == THREAD_ROOT {
   169  			message = "@" + senderName + userLocale("api.post.send_notification_and_forget.push_comment_on_post")
   170  		} else if replyToThreadType == THREAD_ANY {
   171  			message = "@" + senderName + userLocale("api.post.send_notification_and_forget.push_comment_on_thread")
   172  		} else {
   173  			message = "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_general_message")
   174  		}
   175  	}
   176  
   177  	// If the post only has images then push an appropriate message
   178  	if len(postMessage) == 0 && hasFiles {
   179  		if channelType == model.CHANNEL_DIRECT {
   180  			message = strings.Trim(userLocale("api.post.send_notifications_and_forget.push_image_only"), " ")
   181  		} else {
   182  			message = "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_image_only")
   183  		}
   184  	}
   185  
   186  	return message
   187  }
   188  
   189  func (a *App) ClearPushNotificationSync(userId string, channelId string) {
   190  	sessions, err := a.getMobileAppSessions(userId)
   191  	if err != nil {
   192  		mlog.Error(err.Error())
   193  		return
   194  	}
   195  
   196  	msg := model.PushNotification{}
   197  	msg.Type = model.PUSH_TYPE_CLEAR
   198  	msg.ChannelId = channelId
   199  	msg.ContentAvailable = 0
   200  	if badge := <-a.Srv.Store.User().GetUnreadCount(userId); badge.Err != nil {
   201  		msg.Badge = 0
   202  		mlog.Error(fmt.Sprint("We could not get the unread message count for the user", userId, badge.Err), mlog.String("user_id", userId))
   203  	} else {
   204  		msg.Badge = int(badge.Data.(int64))
   205  	}
   206  
   207  	mlog.Debug(fmt.Sprintf("Clearing push notification to %v with channel_id %v", msg.DeviceId, msg.ChannelId))
   208  
   209  	for _, session := range sessions {
   210  		tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
   211  		tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
   212  		a.sendToPushProxy(tmpMessage, session)
   213  	}
   214  }
   215  
   216  func (a *App) ClearPushNotification(userId string, channelId string) {
   217  	channel := a.PushNotificationsHub.GetGoChannelFromUserId(userId)
   218  	channel <- PushNotification{
   219  		notificationType: NOTIFICATION_TYPE_CLEAR,
   220  		userId:           userId,
   221  		channelId:        channelId,
   222  	}
   223  }
   224  
   225  func (a *App) CreatePushNotificationsHub() {
   226  	hub := PushNotificationsHub{
   227  		Channels: []chan PushNotification{},
   228  	}
   229  	for x := 0; x < PUSH_NOTIFICATION_HUB_WORKERS; x++ {
   230  		hub.Channels = append(hub.Channels, make(chan PushNotification, PUSH_NOTIFICATIONS_HUB_BUFFER_PER_WORKER))
   231  	}
   232  	a.PushNotificationsHub = hub
   233  }
   234  
   235  func (a *App) pushNotificationWorker(notifications chan PushNotification) {
   236  	for notification := range notifications {
   237  		switch notification.notificationType {
   238  		case NOTIFICATION_TYPE_CLEAR:
   239  			a.ClearPushNotificationSync(notification.userId, notification.channelId)
   240  		case NOTIFICATION_TYPE_MESSAGE:
   241  			a.sendPushNotificationSync(
   242  				notification.post,
   243  				notification.user,
   244  				notification.channel,
   245  				notification.channelName,
   246  				notification.senderName,
   247  				notification.explicitMention,
   248  				notification.channelWideMention,
   249  				notification.replyToThreadType,
   250  			)
   251  		default:
   252  			mlog.Error(fmt.Sprintf("Invalid notification type %v", notification.notificationType))
   253  		}
   254  	}
   255  }
   256  
   257  func (a *App) StartPushNotificationsHubWorkers() {
   258  	for x := 0; x < PUSH_NOTIFICATION_HUB_WORKERS; x++ {
   259  		channel := a.PushNotificationsHub.Channels[x]
   260  		a.Go(func() { a.pushNotificationWorker(channel) })
   261  	}
   262  }
   263  
   264  func (a *App) StopPushNotificationsHubWorkers() {
   265  	for _, channel := range a.PushNotificationsHub.Channels {
   266  		close(channel)
   267  	}
   268  }
   269  
   270  func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) {
   271  	msg.ServerId = a.DiagnosticId()
   272  
   273  	request, _ := http.NewRequest("POST", strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/")+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))
   274  
   275  	if resp, err := a.HTTPService.MakeClient(true).Do(request); err != nil {
   276  		mlog.Error(fmt.Sprintf("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, err.Error()), mlog.String("user_id", session.UserId))
   277  	} else {
   278  		pushResponse := model.PushResponseFromJson(resp.Body)
   279  		if resp.Body != nil {
   280  			consumeAndClose(resp)
   281  		}
   282  
   283  		if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_REMOVE {
   284  			mlog.Info(fmt.Sprintf("Device was reported as removed for UserId=%v SessionId=%v removing push for this session", session.UserId, session.Id), mlog.String("user_id", session.UserId))
   285  			a.AttachDeviceId(session.Id, "", session.ExpiresAt)
   286  			a.ClearSessionCacheForUser(session.UserId)
   287  		}
   288  
   289  		if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_FAIL {
   290  			mlog.Error(fmt.Sprintf("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, pushResponse[model.PUSH_STATUS_ERROR_MSG]), mlog.String("user_id", session.UserId))
   291  		}
   292  	}
   293  }
   294  
   295  func (a *App) getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) {
   296  	if result := <-a.Srv.Store.Session().GetSessionsWithActiveDeviceIds(userId); result.Err != nil {
   297  		return nil, result.Err
   298  	} else {
   299  		return result.Data.([]*model.Session), nil
   300  	}
   301  }
   302  
   303  func ShouldSendPushNotification(user *model.User, channelNotifyProps model.StringMap, wasMentioned bool, status *model.Status, post *model.Post) bool {
   304  	return DoesNotifyPropsAllowPushNotification(user, channelNotifyProps, post, wasMentioned) &&
   305  		DoesStatusAllowPushNotification(user.NotifyProps, status, post.ChannelId)
   306  }
   307  
   308  func DoesNotifyPropsAllowPushNotification(user *model.User, channelNotifyProps model.StringMap, post *model.Post, wasMentioned bool) bool {
   309  	userNotifyProps := user.NotifyProps
   310  	userNotify := userNotifyProps[model.PUSH_NOTIFY_PROP]
   311  	channelNotify, ok := channelNotifyProps[model.PUSH_NOTIFY_PROP]
   312  
   313  	// If the channel is muted do not send push notifications
   314  	if channelMuted, ok := channelNotifyProps[model.MARK_UNREAD_NOTIFY_PROP]; ok {
   315  		if channelMuted == model.CHANNEL_MARK_UNREAD_MENTION {
   316  			return false
   317  		}
   318  	}
   319  
   320  	if post.IsSystemMessage() {
   321  		return false
   322  	}
   323  
   324  	if channelNotify == model.USER_NOTIFY_NONE {
   325  		return false
   326  	}
   327  
   328  	if channelNotify == model.CHANNEL_NOTIFY_MENTION && !wasMentioned {
   329  		return false
   330  	}
   331  
   332  	if userNotify == model.USER_NOTIFY_MENTION && (!ok || channelNotify == model.CHANNEL_NOTIFY_DEFAULT) && !wasMentioned {
   333  		return false
   334  	}
   335  
   336  	if (userNotify == model.USER_NOTIFY_ALL || channelNotify == model.CHANNEL_NOTIFY_ALL) &&
   337  		(post.UserId != user.Id || post.Props["from_webhook"] == "true") {
   338  		return true
   339  	}
   340  
   341  	if userNotify == model.USER_NOTIFY_NONE &&
   342  		(!ok || channelNotify == model.CHANNEL_NOTIFY_DEFAULT) {
   343  		return false
   344  	}
   345  
   346  	return true
   347  }
   348  
   349  func DoesStatusAllowPushNotification(userNotifyProps model.StringMap, status *model.Status, channelId string) bool {
   350  	// If User status is DND or OOO return false right away
   351  	if status.Status == model.STATUS_DND || status.Status == model.STATUS_OUT_OF_OFFICE {
   352  		return false
   353  	}
   354  
   355  	if pushStatus, ok := userNotifyProps["push_status"]; (pushStatus == model.STATUS_ONLINE || !ok) && (status.ActiveChannel != channelId || model.GetMillis()-status.LastActivityAt > model.STATUS_CHANNEL_TIMEOUT) {
   356  		return true
   357  	} else if pushStatus == model.STATUS_AWAY && (status.Status == model.STATUS_AWAY || status.Status == model.STATUS_OFFLINE) {
   358  		return true
   359  	} else if pushStatus == model.STATUS_OFFLINE && status.Status == model.STATUS_OFFLINE {
   360  		return true
   361  	}
   362  
   363  	return false
   364  }