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);
            }
        }
Exemplo n.º 2
0
        private void NotifyNewTopics(Topic topic, IUnitOfWork unitOfWork)
        {
            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(LoggedOnUser.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));
                        sb.AppendFormat("<p>{0}</p>", string.Concat(SettingsService.GetSettings().ForumUrl, topic.NiceUrl));

                        // create the emails only to people who haven't had notifications disabled
                        var emails = usersToNotify.Where(x => x.DisableEmailNotifications != true && !x.IsLockedOut).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);

                        unitOfWork.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();
                LoggingService.Error(ex);
            }
        }