github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/app/notification.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  	"sort"
     9  	"strings"
    10  	"unicode"
    11  	"unicode/utf8"
    12  
    13  	"github.com/mattermost/mattermost-server/mlog"
    14  	"github.com/mattermost/mattermost-server/model"
    15  	"github.com/mattermost/mattermost-server/store"
    16  	"github.com/mattermost/mattermost-server/utils"
    17  	"github.com/mattermost/mattermost-server/utils/markdown"
    18  )
    19  
    20  const (
    21  	THREAD_ANY  = "any"
    22  	THREAD_ROOT = "root"
    23  )
    24  
    25  func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList) ([]string, *model.AppError) {
    26  	// Do not send notifications in archived channels
    27  	if channel.DeleteAt > 0 {
    28  		return []string{}, nil
    29  	}
    30  
    31  	pchan := a.Srv.Store.User().GetAllProfilesInChannel(channel.Id, true)
    32  	cmnchan := a.Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true)
    33  	var fchan store.StoreChannel
    34  
    35  	if len(post.FileIds) != 0 {
    36  		fchan = a.Srv.Store.FileInfo().GetForPost(post.Id, true, true)
    37  	}
    38  
    39  	result := <-pchan
    40  	if result.Err != nil {
    41  		return nil, result.Err
    42  	}
    43  	profileMap := result.Data.(map[string]*model.User)
    44  
    45  	result = <-cmnchan
    46  	if result.Err != nil {
    47  		return nil, result.Err
    48  	}
    49  	channelMemberNotifyPropsMap := result.Data.(map[string]model.StringMap)
    50  
    51  	mentionedUserIds := make(map[string]bool)
    52  	threadMentionedUserIds := make(map[string]string)
    53  	allActivityPushUserIds := []string{}
    54  	hereNotification := false
    55  	channelNotification := false
    56  	allNotification := false
    57  	updateMentionChans := []store.StoreChannel{}
    58  
    59  	if channel.Type == model.CHANNEL_DIRECT {
    60  		var otherUserId string
    61  
    62  		userIds := strings.Split(channel.Name, "__")
    63  
    64  		if userIds[0] != userIds[1] {
    65  			if userIds[0] == post.UserId {
    66  				otherUserId = userIds[1]
    67  			} else {
    68  				otherUserId = userIds[0]
    69  			}
    70  		}
    71  
    72  		otherUser, ok := profileMap[otherUserId]
    73  		if ok {
    74  			mentionedUserIds[otherUserId] = true
    75  		}
    76  
    77  		if post.Props["from_webhook"] == "true" {
    78  			mentionedUserIds[post.UserId] = true
    79  		}
    80  
    81  		if post.Type != model.POST_AUTO_RESPONDER {
    82  			a.Srv.Go(func() {
    83  				a.SendAutoResponse(channel, otherUser)
    84  			})
    85  		}
    86  
    87  	} else {
    88  		keywords := a.GetMentionKeywordsInChannel(profileMap, post.Type != model.POST_HEADER_CHANGE && post.Type != model.POST_PURPOSE_CHANGE, channelMemberNotifyPropsMap)
    89  
    90  		m := GetExplicitMentions(post, keywords)
    91  
    92  		// Add an implicit mention when a user is added to a channel
    93  		// even if the user has set 'username mentions' to false in account settings.
    94  		if post.Type == model.POST_ADD_TO_CHANNEL {
    95  			val := post.Props[model.POST_PROPS_ADDED_USER_ID]
    96  			if val != nil {
    97  				uid := val.(string)
    98  				m.MentionedUserIds[uid] = true
    99  			}
   100  		}
   101  
   102  		mentionedUserIds, hereNotification, channelNotification, allNotification = m.MentionedUserIds, m.HereMentioned, m.ChannelMentioned, m.AllMentioned
   103  
   104  		// get users that have comment thread mentions enabled
   105  		if len(post.RootId) > 0 && parentPostList != nil {
   106  			for _, threadPost := range parentPostList.Posts {
   107  				profile := profileMap[threadPost.UserId]
   108  				if profile != nil && (profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == THREAD_ANY || (profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == THREAD_ROOT && threadPost.Id == parentPostList.Order[0])) {
   109  					if threadPost.Id == parentPostList.Order[0] {
   110  						threadMentionedUserIds[threadPost.UserId] = THREAD_ROOT
   111  					} else {
   112  						threadMentionedUserIds[threadPost.UserId] = THREAD_ANY
   113  					}
   114  
   115  					if _, ok := mentionedUserIds[threadPost.UserId]; !ok {
   116  						mentionedUserIds[threadPost.UserId] = false
   117  					}
   118  				}
   119  			}
   120  		}
   121  
   122  		// prevent the user from mentioning themselves
   123  		if post.Props["from_webhook"] != "true" {
   124  			delete(mentionedUserIds, post.UserId)
   125  		}
   126  
   127  		if len(m.OtherPotentialMentions) > 0 && !post.IsSystemMessage() {
   128  			if result := <-a.Srv.Store.User().GetProfilesByUsernames(m.OtherPotentialMentions, team.Id); result.Err == nil {
   129  				outOfChannelMentions := result.Data.([]*model.User)
   130  				if channel.Type != model.CHANNEL_GROUP {
   131  					a.Srv.Go(func() {
   132  						a.sendOutOfChannelMentions(sender, post, outOfChannelMentions)
   133  					})
   134  				}
   135  			}
   136  		}
   137  
   138  		// find which users in the channel are set up to always receive mobile notifications
   139  		for _, profile := range profileMap {
   140  			if (profile.NotifyProps[model.PUSH_NOTIFY_PROP] == model.USER_NOTIFY_ALL ||
   141  				channelMemberNotifyPropsMap[profile.Id][model.PUSH_NOTIFY_PROP] == model.CHANNEL_NOTIFY_ALL) &&
   142  				(post.UserId != profile.Id || post.Props["from_webhook"] == "true") &&
   143  				!post.IsSystemMessage() {
   144  				allActivityPushUserIds = append(allActivityPushUserIds, profile.Id)
   145  			}
   146  		}
   147  	}
   148  
   149  	mentionedUsersList := make([]string, 0, len(mentionedUserIds))
   150  	for id := range mentionedUserIds {
   151  		mentionedUsersList = append(mentionedUsersList, id)
   152  		updateMentionChans = append(updateMentionChans, a.Srv.Store.Channel().IncrementMentionCount(post.ChannelId, id))
   153  	}
   154  
   155  	notification := &postNotification{
   156  		post:       post,
   157  		channel:    channel,
   158  		profileMap: profileMap,
   159  		sender:     sender,
   160  	}
   161  
   162  	if *a.Config().EmailSettings.SendEmailNotifications {
   163  		for _, id := range mentionedUsersList {
   164  			if profileMap[id] == nil {
   165  				continue
   166  			}
   167  
   168  			userAllowsEmails := profileMap[id].NotifyProps[model.EMAIL_NOTIFY_PROP] != "false"
   169  			if channelEmail, ok := channelMemberNotifyPropsMap[id][model.EMAIL_NOTIFY_PROP]; ok {
   170  				if channelEmail != model.CHANNEL_NOTIFY_DEFAULT {
   171  					userAllowsEmails = channelEmail != "false"
   172  				}
   173  			}
   174  
   175  			// Remove the user as recipient when the user has muted the channel.
   176  			if channelMuted, ok := channelMemberNotifyPropsMap[id][model.MARK_UNREAD_NOTIFY_PROP]; ok {
   177  				if channelMuted == model.CHANNEL_MARK_UNREAD_MENTION {
   178  					mlog.Debug(fmt.Sprintf("Channel muted for user_id %v, channel_mute %v", id, channelMuted))
   179  					userAllowsEmails = false
   180  				}
   181  			}
   182  
   183  			//If email verification is required and user email is not verified don't send email.
   184  			if *a.Config().EmailSettings.RequireEmailVerification && !profileMap[id].EmailVerified {
   185  				mlog.Error(fmt.Sprintf("Skipped sending notification email to %v, address not verified. [details: user_id=%v]", profileMap[id].Email, id))
   186  				continue
   187  			}
   188  
   189  			var status *model.Status
   190  			var err *model.AppError
   191  			if status, err = a.GetStatus(id); err != nil {
   192  				status = &model.Status{
   193  					UserId:         id,
   194  					Status:         model.STATUS_OFFLINE,
   195  					Manual:         false,
   196  					LastActivityAt: 0,
   197  					ActiveChannel:  "",
   198  				}
   199  			}
   200  
   201  			autoResponderRelated := status.Status == model.STATUS_OUT_OF_OFFICE || post.Type == model.POST_AUTO_RESPONDER
   202  
   203  			if userAllowsEmails && status.Status != model.STATUS_ONLINE && profileMap[id].DeleteAt == 0 && !autoResponderRelated {
   204  				a.sendNotificationEmail(notification, profileMap[id], team)
   205  			}
   206  		}
   207  	}
   208  
   209  	T := utils.GetUserTranslations(sender.Locale)
   210  
   211  	// If the channel has more than 1K users then @here is disabled
   212  	if hereNotification && int64(len(profileMap)) > *a.Config().TeamSettings.MaxNotificationsPerChannel {
   213  		hereNotification = false
   214  		a.SendEphemeralPost(
   215  			post.UserId,
   216  			&model.Post{
   217  				ChannelId: post.ChannelId,
   218  				Message:   T("api.post.disabled_here", map[string]interface{}{"Users": *a.Config().TeamSettings.MaxNotificationsPerChannel}),
   219  				CreateAt:  post.CreateAt + 1,
   220  			},
   221  		)
   222  	}
   223  
   224  	// If the channel has more than 1K users then @channel is disabled
   225  	if channelNotification && int64(len(profileMap)) > *a.Config().TeamSettings.MaxNotificationsPerChannel {
   226  		a.SendEphemeralPost(
   227  			post.UserId,
   228  			&model.Post{
   229  				ChannelId: post.ChannelId,
   230  				Message:   T("api.post.disabled_channel", map[string]interface{}{"Users": *a.Config().TeamSettings.MaxNotificationsPerChannel}),
   231  				CreateAt:  post.CreateAt + 1,
   232  			},
   233  		)
   234  	}
   235  
   236  	// If the channel has more than 1K users then @all is disabled
   237  	if allNotification && int64(len(profileMap)) > *a.Config().TeamSettings.MaxNotificationsPerChannel {
   238  		a.SendEphemeralPost(
   239  			post.UserId,
   240  			&model.Post{
   241  				ChannelId: post.ChannelId,
   242  				Message:   T("api.post.disabled_all", map[string]interface{}{"Users": *a.Config().TeamSettings.MaxNotificationsPerChannel}),
   243  				CreateAt:  post.CreateAt + 1,
   244  			},
   245  		)
   246  	}
   247  
   248  	// Make sure all mention updates are complete to prevent race
   249  	// Probably better to batch these DB updates in the future
   250  	// MUST be completed before push notifications send
   251  	for _, uchan := range updateMentionChans {
   252  		if result := <-uchan; result.Err != nil {
   253  			mlog.Warn(fmt.Sprintf("Failed to update mention count, post_id=%v channel_id=%v err=%v", post.Id, post.ChannelId, result.Err), mlog.String("post_id", post.Id))
   254  		}
   255  	}
   256  
   257  	sendPushNotifications := false
   258  	if *a.Config().EmailSettings.SendPushNotifications {
   259  		pushServer := *a.Config().EmailSettings.PushNotificationServer
   260  		if license := a.License(); pushServer == model.MHPNS && (license == nil || !*license.Features.MHPNS) {
   261  			mlog.Warn("Push notifications are disabled. Go to System Console > Notifications > Mobile Push to enable them.")
   262  			sendPushNotifications = false
   263  		} else {
   264  			sendPushNotifications = true
   265  		}
   266  	}
   267  
   268  	if sendPushNotifications {
   269  		for _, id := range mentionedUsersList {
   270  			if profileMap[id] == nil {
   271  				continue
   272  			}
   273  
   274  			var status *model.Status
   275  			var err *model.AppError
   276  			if status, err = a.GetStatus(id); err != nil {
   277  				status = &model.Status{UserId: id, Status: model.STATUS_OFFLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
   278  			}
   279  
   280  			if ShouldSendPushNotification(profileMap[id], channelMemberNotifyPropsMap[id], true, status, post) {
   281  				replyToThreadType := ""
   282  				if value, ok := threadMentionedUserIds[id]; ok {
   283  					replyToThreadType = value
   284  				}
   285  
   286  				a.sendPushNotification(
   287  					notification,
   288  					profileMap[id],
   289  					mentionedUserIds[id],
   290  					(channelNotification || hereNotification || allNotification),
   291  					replyToThreadType,
   292  				)
   293  			}
   294  		}
   295  
   296  		for _, id := range allActivityPushUserIds {
   297  			if profileMap[id] == nil {
   298  				continue
   299  			}
   300  
   301  			if _, ok := mentionedUserIds[id]; !ok {
   302  				var status *model.Status
   303  				var err *model.AppError
   304  				if status, err = a.GetStatus(id); err != nil {
   305  					status = &model.Status{UserId: id, Status: model.STATUS_OFFLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
   306  				}
   307  
   308  				if ShouldSendPushNotification(profileMap[id], channelMemberNotifyPropsMap[id], false, status, post) {
   309  					a.sendPushNotification(
   310  						notification,
   311  						profileMap[id],
   312  						false,
   313  						false,
   314  						"",
   315  					)
   316  				}
   317  			}
   318  		}
   319  	}
   320  
   321  	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POSTED, "", post.ChannelId, "", nil)
   322  
   323  	// Note that PreparePostForClient should've already been called by this point
   324  	message.Add("post", post.ToJson())
   325  
   326  	message.Add("channel_type", channel.Type)
   327  	message.Add("channel_display_name", notification.GetChannelName(model.SHOW_USERNAME, ""))
   328  	message.Add("channel_name", channel.Name)
   329  	message.Add("sender_name", notification.GetSenderName(model.SHOW_USERNAME, *a.Config().ServiceSettings.EnablePostUsernameOverride))
   330  	message.Add("team_id", team.Id)
   331  
   332  	if len(post.FileIds) != 0 && fchan != nil {
   333  		message.Add("otherFile", "true")
   334  
   335  		var infos []*model.FileInfo
   336  		if result := <-fchan; result.Err != nil {
   337  			mlog.Warn(fmt.Sprint("Unable to get fileInfo for push notifications.", post.Id, result.Err), mlog.String("post_id", post.Id))
   338  		} else {
   339  			infos = result.Data.([]*model.FileInfo)
   340  		}
   341  
   342  		for _, info := range infos {
   343  			if info.IsImage() {
   344  				message.Add("image", "true")
   345  				break
   346  			}
   347  		}
   348  	}
   349  
   350  	if len(mentionedUsersList) != 0 {
   351  		message.Add("mentions", model.ArrayToJson(mentionedUsersList))
   352  	}
   353  
   354  	a.Publish(message)
   355  	return mentionedUsersList, nil
   356  }
   357  
   358  func (a *App) sendOutOfChannelMentions(sender *model.User, post *model.Post, users []*model.User) *model.AppError {
   359  	if len(users) == 0 {
   360  		return nil
   361  	}
   362  
   363  	var usernames []string
   364  	for _, user := range users {
   365  		usernames = append(usernames, user.Username)
   366  	}
   367  	sort.Strings(usernames)
   368  
   369  	var userIds []string
   370  	for _, user := range users {
   371  		userIds = append(userIds, user.Id)
   372  	}
   373  
   374  	T := utils.GetUserTranslations(sender.Locale)
   375  
   376  	ephemeralPostId := model.NewId()
   377  	var message string
   378  	if len(users) == 1 {
   379  		message = T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{
   380  			"Username": usernames[0],
   381  		})
   382  	} else {
   383  		message = T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{
   384  			"Usernames":    strings.Join(usernames[:len(usernames)-1], ", @"),
   385  			"LastUsername": usernames[len(usernames)-1],
   386  		})
   387  	}
   388  
   389  	props := model.StringInterface{
   390  		model.PROPS_ADD_CHANNEL_MEMBER: model.StringInterface{
   391  			"post_id":   ephemeralPostId,
   392  			"usernames": usernames,
   393  			"user_ids":  userIds,
   394  		},
   395  	}
   396  
   397  	a.SendEphemeralPost(
   398  		post.UserId,
   399  		&model.Post{
   400  			Id:        ephemeralPostId,
   401  			RootId:    post.RootId,
   402  			ChannelId: post.ChannelId,
   403  			Message:   message,
   404  			CreateAt:  post.CreateAt + 1,
   405  			Props:     props,
   406  		},
   407  	)
   408  
   409  	return nil
   410  }
   411  
   412  type ExplicitMentions struct {
   413  	// MentionedUserIds contains a key for each user mentioned by keyword.
   414  	MentionedUserIds map[string]bool
   415  
   416  	// OtherPotentialMentions contains a list of strings that looked like mentions, but didn't have
   417  	// a corresponding keyword.
   418  	OtherPotentialMentions []string
   419  
   420  	// HereMentioned is true if the message contained @here.
   421  	HereMentioned bool
   422  
   423  	// AllMentioned is true if the message contained @all.
   424  	AllMentioned bool
   425  
   426  	// ChannelMentioned is true if the message contained @channel.
   427  	ChannelMentioned bool
   428  }
   429  
   430  // Given a message and a map mapping mention keywords to the users who use them, returns a map of mentioned
   431  // users and a slice of potential mention users not in the channel and whether or not @here was mentioned.
   432  func GetExplicitMentions(post *model.Post, keywords map[string][]string) *ExplicitMentions {
   433  	ret := &ExplicitMentions{
   434  		MentionedUserIds: make(map[string]bool),
   435  	}
   436  	systemMentions := map[string]bool{"@here": true, "@channel": true, "@all": true}
   437  
   438  	addMentionedUsers := func(ids []string) {
   439  		for _, id := range ids {
   440  			ret.MentionedUserIds[id] = true
   441  		}
   442  	}
   443  	checkForMention := func(word string) bool {
   444  		isMention := false
   445  
   446  		if strings.ToLower(word) == "@here" {
   447  			ret.HereMentioned = true
   448  		}
   449  
   450  		if strings.ToLower(word) == "@channel" {
   451  			ret.ChannelMentioned = true
   452  		}
   453  
   454  		if strings.ToLower(word) == "@all" {
   455  			ret.AllMentioned = true
   456  		}
   457  
   458  		// Non-case-sensitive check for regular keys
   459  		if ids, match := keywords[strings.ToLower(word)]; match {
   460  			addMentionedUsers(ids)
   461  			isMention = true
   462  		}
   463  
   464  		// Case-sensitive check for first name
   465  		if ids, match := keywords[word]; match {
   466  			addMentionedUsers(ids)
   467  			isMention = true
   468  		}
   469  
   470  		return isMention
   471  	}
   472  
   473  	var multibyteKeywords []string
   474  	for keyword := range keywords {
   475  		if len(keyword) != utf8.RuneCountInString(keyword) {
   476  			multibyteKeywords = append(multibyteKeywords, keyword)
   477  		}
   478  	}
   479  
   480  	processText := func(text string) {
   481  		for _, word := range strings.FieldsFunc(text, func(c rune) bool {
   482  			// Split on any whitespace or punctuation that can't be part of an at mention or emoji pattern
   483  			return !(c == ':' || c == '.' || c == '-' || c == '_' || c == '@' || unicode.IsLetter(c) || unicode.IsNumber(c))
   484  		}) {
   485  			// skip word with format ':word:' with an assumption that it is an emoji format only
   486  			if word[0] == ':' && word[len(word)-1] == ':' {
   487  				continue
   488  			}
   489  
   490  			word = strings.TrimLeft(word, ":.-_")
   491  
   492  			if checkForMention(word) {
   493  				continue
   494  			}
   495  
   496  			foundWithoutSuffix := false
   497  			wordWithoutSuffix := word
   498  			for len(wordWithoutSuffix) > 0 && strings.LastIndexAny(wordWithoutSuffix, ".-:_") == (len(wordWithoutSuffix)-1) {
   499  				wordWithoutSuffix = wordWithoutSuffix[0 : len(wordWithoutSuffix)-1]
   500  
   501  				if checkForMention(wordWithoutSuffix) {
   502  					foundWithoutSuffix = true
   503  					break
   504  				}
   505  			}
   506  
   507  			if foundWithoutSuffix {
   508  				continue
   509  			}
   510  
   511  			if _, ok := systemMentions[word]; !ok && strings.HasPrefix(word, "@") {
   512  				ret.OtherPotentialMentions = append(ret.OtherPotentialMentions, word[1:])
   513  			} else if strings.ContainsAny(word, ".-:") {
   514  				// This word contains a character that may be the end of a sentence, so split further
   515  				splitWords := strings.FieldsFunc(word, func(c rune) bool {
   516  					return c == '.' || c == '-' || c == ':'
   517  				})
   518  
   519  				for _, splitWord := range splitWords {
   520  					if checkForMention(splitWord) {
   521  						continue
   522  					}
   523  					if _, ok := systemMentions[splitWord]; !ok && strings.HasPrefix(splitWord, "@") {
   524  						ret.OtherPotentialMentions = append(ret.OtherPotentialMentions, splitWord[1:])
   525  					}
   526  				}
   527  			}
   528  
   529  			// If word contains a multibyte character, check if it contains a multibyte keyword
   530  			if len(word) != utf8.RuneCountInString(word) {
   531  				for _, key := range multibyteKeywords {
   532  					if strings.Contains(word, key) {
   533  						if ids, match := keywords[key]; match {
   534  							addMentionedUsers(ids)
   535  						}
   536  					}
   537  				}
   538  			}
   539  		}
   540  	}
   541  
   542  	buf := ""
   543  	mentionsEnabledFields := GetMentionsEnabledFields(post)
   544  	for _, message := range mentionsEnabledFields {
   545  		markdown.Inspect(message, func(node interface{}) bool {
   546  			text, ok := node.(*markdown.Text)
   547  			if !ok {
   548  				processText(buf)
   549  				buf = ""
   550  				return true
   551  			}
   552  			buf += text.Text
   553  			return false
   554  		})
   555  	}
   556  	processText(buf)
   557  
   558  	return ret
   559  }
   560  
   561  // Given a post returns the values of the fields in which mentions are possible.
   562  // post.message, preText and text in the attachment are enabled.
   563  func GetMentionsEnabledFields(post *model.Post) model.StringArray {
   564  	ret := []string{}
   565  
   566  	ret = append(ret, post.Message)
   567  	for _, attachment := range post.Attachments() {
   568  
   569  		if len(attachment.Pretext) != 0 {
   570  			ret = append(ret, attachment.Pretext)
   571  		}
   572  		if len(attachment.Text) != 0 {
   573  			ret = append(ret, attachment.Text)
   574  		}
   575  	}
   576  	return ret
   577  }
   578  
   579  // Given a map of user IDs to profiles, returns a list of mention
   580  // keywords for all users in the channel.
   581  func (a *App) GetMentionKeywordsInChannel(profiles map[string]*model.User, lookForSpecialMentions bool, channelMemberNotifyPropsMap map[string]model.StringMap) map[string][]string {
   582  	keywords := make(map[string][]string)
   583  
   584  	for id, profile := range profiles {
   585  		userMention := "@" + strings.ToLower(profile.Username)
   586  		keywords[userMention] = append(keywords[userMention], id)
   587  
   588  		if len(profile.NotifyProps[model.MENTION_KEYS_NOTIFY_PROP]) > 0 {
   589  			// Add all the user's mention keys
   590  			splitKeys := strings.Split(profile.NotifyProps[model.MENTION_KEYS_NOTIFY_PROP], ",")
   591  			for _, k := range splitKeys {
   592  				// note that these are made lower case so that we can do a case insensitive check for them
   593  				key := strings.ToLower(k)
   594  				keywords[key] = append(keywords[key], id)
   595  			}
   596  		}
   597  
   598  		// If turned on, add the user's case sensitive first name
   599  		if profile.NotifyProps[model.FIRST_NAME_NOTIFY_PROP] == "true" {
   600  			keywords[profile.FirstName] = append(keywords[profile.FirstName], profile.Id)
   601  		}
   602  
   603  		ignoreChannelMentions := false
   604  		if ignoreChannelMentionsNotifyProp, ok := channelMemberNotifyPropsMap[profile.Id][model.IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP]; ok {
   605  			if ignoreChannelMentionsNotifyProp == model.IGNORE_CHANNEL_MENTIONS_ON {
   606  				ignoreChannelMentions = true
   607  			}
   608  		}
   609  
   610  		// Add @channel and @all to keywords if user has them turned on
   611  		if lookForSpecialMentions {
   612  			if int64(len(profiles)) <= *a.Config().TeamSettings.MaxNotificationsPerChannel && profile.NotifyProps[model.CHANNEL_MENTIONS_NOTIFY_PROP] == "true" && !ignoreChannelMentions {
   613  				keywords["@channel"] = append(keywords["@channel"], profile.Id)
   614  				keywords["@all"] = append(keywords["@all"], profile.Id)
   615  
   616  				status := GetStatusFromCache(profile.Id)
   617  				if status != nil && status.Status == model.STATUS_ONLINE {
   618  					keywords["@here"] = append(keywords["@here"], profile.Id)
   619  				}
   620  			}
   621  		}
   622  	}
   623  
   624  	return keywords
   625  }
   626  
   627  // Represents either an email or push notification and contains the fields required to send it to any user.
   628  type postNotification struct {
   629  	channel    *model.Channel
   630  	post       *model.Post
   631  	profileMap map[string]*model.User
   632  	sender     *model.User
   633  }
   634  
   635  // Returns the name of the channel for this notification. For direct messages, this is the sender's name
   636  // preceeded by an at sign. For group messages, this is a comma-separated list of the members of the
   637  // channel, with an option to exclude the recipient of the message from that list.
   638  func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string {
   639  	switch n.channel.Type {
   640  	case model.CHANNEL_DIRECT:
   641  		return fmt.Sprintf("@%s", n.sender.GetDisplayName(userNameFormat))
   642  	case model.CHANNEL_GROUP:
   643  		names := []string{}
   644  		for _, user := range n.profileMap {
   645  			if user.Id != excludeId {
   646  				names = append(names, user.GetDisplayName(userNameFormat))
   647  			}
   648  		}
   649  
   650  		sort.Strings(names)
   651  
   652  		return strings.Join(names, ", ")
   653  	default:
   654  		return n.channel.DisplayName
   655  	}
   656  }
   657  
   658  // Returns the name of the sender of this notification, accounting for things like system messages
   659  // and whether or not the username has been overridden by an integration.
   660  func (n *postNotification) GetSenderName(userNameFormat string, overridesAllowed bool) string {
   661  	if n.post.IsSystemMessage() {
   662  		return utils.T("system.message.name")
   663  	}
   664  
   665  	if overridesAllowed && n.channel.Type != model.CHANNEL_DIRECT {
   666  		if value, ok := n.post.Props["override_username"]; ok && n.post.Props["from_webhook"] == "true" {
   667  			return value.(string)
   668  		}
   669  	}
   670  
   671  	return n.sender.GetDisplayName(userNameFormat)
   672  }