github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+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  
   152  	// If the post only has images then push an appropriate message
   153  	if len(postMessage) == 0 && hasFiles {
   154  		if channelType == model.CHANNEL_DIRECT {
   155  			return strings.Trim(userLocale("api.post.send_notifications_and_forget.push_image_only"), " ")
   156  		}
   157  		return "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_image_only")
   158  	}
   159  
   160  	contentsConfig := *a.Config().EmailSettings.PushNotificationContents
   161  
   162  	if contentsConfig == model.FULL_NOTIFICATION {
   163  		if channelType == model.CHANNEL_DIRECT {
   164  			return model.ClearMentionTags(postMessage)
   165  		}
   166  		return "@" + senderName + ": " + model.ClearMentionTags(postMessage)
   167  	}
   168  
   169  	if channelType == model.CHANNEL_DIRECT {
   170  		return userLocale("api.post.send_notifications_and_forget.push_message")
   171  	}
   172  
   173  	if channelWideMention {
   174  		return "@" + senderName + userLocale("api.post.send_notification_and_forget.push_channel_mention")
   175  	}
   176  
   177  	if explicitMention {
   178  		return "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_explicit_mention")
   179  	}
   180  
   181  	if replyToThreadType == THREAD_ROOT {
   182  		return "@" + senderName + userLocale("api.post.send_notification_and_forget.push_comment_on_post")
   183  	}
   184  
   185  	if replyToThreadType == THREAD_ANY {
   186  		return "@" + senderName + userLocale("api.post.send_notification_and_forget.push_comment_on_thread")
   187  	}
   188  
   189  	return "@" + senderName + userLocale("api.post.send_notifications_and_forget.push_general_message")
   190  }
   191  
   192  func (a *App) ClearPushNotificationSync(userId string, channelId string) {
   193  	sessions, err := a.getMobileAppSessions(userId)
   194  	if err != nil {
   195  		mlog.Error(err.Error())
   196  		return
   197  	}
   198  
   199  	msg := model.PushNotification{}
   200  	msg.Type = model.PUSH_TYPE_CLEAR
   201  	msg.ChannelId = channelId
   202  	msg.ContentAvailable = 0
   203  	if badge := <-a.Srv.Store.User().GetUnreadCount(userId); badge.Err != nil {
   204  		msg.Badge = 0
   205  		mlog.Error(fmt.Sprint("We could not get the unread message count for the user", userId, badge.Err), mlog.String("user_id", userId))
   206  	} else {
   207  		msg.Badge = int(badge.Data.(int64))
   208  	}
   209  
   210  	mlog.Debug(fmt.Sprintf("Clearing push notification to %v with channel_id %v", msg.DeviceId, msg.ChannelId))
   211  
   212  	for _, session := range sessions {
   213  		tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
   214  		tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
   215  		a.sendToPushProxy(tmpMessage, session)
   216  	}
   217  }
   218  
   219  func (a *App) ClearPushNotification(userId string, channelId string) {
   220  	channel := a.PushNotificationsHub.GetGoChannelFromUserId(userId)
   221  	channel <- PushNotification{
   222  		notificationType: NOTIFICATION_TYPE_CLEAR,
   223  		userId:           userId,
   224  		channelId:        channelId,
   225  	}
   226  }
   227  
   228  func (a *App) CreatePushNotificationsHub() {
   229  	hub := PushNotificationsHub{
   230  		Channels: []chan PushNotification{},
   231  	}
   232  	for x := 0; x < PUSH_NOTIFICATION_HUB_WORKERS; x++ {
   233  		hub.Channels = append(hub.Channels, make(chan PushNotification, PUSH_NOTIFICATIONS_HUB_BUFFER_PER_WORKER))
   234  	}
   235  	a.PushNotificationsHub = hub
   236  }
   237  
   238  func (a *App) pushNotificationWorker(notifications chan PushNotification) {
   239  	for notification := range notifications {
   240  		switch notification.notificationType {
   241  		case NOTIFICATION_TYPE_CLEAR:
   242  			a.ClearPushNotificationSync(notification.userId, notification.channelId)
   243  		case NOTIFICATION_TYPE_MESSAGE:
   244  			a.sendPushNotificationSync(
   245  				notification.post,
   246  				notification.user,
   247  				notification.channel,
   248  				notification.channelName,
   249  				notification.senderName,
   250  				notification.explicitMention,
   251  				notification.channelWideMention,
   252  				notification.replyToThreadType,
   253  			)
   254  		default:
   255  			mlog.Error(fmt.Sprintf("Invalid notification type %v", notification.notificationType))
   256  		}
   257  	}
   258  }
   259  
   260  func (a *App) StartPushNotificationsHubWorkers() {
   261  	for x := 0; x < PUSH_NOTIFICATION_HUB_WORKERS; x++ {
   262  		channel := a.PushNotificationsHub.Channels[x]
   263  		a.Go(func() { a.pushNotificationWorker(channel) })
   264  	}
   265  }
   266  
   267  func (a *App) StopPushNotificationsHubWorkers() {
   268  	for _, channel := range a.PushNotificationsHub.Channels {
   269  		close(channel)
   270  	}
   271  }
   272  
   273  func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) {
   274  	msg.ServerId = a.DiagnosticId()
   275  
   276  	request, _ := http.NewRequest("POST", strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/")+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))
   277  
   278  	resp, err := a.HTTPService.MakeClient(true).Do(request)
   279  	if err != nil {
   280  		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))
   281  		return
   282  	}
   283  
   284  	pushResponse := model.PushResponseFromJson(resp.Body)
   285  	if resp.Body != nil {
   286  		consumeAndClose(resp)
   287  	}
   288  
   289  	if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_REMOVE {
   290  		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))
   291  		a.AttachDeviceId(session.Id, "", session.ExpiresAt)
   292  		a.ClearSessionCacheForUser(session.UserId)
   293  	}
   294  
   295  	if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_FAIL {
   296  		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))
   297  	}
   298  }
   299  
   300  func (a *App) getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) {
   301  	result := <-a.Srv.Store.Session().GetSessionsWithActiveDeviceIds(userId)
   302  	if result.Err != nil {
   303  		return nil, result.Err
   304  	}
   305  	return result.Data.([]*model.Session), nil
   306  }
   307  
   308  func ShouldSendPushNotification(user *model.User, channelNotifyProps model.StringMap, wasMentioned bool, status *model.Status, post *model.Post) bool {
   309  	return DoesNotifyPropsAllowPushNotification(user, channelNotifyProps, post, wasMentioned) &&
   310  		DoesStatusAllowPushNotification(user.NotifyProps, status, post.ChannelId)
   311  }
   312  
   313  func DoesNotifyPropsAllowPushNotification(user *model.User, channelNotifyProps model.StringMap, post *model.Post, wasMentioned bool) bool {
   314  	userNotifyProps := user.NotifyProps
   315  	userNotify := userNotifyProps[model.PUSH_NOTIFY_PROP]
   316  	channelNotify, ok := channelNotifyProps[model.PUSH_NOTIFY_PROP]
   317  
   318  	// If the channel is muted do not send push notifications
   319  	if channelMuted, ok := channelNotifyProps[model.MARK_UNREAD_NOTIFY_PROP]; ok {
   320  		if channelMuted == model.CHANNEL_MARK_UNREAD_MENTION {
   321  			return false
   322  		}
   323  	}
   324  
   325  	if post.IsSystemMessage() {
   326  		return false
   327  	}
   328  
   329  	if channelNotify == model.USER_NOTIFY_NONE {
   330  		return false
   331  	}
   332  
   333  	if channelNotify == model.CHANNEL_NOTIFY_MENTION && !wasMentioned {
   334  		return false
   335  	}
   336  
   337  	if userNotify == model.USER_NOTIFY_MENTION && (!ok || channelNotify == model.CHANNEL_NOTIFY_DEFAULT) && !wasMentioned {
   338  		return false
   339  	}
   340  
   341  	if (userNotify == model.USER_NOTIFY_ALL || channelNotify == model.CHANNEL_NOTIFY_ALL) &&
   342  		(post.UserId != user.Id || post.Props["from_webhook"] == "true") {
   343  		return true
   344  	}
   345  
   346  	if userNotify == model.USER_NOTIFY_NONE &&
   347  		(!ok || channelNotify == model.CHANNEL_NOTIFY_DEFAULT) {
   348  		return false
   349  	}
   350  
   351  	return true
   352  }
   353  
   354  func DoesStatusAllowPushNotification(userNotifyProps model.StringMap, status *model.Status, channelId string) bool {
   355  	// If User status is DND or OOO return false right away
   356  	if status.Status == model.STATUS_DND || status.Status == model.STATUS_OUT_OF_OFFICE {
   357  		return false
   358  	}
   359  
   360  	pushStatus, ok := userNotifyProps["push_status"]
   361  	if (pushStatus == model.STATUS_ONLINE || !ok) && (status.ActiveChannel != channelId || model.GetMillis()-status.LastActivityAt > model.STATUS_CHANNEL_TIMEOUT) {
   362  		return true
   363  	}
   364  
   365  	if pushStatus == model.STATUS_AWAY && (status.Status == model.STATUS_AWAY || status.Status == model.STATUS_OFFLINE) {
   366  		return true
   367  	}
   368  
   369  	if pushStatus == model.STATUS_OFFLINE && status.Status == model.STATUS_OFFLINE {
   370  		return true
   371  	}
   372  
   373  	return false
   374  }