Пример #1
0
        // submit submission downvote
        public static void DownvoteSubmission(int submissionID, string userName, string clientIp)
        {
            //int result = CheckIfVoted(userWhichDownvoted, submissionId);

            using (var db = new voatEntities())
            {
                Submission submission = db.Submissions.Find(submissionID);

                SubmissionVoteTracker previousVote = db.SubmissionVoteTrackers.Where(u => u.UserName == userName && u.SubmissionID == submissionID).FirstOrDefault();

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

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

                int result = (previousVote == null ? 0 : previousVote.VoteStatus.Value);

                switch (result)
                {
                    // never voted before
                    case 0:
                        {
                            // this user is downvoting more than upvoting, don't register the downvote
                            if (UserHelper.IsUserCommentVotingMeanie(userName))
                            {
                                return;
                            }

                            // check if this IP already voted on the same submission, abort voting if true
                            var ipVotedAlready = db.SubmissionVoteTrackers.Where(x => x.SubmissionID == submissionID && x.IPAddress == clientIp);
                            if (ipVotedAlready.Any()) return;

                            submission.DownCount++;

                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;

                            // register downvote
                            var tmpVotingTracker = new SubmissionVoteTracker
                            {
                                SubmissionID = submissionID,
                                UserName = userName,
                                VoteStatus = -1,
                                CreationDate = DateTime.Now,
                                IPAddress = clientIp
                            };
                            db.SubmissionVoteTrackers.Add(tmpVotingTracker);
                            db.SaveChanges();

                            SendVoteNotification(submission.UserName, "downvote");
                        }

                        break;

                    // upvoted before, turn upvote to downvote
                    case 1:
                        {
                            submission.UpCount--;
                            submission.DownCount++;

                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;

                            // register Turn DownVote To UpVote
                            var votingTracker = db.SubmissionVoteTrackers.FirstOrDefault(b => b.SubmissionID == submissionID && b.UserName == userName);

                            previousVote.VoteStatus = -1;
                            previousVote.CreationDate = DateTime.Now;

                            db.SaveChanges();

                            SendVoteNotification(submission.UserName, "uptodownvote");
                        }

                        break;

                    // downvoted before, reset
                    case -1:
                        {
                            //ResetMessageVote(userName, submissionID);
                            submission.DownCount--;

                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;
                            db.SubmissionVoteTrackers.Remove(previousVote);
                            db.SaveChanges();

                            SendVoteNotification(submission.UserName, "upvote");
                        }

                        break;
                }
            }
        }
Пример #2
0
        // submit submission upvote
        public static void UpvoteSubmission(int submissionID, string userName, string clientIp)
        {
            //// user account voting check
            //int result = CheckIfVoted(userName, submissionID);

            using (var db = new voatEntities())
            {

                SubmissionVoteTracker previousVote = db.SubmissionVoteTrackers.Where(u => u.UserName == userName && u.SubmissionID == submissionID).FirstOrDefault();

                Submission submission = db.Submissions.Find(submissionID);

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

                int result = (previousVote == null ? 0 : previousVote.VoteStatus.Value);

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

                        if (submission.UserName != userName)
                        {
                            // check if this IP already voted on the same submission, abort voting if true
                            var ipVotedAlready = db.SubmissionVoteTrackers.Where(x => x.SubmissionID == submissionID && x.IPAddress == clientIp);
                            if (ipVotedAlready.Any()) return;

                            submission.UpCount++;
                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);
                            submission.Rank = newRank;

                            // register upvote
                            var tmpVotingTracker = new SubmissionVoteTracker
                            {
                                SubmissionID = submissionID,
                                UserName = userName,
                                VoteStatus = 1,
                                CreationDate = DateTime.Now,
                                IPAddress = clientIp
                            };

                            db.SubmissionVoteTrackers.Add(tmpVotingTracker);
                            db.SaveChanges();

                            SendVoteNotification(submission.UserName, "upvote");
                        }

                        break;

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

                        if (submission.UserName != userName)
                        {
                            submission.UpCount++;
                            submission.DownCount--;

                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);
                            submission.Rank = newRank;

                            previousVote.VoteStatus = 1;
                            previousVote.CreationDate = DateTime.Now;

                            db.SaveChanges();

                            SendVoteNotification(submission.UserName, "downtoupvote");
                        }

                        break;

                    // upvoted before, reset
                    case 1:
                        {
                            submission.UpCount--;

                            double currentScore = submission.UpCount - submission.DownCount;
                            double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.CreationDate);
                            double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore);

                            submission.Rank = newRank;
                            db.SubmissionVoteTrackers.Remove(previousVote);
                            db.SaveChanges();

                            //ResetMessageVote(userName, submissionID);

                            SendVoteNotification(submission.UserName, "downvote");
                        }

                        break;
                }
            }
        }