Ejemplo n.º 1
0
        // submit comment upvote
        public static void UpvoteComment(int commentId, string userWhichUpvoted, string clientIpHash)
        {
            int result = CheckIfVotedComment(userWhichUpvoted, commentId);

            using (voatEntities db = new voatEntities())
            {
                Comment comment = db.Comments.Find(commentId);

                if (comment.Submission.IsAnonymized)
                {
                    // do not execute voting, subverse is in anonymized mode
                    return;
                }

                switch (result)
                {
                // never voted before
                case 0:

                    if (comment.UserName != userWhichUpvoted)
                    {
                        // check if this IP already voted on the same comment, abort voting if true
                        var ipVotedAlready = db.CommentVoteTrackers.Where(x => x.CommentID == commentId && x.IPAddress == clientIpHash);
                        if (ipVotedAlready.Any())
                        {
                            return;
                        }

                        comment.UpCount++;

                        // register upvote
                        var tmpVotingTracker = new CommentVoteTracker
                        {
                            CommentID    = commentId,
                            UserName     = userWhichUpvoted,
                            VoteStatus   = 1,
                            CreationDate = DateTime.Now,
                            IPAddress    = clientIpHash
                        };
                        db.CommentVoteTrackers.Add(tmpVotingTracker);
                        db.SaveChanges();

                        Voting.SendVoteNotification(comment.UserName, "upvote");
                    }

                    break;

                // downvoted before, turn downvote to upvote
                case -1:

                    if (comment.UserName != userWhichUpvoted)
                    {
                        comment.UpCount++;
                        comment.DownCount--;

                        // register Turn DownVote To UpVote
                        var votingTracker = db.CommentVoteTrackers.FirstOrDefault(b => b.CommentID == commentId && b.UserName == userWhichUpvoted);

                        if (votingTracker != null)
                        {
                            votingTracker.VoteStatus   = 1;
                            votingTracker.CreationDate = DateTime.Now;
                        }
                        db.SaveChanges();

                        Voting.SendVoteNotification(comment.UserName, "downtoupvote");
                    }

                    break;

                // upvoted before, reset
                case 1:

                    comment.UpCount--;
                    db.SaveChanges();

                    Voting.SendVoteNotification(comment.UserName, "downvote");

                    ResetCommentVote(userWhichUpvoted, commentId);

                    break;
                }
            }
        }
Ejemplo n.º 2
0
        // submit submission downvote
        public static void DownvoteComment(int commentId, string userWhichDownvoted, string clientIpHash)
        {
            int result = CheckIfVotedComment(userWhichDownvoted, commentId);

            using (voatEntities db = new voatEntities())
            {
                Comment comment = db.Comments.Find(commentId);

                // do not execute downvoting, subverse is in anonymized mode
                if (comment.Submission.IsAnonymized)
                {
                    return;
                }

                // do not execute downvoting if user has insufficient CCP for target subverse
                if (Karma.CommentKarmaForSubverse(userWhichDownvoted, comment.Submission.Subverse) < comment.Submission.Subverse1.MinCCPForDownvote)
                {
                    return;
                }

                switch (result)
                {
                // never voted before
                case 0:

                {
                    // this user is downvoting more than upvoting, don't register the downvote
                    if (UserHelper.IsUserCommentVotingMeanie(userWhichDownvoted))
                    {
                        return;
                    }

                    // check if this IP already voted on the same comment, abort voting if true
                    var ipVotedAlready = db.CommentVoteTrackers.Where(x => x.CommentID == commentId && x.IPAddress == clientIpHash);
                    if (ipVotedAlready.Any())
                    {
                        return;
                    }

                    comment.DownCount++;

                    // register downvote
                    var tmpVotingTracker = new CommentVoteTracker
                    {
                        CommentID    = commentId,
                        UserName     = userWhichDownvoted,
                        VoteStatus   = -1,
                        CreationDate = DateTime.Now,
                        IPAddress    = clientIpHash
                    };
                    db.CommentVoteTrackers.Add(tmpVotingTracker);
                    db.SaveChanges();

                    Voting.SendVoteNotification(comment.UserName, "downvote");
                }

                break;

                // upvoted before, turn upvote to downvote
                case 1:

                {
                    comment.UpCount--;
                    comment.DownCount++;

                    //register Turn DownVote To UpVote
                    var votingTracker = db.CommentVoteTrackers.FirstOrDefault(b => b.CommentID == commentId && b.UserName == userWhichDownvoted);

                    if (votingTracker != null)
                    {
                        votingTracker.VoteStatus   = -1;
                        votingTracker.CreationDate = DateTime.Now;
                    }
                    db.SaveChanges();

                    Voting.SendVoteNotification(comment.UserName, "uptodownvote");
                }

                break;

                // downvoted before, reset
                case -1:

                    comment.DownCount--;
                    db.SaveChanges();
                    ResetCommentVote(userWhichDownvoted, commentId);

                    Voting.SendVoteNotification(comment.UserName, "upvote");

                    break;
                }
            }
        }