示例#1
0
        public async Task <ActionResult> Submit([Bind(Include = "Id,Votes,Name,Date,Type,Linkdescription,Title,Rank,MessageContent,Subverse")] Message submission)
        {
            // abort if model state is invalid
            if (!ModelState.IsValid)
            {
                return(View());
            }

            // save temp values for the view in case submission fails
            ViewBag.selectedSubverse = submission.Subverse;
            ViewBag.message          = submission.MessageContent;
            ViewBag.title            = submission.Title;
            ViewBag.linkDescription  = submission.Linkdescription;

            // grab server timestamp and modify submission timestamp to have posting time instead of "started writing submission" time
            submission.Date = DateTime.Now;

            // check if user is banned
            if (UserHelper.IsUserGloballyBanned(User.Identity.Name) || UserHelper.IsUserBannedFromSubverse(User.Identity.Name, submission.Subverse))
            {
                ViewBag.SelectedSubverse = submission.Subverse;
                return(View("~/Views/Home/Comments.cshtml", submission));
            }

            // check if subverse exists
            var targetSubverse = _db.Subverses.Find(submission.Subverse.Trim());

            if (targetSubverse == null || submission.Subverse.Equals("all", StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to post to does not exist.");
                return(View("Submit"));
            }

            //wrap captcha check in anon method as following method is in non UI dll
            var captchaCheck = new Func <HttpRequestBase, Task <bool> >(request => {
                return(ReCaptchaUtility.Validate(request));
            });

            // check if this submission is valid and good to go
            var preProcessCheckResult = await Submissions.PreAddSubmissionCheck(submission, Request, User.Identity.Name, targetSubverse, captchaCheck);

            if (preProcessCheckResult != null)
            {
                ModelState.AddModelError(string.Empty, preProcessCheckResult);
                return(View("Submit"));
            }

            // submission is a link post
            if (submission.Type == 2 && submission.MessageContent != null && submission.Linkdescription != null)
            {
                // check if same link was submitted before and deny submission
                var existingSubmission = _db.Messages.FirstOrDefault(s => s.MessageContent.Equals(submission.MessageContent, StringComparison.OrdinalIgnoreCase) && s.Subverse.Equals(submission.Subverse, StringComparison.OrdinalIgnoreCase));

                // submission is a repost, discard it and inform the user
                if (existingSubmission != null)
                {
                    ModelState.AddModelError(string.Empty, "Sorry, this link has already been submitted by someone else.");

                    // todo: offer the option to repost after informing the user about it
                    return(RedirectToRoute(
                               "SubverseComments",
                               new
                    {
                        controller = "Comment",
                        action = "Comments",
                        id = existingSubmission.Id,
                        subversetoshow = existingSubmission.Subverse
                    }
                               ));
                }

                // process new link submission
                var addLinkSubmissionResult = await Submissions.AddNewSubmission(submission, targetSubverse, User.Identity.Name);

                if (addLinkSubmissionResult != null)
                {
                    ModelState.AddModelError(string.Empty, addLinkSubmissionResult);
                    return(View("Submit"));
                }
                // update last submission received date for target subverse
                targetSubverse.last_submission_received = DateTime.Now;
                await _db.SaveChangesAsync();
            }
            // submission is a message type submission
            else if (submission.Type == 1 && submission.Title != null)
            {
                // process new message type submission
                var addMessageSubmissionResult = await Submissions.AddNewSubmission(submission, targetSubverse, User.Identity.Name);

                if (addMessageSubmissionResult != null)
                {
                    ModelState.AddModelError(string.Empty, addMessageSubmissionResult);
                    return(View("Submit"));
                }
                // update last submission received date for target subverse
                targetSubverse.last_submission_received = DateTime.Now;
                await _db.SaveChangesAsync();
            }

            // redirect to comments section of newly posted submission
            return(RedirectToRoute(
                       "SubverseComments",
                       new
            {
                controller = "Comment",
                action = "Comments",
                id = submission.Id,
                subversetoshow = submission.Subverse
            }
                       ));
        }