Example #1
0
        // 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 voatEntities())
            {
                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;
                }
            }
        }
Example #2
0
        // 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 voatEntities())
            {
                if (result == true)
                {
                    // Already saved, unsave
                    UnSaveComment(userWhichSaved, commentId);
                }
                else
                {
                    // register save
                    var tmpSavingTracker = new CommentSaveTracker
                    {
                        CommentID = commentId,
                        UserName = userWhichSaved,
                        CreationDate = DateTime.Now
                    };
                    db.CommentSaveTrackers.Add(tmpSavingTracker);
                    db.SaveChanges();
                }
            }

        }
Example #3
0
        // 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 voatEntities())
            {
                if (result == true)
                {
                    // Already saved, unsave
                    UnSaveSubmission(userWhichSaved, submissionId);
                }
                else
                {
                    // register save
                    var tmpSavingTracker = new SubmissionSaveTracker
                    {
                        SubmissionID = submissionId,
                        UserName = userWhichSaved,
                        CreationDate = DateTime.Now
                    };
                    db.SubmissionSaveTrackers.Add(tmpSavingTracker);
                    db.SaveChanges();
                }
            }

        }
Example #4
0
        //// returns -1:downvoted, 1:upvoted, 0:not voted
        //public static SubmissionVoteTracker GetVote(voatEntities db, string userToCheck, int submissionID)
        //{
        //        var checkResult = db.SubmissionVoteTrackers.Where(u => u.UserName == userToCheck && u.SubmissionID == submissionID)
        //                .AsNoTracking()
        //                .FirstOrDefault();
        //}
        // 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 submissionID)
        {
            using (var db = new voatEntities())
            {
                var votingTracker = db.SubmissionVoteTrackers.FirstOrDefault(b => b.SubmissionID == submissionID && b.UserName == userWhichVoted);

                if (votingTracker == null) return;
                //delete vote history
                db.SubmissionVoteTrackers.Remove(votingTracker);
                db.SaveChanges();
            }
        }
Example #5
0
File: Voting.cs Project: rnand/voat
        // 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;
                }
            }
        }
Example #6
0
File: Voting.cs Project: rnand/voat
        // 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;
                }
            }
        }
Example #7
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;
                }
            }

        }
Example #8
0
        // a user has either upvoted or downvoted this submission earlier and wishes to reset the vote, delete the record
        public static void ResetCommentVote(string userWhichVoted, int commentId)
        {
            using (var db = new voatEntities())
            {
                var votingTracker = db.CommentVoteTrackers.FirstOrDefault(b => b.CommentID == commentId && b.UserName == userWhichVoted);

                if (votingTracker == null) return;
                // delete vote history
                db.CommentVoteTrackers.Remove(votingTracker);
                db.SaveChanges();
            }
        }
Example #9
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.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 (UserHelper.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();

                        Karma.UpdateUserCcp(comment.Name, -1);

                        Voting.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();

                        Karma.UpdateUserCcp(comment.Name, -2);

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

                        break;

                    // downvoted before, reset
                    case -1:

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

                        Karma.UpdateUserCcp(comment.Name, 1);
                        ResetCommentVote(userWhichDownvoted, commentId);

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

                        break;
                }
            }
        }
Example #10
0
        // 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 voatEntities())
            {
                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();
            }
        }
Example #11
0
        // update user SCP
        public static void UpdateUserScp(string userName, int value)
        {
            using (voatEntities db = new voatEntities())
            {
                var storedUserScp = db.Userscores.FirstOrDefault(x => x.Username.Equals(userName, StringComparison.OrdinalIgnoreCase));

                if (storedUserScp == null)
                {
                    var newUserScoreEntry = new Userscore
                    {
                        CCP = CommentKarma(userName),
                        SCP = LinkKarma(userName) + value,
                        Username = userName
                    };
                    db.Userscores.Add(newUserScoreEntry);
                }
                else
                {
                    storedUserScp.SCP = storedUserScp.SCP + value;
                }

                db.SaveChanges();
            }
        }
Example #12
0
        // 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;
                }
            }
        }
Example #13
0
        // 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;
                }
            }
        }
Example #14
0
        // a method to send a private message to a user, invoked by other methods
        public static bool SendPrivateMessage(string sender, string recipientList, string subject, string body)
        {
            if (Voat.Utilities.UserHelper.IsUserGloballyBanned(System.Web.HttpContext.Current.User.Identity.Name))
            {
                return false;
            }

            List<PrivateMessage> messages = new List<PrivateMessage>();
            MatchCollection col = Regex.Matches(recipientList, @"((?'prefix'@|u/|/u/|v/|/v/)?(?'recipient'[\w-.]+))", RegexOptions.IgnoreCase);

            foreach (Match m in col)
            {
                var recipient = m.Groups["recipient"].Value;
                var prefix = m.Groups["prefix"].Value;

                if (!String.IsNullOrEmpty(prefix) && prefix.ToLower().Contains("v"))
                {
                    //don't allow banned users to send to subverses
                    if (!UserHelper.IsUserBannedFromSubverse(System.Web.HttpContext.Current.User.Identity.Name, recipient))
                    {
                        //send to subverse mods
                        using (var db = new voatEntities())
                        {
                            //designed to limit abuse by taking the level 1 mod and the next four oldest
                            var mods = (from mod in db.SubverseModerators
                                        where mod.Subverse.Equals(recipient, StringComparison.OrdinalIgnoreCase) && mod.UserName != "system" && mod.UserName != "youcanclaimthissub"
                                        orderby mod.Power ascending, mod.CreationDate descending
                                        select mod).Take(5);

                            foreach (var moderator in mods)
                            {
                                messages.Add(new PrivateMessage
                                {
                                    Sender = sender,
                                    Recipient = moderator.UserName,
                                    CreationDate = DateTime.Now,
                                    Subject = String.Format("[v/{0}] {1}", recipient, subject),
                                    Body = body,
                                    IsUnread = true,
                                    MarkedAsUnread = true
                                });
                            }
                        }
                    }
                }
                else
                {
                    //ensure proper cased
                    recipient = UserHelper.OriginalUsername(recipient);

                    if (Voat.Utilities.UserHelper.UserExists(recipient))
                    {
                        messages.Add(new PrivateMessage
                        {
                            Sender = sender,
                            Recipient = recipient,
                            CreationDate = DateTime.Now,
                            Subject = subject,
                            Body = body,
                            IsUnread = true,
                            MarkedAsUnread = true
                        });
                    }
                }
            }

            if (messages.Count > 0)
            {
                using (var db = new voatEntities())
                {
                    try
                    {
                        db.PrivateMessages.AddRange(messages);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
Example #15
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;
                }

                // do not execute downvoting if comment is older than 7 days
                var commentPostingDate = comment.CreationDate;
                TimeSpan timeElapsed = DateTime.Now - commentPostingDate;
                if (timeElapsed.TotalDays > 7)
                {
                    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;
                }
            }

        }
Example #16
0
        // 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 voatEntities())
            {
                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();
            }
        }
Example #17
0
        // a method to send a private message to a user, invoked by other methods
        public static bool SendPrivateMessage(string sender, string recipientList, string subject, string body)
        {
            if (Voat.Utilities.UserHelper.IsUserGloballyBanned(System.Web.HttpContext.Current.User.Identity.Name))
            {
                return false;
            }

            if (Voat.Utilities.Karma.CommentKarma(System.Web.HttpContext.Current.User.Identity.Name) < 10)
            {
                return false;
            }

            List<PrivateMessage> messages = new List<PrivateMessage>();
            MatchCollection col = Regex.Matches(recipientList, @"((?'prefix'@|u/|/u/|v/|/v/)?(?'recipient'[\w-.]+))", RegexOptions.IgnoreCase);
            if (col.Count <= 0)
            {
                return false;
            }

            //Have to filter distinct because of spamming. If you copy a user name 
            //1,000 times into the recipient list the previous 
            //logic would send that user 1,000 messages. These guys find everything.
            var filtered = (from x in col.Cast<Match>()
                            select new
                            {
                                recipient = x.Groups["recipient"].Value,
                                prefix = (x.Groups["prefix"].Value.ToLower().Contains("v") ? "v" : "") //stop users from sending multiple messages using diff prefixes @user, /u/user, and u/user 
                            }).Distinct();

            foreach (var m in filtered)
            {
                var recipient = m.recipient;
                var prefix = m.prefix;

                if (!String.IsNullOrEmpty(prefix) && prefix.ToLower().Contains("v"))
                {
                    //don't allow banned users to send to subverses
                    if (!UserHelper.IsUserBannedFromSubverse(System.Web.HttpContext.Current.User.Identity.Name, recipient))
                    {
                        //send to subverse mods
                        using (var db = new voatEntities())
                        {
                            //designed to limit abuse by taking the level 1 mod and the next four oldest
                            var mods = (from mod in db.SubverseModerators
                                        where mod.Subverse.Equals(recipient, StringComparison.OrdinalIgnoreCase) && mod.UserName != "system" && mod.UserName != "youcanclaimthissub"
                                        orderby mod.Power ascending, mod.CreationDate descending
                                        select mod).Take(5);

                            foreach (var moderator in mods)
                            {
                                messages.Add(new PrivateMessage
                                {
                                    Sender = sender,
                                    Recipient = moderator.UserName,
                                    CreationDate = DateTime.Now,
                                    Subject = String.Format("[v/{0}] {1}", recipient, subject),
                                    Body = body,
                                    IsUnread = true,
                                    MarkedAsUnread = false
                                });
                            }
                        }
                    }
                }
                else
                {
                    //ensure proper cased
                    recipient = UserHelper.OriginalUsername(recipient);

                    if (Voat.Utilities.UserHelper.UserExists(recipient))
                    {
                        messages.Add(new PrivateMessage
                        {
                            Sender = sender,
                            Recipient = recipient,
                            CreationDate = DateTime.Now,
                            Subject = subject,
                            Body = body,
                            IsUnread = true,
                            MarkedAsUnread = false
                        });
                    }
                }
            }

            if (messages.Count > 0)
            {
                using (var db = new voatEntities())
                {
                    try
                    {
                        db.PrivateMessages.AddRange(messages);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            return true;
        }