/// <inheritdoc />
        public void DeleteSection(Guid id)
        {
            var section = _context.Section.Find(id);

            if (section != null)
            {
                _context.Section.Remove(section);
                _context.SaveChanges();
            }
        }
        /// <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);
                    }
                }
            }
        }
Exemple #3
0
        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);
            }
        }
Exemple #4
0
        /// <inheritdoc />
        public void RefreshEditedPoll(Topic originalTopic, IList <PollAnswer> pollAnswers, int pollCloseAfterDays)
        {
            var panswers = pollAnswers.Where(x => !string.IsNullOrWhiteSpace(x?.Answer)).ToArray();

            if (panswers.Any() && originalTopic.Poll != null)
            {
                // Now sort the poll answers, what to add and what to remove
                // Poll answers already in this poll.
                var newPollAnswerIds = panswers.Select(x => x.Id);

                // This post might not have a poll on it, if not they are creating a poll for the first time
                var originalPollAnswerIds = originalTopic.Poll.PollAnswers.Select(p => p.Id).ToList();
                var pollAnswersToRemove   = originalTopic.Poll.PollAnswers.Where(x => !newPollAnswerIds.Contains(x.Id))
                                            .ToList();

                // Set the amount of days to close the poll
                originalTopic.Poll.ClosePollAfterDays = pollCloseAfterDays;

                // Get existing answers
                var existingAnswers = panswers.Where(x =>
                                                     !string.IsNullOrWhiteSpace(x.Answer) && originalPollAnswerIds.Contains(x.Id)).ToList();

                // Get new poll answers to add
                var newPollAnswers = panswers.Where(x =>
                                                    !string.IsNullOrWhiteSpace(x.Answer) && !originalPollAnswerIds.Contains(x.Id)).ToList();

                // Loop through existing and update names if need be
                // If name changes remove the poll
                foreach (var existPollAnswer in existingAnswers)
                {
                    // Get the existing answer from the current topic
                    var pa = originalTopic.Poll.PollAnswers.FirstOrDefault(x => x.Id == existPollAnswer.Id);
                    if (pa != null && pa.Answer != existPollAnswer.Answer)
                    {
                        var pollVotestToRemove = new List <PollVote>();
                        pollVotestToRemove.AddRange(pa.PollVotes);
                        // Remove all the poll votes, as the answer has changed
                        foreach (var answerPollVote in pollVotestToRemove)
                        {
                            pa.PollVotes.Remove(answerPollVote);
                            Delete(answerPollVote);
                        }
                        pa.PollVotes.Clear();
                        _context.SaveChanges();

                        // If the answer has changed then update it
                        pa.Answer = existPollAnswer.Answer;
                    }
                }

                // Save existing
                _context.SaveChanges();

                // Loop through and remove the old poll answers and delete
                foreach (var oldPollAnswer in pollAnswersToRemove)
                {
                    // Clear poll votes if it's changed
                    var pollVotestToRemove = new List <PollVote>();
                    pollVotestToRemove.AddRange(oldPollAnswer.PollVotes);
                    foreach (var answerPollVote in pollVotestToRemove)
                    {
                        oldPollAnswer.PollVotes.Remove(answerPollVote);
                        Delete(answerPollVote);
                    }
                    oldPollAnswer.PollVotes.Clear();
                    _context.SaveChanges();

                    // Remove from Poll
                    originalTopic.Poll.PollAnswers.Remove(oldPollAnswer);

                    // Delete
                    Delete(oldPollAnswer);
                }

                // Save removed
                _context.SaveChanges();

                // Poll answers to add
                foreach (var newPollAnswer in newPollAnswers)
                {
                    if (newPollAnswer != null)
                    {
                        var npa = new PollAnswer
                        {
                            Poll   = originalTopic.Poll,
                            Answer = newPollAnswer.Answer
                        };
                        Add(npa);
                        originalTopic.Poll.PollAnswers.Add(npa);
                    }
                }
            }
            else if (originalTopic.Poll != null)
            {
                // Now delete the poll
                Delete(originalTopic.Poll);

                // Remove from topic.
                originalTopic.Poll = null;
            }
        }