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
File: User.cs Project: scott71/voat
        // block a subverse
        public static void BlockSubverse(string userName, string subverse)
        {
            using (var db = new voatEntities())
            {
                // 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();
            }
        }
Example #3
0
File: User.cs Project: scott71/voat
        // subscribe to a subverse
        public static void SubscribeToSubverse(string userName, string subverse)
        {
            if (IsUserSubverseSubscriber(userName, subverse)) return;
            using (var db = new voatEntities())
            {
                // 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();
            }
        }
Example #4
0
File: User.cs Project: scott71/voat
        // 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 voatEntities())
            {
                // 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();
            }
        }
Example #5
0
File: User.cs Project: scott71/voat
        // delete a user account and all history: comments, posts and votes
        public static bool DeleteUser(string userName)
        {
            using (var db = new voatEntities())
            {
                using (var tmpUserManager = new UserManager<VoatUser>(new UserStore<VoatUser>(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;
                }

            }
        }
Example #6
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 (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;
                }
            }
        }
Example #7
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 #8
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 (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;
                }
            }
        }
Example #9
0
File: User.cs Project: scott71/voat
        // unsubscribe from a subverse
        public static void UnSubscribeFromSubverse(string userName, string subverse)
        {
            if (IsUserSubverseSubscriber(userName, subverse))
            {
                using (var db = new voatEntities())
                {
                    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();
                }
            }
        }
Example #10
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 Savingtracker
                    {
                        MessageId = submissionId,
                        UserName = userWhichSaved,
                        Timestamp = DateTime.Now
                    };
                    db.Savingtrackers.Add(tmpSavingTracker);
                    db.SaveChanges();
                }
            }
        }
Example #11
0
        // add a new session
        public static void Add(string subverseName, string sessionId)
        {
            try
            {
                if (SessionExists(sessionId, subverseName)) return;
                using (var db = new voatEntities())
                {
                    var newSession = new Sessiontracker { SessionId = sessionId, Subverse = subverseName, Timestamp = DateTime.Now };

                    db.Sessiontrackers.Add(newSession);
                    db.SaveChanges();

                }
            }
            catch (Exception)
            {
                //
            }
        }
Example #12
0
 // clear all sessions
 public static void RemoveAllSessions()
 {
     try
     {
         using (var db = new voatEntities())
         {
             db.Database.ExecuteSqlCommand("TRUNCATE TABLE SESSIONTRACKER");
             db.SaveChanges();
         }
     }
     catch (Exception)
     {
         //
     }
 }
Example #13
0
 // remove a session
 public static void Remove(string sessionIdToRemove)
 {
     try
     {
         using (var db = new voatEntities())
         {
             // remove all records for given session id
             db.Sessiontrackers.RemoveRange(db.Sessiontrackers.Where(s => s.SessionId == sessionIdToRemove));
             db.SaveChanges();
         }
     }
     catch (Exception)
     {
         //
     }
 }
Example #14
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 #15
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 Commentsavingtracker
                    {
                        CommentId = commentId,
                        UserName = userWhichSaved,
                        Timestamp = DateTime.Now
                    };
                    db.Commentsavingtrackers.Add(tmpSavingTracker);
                    db.SaveChanges();
                }
            }
        }
Example #16
0
File: User.cs Project: scott71/voat
        // 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 voatEntities())
            {
                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();
            }
        }
Example #17
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 #18
0
        // 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 voatEntities())
            {
                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();
            }
        }
Example #19
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.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;
                }
            }
        }