コード例 #1
0
ファイル: VoteController.cs プロジェクト: zhyqhb/mvcforum
        private async Task <bool> MarkPostUpOrDown(Post post, MembershipUser postWriter, MembershipUser voter, PostType postType,
                                                   MembershipUser loggedOnReadOnlyUser)
        {
            var settings = SettingsService.GetSettings();

            // Check this user is not the post owner
            if (voter.Id != postWriter.Id)
            {
                // Not the same person, now check they haven't voted on this post before
                var votes = post.Votes.Where(x => x.VotedByMembershipUser.Id == loggedOnReadOnlyUser.Id).ToList();
                if (votes.Any())
                {
                    // Already voted, so delete the vote and remove the points
                    var votesToDelete = new List <Vote>();
                    votesToDelete.AddRange(votes);
                    foreach (var vote in votesToDelete)
                    {
                        _voteService.Delete(vote);
                    }

                    // Update the post with the new points amount
                    var newPointTotal = postType == PostType.Negative ? post.VoteCount + 1 : post.VoteCount - 1;
                    post.VoteCount = newPointTotal;
                }
                else
                {
                    // Points to add or subtract to a user
                    var usersPoints = postType == PostType.Negative
                        ? -settings.PointsDeductedNagativeVote
                        : settings.PointsAddedPostiveVote;

                    // Update the post with the new vote of the voter
                    var vote = new Vote
                    {
                        Post   = post,
                        User   = postWriter,
                        Amount = postType == PostType.Negative ? -1 : 1,
                        VotedByMembershipUser = voter,
                        DateVoted             = DateTime.UtcNow
                    };
                    _voteService.Add(vote);

                    // Update the users points who wrote the post
                    await _membershipUserPointsService.Add(new MembershipUserPoints
                    {
                        Points      = usersPoints,
                        User        = postWriter,
                        PointsFor   = PointsFor.Vote,
                        PointsForId = vote.Id
                    });

                    // Update the post with the new points amount
                    var newPointTotal = postType == PostType.Negative ? post.VoteCount - 1 : post.VoteCount + 1;
                    post.VoteCount = newPointTotal;
                }
            }

            return(true);
        }
コード例 #2
0
        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);
            }
        }