// submit submission downvote public static void DownvoteSubmission(int submissionId, string userWhichDownvoted, string clientIp) { int result = CheckIfVoted(userWhichDownvoted, submissionId); using (var db = new voatEntities()) { Message submission = db.Messages.Find(submissionId); // do not execute downvoting if subverse is in anonymized mode if (submission.Anonymized) { return; } // do not execute downvoting if user has insufficient CCP for target subverse if (Karma.CommentKarmaForSubverse(userWhichDownvoted, submission.Subverse) < submission.Subverses.minimumdownvoteccp) { 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 submission, abort voting if true var ipVotedAlready = db.Votingtrackers.Where(x => x.MessageId == submissionId && x.ClientIpAddress == clientIp); if (ipVotedAlready.Any()) return; submission.Dislikes++; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; // register downvote var tmpVotingTracker = new Votingtracker { MessageId = submissionId, UserName = userWhichDownvoted, VoteStatus = -1, Timestamp = DateTime.Now, ClientIpAddress = clientIp }; db.Votingtrackers.Add(tmpVotingTracker); db.SaveChanges(); SendVoteNotification(submission.Name, "downvote"); } break; // upvoted before, turn upvote to downvote case 1: { submission.Likes--; submission.Dislikes++; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; // register Turn DownVote To UpVote var votingTracker = db.Votingtrackers.FirstOrDefault(b => b.MessageId == submissionId && b.UserName == userWhichDownvoted); if (votingTracker != null) { votingTracker.VoteStatus = -1; votingTracker.Timestamp = DateTime.Now; } db.SaveChanges(); SendVoteNotification(submission.Name, "uptodownvote"); } break; // downvoted before, reset case -1: { submission.Dislikes--; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; db.SaveChanges(); ResetMessageVote(userWhichDownvoted, submissionId); SendVoteNotification(submission.Name, "upvote"); } break; } } }
// submit submission upvote public static void UpvoteSubmission(int submissionId, string userWhichUpvoted, string clientIp) { // user account voting check int result = CheckIfVoted(userWhichUpvoted, submissionId); using (var db = new voatEntities()) { Message submission = db.Messages.Find(submissionId); if (submission.Anonymized) { // do not execute voting, subverse is in anonymized mode return; } switch (result) { // never voted before case 0: if (submission.Name != userWhichUpvoted) { // check if this IP already voted on the same submission, abort voting if true var ipVotedAlready = db.Votingtrackers.Where(x => x.MessageId == submissionId && x.ClientIpAddress == clientIp); if (ipVotedAlready.Any()) return; submission.Likes++; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; // register upvote var tmpVotingTracker = new Votingtracker { MessageId = submissionId, UserName = userWhichUpvoted, VoteStatus = 1, Timestamp = DateTime.Now, ClientIpAddress = clientIp }; db.Votingtrackers.Add(tmpVotingTracker); db.SaveChanges(); SendVoteNotification(submission.Name, "upvote"); } break; // downvoted before, turn downvote to upvote case -1: if (submission.Name != userWhichUpvoted) { submission.Likes++; submission.Dislikes--; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; // register Turn DownVote To UpVote var votingTracker = db.Votingtrackers.FirstOrDefault(b => b.MessageId == submissionId && b.UserName == userWhichUpvoted); if (votingTracker != null) { votingTracker.VoteStatus = 1; votingTracker.Timestamp = DateTime.Now; } db.SaveChanges(); SendVoteNotification(submission.Name, "downtoupvote"); } break; // upvoted before, reset case 1: { submission.Likes--; double currentScore = submission.Likes - submission.Dislikes; double submissionAge = Submissions.CalcSubmissionAgeDouble(submission.Date); double newRank = Ranking.CalculateNewRank(submission.Rank, submissionAge, currentScore); submission.Rank = newRank; db.SaveChanges(); ResetMessageVote(userWhichUpvoted, submissionId); SendVoteNotification(submission.Name, "downvote"); } break; } } }