Пример #1
0
        public ActionResult ApplyLinkFlair(int?submissionID, int?flairId)
        {
            if (submissionID == null || flairId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var submission = _db.Submissions.Find(submissionID);

            if (submission == null || submission.IsDeleted)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (!ModeratorPermission.HasPermission(User.Identity.Name, submission.Subverse, Domain.Models.ModeratorAction.AssignFlair))
            {
                return(new HttpUnauthorizedResult());
            }

            // find flair by id, apply it to submission
            var flairModel = _db.SubverseFlairs.Find(flairId);

            if (flairModel == null || flairModel.Subverse != submission.Subverse)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // apply flair and save submission
            submission.FlairCss   = flairModel.CssClass;
            submission.FlairLabel = flairModel.Label;
            _db.SaveChanges();
            DataCache.Submission.Remove(submissionID.Value);

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Пример #2
0
        public ActionResult ApplyLinkFlair(int?submissionID, int?flairId)
        {
            if (submissionID == null || flairId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var submission = _db.Submissions.Find(submissionID);

            if (submission == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            // check if caller is subverse moderator, if not, deny posting
            if (!UserHelper.IsUserSubverseModerator(User.Identity.Name, submission.Subverse))
            {
                return(new HttpUnauthorizedResult());
            }

            // find flair by id, apply it to submission
            var flairModel = _db.SubverseFlairs.Find(flairId);

            if (flairModel == null || flairModel.Subverse != submission.Subverse)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // apply flair and save submission
            submission.FlairCss   = flairModel.CssClass;
            submission.FlairLabel = flairModel.Label;
            _db.SaveChanges();
            DataCache.Submission.Remove(submissionID.Value);

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Пример #3
0
        public void PreventBannedDomainPost_MultiPartDomains()
        {
            using (var repo = new voatEntities())
            {
                repo.BannedDomains.Add(new BannedDomain()
                {
                    Domain = "one.two.three.com", Reason = "People hate counting", CreatedBy = "UnitTest", CreationDate = DateTime.UtcNow
                });
                repo.SaveChanges();
            }

            TestHelper.SetPrincipal("TestUser2");

            var cmd = new CreateSubmissionCommand(new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Hello Man - Longer because of Rules", Url = "http://www.one.two.three.com/images/feelsgoodman.jpg"
            });
            var r = cmd.Execute().Result;

            Assert.IsNotNull(r, "Response was null");
            Assert.IsFalse(r.Success, r.Message);
            Assert.AreEqual(r.Message, "Submission contains banned domains");

            cmd = new CreateSubmissionCommand(new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Hello Man - Longer because of Rules", Content = "Check out this cool image I found using dogpile.com: HTTP://one.TWO.three.com/images/feelsgoodman.jpg"
            });
            r = cmd.Execute().Result;
            Assert.IsNotNull(r, "Response was null");
            Assert.IsFalse(r.Success, r.Message);
            Assert.AreEqual(r.Message, "Submission contains banned domains");
        }
Пример #4
0
        public void BannedDomainTest()
        {
            var domain      = "paydayloansforeverybody.com";
            var reason      = "Total Rip off!";
            var createdBy   = "AntiSpamcist";
            var createdDate = DateTime.UtcNow.AddDays(-10);

            using (var db = new voatEntities())
            {
                db.BannedDomains.Add(new BannedDomain()
                {
                    Domain       = domain,
                    Reason       = reason,
                    CreatedBy    = createdBy,
                    CreationDate = createdDate
                });
                db.SaveChanges();
            }
            using (var repo = new Voat.Data.Repository())
            {
                var result = repo.BannedDomains("yahoo.com", "google.com", domain, domain.ToUpper(), "testuri.org");
                Assert.IsNotNull(result, "Result was null");
                Assert.IsTrue(result.Any(), "Result expected");
                Assert.AreEqual(1, result.Count(), "Count off");
                var bd = result.First();
                Assert.AreEqual(domain, bd.Domain);
                Assert.AreEqual(reason, bd.Reason);
                Assert.AreEqual(createdBy, bd.CreatedBy);
                Assert.AreEqual(createdDate.ToString(), bd.CreationDate.ToString());
            }
        }
Пример #5
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);
                }
            }
        }
Пример #6
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();
                }
            }
        }
Пример #7
0
        // 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 SubverseSubscription {
                    UserName = userName, Subverse = subverse
                };
                db.SubverseSubscriptions.Add(newSubscription);

                // record new subscription in subverse table subscribers field
                Subverse tmpSubverse = db.Subverses.Find(subverse);

                if (tmpSubverse != null)
                {
                    tmpSubverse.SubscriberCount++;
                }

                db.SaveChanges();
            }
        }
Пример #8
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();
                }
            }
        }
Пример #9
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 = Repository.CurrentDate
                    };
                    db.CommentSaveTrackers.Add(tmpSavingTracker);
                    db.SaveChanges();
                }
            }
        }
Пример #10
0
        // 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, UserSetID = setId
                };
                db.UserSetSubscriptions.Add(newSubscription);

                // record new set subscription in sets table subscribers field
                var tmpUserSet = db.UserSets.Find(setId);

                if (tmpUserSet != null)
                {
                    tmpUserSet.SubscriberCount++;
                }

                db.SaveChanges();
            }
        }
Пример #11
0
        // 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.UserSetID == 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.SubscriberCount--;
                }

                db.SaveChanges();
            }
        }
Пример #12
0
        public void CreateComment_DisabledSubverse()
        {
            //insert post via db into disabled sub
            Submission submission = null;

            using (var db = new voatEntities())
            {
                submission = new Submission()
                {
                    Subverse     = "Disabled",
                    Title        = "Super Sneaky",
                    Content      = "How can I post to disabled subs?",
                    UserName     = "******",
                    CreationDate = DateTime.UtcNow
                };
                db.Submissions.Add(submission);
                db.SaveChanges();
            }

            TestHelper.SetPrincipal("TestUser5");
            var cmd = new CreateCommentCommand(submission.ID, null, "Are you @FuzzyWords?");
            var c   = cmd.Execute().Result;

            Assert.IsFalse(c.Success, "Disabled subs should not allow comments");
            Assert.AreEqual(Status.Denied, c.Status);
            Assert.AreEqual(c.Message, "Subverse is disabled");
        }
Пример #13
0
        // unsubscribe from a subverse
        public static void UnSubscribeFromSubverse(string userName, string subverse)
        {
            if (IsUserSubverseSubscriber(userName, subverse))
            {
                using (var db = new voatEntities())
                {
                    var subscription = db.SubverseSubscriptions.FirstOrDefault(b => b.UserName == userName && b.Subverse == subverse);

                    if (subverse == null)
                    {
                        return;
                    }
                    // remove subscription record
                    db.SubverseSubscriptions.Remove(subscription);

                    // record new unsubscription in subverse table subscribers field
                    Subverse tmpSubverse = db.Subverses.Find(subverse);

                    if (tmpSubverse != null)
                    {
                        tmpSubverse.SubscriberCount--;
                    }

                    db.SaveChanges();
                }
            }
        }
Пример #14
0
        public ActionResult InboxCommentReplies(int?page)
        {
            ViewBag.PmView = "inbox";
            SetViewBagCounts();
            const int pageSize   = 25;
            int       pageNumber = (page ?? 0);

            if (pageNumber < 0)
            {
                return(View("~/Views/Errors/Error_404.cshtml"));
            }

            // get logged in username and fetch received comment replies
            try
            {
                IQueryable <CommentReplyNotification> commentReplyNotifications = _db.CommentReplyNotifications.Where(s => s.Recipient.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
                IQueryable <Comment> commentReplies = _db.Comments.Where(p => commentReplyNotifications.Any(p2 => p2.CommentID == p.ID)).OrderByDescending(s => s.CreationDate);

                // mark all unread messages as read as soon as the inbox is served, except for manually marked as unread
                if (commentReplyNotifications.Any())
                {
                    var unreadCommentReplies = commentReplyNotifications.Where(s => s.IsUnread && s.MarkedAsUnread == false);

                    // todo: implement a delay in the marking of messages as read until the returned inbox view is rendered
                    if (unreadCommentReplies.Any())
                    {
                        foreach (var singleCommentReply in unreadCommentReplies)
                        {
                            // status: true = unread, false = read
                            singleCommentReply.IsUnread = false;
                        }
                        _db.SaveChanges();
                        // update notification icon
                        UpdateNotificationCounts();
                    }
                }

                ViewBag.CommentRepliesCount = commentReplyNotifications.Count();

                PaginatedList <Comment> paginatedComments = new PaginatedList <Comment>(commentReplies, page ?? 0, pageSize);
                return(View("InboxCommentReplies", paginatedComments));
            }
            catch (Exception)
            {
                return(View("~/Views/Errors/DbNotResponding.cshtml"));
            }
        }
Пример #15
0
        // 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.Subverse.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, Subverse = subverse };
                db.UserBlockedSubverses.Add(blockedSubverse);
                db.SaveChanges();
            }
        }
Пример #16
0
        // 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.Subverse.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, Subverse = subverse
                };
                db.UserBlockedSubverses.Add(blockedSubverse);
                db.SaveChanges();
            }
        }
Пример #17
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.CommentSaveTrackers.FirstOrDefault(b => b.CommentID == commentId && b.UserName == userWhichSaved);

                if (votingTracker == null)
                {
                    return;
                }
                // delete vote history
                db.CommentSaveTrackers.Remove(votingTracker);
                db.SaveChanges();
            }
        }
Пример #18
0
 // clear all sessions
 public static void RemoveAllSessions()
 {
     try
     {
         using (var db = new voatEntities())
         {
             db.Database.ExecuteSqlCommand("TRUNCATE TABLE SESSIONTRACKER");
             db.SaveChanges();
         }
     }
     catch (Exception)
     {
         //
     }
 }
Пример #19
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.Commentvotingtrackers.FirstOrDefault(b => b.CommentId == commentId && b.UserName == userWhichVoted);

                if (votingTracker == null)
                {
                    return;
                }
                // delete vote history
                db.Commentvotingtrackers.Remove(votingTracker);
                db.SaveChanges();
            }
        }
Пример #20
0
        public ActionResult ApplyLinkFlair(int?submissionId, int?flairId)
        {
            if (submissionId == null || flairId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var submissionModel = _db.Messages.Find(submissionId);

            if (submissionModel == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // check if caller is subverse moderator, if not, deny posting
            if (!Utils.User.IsUserSubverseModerator(User.Identity.Name, submissionModel.Subverse) &&
                !Utils.User.IsUserSubverseAdmin(User.Identity.Name, submissionModel.Subverse))
            {
                return(new HttpUnauthorizedResult());
            }

            // find flair by id, apply it to submission
            var flairModel = _db.Subverseflairsettings.Find(flairId);

            if (flairModel == null || flairModel.Subversename != submissionModel.Subverse)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // apply flair and save submission
            submissionModel.FlairCss   = flairModel.CssClass;
            submissionModel.FlairLabel = flairModel.Label;
            _db.SaveChanges();

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Пример #21
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();
            }
        }
Пример #22
0
 // clear all sessions
 public static void RemoveAllSessions()
 {
     try
     {
         using (var db = new voatEntities())
         {
             db.Database.ExecuteSqlCommand("TRUNCATE TABLE SESSIONTRACKER");
             db.SaveChanges();
         }
     }
     catch (Exception)
     {
         //
     }
 }
Пример #23
0
        // a user has saved this submission earlier and wishes to unsave it, delete the record
        private static void UnSaveSubmission(string userWhichSaved, int submissionID)
        {
            using (var db = new voatEntities())
            {
                var saveTracker = db.SubmissionSaveTrackers.FirstOrDefault(b => b.SubmissionID == submissionID && b.UserName == userWhichSaved);

                if (saveTracker == null)
                {
                    return;
                }
                //delete vote history
                db.SubmissionSaveTrackers.Remove(saveTracker);
                db.SaveChanges();
            }
        }
Пример #24
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)
     {
         //
     }
 }
Пример #25
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)
     {
         //
     }
 }
Пример #26
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, CreationDate = DateTime.Now };

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

                }
            }
            catch (Exception)
            {
                //
            }
        }
Пример #27
0
        public void PreventBannedDomainPost()
        {
            using (var repo = new voatEntities())
            {
                repo.BannedDomains.Add(new BannedDomain()
                {
                    Domain = "saiddit.com", Reason = "No one really likes you.", CreatedBy = "UnitTest", CreationDate = DateTime.UtcNow
                });
                repo.SaveChanges();
            }

            TestHelper.SetPrincipal("TestUser2");

            var cmd = new CreateSubmissionCommand(new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Hello Man - Longer because of Rules", Url = "http://www.saiddit.com/images/feelsgoodman.jpg"
            });
            var r = cmd.Execute().Result;

            Assert.IsNotNull(r, "Response was null");
            Assert.IsFalse(r.Success, r.Message);
            Assert.AreEqual(r.Message, "Submission contains banned domains");

            cmd = new CreateSubmissionCommand(new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Hello Man - Longer because of Rules", Content = "Check out this cool image I found using dogpile.com: http://saiddit.com/images/feelsgoodman.jpg"
            });
            r = cmd.Execute().Result;
            Assert.IsNotNull(r, "Response was null");
            Assert.IsFalse(r.Success, r.Message);
            Assert.AreEqual(r.Message, "Submission contains banned domains");

            cmd = new CreateSubmissionCommand(new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Test Embedded Banned Domain",
                Content  = "http://yahoo.com/index.html Check out this cool image I found using compuserve.com: http://saiddit.com/images/feelsgoodman.jpg. http://www2.home.geocities.com/index.html"
            });
            r = cmd.Execute().Result;
            Assert.IsNotNull(r, "Response was null");
            Assert.IsFalse(r.Success, r.Message);
            Assert.AreEqual(r.Message, "Submission contains banned domains");
        }
Пример #28
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)
            {
                //
            }
        }
Пример #29
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, CreationDate = Repository.CurrentDate
                    };

                    db.SessionTrackers.Add(newSession);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                //
            }
        }
Пример #30
0
        public void TestNegativeSCPSubmission()
        {
            var userName = "******";

            //Create user
            VoatDataInitializer.CreateUser(userName, DateTime.UtcNow.AddDays(-450));
            //Add submission with negatives directly to db
            using (var context = new voatEntities())
            {
                var s = context.Submissions.Add(new Submission()
                {
                    CreationDate = DateTime.UtcNow.AddHours(-12),
                    Subverse     = "unit",
                    Title        = "Test Negative SCP",
                    Url          = "https://www.youtube.com/watch?v=pnbJEg9r1o8",
                    Type         = 2,
                    UpCount      = 2,
                    DownCount    = 13,
                    UserName     = userName
                });
                context.SaveChanges();
            }



            TestHelper.SetPrincipal(userName);
            var userSubmission = new Domain.Models.UserSubmission()
            {
                Subverse = "unit", Title = "Le Censorship!", Content = "Will this work?"
            };
            var cmd = new CreateSubmissionCommand(userSubmission);
            var r   = cmd.Execute().Result;

            Assert.IsNotNull(r, "Response was null");
            Assert.IsTrue(r.Success, r.Message);
        }
Пример #31
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();
            }
        }
Пример #32
0
        // unsubscribe from a subverse
        public static void UnSubscribeFromSubverse(string userName, string subverse)
        {
            if (IsUserSubverseSubscriber(userName, subverse))
            {
                using (var db = new voatEntities())
                {
                    var subscription = db.SubverseSubscriptions.FirstOrDefault(b => b.UserName == userName && b.Subverse == subverse);

                    if (subverse == null) return;
                    // remove subscription record
                    db.SubverseSubscriptions.Remove(subscription);

                    // record new unsubscription in subverse table subscribers field
                    Subverse tmpSubverse = db.Subverses.Find(subverse);

                    if (tmpSubverse != null)
                    {
                        tmpSubverse.SubscriberCount--;
                    }

                    db.SaveChanges();
                }
            }
        }
Пример #33
0
        // 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.UserSetID == 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.SubscriberCount--;
                }

                db.SaveChanges();
            }
        }
Пример #34
0
        // 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 SubverseSubscription { UserName = userName, Subverse = subverse };
                db.SubverseSubscriptions.Add(newSubscription);

                // record new subscription in subverse table subscribers field
                Subverse tmpSubverse = db.Subverses.Find(subverse);

                if (tmpSubverse != null)
                {
                    tmpSubverse.SubscriberCount++;
                }

                db.SaveChanges();
            }
        }
Пример #35
0
        // 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, UserSetID = setId };
                db.UserSetSubscriptions.Add(newSubscription);

                // record new set subscription in sets table subscribers field
                var tmpUserSet = db.UserSets.Find(setId);

                if (tmpUserSet != null)
                {
                    tmpUserSet.SubscriberCount++;
                }

                db.SaveChanges();
            }
        }
Пример #36
0
        // 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.SubmissionVoteTrackers.RemoveRange(db.SubmissionVoteTrackers.Where(x => x.UserName == userName));

                        // remove voting history for comments
                        db.CommentVoteTrackers.RemoveRange(db.CommentVoteTrackers.Where(x => x.UserName == userName));

                        // remove all comments
                        var comments = db.Comments.Where(c => c.UserName == userName).ToList();
                        foreach (Comment c in comments)
                        {
                            c.IsDeleted = true;
                            c.Content = "deleted by user";
                        }
                        db.SaveChanges();

                        // remove all submissions
                        var submissions = db.Submissions.Where(c => c.UserName == userName).ToList();
                        foreach (var s in submissions)
                        {
                            if (s.Type == 1)
                            {
                                s.IsDeleted = true;
                                s.Content = "deleted by user";
                                s.Title = "deleted by user";
                            }
                            else
                            {
                                s.IsDeleted = true;
                                s.LinkDescription = "deleted by user";
                                s.Content = "http://voat.co";
                            }
                        }

                        // resign from all moderating positions
                        db.SubverseModerators.RemoveRange(db.SubverseModerators.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.SubmissionReplyNotifications.RemoveRange(db.SubmissionReplyNotifications.Where(prp => prp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete private messages
                        db.PrivateMessages.RemoveRange(db.PrivateMessages.Where(pm => pm.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete short bio if userprefs record exists for this user
                        var userPrefs = db.UserPreferences.Find(userName);
                        if (userPrefs != null)
                        {
                            userPrefs.Bio = null;
                        }

                        // TODO: delete avatar
                        // userPrefs.Avatar = ""

                        // TODO:
                        // keep this updated as new features are added (delete sets etc)

                        // username will stay permanently reserved to prevent someone else from registering it and impersonating

                        db.SaveChanges();

                        return true;
                    }
                    // user account could not be found
                    return false;
                }

            }
        }
Пример #37
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();

                        Karma.UpdateUserCcp(comment.Name, 1);

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

                        Karma.UpdateUserCcp(comment.Name, 2);

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

                    break;

                // upvoted before, reset
                case 1:

                    comment.Likes--;
                    db.SaveChanges();
                    Karma.UpdateUserCcp(comment.Name, -1);

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

                    ResetCommentVote(userWhichUpvoted, commentId);

                    break;
                }
            }
        }
Пример #38
0
        // 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.SubmissionVoteTrackers.RemoveRange(db.SubmissionVoteTrackers.Where(x => x.UserName == userName));

                        // remove voting history for comments
                        db.CommentVoteTrackers.RemoveRange(db.CommentVoteTrackers.Where(x => x.UserName == userName));

                        // remove all comments
                        var comments = db.Comments.Where(c => c.UserName == userName).ToList();
                        foreach (Comment c in comments)
                        {
                            c.IsDeleted = true;
                            c.Content = "deleted by user";
                        }
                        db.SaveChanges();

                        // remove all submissions
                        var submissions = db.Submissions.Where(c => c.UserName == userName).ToList();
                        foreach (var s in submissions)
                        {
                            if (s.Type == 1)
                            {
                                s.IsDeleted = true;
                                s.Content = "deleted by user";
                                s.Title = "deleted by user";
                            }
                            else
                            {
                                s.IsDeleted = true;
                                s.LinkDescription = "deleted by user";
                                s.Content = "http://voat.co";
                            }
                        }

                        // resign from all moderating positions
                        db.SubverseModerators.RemoveRange(db.SubverseModerators.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.SubmissionReplyNotifications.RemoveRange(db.SubmissionReplyNotifications.Where(prp => prp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete private messages
                        db.PrivateMessages.RemoveRange(db.PrivateMessages.Where(pm => pm.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete user preferences
                        var userPrefs = db.UserPreferences.Find(userName);
                        if (userPrefs != null)
                        {
                            // delete short bio
                            userPrefs.Bio = null;

                            // delete avatar
                            if (userPrefs.Avatar != null)
                            {
                                var avatarFilename = userPrefs.Avatar;
                                if (Settings.UseContentDeliveryNetwork)
                                {
                                    // try to delete from CDN
                                    CloudStorageUtility.DeleteBlob(avatarFilename, "avatars");
                                }
                                else
                                {
                                    // try to remove from local FS
                                    string tempAvatarLocation = Settings.DestinationPathAvatars + '\\' + userName + ".jpg";

                                    // the avatar file was not found at expected path, abort
                                    if (!FileSystemUtility.FileExists(tempAvatarLocation, Settings.DestinationPathAvatars)) return false;

                                    // exec delete
                                    File.Delete(tempAvatarLocation);
                                }
                            }
                        }

                        // TODO:
                        // keep this updated as new features are added (delete sets etc)

                        // username will stay permanently reserved to prevent someone else from registering it and impersonating

                        db.SaveChanges();

                        return true;
                    }
                    // user account could not be found
                    return false;
                }

            }
        }
Пример #39
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;
                }
            }
        }
Пример #40
0
        // 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.SubmissionVoteTrackers.RemoveRange(db.SubmissionVoteTrackers.Where(x => x.UserName == userName));

                        // remove voting history for comments
                        db.CommentVoteTrackers.RemoveRange(db.CommentVoteTrackers.Where(x => x.UserName == userName));

                        // remove all comments
                        var comments = db.Comments.Where(c => c.UserName == userName).ToList();
                        foreach (Comment c in comments)
                        {
                            c.IsDeleted = true;
                            c.Content   = "deleted by user";
                        }
                        db.SaveChanges();

                        // remove all submissions
                        var submissions = db.Submissions.Where(c => c.UserName == userName).ToList();
                        foreach (var s in submissions)
                        {
                            if (s.Type == 1)
                            {
                                s.IsDeleted = true;
                                s.Content   = "deleted by user";
                                s.Title     = "deleted by user";
                            }
                            else
                            {
                                s.IsDeleted       = true;
                                s.LinkDescription = "deleted by user";
                                s.Content         = "http://voat.co";
                            }
                        }

                        // resign from all moderating positions
                        db.SubverseModerators.RemoveRange(db.SubverseModerators.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.SubmissionReplyNotifications.RemoveRange(db.SubmissionReplyNotifications.Where(prp => prp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete private messages
                        db.PrivateMessages.RemoveRange(db.PrivateMessages.Where(pm => pm.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase)));

                        // delete short bio if userprefs record exists for this user
                        var userPrefs = db.UserPreferences.Find(userName);
                        if (userPrefs != null)
                        {
                            userPrefs.Bio = null;
                        }

                        // TODO: delete avatar
                        // userPrefs.Avatar = ""

                        // TODO:
                        // keep this updated as new features are added (delete sets etc)

                        // username will stay permanently reserved to prevent someone else from registering it and impersonating

                        db.SaveChanges();

                        return(true);
                    }
                    // user account could not be found
                    return(false);
                }
            }
        }