public override void OnActionExecuting(ActionExecutingContext filterContext) { var loggedInUser = filterContext.HttpContext.User.Identity.Name; // user is submitting a message if (filterContext.ActionParameters.ContainsKey("message")) { Message incomingMessage = (Message)filterContext.ActionParameters["message"]; var targetSubverse = incomingMessage.Subverse; // check user LCP for target subverse if (targetSubverse != null) { var LCPForSubverse = Karma.LinkKarmaForSubverse(loggedInUser, targetSubverse); if (LCPForSubverse >= 40) { // lower DelayRequest time DelayRequest = 10; } else if (User.IsUserSubverseModerator(loggedInUser, targetSubverse)) { // lower DelayRequest time DelayRequest = 10; } } } // user is submitting a comment else if (filterContext.ActionParameters.ContainsKey("comment")) { Comment incomingComment = (Comment)filterContext.ActionParameters["comment"]; using (whoaverseEntities db = new whoaverseEntities()) { var relatedMessage = db.Messages.Find(incomingComment.MessageId); if (relatedMessage != null) { var targetSubverseName = relatedMessage.Subverse; // check user CCP for target subverse int CCPForSubverse = Karma.CommentKarmaForSubverse(loggedInUser, targetSubverseName); if (CCPForSubverse >= 40) { // lower DelayRequest time DelayRequest = 10; } else if (User.IsUserSubverseModerator(loggedInUser, targetSubverseName)) { // lower DelayRequest time DelayRequest = 10; } } } } // Store our HttpContext (for easier reference and code brevity) var request = filterContext.HttpContext.Request; // Store our HttpContext.Cache (for easier reference and code brevity) var cache = filterContext.HttpContext.Cache; // Grab the IP Address from the originating Request (very simple implementation for example purposes) var originationInfo = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress; // Append the User Agent originationInfo += request.UserAgent; // Now we just need the target URL Information var targetInfo = request.RawUrl + request.QueryString; // Generate a hash for your strings (this appends each of the bytes of the value into a single hashed string var hashValue = string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(originationInfo + targetInfo)).Select(s => s.ToString("x2"))); // TODO: // Override spam filter if user is authorized poster to target subverse // trustedUser = true; // Checks if the hashed value is contained in the Cache (indicating a repeat request) if (cache[hashValue] != null && loggedInUser != "system" && trustedUser != true) { // Adds the Error Message to the Model and Redirect filterContext.Controller.ViewData.ModelState.AddModelError(string.Empty, ErrorMessage); } else { // Adds an empty object to the cache using the hashValue to a key (This sets the expiration that will determine // if the Request is valid or not cache.Add(hashValue, "", null, DateTime.Now.AddSeconds(DelayRequest), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } base.OnActionExecuting(filterContext); }
// submit submission downvote public static void DownvoteSubmission(int submissionId, string userWhichDownvoted) { int result = CheckIfVoted(userWhichDownvoted, submissionId); using (var db = new whoaverseEntities()) { 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 (User.IsUserCommentVotingMeanie(userWhichDownvoted)) { 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 }; 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 downvote public static void DownvoteComment(int commentId, string userWhichDownvoted, string clientIpHash) { int result = CheckIfVotedComment(userWhichDownvoted, commentId); using (whoaverseEntities db = new whoaverseEntities()) { Comment comment = db.Comments.Find(commentId); // do not execute downvoting, subverse is in anonymized mode if (comment.Message.Anonymized) { return; } // do not execute downvoting if user has insufficient CCP for target subverse if (Karma.CommentKarmaForSubverse(userWhichDownvoted, comment.Message.Subverse) < comment.Message.Subverses.minimumdownvoteccp) { return; } switch (result) { // never voted before case 0: { // this user is downvoting more than upvoting, don't register the downvote if (User.IsUserCommentVotingMeanie(userWhichDownvoted)) { return; } // check if this IP already voted on the same comment, abort voting if true var ipVotedAlready = db.Commentvotingtrackers.Where(x => x.CommentId == commentId && x.ClientIpAddress == clientIpHash); if (ipVotedAlready.Any()) { return; } comment.Dislikes++; // register downvote var tmpVotingTracker = new Commentvotingtracker { CommentId = commentId, UserName = userWhichDownvoted, VoteStatus = -1, Timestamp = DateTime.Now, ClientIpAddress = clientIpHash }; db.Commentvotingtrackers.Add(tmpVotingTracker); db.SaveChanges(); SendVoteNotification(comment.Name, "downvote"); } break; // upvoted before, turn upvote to downvote case 1: { comment.Likes--; comment.Dislikes++; //register Turn DownVote To UpVote var votingTracker = db.Commentvotingtrackers.FirstOrDefault(b => b.CommentId == commentId && b.UserName == userWhichDownvoted); if (votingTracker != null) { votingTracker.VoteStatus = -1; votingTracker.Timestamp = DateTime.Now; } db.SaveChanges(); SendVoteNotification(comment.Name, "uptodownvote"); } break; // downvoted before, reset case -1: comment.Dislikes--; db.SaveChanges(); ResetCommentVote(userWhichDownvoted, commentId); SendVoteNotification(comment.Name, "upvote"); break; } } }