Example #1
0
        // GET: comments for a given submission
        public ActionResult Comments(int?id, string subversetoshow, int?startingcommentid, string sort, int?commentToHighLight)
        {
            #region Validation

            if (id == null)
            {
                return(View("~/Views/Errors/Error.cshtml"));
            }

            var submission = _db.Submissions.Find(id.Value);

            if (submission == null)
            {
                return(View("~/Views/Errors/Error_404.cshtml"));
            }

            // make sure that the combination of selected subverse and submission subverse are linked
            if (!submission.Subverse.Equals(subversetoshow, StringComparison.OrdinalIgnoreCase))
            {
                return(View("~/Views/Errors/Error_404.cshtml"));
            }

            var subverse = DataCache.Subverse.Retrieve(subversetoshow);
            //var subverse = _db.Subverse.Find(subversetoshow);

            if (subverse == null)
            {
                return(View("~/Views/Errors/Error_404.cshtml"));
            }

            //HACK: Disable subverse
            if (subverse.IsAdminDisabled.HasValue && subverse.IsAdminDisabled.Value)
            {
                ViewBag.Subverse = subverse.Name;
                return(View("~/Views/Errors/SubverseDisabled.cshtml"));
            }

            #endregion

            ViewBag.SelectedSubverse   = subverse.Name;
            ViewBag.SubverseAnonymized = subverse.IsAnonymized;

            //Temp cache user votes for this thread
            ViewBag.VoteCache         = UserCommentVotesBySubmission(id.Value);
            ViewBag.SavedCommentCache = UserSavedCommentsBySubmission(id.Value);
            ViewBag.CCP = Karma.CommentKarma(User.Identity.Name);

            if (startingcommentid != null)
            {
                ViewBag.StartingCommentId = startingcommentid;
            }

            if (commentToHighLight != null)
            {
                ViewBag.CommentToHighLight = commentToHighLight;
            }

            var SortingMode = (sort == null ? "top" : sort).ToLower();
            ViewBag.SortingMode = SortingMode;



            // experimental: register a new session for this subverse
            string clientIpAddress = String.Empty;

            if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                clientIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            else if (Request.UserHostAddress.Length != 0)
            {
                clientIpAddress = Request.UserHostAddress;
            }

            if (clientIpAddress != String.Empty)
            {
                // generate salted hash of client IP address
                string ipHash = IpHash.CreateHash(clientIpAddress);

                var currentSubverse = (string)RouteData.Values["subversetoshow"];

                // register a new session for this subverse
                SessionHelper.Add(currentSubverse, ipHash);

                // register a new view for this thread
                // check if this hash is present for this submission id in viewstatistics table
                var existingView = _db.ViewStatistics.Find(submission.ID, ipHash);

                // this IP has already viwed this thread, skip registering a new view
                if (existingView == null)
                {
                    // this is a new view, register it for this submission
                    var view = new ViewStatistic {
                        SubmissionID = submission.ID, ViewerID = ipHash
                    };
                    _db.ViewStatistics.Add(view);

                    submission.Views++;

                    _db.SaveChanges();
                }
            }

            var commentTree = DataCache.CommentTree.Retrieve <usp_CommentTree_Result>(submission.ID, null, null);

            var model = new CommentBucketViewModel()
            {
                StartingIndex = 0,
                EndingIndex   = 5,
                Subverse      = subverse,
                Submission    = submission,
                CommentTree   = commentTree,
                //DisplayTree = displayTree,
                ParentID = null,
                Sort     = (CommentSort)Enum.Parse(typeof(CommentSort), SortingMode, true)
            };

            IQueryable <usp_CommentTree_Result> displayTree = commentTree.AsQueryable().Where(x => x.ParentID == null);
            model.TotalInDisplayBranch = displayTree.Count();

            if (model.Sort == CommentSort.Top)
            {
                displayTree = displayTree.OrderByDescending(x => x.UpCount - x.DownCount).Take(model.EndingIndex);
            }
            else
            {
                displayTree = displayTree.OrderByDescending(x => x.CreationDate).Take(model.EndingIndex);
            }
            model.DisplayTree = displayTree;


            return(View("~/Views/Home/Comments.cshtml", model));
        }
        // GET: Renders Primary Submission Comments Page
        public async Task <ActionResult> Comments(int?submissionID, string subverseName, int?commentID, string sort, int?context)
        {
            #region Validation

            if (submissionID == null)
            {
                return(GenericErrorView(new ErrorViewModel()
                {
                    Description = "Can not find what was requested because input is not valid"
                }));
            }

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

            if (submission == null)
            {
                return(NotFoundErrorView());
            }

            // make sure that the combination of selected subverse and submission subverse are linked
            if (!submission.Subverse.Equals(subverseName, StringComparison.OrdinalIgnoreCase))
            {
                return(NotFoundErrorView());
            }

            var subverse = DataCache.Subverse.Retrieve(subverseName);
            //var subverse = _db.Subverse.Find(subversetoshow);

            if (subverse == null)
            {
                return(NotFoundErrorView());
            }

            if (subverse.IsAdminDisabled.HasValue && subverse.IsAdminDisabled.Value)
            {
                ViewBag.Subverse = subverse.Name;
                return(SubverseDisabledErrorView());
            }

            #endregion

            if (commentID != null)
            {
                ViewBag.StartingCommentId  = commentID;
                ViewBag.CommentToHighLight = commentID;
            }

            #region Set ViewBag
            ViewBag.Subverse   = subverse;
            ViewBag.Submission = submission;
            //This is a required view bag property in _Layout.cshtml
            ViewBag.SelectedSubverse = subverse.Name;

            var SortingMode = (sort == null ? "top" : sort).ToLower();
            ViewBag.SortingMode = SortingMode;

            #endregion

            #region Track Views

            // experimental: register a new session for this subverse
            string clientIpAddress = UserHelper.UserIpAddress(Request);
            if (clientIpAddress != String.Empty)
            {
                // generate salted hash of client IP address
                string ipHash = IpHash.CreateHash(clientIpAddress);

                // register a new session for this subverse
                SessionHelper.Add(subverse.Name, ipHash);

                //TODO: This needs to be executed in seperate task
                #region TODO

                // register a new view for this thread
                // check if this hash is present for this submission id in viewstatistics table
                var existingView = _db.ViewStatistics.Find(submission.ID, ipHash);

                // this IP has already viwed this thread, skip registering a new view
                if (existingView == null)
                {
                    // this is a new view, register it for this submission
                    var view = new ViewStatistic {
                        SubmissionID = submission.ID, ViewerID = ipHash
                    };
                    _db.ViewStatistics.Add(view);
                    submission.Views++;
                    await _db.SaveChangesAsync();
                }

                #endregion
            }

            #endregion
            CommentSegment model = null;
            if (commentID != null)
            {
                ViewBag.CommentToHighLight = commentID.Value;
                model = await GetCommentContext(submission.ID, commentID.Value, context, sort);
            }
            else
            {
                model = await GetCommentSegment(submission.ID, null, 0, sort);
            }

            var q = new QuerySubverseModerators(subverseName);
            ViewBag.ModeratorList = await q.ExecuteAsync();

            return(View("~/Views/Home/Comments.cshtml", model));
        }