public void PullsDefaultFrontpage(whoaverseEntities db, Defaultsubverse defaultsubverse, List<Message> messages) { // Arrange // TODO: See if we can coax Autofixture into handling this for us if (defaultsubverse.name.Length > 20) defaultsubverse.name = defaultsubverse.name.Substring(0, 19); db.Defaultsubverses.Add(defaultsubverse); db.Subverses.Add(new Subverse {name = defaultsubverse.name, title = new Guid().ToString()}); db.SaveChanges(); foreach (var message in messages) { // TODO: See if we can coax Autofixture into handling this for us if (message.Thumbnail.Length > 40) message.Thumbnail = message.Thumbnail.Substring(0, 39); message.Subverse = defaultsubverse.name; } db.Messages.AddRange(messages); db.SaveChanges(); var homeController = new HomeController(db); homeController.ControllerContext = GetControllerContext(false); // Act var actionResult = (ViewResult) homeController.Index(null); var model = (PaginatedList<Message>) actionResult.Model; // Assert Assert.True(model.Count == messages.Count); }
// a user wishes to save a submission, save it public static void SaveSubmission(int submissionId, string userWhichSaved) { var result = CheckIfSaved(userWhichSaved, submissionId); using (var db = new whoaverseEntities()) { if (result == true) { // Already saved, unsave UnSaveSubmission(userWhichSaved, submissionId); } else { // register save var tmpSavingTracker = new Savingtracker { MessageId = submissionId, UserName = userWhichSaved, Timestamp = DateTime.Now }; db.Savingtrackers.Add(tmpSavingTracker); db.SaveChanges(); } } }
// a method to send a private message to a user, invoked by other methods public static bool SendPrivateMessage(string sender, string recipient, string subject, string body) { using (var db = new whoaverseEntities()) { try { var privateMessage = new Privatemessage { Sender = sender, Recipient = recipient, Timestamp = DateTime.Now, Subject = subject, Body = body, Status = true, Markedasunread = true }; db.Privatemessages.Add(privateMessage); db.SaveChanges(); return true; } catch (Exception) { return false; } } }
// a user wishes to save a comment, save it public static void SaveComment(int commentId, string userWhichSaved) { var result = CheckIfSavedComment(userWhichSaved, commentId); using (var db = new whoaverseEntities()) { if (result == true) { // Already saved, unsave UnSaveComment(userWhichSaved, commentId); } else { // register save var tmpSavingTracker = new Commentsavingtracker { CommentId = commentId, UserName = userWhichSaved, Timestamp = DateTime.Now }; db.Commentsavingtrackers.Add(tmpSavingTracker); db.SaveChanges(); } } }
// block a subverse public static void BlockSubverse(string userName, string subverse) { using (var db = new whoaverseEntities()) { // unblock if subverse is already blocked if (IsUserBlockingSubverse(userName, subverse)) { var subverseBlock = db.UserBlockedSubverses.FirstOrDefault(n => n.SubverseName.ToLower() == subverse.ToLower() && n.Username == userName); if (subverseBlock != null) db.UserBlockedSubverses.Remove(subverseBlock); db.SaveChanges(); return; } // add a new block var blockedSubverse = new UserBlockedSubverse { Username = userName, SubverseName = subverse }; db.UserBlockedSubverses.Add(blockedSubverse); db.SaveChanges(); } }
// a user has saved this submission earlier and wishes to unsave it, delete the record private static void UnSaveSubmission(string userWhichSaved, int messageId) { using (var db = new whoaverseEntities()) { var saveTracker = db.Savingtrackers.FirstOrDefault(b => b.MessageId == messageId && b.UserName == userWhichSaved); if (saveTracker == null) return; //delete vote history db.Savingtrackers.Remove(saveTracker); db.SaveChanges(); } }
// a user has saved this comment earlier and wishes to unsave it, delete the record private static void UnSaveComment(string userWhichSaved, int commentId) { using (var db = new whoaverseEntities()) { var votingTracker = db.Commentsavingtrackers.FirstOrDefault(b => b.CommentId == commentId && b.UserName == userWhichSaved); if (votingTracker == null) return; // delete vote history db.Commentsavingtrackers.Remove(votingTracker); db.SaveChanges(); } }
// delete a user account and all history: comments, posts and votes public static bool DeleteUser(string userName) { using (var db = new whoaverseEntities()) { using (var tmpUserManager = new UserManager<WhoaVerseUser>(new UserStore<WhoaVerseUser>(new ApplicationDbContext()))) { var tmpuser = tmpUserManager.FindByName(userName); if (tmpuser != null) { // remove voting history for submisions db.Votingtrackers.RemoveRange(db.Votingtrackers.Where(x => x.UserName == userName)); // remove voting history for comments db.Commentvotingtrackers.RemoveRange(db.Commentvotingtrackers.Where(x => x.UserName == userName)); // remove all comments var comments = db.Comments.Where(c => c.Name == userName).ToList(); foreach (Comment c in comments) { c.Name = "deleted"; c.CommentContent = "deleted by user"; } db.SaveChanges(); // remove all submissions var submissions = db.Messages.Where(c => c.Name == userName).ToList(); foreach (Message s in submissions) { if (s.Type == 1) { s.Name = "deleted"; s.MessageContent = "deleted by user"; s.Title = "deleted by user"; } else { s.Name = "deleted"; s.Linkdescription = "deleted by user"; s.MessageContent = "http://voat.co"; } } // resign from all moderating positions db.SubverseAdmins.RemoveRange(db.SubverseAdmins.Where(m => m.Username.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete comment reply notifications db.Commentreplynotifications.RemoveRange(db.Commentreplynotifications.Where(crp => crp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete post reply notifications db.Postreplynotifications.RemoveRange(db.Postreplynotifications.Where(prp => prp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete private messages db.Privatemessages.RemoveRange(db.Privatemessages.Where(pm => pm.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // TODO: // keep this updated as new features are added (delete sets etc) // username will stay permanently reserved db.SaveChanges(); return true; } // user account could not be found return false; } } }
// a user has either upvoted or downvoted this submission earlier and wishes to reset the vote, delete the record public static void ResetMessageVote(string userWhichVoted, int messageId) { using (var db = new whoaverseEntities()) { var votingTracker = db.Votingtrackers.FirstOrDefault(b => b.MessageId == messageId && b.UserName == userWhichVoted); if (votingTracker == null) return; //delete vote history db.Votingtrackers.Remove(votingTracker); db.SaveChanges(); } }
// 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 whoaverseEntities()) { 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; } } }
// unsubscribe from a subverse public static void UnSubscribeFromSubverse(string userName, string subverse) { if (IsUserSubverseSubscriber(userName, subverse)) { using (var db = new whoaverseEntities()) { var subscription = db.Subscriptions.FirstOrDefault(b => b.Username == userName && b.SubverseName == subverse); if (subverse == null) return; // remove subscription record db.Subscriptions.Remove(subscription); // record new unsubscription in subverse table subscribers field Subverse tmpSubverse = db.Subverses.Find(subverse); if (tmpSubverse != null) { tmpSubverse.subscribers--; } db.SaveChanges(); } } }
// unsubscribe from a set public static void UnSubscribeFromSet(string userName, int setId) { // do nothing if user is not subscribed to given set if (!IsUserSetSubscriber(userName, setId)) return; using (var db = new whoaverseEntities()) { var subscription = db.Usersetsubscriptions.FirstOrDefault(b => b.Username == userName && b.Set_id == setId); // remove subscription record db.Usersetsubscriptions.Remove(subscription); // record new unsubscription in sets table subscribers field var tmpUserset = db.Usersets.Find(setId); if (tmpUserset != null) { tmpUserset.Subscribers--; } db.SaveChanges(); } }
// submit submission downvote public static void DownvoteSubmission(int submissionId, string userWhichDownvoted, string clientIp) { 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; } // 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; } } }
// subscribe to a subverse public static void SubscribeToSubverse(string userName, string subverse) { if (IsUserSubverseSubscriber(userName, subverse)) return; using (var db = new whoaverseEntities()) { // add a new subscription var newSubscription = new Subscription { Username = userName, SubverseName = subverse }; db.Subscriptions.Add(newSubscription); // record new subscription in subverse table subscribers field Subverse tmpSubverse = db.Subverses.Find(subverse); if (tmpSubverse != null) { tmpSubverse.subscribers++; } db.SaveChanges(); } }
// subscribe to a set public static void SubscribeToSet(string userName, int setId) { // do nothing if user is already subscribed if (IsUserSetSubscriber(userName, setId)) return; using (var db = new whoaverseEntities()) { // add a new set subscription var newSubscription = new Usersetsubscription { Username = userName, Set_id = setId }; db.Usersetsubscriptions.Add(newSubscription); // record new set subscription in sets table subscribers field var tmpUserSet = db.Usersets.Find(setId); if (tmpUserSet != null) { tmpUserSet.Subscribers++; } db.SaveChanges(); } }
// 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; } } }
// submit comment upvote public static void UpvoteComment(int commentId, string userWhichUpvoted, string clientIpHash) { int result = CheckIfVotedComment(userWhichUpvoted, commentId); using (whoaverseEntities db = new whoaverseEntities()) { Comment comment = db.Comments.Find(commentId); if (comment.Message.Anonymized) { // do not execute voting, subverse is in anonymized mode return; } switch (result) { // never voted before case 0: if (comment.Name != userWhichUpvoted) { // 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.Likes++; // register upvote var tmpVotingTracker = new Commentvotingtracker { CommentId = commentId, UserName = userWhichUpvoted, VoteStatus = 1, Timestamp = DateTime.Now, ClientIpAddress = clientIpHash }; db.Commentvotingtrackers.Add(tmpVotingTracker); db.SaveChanges(); SendVoteNotification(comment.Name, "upvote"); } break; // downvoted before, turn downvote to upvote case -1: if (comment.Name != userWhichUpvoted) { comment.Likes++; comment.Dislikes--; // register Turn DownVote To UpVote var votingTracker = db.Commentvotingtrackers.FirstOrDefault(b => b.CommentId == commentId && b.UserName == userWhichUpvoted); if (votingTracker != null) { votingTracker.VoteStatus = 1; votingTracker.Timestamp = DateTime.Now; } db.SaveChanges(); SendVoteNotification(comment.Name, "downtoupvote"); } break; // upvoted before, reset case 1: comment.Likes--; db.SaveChanges(); SendVoteNotification(comment.Name, "downvote"); ResetCommentVote(userWhichUpvoted, commentId); break; } } }