/// <inheritdoc /> public void Notify(Topic topic, MembershipUser loggedOnReadOnlyUser, NotificationType notificationType) { List <Guid> userIdsToNotify; var settings = _settingsService.GetSettings(); if (notificationType == NotificationType.Post) { userIdsToNotify = GetTopicNotificationsByTopic(topic).Select(x => x.User.Id).ToList(); } else { // Get all notifications for this category and for the tags on the topic userIdsToNotify = GetCategoryNotificationsByCategory(topic.Category).Select(x => x.User.Id).ToList(); // Merge and remove duplicate ids if (topic.Tags != null && topic.Tags.Any()) { var tagNotifications = GetTagNotificationsByTag(topic.Tags.ToList()).Select(x => x.User.Id) .ToList(); userIdsToNotify = userIdsToNotify.Union(tagNotifications).ToList(); } } if (userIdsToNotify.Any()) { // remove the current user from the notification, don't want to notify yourself that you // have just made a topic! userIdsToNotify.Remove(loggedOnReadOnlyUser.Id); if (userIdsToNotify.Count > 0) { try { // Now get all the users that need notifying var users = _context.MembershipUser .Where(x => userIdsToNotify.Contains(x.Id)) .AsNoTracking() .ToList(); // Create the email var sb = new StringBuilder(); sb.AppendFormat("<p>{0}</p>", string.Format(_localizationService.GetResourceString(notificationType == NotificationType.Post ? "Post.Notification.NewPosts" : "Topic.Notification.NewTopics"), topic.Category.Name)); sb.Append($"<p>{topic.Name}</p>"); if (ForumConfiguration.Instance.IncludeFullPostInEmailNotifications) { sb.Append(topic.LastPost.PostContent.ConvertPostContent()); } sb.AppendFormat("<p><a href=\"{0}\">{0}</a></p>", string.Concat(StringUtils.ReturnCurrentDomain(), topic.Category.NiceUrl)); // create the emails and only send them to people who have not had notifications disabled var emails = users .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(notificationType == NotificationType.Post ? "Post.Notification.Subject" : "Topic.Notification.Subject"), settings.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 void SendMarkAsSolutionReminders() { try { var settings = _settingsService.GetSettings(false); var timeFrame = settings.MarkAsSolutionReminderTimeFrame ?? 0; if (timeFrame > 0 && settings.EnableMarkAsSolution) { var remindersToSend = _topicService.GetMarkAsSolutionReminderList(timeFrame); if (remindersToSend.Any()) { var amount = remindersToSend.Count; // Use settings days amount and also mark topics as reminded // Only send if markassolution is enabled and day is not 0 var emailListToSend = new List <Email>(); foreach (var markAsSolutionReminder in remindersToSend) { // Topic Link var topicLink = $"<a href=\"{settings.ForumUrl.TrimEnd('/')}{markAsSolutionReminder.Topic.NiceUrl}\">{markAsSolutionReminder.Topic.Name}</a>"; // Create the email var sb = new StringBuilder(); sb.AppendFormat("<p>{0}</p>", string.Format( _localizationService.GetResourceString("Tasks.MarkAsSolutionReminderJob.EmailBody"), topicLink, settings.ForumName, markAsSolutionReminder.PostCount)); // create the emails and only send them to people who have not had notifications disabled var user = markAsSolutionReminder.Topic.User; var email = new Email { Body = _emailService.EmailTemplate(user.UserName, sb.ToString(), settings), EmailTo = user.Email, NameTo = user.UserName, Subject = string.Format( _localizationService.GetResourceString( "Tasks.MarkAsSolutionReminderJob.Subject"), settings.ForumName) }; // Add to list emailListToSend.Add(email); // And now mark the topic as reminder sent markAsSolutionReminder.Topic.SolvedReminderSent = true; } // and now pass the emails in to be sent // We have to pass the current settings to SendMail when it's within a task _emailService.SendMail(emailListToSend, settings); _context.SaveChanges(); _loggingService.Error($"{amount} Mark as solution reminder emails sent"); } } } catch (Exception ex) { _context.RollBack(); _loggingService.Error(ex); } }