private void NotifyNewTopics(Topic topic, MembershipUser loggedOnReadOnlyUser)
        {
            try
            {
                // Get all notifications for this category
                var notifications = _topicNotificationService.GetByTopic(topic).Select(x => x.User.Id).ToList();

                if (notifications.Any())
                {
                    // remove the current user from the notification, don't want to notify yourself that you
                    // have just made a topic!
                    notifications.Remove(loggedOnReadOnlyUser.Id);

                    if (notifications.Count > 0)
                    {
                        // Now get all the users that need notifying
                        var usersToNotify = MembershipService.GetUsersById(notifications);

                        // Create the email
                        var sb = new StringBuilder();
                        sb.AppendFormat("<p>{0}</p>",
                                        string.Format(LocalizationService.GetResourceString("Post.Notification.NewPosts"),
                                                      topic.Name));
                        if (SiteConstants.Instance.IncludeFullPostInEmailNotifications)
                        {
                            sb.Append(AppHelpers.ConvertPostContent(topic.LastPost.PostContent));
                        }
                        sb.AppendFormat("<p><a href=\"{0}\">{0}</a></p>", string.Concat(Domain, topic.NiceUrl));

                        // create the emails only to people who haven't had notifications disabled
                        var emails = usersToNotify
                                     .Where(x => x.DisableEmailNotifications != true && !x.IsLockedOut && x.IsBanned != true)
                                     .Select(user => new Email
                        {
                            Body    = _emailService.EmailTemplate(user.UserName, sb.ToString()),
                            EmailTo = user.Email,
                            NameTo  = user.UserName,
                            Subject = string.Concat(
                                LocalizationService.GetResourceString("Post.Notification.Subject"),
                                SettingsService.GetSettings().ForumName)
                        }).ToList();

                        // and now pass the emails in to be sent
                        _emailService.SendMail(emails);

                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
            }
        }
        public ActionResult Create(CreatePrivateMessageViewModel createPrivateMessageViewModel)
        {
            var settings = SettingsService.GetSettings();

            if (!settings.EnablePrivateMessages || LoggedOnReadOnlyUser.DisablePrivateMessages == true)
            {
                throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                if (ModelState.IsValid)
                {
                    var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser.Id);
                    var memberTo     = MembershipService.GetUser(createPrivateMessageViewModel.To);

                    // Check the user they are trying to message hasn't blocked them
                    if (loggedOnUser.BlockedByOtherUsers.Any(x => x.Blocker.Id == memberTo.Id))
                    {
                        return(Content(PmAjaxError(LocalizationService.GetResourceString("PM.BlockedMessage"))));
                    }

                    // Check flood control
                    var lastMessage = _privateMessageService.GetLastSentPrivateMessage(LoggedOnReadOnlyUser.Id);
                    // If this message they are sending now, is to the same person then ignore flood control
                    if (lastMessage != null && createPrivateMessageViewModel.To != lastMessage.UserTo.Id)
                    {
                        if (DateUtils.TimeDifferenceInSeconds(DateTime.UtcNow, lastMessage.DateSent) < settings.PrivateMessageFloodControl)
                        {
                            return(Content(PmAjaxError(LocalizationService.GetResourceString("PM.SendingToQuickly"))));
                        }
                    }

                    // first check they are not trying to message themself!
                    if (memberTo != null)
                    {
                        // Map the view model to message
                        var privateMessage = new PrivateMessage
                        {
                            UserFrom = loggedOnUser,
                            Message  = createPrivateMessageViewModel.Message,
                        };

                        // Check settings
                        if (settings.EnableEmoticons == true)
                        {
                            privateMessage.Message = _configService.Emotify(privateMessage.Message);
                        }

                        // check the member
                        if (!String.Equals(memberTo.UserName, LoggedOnReadOnlyUser.UserName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            // Check in box size for both
                            var receiverCount = _privateMessageService.GetAllReceivedByUser(memberTo.Id).Count;
                            if (receiverCount > settings.MaxPrivateMessagesPerMember)
                            {
                                return(Content(string.Format(LocalizationService.GetResourceString("PM.ReceivedItemsOverCapcity"), memberTo.UserName)));
                            }

                            // If the receiver is about to go over the allowance them let then know too
                            if (receiverCount > (settings.MaxPrivateMessagesPerMember - SiteConstants.Instance.PrivateMessageWarningAmountLessThanAllowedSize))
                            {
                                // Send user a warning they are about to exceed
                                var sb = new StringBuilder();
                                sb.Append($"<p>{LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody")}</p>");
                                var email = new Email
                                {
                                    EmailTo = memberTo.Email,
                                    NameTo  = memberTo.UserName,
                                    Subject = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                                };
                                email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                                _emailService.SendMail(email);
                            }

                            // Good to go send the message!
                            privateMessage.UserTo = memberTo;
                            _privateMessageService.Add(privateMessage);

                            try
                            {
                                // Finally send an email to the user so they know they have a new private message
                                // As long as they have not had notifications disabled
                                if (memberTo.DisableEmailNotifications != true)
                                {
                                    var email = new Email
                                    {
                                        EmailTo = memberTo.Email,
                                        Subject = LocalizationService.GetResourceString("PM.NewPrivateMessageSubject"),
                                        NameTo  = memberTo.UserName
                                    };

                                    var sb = new StringBuilder();
                                    sb.Append($"<p>{string.Format(LocalizationService.GetResourceString("PM.NewPrivateMessageBody"), LoggedOnReadOnlyUser.UserName)}</p>");
                                    sb.Append(AppHelpers.ConvertPostContent(createPrivateMessageViewModel.Message));
                                    email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                                    _emailService.SendMail(email);
                                }

                                unitOfWork.Commit();

                                return(PartialView("_PrivateMessage", privateMessage));
                            }
                            catch (Exception ex)
                            {
                                unitOfWork.Rollback();
                                LoggingService.Error(ex);
                                return(Content(PmAjaxError(LocalizationService.GetResourceString("Errors.GenericMessage"))));
                            }
                        }
                        else
                        {
                            return(Content(PmAjaxError(LocalizationService.GetResourceString("PM.TalkToSelf"))));
                        }
                    }
                    else
                    {
                        // Error send back to user
                        return(Content(PmAjaxError(LocalizationService.GetResourceString("PM.UnableFindMember"))));
                    }
                }
                return(Content(PmAjaxError(LocalizationService.GetResourceString("Errors.GenericMessage"))));
            }
        }