Example #1
0
func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) {
	sessions := getMobileAppSessions(user.Id)

	if sessions == nil {
		return
	}

	var channelName string

	if channel.Type == model.CHANNEL_DIRECT {
		channelName = senderName
	} else {
		channelName = channel.DisplayName
	}

	userLocale := utils.GetUserTranslations(user.Locale)

	msg := model.PushNotification{}
	if badge := <-Srv.Store.User().GetUnreadCount(user.Id); badge.Err != nil {
		msg.Badge = 1
		l4g.Error(utils.T("store.sql_user.get_unread_count.app_error"), user.Id, badge.Err)
	} else {
		msg.Badge = int(badge.Data.(int64))
	}
	msg.Type = model.PUSH_TYPE_MESSAGE
	msg.ChannelId = channel.Id
	msg.ChannelName = channel.Name

	if *utils.Cfg.EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION {
		if channel.Type == model.CHANNEL_DIRECT {
			msg.Category = model.CATEGORY_DM
			msg.Message = "@" + senderName + ": " + model.ClearMentionTags(post.Message)
		} else {
			msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_in") + channelName + ": " + model.ClearMentionTags(post.Message)
		}
	} else {
		if channel.Type == model.CHANNEL_DIRECT {
			msg.Category = model.CATEGORY_DM
			msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message")
		} else if wasMentioned {
			msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName
		} else {
			msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention") + channelName
		}
	}

	l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message)

	for _, session := range sessions {
		tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
		tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
		sendToPushProxy(tmpMessage)
	}
}
Example #2
0
func (me *ServerTemplatePage) Render() string {
	var text bytes.Buffer

	T := utils.GetUserTranslations(me.Locale)
	me.Props["Footer"] = T("api.templates.email_footer")
	me.Html["EmailInfo"] = template.HTML(T("api.templates.email_info",
		map[string]interface{}{"SupportEmail": me.ClientCfg["SupportEmail"], "SiteName": me.ClientCfg["SiteName"]}))

	if err := ServerTemplates.ExecuteTemplate(&text, me.TemplateName, me); err != nil {
		l4g.Error(utils.T("api.api.render.error"), me.TemplateName, err)
	}

	return text.String()
}
Example #3
0
func sendBatchedEmailNotification(userId string, notifications []*batchedNotification) {
	uchan := Srv.Store.User().Get(userId)
	pchan := Srv.Store.Preference().Get(userId, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_DISPLAY_NAME_FORMAT)

	var user *model.User
	if result := <-uchan; result.Err != nil {
		l4g.Warn("api.email_batching.send_batched_email_notification.user.app_error")
		return
	} else {
		user = result.Data.(*model.User)
	}

	translateFunc := utils.GetUserTranslations(user.Locale)

	var displayNameFormat string
	if result := <-pchan; result.Err != nil && result.Err.DetailedError != sql.ErrNoRows.Error() {
		l4g.Warn("api.email_batching.send_batched_email_notification.preferences.app_error")
		return
	} else if result.Err != nil {
		// no display name format saved, so fall back to default
		displayNameFormat = model.PREFERENCE_DEFAULT_DISPLAY_NAME_FORMAT
	} else {
		displayNameFormat = result.Data.(model.Preference).Value
	}

	var contents string
	for _, notification := range notifications {
		template := utils.NewHTMLTemplate("post_batched_post", user.Locale)

		contents += renderBatchedPost(template, notification.post, notification.teamName, displayNameFormat, translateFunc)
	}

	tm := time.Unix(notifications[0].post.CreateAt/1000, 0)

	subject := translateFunc("api.email_batching.send_batched_email_notification.subject", len(notifications), map[string]interface{}{
		"SiteName": utils.Cfg.TeamSettings.SiteName,
		"Year":     tm.Year(),
		"Month":    translateFunc(tm.Month().String()),
		"Day":      tm.Day(),
	})

	body := utils.NewHTMLTemplate("post_batched_body", user.Locale)
	body.Props["SiteURL"] = *utils.Cfg.ServiceSettings.SiteURL
	body.Props["Posts"] = template.HTML(contents)
	body.Props["BodyText"] = translateFunc("api.email_batching.send_batched_email_notification.body_text", len(notifications))

	if err := utils.SendMail(user.Email, subject, body.Render()); err != nil {
		l4g.Warn(utils.T("api.email_batchings.send_batched_email_notification.send.app_error"), user.Email, err)
	}
}
Example #4
0
func TestGetMessageForNotification(t *testing.T) {
	Setup()
	translateFunc := utils.GetUserTranslations("en")

	post := &model.Post{
		Message:   "test",
		Filenames: model.StringArray{},
	}

	if getMessageForNotification(post, translateFunc) != "test" {
		t.Fatal("should've returned message text")
	}

	post.Filenames = model.StringArray{"test1.png"}
	if getMessageForNotification(post, translateFunc) != "test" {
		t.Fatal("should've returned message text, even with attachments")
	}

	post.Message = ""
	if message := getMessageForNotification(post, translateFunc); message != "1 image sent: test1.png" {
		t.Fatal("should've returned number of images:", message)
	}

	post.Filenames = model.StringArray{"test1.png", "test2.jpg"}
	if message := getMessageForNotification(post, translateFunc); message != "2 images sent: test1.png, test2.jpg" {
		t.Fatal("should've returned number of images:", message)
	}

	post.Filenames = model.StringArray{"test1.go"}
	if message := getMessageForNotification(post, translateFunc); message != "1 file sent: test1.go" {
		t.Fatal("should've returned number of files:", message)
	}

	post.Filenames = model.StringArray{"test1.go", "test2.jpg"}
	if message := getMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test2.jpg" {
		t.Fatal("should've returned number of mixed files:", message)
	}
}
Example #5
0
func sendOutOfChannelMentions(post *model.Post, teamId string, profiles map[string]*model.User) *model.AppError {
	if len(profiles) == 0 {
		return nil
	}

	var usernames []string
	for _, user := range profiles {
		usernames = append(usernames, user.Username)
	}
	sort.Strings(usernames)

	T := utils.GetUserTranslations(profiles[post.UserId].Locale)

	var message string
	if len(usernames) == 1 {
		message = T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{
			"Username": usernames[0],
		})
	} else {
		message = T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{
			"Usernames":    strings.Join(usernames[:len(usernames)-1], ", "),
			"LastUsername": usernames[len(usernames)-1],
		})
	}

	SendEphemeralPost(
		teamId,
		post.UserId,
		&model.Post{
			ChannelId: post.ChannelId,
			Message:   message,
			CreateAt:  post.CreateAt + 1,
		},
	)

	return nil
}
Example #6
0
func sendNotifications(c *Context, post *model.Post, team *model.Team, channel *model.Channel, profileMap map[string]*model.User, members []model.ChannelMember) {
	var channelName string
	var bodyText string
	var subjectText string

	var mentionedUsers []string

	if _, ok := profileMap[post.UserId]; !ok {
		l4g.Error(utils.T("api.post.send_notifications_and_forget.user_id.error"), post.UserId)
		return
	}
	senderName := profileMap[post.UserId].Username

	toEmailMap := make(map[string]bool)

	if channel.Type == model.CHANNEL_DIRECT {

		var otherUserId string
		if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId {
			otherUserId = userIds[1]
			channelName = profileMap[userIds[1]].Username
		} else {
			otherUserId = userIds[0]
			channelName = profileMap[userIds[0]].Username
		}

		otherUser := profileMap[otherUserId]
		sendEmail := true
		if _, ok := otherUser.NotifyProps["email"]; ok && otherUser.NotifyProps["email"] == "false" {
			sendEmail = false
		}
		if sendEmail && (otherUser.IsOffline() || otherUser.IsAway()) {
			toEmailMap[otherUserId] = true
		}

	} else {
		// Find out who is a member of the channel, only keep those profiles
		tempProfileMap := make(map[string]*model.User)
		for _, member := range members {
			tempProfileMap[member.UserId] = profileMap[member.UserId]
		}

		profileMap = tempProfileMap

		// Build map for keywords
		keywordMap := make(map[string][]string)
		for _, profile := range profileMap {
			if len(profile.NotifyProps["mention_keys"]) > 0 {

				// Add all the user's mention keys
				splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",")
				for _, k := range splitKeys {
					keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id)
				}
			}

			// If turned on, add the user's case sensitive first name
			if profile.NotifyProps["first_name"] == "true" {
				keywordMap[profile.FirstName] = append(keywordMap[profile.FirstName], profile.Id)
			}

			// Add @all to keywords if user has them turned on
			// if profile.NotifyProps["all"] == "true" {
			// 	keywordMap["@all"] = append(keywordMap["@all"], profile.Id)
			// }

			// Add @channel to keywords if user has them turned on
			if profile.NotifyProps["channel"] == "true" {
				keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id)
			}
		}

		// Build a map as a list of unique user_ids that are mentioned in this post
		splitF := func(c rune) bool {
			return model.SplitRunes[c]
		}
		splitMessage := strings.Fields(post.Message)
		for _, word := range splitMessage {
			var userIds []string

			// Non-case-sensitive check for regular keys
			if ids, match := keywordMap[strings.ToLower(word)]; match {
				userIds = append(userIds, ids...)
			}

			// Case-sensitive check for first name
			if ids, match := keywordMap[word]; match {
				userIds = append(userIds, ids...)
			}

			if len(userIds) == 0 {
				// No matches were found with the string split just on whitespace so try further splitting
				// the message on punctuation
				splitWords := strings.FieldsFunc(word, splitF)

				for _, splitWord := range splitWords {
					// Non-case-sensitive check for regular keys
					if ids, match := keywordMap[strings.ToLower(splitWord)]; match {
						userIds = append(userIds, ids...)
					}

					// Case-sensitive check for first name
					if ids, match := keywordMap[splitWord]; match {
						userIds = append(userIds, ids...)
					}
				}
			}

			for _, userId := range userIds {
				if post.UserId == userId && post.Props["from_webhook"] != "true" {
					continue
				}
				sendEmail := true
				if _, ok := profileMap[userId].NotifyProps["email"]; ok && profileMap[userId].NotifyProps["email"] == "false" {
					sendEmail = false
				}
				if sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) {
					toEmailMap[userId] = true
				} else {
					toEmailMap[userId] = false
				}
			}
		}

		for id := range toEmailMap {
			updateMentionCountAndForget(post.ChannelId, id)
		}
	}

	if len(toEmailMap) != 0 {
		mentionedUsers = make([]string, 0, len(toEmailMap))
		for k := range toEmailMap {
			mentionedUsers = append(mentionedUsers, k)
		}

		teamURL := c.GetSiteURL() + "/" + team.Name

		// Build and send the emails
		tm := time.Unix(post.CreateAt/1000, 0)

		for id, doSend := range toEmailMap {

			if !doSend {
				continue
			}

			// skip if inactive
			if profileMap[id].DeleteAt > 0 {
				continue
			}

			userLocale := utils.GetUserTranslations(profileMap[id].Locale)

			if channel.Type == model.CHANNEL_DIRECT {
				bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
				subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")
			} else {
				bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
				subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
				channelName = channel.DisplayName
			}

			month := userLocale(tm.Month().String())
			day := fmt.Sprintf("%d", tm.Day())
			year := fmt.Sprintf("%d", tm.Year())
			zone, _ := tm.Zone()

			subjectPage := utils.NewHTMLTemplate("post_subject", profileMap[id].Locale)
			subjectPage.Props["Subject"] = userLocale("api.templates.post_subject",
				map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
					"Month": month[:3], "Day": day, "Year": year})
			subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName

			bodyPage := utils.NewHTMLTemplate("post_body", profileMap[id].Locale)
			bodyPage.Props["SiteURL"] = c.GetSiteURL()
			bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message)
			bodyPage.Props["TeamLink"] = teamURL + "/channels/" + channel.Name
			bodyPage.Props["BodyText"] = bodyText
			bodyPage.Props["Button"] = userLocale("api.templates.post_body.button")
			bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info",
				map[string]interface{}{"ChannelName": channelName, "SenderName": senderName,
					"Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()),
					"TimeZone": zone, "Month": month, "Day": day}))

			// attempt to fill in a message body if the post doesn't have any text
			if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 {
				// extract the filenames from their paths and determine what type of files are attached
				filenames := make([]string, len(post.Filenames))
				onlyImages := true
				for i, filename := range post.Filenames {
					var err error
					if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil {
						// this should never error since filepath was escaped using url.QueryEscape
						filenames[i] = filepath.Base(filename)
					}

					ext := filepath.Ext(filename)
					onlyImages = onlyImages && model.IsFileExtImage(ext)
				}
				filenamesString := strings.Join(filenames, ", ")

				var attachmentPrefix string
				if onlyImages {
					attachmentPrefix = "Image"
				} else {
					attachmentPrefix = "File"
				}
				if len(post.Filenames) > 1 {
					attachmentPrefix += "s"
				}

				bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent",
					map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString})
			}

			if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil {
				l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err)
			}

			if *utils.Cfg.EmailSettings.SendPushNotifications {
				sessionChan := Srv.Store.Session().GetSessions(id)
				if result := <-sessionChan; result.Err != nil {
					l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), id, result.Err)
				} else {
					sessions := result.Data.([]*model.Session)
					alreadySeen := make(map[string]string)

					pushServer := *utils.Cfg.EmailSettings.PushNotificationServer
					if pushServer == model.MHPNS && (!utils.IsLicensed || !*utils.License.Features.MHPNS) {
						l4g.Warn(utils.T("api.post.send_notifications_and_forget.push_notification.mhpnsWarn"))
					} else {
						for _, session := range sessions {
							if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" &&
								(strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) {
								alreadySeen[session.DeviceId] = session.DeviceId

								msg := model.PushNotification{}
								if badge := <-Srv.Store.User().GetUnreadCount(id); badge.Err != nil {
									msg.Badge = 1
									l4g.Error(utils.T("store.sql_user.get_unread_count.app_error"), id, badge.Err)
								} else {
									msg.Badge = int(badge.Data.(int64))
								}
								msg.ServerId = utils.CfgDiagnosticId
								msg.ChannelId = channel.Id
								msg.ChannelName = channel.Name

								if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") {
									msg.Platform = model.PUSH_NOTIFY_APPLE
									msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":")
								} else if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") {
									msg.Platform = model.PUSH_NOTIFY_ANDROID
									msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")
								}

								if *utils.Cfg.EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION {
									if channel.Type == model.CHANNEL_DIRECT {
										msg.Category = model.CATEGORY_DM
										msg.Message = "@" + senderName + ": " + model.ClearMentionTags(post.Message)
									} else {
										msg.Message = "@" + senderName + " @ " + channelName + ": " + model.ClearMentionTags(post.Message)
									}
								} else {
									if channel.Type == model.CHANNEL_DIRECT {
										msg.Category = model.CATEGORY_DM
										msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message")
									} else {
										msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName
									}
								}

								tr := &http.Transport{
									TLSClientConfig: &tls.Config{InsecureSkipVerify: *utils.Cfg.ServiceSettings.EnableInsecureOutgoingConnections},
								}
								httpClient := &http.Client{Transport: tr}
								request, _ := http.NewRequest("POST", pushServer+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))

								l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message)
								if _, err := httpClient.Do(request); err != nil {
									l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), id, err)
								}
							}
						}
					}
				}
			}
		}
	}

	message := model.NewMessage(c.TeamId, post.ChannelId, post.UserId, model.ACTION_POSTED)
	message.Add("post", post.ToJson())
	message.Add("channel_type", channel.Type)

	if len(post.Filenames) != 0 {
		message.Add("otherFile", "true")

		for _, filename := range post.Filenames {
			ext := filepath.Ext(filename)
			if model.IsFileExtImage(ext) {
				message.Add("image", "true")
				break
			}
		}
	}

	if len(mentionedUsers) != 0 {
		message.Add("mentions", model.ArrayToJson(mentionedUsers))
	}

	PublishAndForget(message)
}
Example #7
0
func TestGetMessageForNotification(t *testing.T) {
	Setup().InitBasic()

	testPng := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{
		CreatorId: model.NewId(),
		Path:      "test1.png",
		Name:      "test1.png",
		MimeType:  "image/png",
	})).(*model.FileInfo)

	testJpg1 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{
		CreatorId: model.NewId(),
		Path:      "test2.jpg",
		Name:      "test2.jpg",
		MimeType:  "image/jpeg",
	})).(*model.FileInfo)

	testFile := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{
		CreatorId: model.NewId(),
		Path:      "test1.go",
		Name:      "test1.go",
		MimeType:  "text/plain",
	})).(*model.FileInfo)

	testJpg2 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{
		CreatorId: model.NewId(),
		Path:      "test3.jpg",
		Name:      "test3.jpg",
		MimeType:  "image/jpeg",
	})).(*model.FileInfo)

	translateFunc := utils.GetUserTranslations("en")

	post := &model.Post{
		Id:      model.NewId(),
		Message: "test",
	}

	if getMessageForNotification(post, translateFunc) != "test" {
		t.Fatal("should've returned message text")
	}

	post.FileIds = model.StringArray{testPng.Id}
	store.Must(Srv.Store.FileInfo().AttachToPost(testPng.Id, post.Id))
	if getMessageForNotification(post, translateFunc) != "test" {
		t.Fatal("should've returned message text, even with attachments")
	}

	post.Message = ""
	if message := getMessageForNotification(post, translateFunc); message != "1 image sent: test1.png" {
		t.Fatal("should've returned number of images:", message)
	}

	post.FileIds = model.StringArray{testPng.Id, testJpg1.Id}
	store.Must(Srv.Store.FileInfo().AttachToPost(testJpg1.Id, post.Id))
	if message := getMessageForNotification(post, translateFunc); message != "2 images sent: test1.png, test2.jpg" && message != "2 images sent: test2.jpg, test1.png" {
		t.Fatal("should've returned number of images:", message)
	}

	post.Id = model.NewId()
	post.FileIds = model.StringArray{testFile.Id}
	store.Must(Srv.Store.FileInfo().AttachToPost(testFile.Id, post.Id))
	if message := getMessageForNotification(post, translateFunc); message != "1 file sent: test1.go" {
		t.Fatal("should've returned number of files:", message)
	}

	store.Must(Srv.Store.FileInfo().AttachToPost(testJpg2.Id, post.Id))
	post.FileIds = model.StringArray{testFile.Id, testJpg2.Id}
	if message := getMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test3.jpg" && message != "2 files sent: test3.jpg, test1.go" {
		t.Fatal("should've returned number of mixed files:", message)
	}
}
Example #8
0
func sendNotificationEmail(c *Context, post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User) {
	// skip if inactive
	if user.DeleteAt > 0 {
		return
	}

	if channel.Type == model.CHANNEL_DIRECT && channel.TeamId != team.Id {
		// this message is a cross-team DM so it we need to find a team that the recipient is on to use in the link
		if result := <-Srv.Store.Team().GetTeamsByUserId(user.Id); result.Err != nil {
			l4g.Error(utils.T("api.post.send_notifications_and_forget.get_teams.error"), user.Id, result.Err)
			return
		} else {
			// if the recipient isn't in the current user's team, just pick one
			teams := result.Data.([]*model.Team)
			found := false

			for i := range teams {
				if teams[i].Id == team.Id {
					found = true
					break
				}
			}

			if !found {
				team = teams[0]
			}
		}
	}

	if *utils.Cfg.EmailSettings.EnableEmailBatching {
		var sendBatched bool

		if result := <-Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL); result.Err != nil {
			// if the call fails, assume it hasn't been set and use the default
			sendBatched = false
		} else {
			// default to not using batching if the setting is set to immediate
			sendBatched = result.Data.(model.Preference).Value != model.PREFERENCE_DEFAULT_EMAIL_INTERVAL
		}

		if sendBatched {
			if err := AddNotificationEmailToBatch(user, post, team); err == nil {
				return
			}
		}

		// fall back to sending a single email if we can't batch it for some reason
	}

	var channelName string
	var bodyText string
	var subjectText string
	var mailTemplate string
	var mailParameters map[string]interface{}

	teamURL := c.GetSiteURL() + "/" + team.Name
	tm := time.Unix(post.CreateAt/1000, 0)

	userLocale := utils.GetUserTranslations(user.Locale)
	month := userLocale(tm.Month().String())
	day := fmt.Sprintf("%d", tm.Day())
	year := fmt.Sprintf("%d", tm.Year())
	zone, _ := tm.Zone()

	if channel.Type == model.CHANNEL_DIRECT {
		bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")

		senderDisplayName := senderName
		if sender != nil {
			if result := <-Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, "name_format"); result.Err != nil {
				// Show default sender's name if user doesn't set display settings.
				senderDisplayName = senderName
			} else {
				senderDisplayName = sender.GetDisplayNameForPreference(result.Data.(model.Preference).Value)
			}
		}

		mailTemplate = "api.templates.post_subject_in_direct_message"
		mailParameters = map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
			"SenderDisplayName": senderDisplayName, "Month": month, "Day": day, "Year": year}
	} else {
		bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
		channelName = channel.DisplayName
		mailTemplate = "api.templates.post_subject_in_channel"
		mailParameters = map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
			"ChannelName": channelName, "Month": month, "Day": day, "Year": year}
	}

	subjectPage := utils.NewHTMLTemplate("post_subject", user.Locale)
	subjectPage.Props["Subject"] = userLocale(mailTemplate, mailParameters)
	subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName

	bodyPage := utils.NewHTMLTemplate("post_body", user.Locale)
	bodyPage.Props["SiteURL"] = c.GetSiteURL()
	bodyPage.Props["PostMessage"] = getMessageForNotification(post, userLocale)
	bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
	bodyPage.Props["BodyText"] = bodyText
	bodyPage.Props["Button"] = userLocale("api.templates.post_body.button")
	bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info",
		map[string]interface{}{"ChannelName": channelName, "SenderName": senderName,
			"Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()),
			"TimeZone": zone, "Month": month, "Day": day}))

	if err := utils.SendMail(user.Email, html.UnescapeString(subjectPage.Render()), bodyPage.Render()); err != nil {
		l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err)
	}
}
Example #9
0
func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) {
	var sessions []*model.Session
	if result := <-Srv.Store.Session().GetSessions(user.Id); result.Err != nil {
		l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), user.Id, result.Err)
		return
	} else {
		sessions = result.Data.([]*model.Session)
	}

	var channelName string

	if channel.Type == model.CHANNEL_DIRECT {
		channelName = senderName
	} else {
		channelName = channel.DisplayName
	}

	userLocale := utils.GetUserTranslations(user.Locale)

	for _, session := range sessions {
		if len(session.DeviceId) > 0 &&
			(strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) {

			msg := model.PushNotification{}
			if badge := <-Srv.Store.User().GetUnreadCount(user.Id); badge.Err != nil {
				msg.Badge = 1
				l4g.Error(utils.T("store.sql_user.get_unread_count.app_error"), user.Id, badge.Err)
			} else {
				msg.Badge = int(badge.Data.(int64))
			}
			msg.ServerId = utils.CfgDiagnosticId
			msg.ChannelId = channel.Id
			msg.ChannelName = channel.Name

			if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") {
				msg.Platform = model.PUSH_NOTIFY_APPLE
				msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":")
			} else if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") {
				msg.Platform = model.PUSH_NOTIFY_ANDROID
				msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")
			}

			if *utils.Cfg.EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION {
				if channel.Type == model.CHANNEL_DIRECT {
					msg.Category = model.CATEGORY_DM
					msg.Message = "@" + senderName + ": " + model.ClearMentionTags(post.Message)
				} else {
					msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_in") + channelName + ": " + model.ClearMentionTags(post.Message)
				}
			} else {
				if channel.Type == model.CHANNEL_DIRECT {
					msg.Category = model.CATEGORY_DM
					msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message")
				} else if wasMentioned {
					msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName
				} else {
					msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_non_mention") + channelName
				}
			}

			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: *utils.Cfg.ServiceSettings.EnableInsecureOutgoingConnections},
			}
			httpClient := &http.Client{Transport: tr}
			request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))

			l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message)
			if _, err := httpClient.Do(request); err != nil {
				l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), user.Id, err)
			}

			// notification sent, don't need to check other sessions
			break
		}
	}
}
Example #10
0
func sendNotificationEmail(c *Context, post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string) {
	// skip if inactive
	if user.DeleteAt > 0 {
		return
	}

	var channelName string
	var bodyText string
	var subjectText string

	teamURL := c.GetSiteURL() + "/" + team.Name
	tm := time.Unix(post.CreateAt/1000, 0)

	userLocale := utils.GetUserTranslations(user.Locale)

	if channel.Type == model.CHANNEL_DIRECT {
		bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")
		channelName = senderName
	} else {
		bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
		channelName = channel.DisplayName
	}

	month := userLocale(tm.Month().String())
	day := fmt.Sprintf("%d", tm.Day())
	year := fmt.Sprintf("%d", tm.Year())
	zone, _ := tm.Zone()

	subjectPage := utils.NewHTMLTemplate("post_subject", user.Locale)
	subjectPage.Props["Subject"] = userLocale("api.templates.post_subject",
		map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
			"Month": month[:3], "Day": day, "Year": year})
	subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName

	bodyPage := utils.NewHTMLTemplate("post_body", user.Locale)
	bodyPage.Props["SiteURL"] = c.GetSiteURL()
	bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message)
	bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
	bodyPage.Props["BodyText"] = bodyText
	bodyPage.Props["Button"] = userLocale("api.templates.post_body.button")
	bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info",
		map[string]interface{}{"ChannelName": channelName, "SenderName": senderName,
			"Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()),
			"TimeZone": zone, "Month": month, "Day": day}))

	// attempt to fill in a message body if the post doesn't have any text
	if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 {
		// extract the filenames from their paths and determine what type of files are attached
		filenames := make([]string, len(post.Filenames))
		onlyImages := true
		for i, filename := range post.Filenames {
			var err error
			if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil {
				// this should never error since filepath was escaped using url.QueryEscape
				filenames[i] = filepath.Base(filename)
			}

			ext := filepath.Ext(filename)
			onlyImages = onlyImages && model.IsFileExtImage(ext)
		}
		filenamesString := strings.Join(filenames, ", ")

		var attachmentPrefix string
		if onlyImages {
			attachmentPrefix = "Image"
		} else {
			attachmentPrefix = "File"
		}
		if len(post.Filenames) > 1 {
			attachmentPrefix += "s"
		}

		bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent",
			map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString})
	}

	if err := utils.SendMail(user.Email, subjectPage.Render(), bodyPage.Render()); err != nil {
		l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err)
	}
}
Example #11
0
func sendNotificationEmail(c *Context, post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User) {
	// skip if inactive
	if user.DeleteAt > 0 {
		return
	}

	if *utils.Cfg.EmailSettings.EnableEmailBatching {
		if err := AddNotificationEmailToBatch(user, post, team); err == nil {
			return
		}

		// fall back to sending a single email if we can't batch it for some reason
	}

	var channelName string
	var bodyText string
	var subjectText string
	var mailTemplate string
	var mailParameters map[string]interface{}

	teamURL := c.GetSiteURL() + "/" + team.Name
	tm := time.Unix(post.CreateAt/1000, 0)

	userLocale := utils.GetUserTranslations(user.Locale)
	month := userLocale(tm.Month().String())
	day := fmt.Sprintf("%d", tm.Day())
	year := fmt.Sprintf("%d", tm.Year())
	zone, _ := tm.Zone()

	if channel.Type == model.CHANNEL_DIRECT {
		bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")

		senderDisplayName := senderName
		if sender != nil {
			if result := <-Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, "name_format"); result.Err != nil {
				// Show default sender's name if user doesn't set display settings.
				senderDisplayName = senderName
			} else {
				senderDisplayName = sender.GetDisplayNameForPreference(result.Data.(model.Preference).Value)
			}
		}

		mailTemplate = "api.templates.post_subject_in_direct_message"
		mailParameters = map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
			"SenderDisplayName": senderDisplayName, "Month": month, "Day": day, "Year": year}
	} else {
		bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
		subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
		channelName = channel.DisplayName
		mailTemplate = "api.templates.post_subject_in_channel"
		mailParameters = map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
			"ChannelName": channelName, "Month": month, "Day": day, "Year": year}
	}

	subjectPage := utils.NewHTMLTemplate("post_subject", user.Locale)
	subjectPage.Props["Subject"] = userLocale(mailTemplate, mailParameters)
	subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName

	bodyPage := utils.NewHTMLTemplate("post_body", user.Locale)
	bodyPage.Props["SiteURL"] = c.GetSiteURL()
	bodyPage.Props["PostMessage"] = getMessageForNotification(post, userLocale)
	bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
	bodyPage.Props["BodyText"] = bodyText
	bodyPage.Props["Button"] = userLocale("api.templates.post_body.button")
	bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info",
		map[string]interface{}{"ChannelName": channelName, "SenderName": senderName,
			"Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()),
			"TimeZone": zone, "Month": month, "Day": day}))

	if err := utils.SendMail(user.Email, subjectPage.Render(), bodyPage.Render()); err != nil {
		l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err)
	}
}
Example #12
0
func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel) ([]string, *model.AppError) {
	pchan := Srv.Store.User().GetProfilesInChannel(channel.Id, -1, -1, true)
	fchan := Srv.Store.FileInfo().GetForPost(post.Id)

	var profileMap map[string]*model.User
	if result := <-pchan; result.Err != nil {
		return nil, result.Err
	} else {
		profileMap = result.Data.(map[string]*model.User)
	}

	// If the user who made the post isn't in the channel, don't send a notification
	if _, ok := profileMap[post.UserId]; !ok {
		l4g.Debug(utils.T("api.post.send_notifications.user_id.debug"), post.Id, channel.Id, post.UserId)
		return []string{}, nil
	}

	mentionedUserIds := make(map[string]bool)
	allActivityPushUserIds := []string{}
	hereNotification := false
	channelNotification := false
	allNotification := false
	updateMentionChans := []store.StoreChannel{}

	if channel.Type == model.CHANNEL_DIRECT {
		var otherUserId string
		if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId {
			otherUserId = userIds[1]
		} else {
			otherUserId = userIds[0]
		}

		mentionedUserIds[otherUserId] = true
		if post.Props["from_webhook"] == "true" {
			mentionedUserIds[post.UserId] = true
		}
	} else {
		keywords := GetMentionKeywordsInChannel(profileMap)

		var potentialOtherMentions []string
		mentionedUserIds, potentialOtherMentions, hereNotification, channelNotification, allNotification = GetExplicitMentions(post.Message, keywords)

		// get users that have comment thread mentions enabled
		if len(post.RootId) > 0 {
			if result := <-Srv.Store.Post().Get(post.RootId); result.Err != nil {
				return nil, result.Err
			} else {
				list := result.Data.(*model.PostList)

				for _, threadPost := range list.Posts {
					profile := profileMap[threadPost.UserId]
					if profile.NotifyProps["comments"] == "any" || (profile.NotifyProps["comments"] == "root" && threadPost.Id == list.Order[0]) {
						mentionedUserIds[threadPost.UserId] = true
					}
				}
			}
		}

		// prevent the user from mentioning themselves
		if post.Props["from_webhook"] != "true" {
			delete(mentionedUserIds, post.UserId)
		}

		if len(potentialOtherMentions) > 0 {
			if result := <-Srv.Store.User().GetProfilesByUsernames(potentialOtherMentions, team.Id); result.Err == nil {
				outOfChannelMentions := result.Data.(map[string]*model.User)
				go sendOutOfChannelMentions(post, team.Id, outOfChannelMentions)
			}
		}

		// 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 &&
				(post.UserId != profile.Id || post.Props["from_webhook"] == "true") &&
				!post.IsSystemMessage() {
				allActivityPushUserIds = append(allActivityPushUserIds, profile.Id)
			}
		}
	}

	mentionedUsersList := make([]string, 0, len(mentionedUserIds))
	for id := range mentionedUserIds {
		mentionedUsersList = append(mentionedUsersList, id)
		updateMentionChans = append(updateMentionChans, Srv.Store.Channel().IncrementMentionCount(post.ChannelId, id))
	}

	var sender *model.User
	senderName := make(map[string]string)
	for _, id := range mentionedUsersList {
		senderName[id] = ""
		if post.IsSystemMessage() {
			senderName[id] = utils.T("system.message.name")
		} else if profile, ok := profileMap[post.UserId]; ok {
			if value, ok := post.Props["override_username"]; ok && post.Props["from_webhook"] == "true" {
				senderName[id] = value.(string)
			} else {
				// Get the Display name preference from the receiver
				if result := <-Srv.Store.Preference().Get(id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, "name_format"); result.Err != nil {
					// Show default sender's name if user doesn't set display settings.
					senderName[id] = profile.Username
				} else {
					senderName[id] = profile.GetDisplayNameForPreference(result.Data.(model.Preference).Value)
				}
			}
			sender = profile
		}
	}

	var senderUsername string
	if value, ok := post.Props["override_username"]; ok && post.Props["from_webhook"] == "true" {
		senderUsername = value.(string)
	} else {
		senderUsername = profileMap[post.UserId].Username
	}

	if utils.Cfg.EmailSettings.SendEmailNotifications {
		for _, id := range mentionedUsersList {
			userAllowsEmails := profileMap[id].NotifyProps["email"] != "false"

			var status *model.Status
			var err *model.AppError
			if status, err = GetStatus(id); err != nil {
				status = &model.Status{
					UserId:         id,
					Status:         model.STATUS_OFFLINE,
					Manual:         false,
					LastActivityAt: 0,
					ActiveChannel:  "",
				}
			}

			if userAllowsEmails && status.Status != model.STATUS_ONLINE && profileMap[id].DeleteAt == 0 {
				sendNotificationEmail(post, profileMap[id], channel, team, senderName[id], sender)
			}
		}
	}

	T := utils.GetUserTranslations(profileMap[post.UserId].Locale)

	// If the channel has more than 1K users then @here is disabled
	if hereNotification && int64(len(profileMap)) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel {
		hereNotification = false
		SendEphemeralPost(
			team.Id,
			post.UserId,
			&model.Post{
				ChannelId: post.ChannelId,
				Message:   T("api.post.disabled_here", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}),
				CreateAt:  post.CreateAt + 1,
			},
		)
	}

	// If the channel has more than 1K users then @channel is disabled
	if channelNotification && int64(len(profileMap)) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel {
		SendEphemeralPost(
			team.Id,
			post.UserId,
			&model.Post{
				ChannelId: post.ChannelId,
				Message:   T("api.post.disabled_channel", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}),
				CreateAt:  post.CreateAt + 1,
			},
		)
	}

	// If the channel has more than 1K users then @all is disabled
	if allNotification && int64(len(profileMap)) > *utils.Cfg.TeamSettings.MaxNotificationsPerChannel {
		SendEphemeralPost(
			team.Id,
			post.UserId,
			&model.Post{
				ChannelId: post.ChannelId,
				Message:   T("api.post.disabled_all", map[string]interface{}{"Users": *utils.Cfg.TeamSettings.MaxNotificationsPerChannel}),
				CreateAt:  post.CreateAt + 1,
			},
		)
	}

	if hereNotification {
		if result := <-Srv.Store.Status().GetOnline(); result.Err != nil {
			return nil, result.Err
		} else {
			statuses := result.Data.([]*model.Status)
			for _, status := range statuses {
				if status.UserId == post.UserId {
					continue
				}

				_, profileFound := profileMap[status.UserId]
				_, alreadyMentioned := mentionedUserIds[status.UserId]

				if status.Status == model.STATUS_ONLINE && profileFound && !alreadyMentioned {
					mentionedUsersList = append(mentionedUsersList, status.UserId)
					updateMentionChans = append(updateMentionChans, Srv.Store.Channel().IncrementMentionCount(post.ChannelId, status.UserId))
				}
			}
		}
	}

	// Make sure all mention updates are complete to prevent race
	// Probably better to batch these DB updates in the future
	// MUST be completed before push notifications send
	for _, uchan := range updateMentionChans {
		if result := <-uchan; result.Err != nil {
			l4g.Warn(utils.T("api.post.update_mention_count_and_forget.update_error"), post.Id, post.ChannelId, result.Err)
		}
	}

	sendPushNotifications := false
	if *utils.Cfg.EmailSettings.SendPushNotifications {
		pushServer := *utils.Cfg.EmailSettings.PushNotificationServer
		if pushServer == model.MHPNS && (!utils.IsLicensed || !*utils.License.Features.MHPNS) {
			l4g.Warn(utils.T("api.post.send_notifications_and_forget.push_notification.mhpnsWarn"))
			sendPushNotifications = false
		} else {
			sendPushNotifications = true
		}
	}

	if sendPushNotifications {
		for _, id := range mentionedUsersList {
			var status *model.Status
			var err *model.AppError
			if status, err = GetStatus(id); err != nil {
				status = &model.Status{id, model.STATUS_OFFLINE, false, 0, ""}
			}

			if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) {
				sendPushNotification(post, profileMap[id], channel, senderName[id], true)
			}
		}

		for _, id := range allActivityPushUserIds {
			if _, ok := mentionedUserIds[id]; !ok {
				var status *model.Status
				var err *model.AppError
				if status, err = GetStatus(id); err != nil {
					status = &model.Status{id, model.STATUS_OFFLINE, false, 0, ""}
				}

				if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) {
					sendPushNotification(post, profileMap[id], channel, senderName[id], false)
				}
			}
		}
	}

	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POSTED, "", post.ChannelId, "", nil)
	message.Add("post", post.ToJson())
	message.Add("channel_type", channel.Type)
	message.Add("channel_display_name", channel.DisplayName)
	message.Add("channel_name", channel.Name)
	message.Add("sender_name", senderUsername)
	message.Add("team_id", team.Id)

	if len(post.FileIds) != 0 {
		message.Add("otherFile", "true")

		var infos []*model.FileInfo
		if result := <-fchan; result.Err != nil {
			l4g.Warn(utils.T("api.post.send_notifications.files.error"), post.Id, result.Err)
		} else {
			infos = result.Data.([]*model.FileInfo)
		}

		for _, info := range infos {
			if info.IsImage() {
				message.Add("image", "true")
				break
			}
		}
	}

	if len(mentionedUsersList) != 0 {
		message.Add("mentions", model.ArrayToJson(mentionedUsersList))
	}

	Publish(message)
	return mentionedUsersList, nil
}
Example #13
0
func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel) {

	go func() {
		// Get a list of user names (to be used as keywords) and ids for the given team
		uchan := Srv.Store.User().GetProfiles(c.Session.TeamId)
		echan := Srv.Store.Channel().GetMembers(post.ChannelId)

		var channelName string
		var bodyText string
		var subjectText string

		var mentionedUsers []string

		if result := <-uchan; result.Err != nil {
			l4g.Error(utils.T("api.post.send_notifications_and_forget.retrive_profiles.error"), c.Session.TeamId, result.Err)
			return
		} else {
			profileMap := result.Data.(map[string]*model.User)

			if _, ok := profileMap[post.UserId]; !ok {
				l4g.Error(utils.T("api.post.send_notifications_and_forget.user_id.error"), post.UserId)
				return
			}
			senderName := profileMap[post.UserId].Username

			toEmailMap := make(map[string]bool)

			if channel.Type == model.CHANNEL_DIRECT {

				var otherUserId string
				if userIds := strings.Split(channel.Name, "__"); userIds[0] == post.UserId {
					otherUserId = userIds[1]
					channelName = profileMap[userIds[1]].Username
				} else {
					otherUserId = userIds[0]
					channelName = profileMap[userIds[0]].Username
				}

				otherUser := profileMap[otherUserId]
				sendEmail := true
				if _, ok := otherUser.NotifyProps["email"]; ok && otherUser.NotifyProps["email"] == "false" {
					sendEmail = false
				}
				if sendEmail && (otherUser.IsOffline() || otherUser.IsAway()) {
					toEmailMap[otherUserId] = true
				}

			} else {

				// Find out who is a member of the channel, only keep those profiles
				if eResult := <-echan; eResult.Err != nil {
					l4g.Error(utils.T("api.post.send_notifications_and_forget.members.error"), post.ChannelId, eResult.Err.Message)
					return
				} else {
					tempProfileMap := make(map[string]*model.User)
					members := eResult.Data.([]model.ChannelMember)
					for _, member := range members {
						tempProfileMap[member.UserId] = profileMap[member.UserId]
					}

					profileMap = tempProfileMap
				}

				// Build map for keywords
				keywordMap := make(map[string][]string)
				for _, profile := range profileMap {
					if len(profile.NotifyProps["mention_keys"]) > 0 {

						// Add all the user's mention keys
						splitKeys := strings.Split(profile.NotifyProps["mention_keys"], ",")
						for _, k := range splitKeys {
							keywordMap[k] = append(keywordMap[strings.ToLower(k)], profile.Id)
						}
					}

					// If turned on, add the user's case sensitive first name
					if profile.NotifyProps["first_name"] == "true" {
						keywordMap[profile.FirstName] = append(keywordMap[profile.FirstName], profile.Id)
					}

					// Add @all to keywords if user has them turned on
					// if profile.NotifyProps["all"] == "true" {
					// 	keywordMap["@all"] = append(keywordMap["@all"], profile.Id)
					// }

					// Add @channel to keywords if user has them turned on
					if profile.NotifyProps["channel"] == "true" {
						keywordMap["@channel"] = append(keywordMap["@channel"], profile.Id)
					}
				}

				// Build a map as a list of unique user_ids that are mentioned in this post
				splitF := func(c rune) bool {
					return model.SplitRunes[c]
				}
				splitMessage := strings.Fields(post.Message)
				for _, word := range splitMessage {
					var userIds []string

					// Non-case-sensitive check for regular keys
					if ids, match := keywordMap[strings.ToLower(word)]; match {
						userIds = append(userIds, ids...)
					}

					// Case-sensitive check for first name
					if ids, match := keywordMap[word]; match {
						userIds = append(userIds, ids...)
					}

					if len(userIds) == 0 {
						// No matches were found with the string split just on whitespace so try further splitting
						// the message on punctuation
						splitWords := strings.FieldsFunc(word, splitF)

						for _, splitWord := range splitWords {
							// Non-case-sensitive check for regular keys
							if ids, match := keywordMap[strings.ToLower(splitWord)]; match {
								userIds = append(userIds, ids...)
							}

							// Case-sensitive check for first name
							if ids, match := keywordMap[splitWord]; match {
								userIds = append(userIds, ids...)
							}
						}
					}

					for _, userId := range userIds {
						if post.UserId == userId {
							continue
						}
						sendEmail := true
						if _, ok := profileMap[userId].NotifyProps["email"]; ok && profileMap[userId].NotifyProps["email"] == "false" {
							sendEmail = false
						}
						if sendEmail && (profileMap[userId].IsAway() || profileMap[userId].IsOffline()) {
							toEmailMap[userId] = true
						} else {
							toEmailMap[userId] = false
						}
					}
				}

				for id := range toEmailMap {
					updateMentionCountAndForget(post.ChannelId, id)
				}
			}

			if len(toEmailMap) != 0 {
				mentionedUsers = make([]string, 0, len(toEmailMap))
				for k := range toEmailMap {
					mentionedUsers = append(mentionedUsers, k)
				}

				teamURL := c.GetSiteURL() + "/" + team.Name

				// Build and send the emails
				tm := time.Unix(post.CreateAt/1000, 0)

				for id, doSend := range toEmailMap {

					if !doSend {
						continue
					}

					// skip if inactive
					if profileMap[id].DeleteAt > 0 {
						continue
					}

					userLocale := utils.GetUserTranslations(profileMap[id].Locale)

					if channel.Type == model.CHANNEL_DIRECT {
						bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
						subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")
					} else {
						bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
						subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
						channelName = channel.DisplayName
					}

					subjectPage := NewServerTemplatePage("post_subject")
					subjectPage.Props["SiteURL"] = c.GetSiteURL()
					subjectPage.Props["TeamDisplayName"] = team.DisplayName
					subjectPage.Props["SubjectText"] = subjectText
					subjectPage.Props["Month"] = tm.Month().String()[:3]
					subjectPage.Props["Day"] = fmt.Sprintf("%d", tm.Day())
					subjectPage.Props["Year"] = fmt.Sprintf("%d", tm.Year())

					bodyPage := NewServerTemplatePage("post_body")
					bodyPage.Props["SiteURL"] = c.GetSiteURL()
					bodyPage.Props["Nickname"] = profileMap[id].FirstName
					bodyPage.Props["TeamDisplayName"] = team.DisplayName
					bodyPage.Props["ChannelName"] = channelName
					bodyPage.Props["BodyText"] = bodyText
					bodyPage.Props["SenderName"] = senderName
					bodyPage.Props["Hour"] = fmt.Sprintf("%02d", tm.Hour())
					bodyPage.Props["Minute"] = fmt.Sprintf("%02d", tm.Minute())
					bodyPage.Props["Month"] = tm.Month().String()[:3]
					bodyPage.Props["Day"] = fmt.Sprintf("%d", tm.Day())
					bodyPage.Props["TimeZone"], _ = tm.Zone()
					bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message)
					bodyPage.Props["TeamLink"] = teamURL + "/channels/" + channel.Name

					// attempt to fill in a message body if the post doesn't have any text
					if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 {
						// extract the filenames from their paths and determine what type of files are attached
						filenames := make([]string, len(post.Filenames))
						onlyImages := true
						for i, filename := range post.Filenames {
							var err error
							if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil {
								// this should never error since filepath was escaped using url.QueryEscape
								filenames[i] = filepath.Base(filename)
							}

							ext := filepath.Ext(filename)
							onlyImages = onlyImages && model.IsFileExtImage(ext)
						}
						filenamesString := strings.Join(filenames, ", ")

						var attachmentPrefix string
						if onlyImages {
							attachmentPrefix = "Image"
						} else {
							attachmentPrefix = "File"
						}
						if len(post.Filenames) > 1 {
							attachmentPrefix += "s"
						}

						bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent",
							map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString})
					}

					if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil {
						l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), profileMap[id].Email, err)
					}

					if *utils.Cfg.EmailSettings.SendPushNotifications {
						sessionChan := Srv.Store.Session().GetSessions(id)
						if result := <-sessionChan; result.Err != nil {
							l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), id, result.Err)
						} else {
							sessions := result.Data.([]*model.Session)
							alreadySeen := make(map[string]string)

							for _, session := range sessions {
								if len(session.DeviceId) > 0 && alreadySeen[session.DeviceId] == "" &&
									(strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")) {
									alreadySeen[session.DeviceId] = session.DeviceId

									msg := model.PushNotification{}
									msg.Badge = 1
									msg.ServerId = utils.CfgDiagnosticId

									if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":") {
										msg.Platform = model.PUSH_NOTIFY_APPLE
										msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_APPLE+":")
									} else if strings.HasPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":") {
										msg.Platform = model.PUSH_NOTIFY_ANDROID
										msg.DeviceId = strings.TrimPrefix(session.DeviceId, model.PUSH_NOTIFY_ANDROID+":")
									}

									if channel.Type == model.CHANNEL_DIRECT {
										msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_message")
									} else {
										msg.Message = senderName + userLocale("api.post.send_notifications_and_forget.push_mention") + channelName
									}

									httpClient := http.Client{}
									request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+"/api/v1/send_push", strings.NewReader(msg.ToJson()))

									l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message)
									if _, err := httpClient.Do(request); err != nil {
										l4g.Error(utils.T("api.post.send_notifications_and_forget.push_notification.error"), id, err)
									}
								}
							}
						}
					}
				}
			}
		}

		message := model.NewMessage(c.Session.TeamId, post.ChannelId, post.UserId, model.ACTION_POSTED)
		message.Add("post", post.ToJson())

		if len(post.Filenames) != 0 {
			message.Add("otherFile", "true")

			for _, filename := range post.Filenames {
				ext := filepath.Ext(filename)
				if model.IsFileExtImage(ext) {
					message.Add("image", "true")
					break
				}
			}
		}

		if len(mentionedUsers) != 0 {
			message.Add("mentions", model.ArrayToJson(mentionedUsers))
		}

		PublishAndForget(message)
	}()
}