Example #1
0
        public ActionResult Post(string id, string body, bool?subscribe)
        {
            id   = id.NullSafe();
            body = body.NullSafe();

            string captchaChallenge = null;
            string captchaResponse  = null;
            bool   captchaEnabled   = !CurrentUser.ShouldHideCaptcha();

            if (captchaEnabled)
            {
                captchaChallenge = HttpContext.Request.Form[CaptchaValidator.ChallengeInputName];
                captchaResponse  = HttpContext.Request.Form[CaptchaValidator.ResponseInputName];
            }

            JsonViewData viewData = Validate <JsonViewData>(
                new Validation(() => string.IsNullOrEmpty(id), "Story identifier cannot be blank."),
                new Validation(() => id.ToGuid().IsEmpty(), "Invalid story identifier."),
                new Validation(() => string.IsNullOrEmpty(body.NullSafe()), "Comment cannot be blank."),
                new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaChallenge), "Captcha challenge cannot be blank."),
                new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaResponse), "Captcha verification words cannot be blank."),
                new Validation(() => !IsCurrentUserAuthenticated, "You are currently not authenticated."),
                new Validation(() => captchaEnabled && !CaptchaValidator.Validate(CurrentUserIPAddress, captchaChallenge, captchaResponse), "Captcha verification words are incorrect.")
                );

            if (viewData == null)
            {
                try
                {
                    IStory story = _storyRepository.FindById(id.ToGuid());

                    if (story == null)
                    {
                        viewData = new JsonViewData {
                            errorMessage = "Specified story does not exist."
                        };
                    }
                    else
                    {
                        CommentCreateResult result = _storyService.Comment(
                            story,
                            string.Concat(Settings.RootUrl, Url.RouteUrl("Detail", new { name = story.UniqueName })),
                            CurrentUser,
                            body,
                            subscribe ?? false,
                            CurrentUserIPAddress,
                            HttpContext.Request.UserAgent,
                            ((HttpContext.Request.UrlReferrer != null) ? HttpContext.Request.UrlReferrer.ToString() : null),
                            HttpContext.Request.ServerVariables
                            );

                        viewData = string.IsNullOrEmpty(result.ErrorMessage) ? new JsonCreateViewData {
                            isSuccessful = true
                        } : new JsonViewData {
                            errorMessage = result.ErrorMessage
                        };
                    }
                }
                catch (Exception e)
                {
                    Log.Exception(e);

                    viewData = new JsonViewData {
                        errorMessage = FormatStrings.UnknownError.FormatWith("posting comment")
                    };
                }
            }

            return(Json(viewData));
        }
Example #2
0
        public ActionResult Post(string id, string body, bool?subscribe)
        {
            id   = id.NullSafe();
            body = body.NullSafe();

            string captchaChallenge = null;
            string captchaResponse  = null;
            bool   captchaEnabled   = !CurrentUser.ShouldHideCaptcha();

            var    validCaptcha = true;
            string userResponse = null;

            if (captchaEnabled)
            {
                //captchaChallenge = HttpContext.Request.Form[CaptchaValidator.ChallengeInputName];
                //captchaResponse = HttpContext.Request.Form[CaptchaValidator.ResponseInputName];
                userResponse = HttpContext.Request.Params["g-recaptcha-response"];
                validCaptcha = CaptchaValidatorFunc(userResponse);
            }

            JsonViewData viewData = Validate <JsonViewData>(
                new Validation(() => string.IsNullOrEmpty(id), "Identyfikator artykułu nie może być pusty."),
                new Validation(() => id.ToGuid().IsEmpty(), "Niepoprawny identyfikator artykułu."),
                new Validation(() => string.IsNullOrEmpty(body.NullSafe()), "Komentarz nie może być pusty."),
                new Validation(() => captchaEnabled && string.IsNullOrEmpty(userResponse),
                               "Pole Captcha nie może być puste."),
                new Validation(() => captchaEnabled && !validCaptcha, "Weryfikacja Captcha nieudana."),
                new Validation(() => !IsCurrentUserAuthenticated, "Nie jesteś zalogowany.")
                );

            if (viewData == null)
            {
                try
                {
                    IStory story = _storyRepository.FindById(id.ToGuid());

                    if (story == null)
                    {
                        viewData = new JsonViewData {
                            errorMessage = "Podany artykuł nie istnieje."
                        };
                    }
                    else
                    {
                        CommentCreateResult result = _storyService.Comment(
                            story,
                            string.Concat(Settings.RootUrl, Url.RouteUrl("Detail", new { name = story.UniqueName })),
                            CurrentUser,
                            body,
                            subscribe ?? false,
                            CurrentUserIPAddress,
                            HttpContext.Request.UserAgent,
                            ((HttpContext.Request.UrlReferrer != null) ? HttpContext.Request.UrlReferrer.ToString() : null),
                            HttpContext.Request.ServerVariables
                            );

                        viewData = string.IsNullOrEmpty(result.ErrorMessage) ? new JsonCreateViewData {
                            isSuccessful = true
                        } : new JsonViewData {
                            errorMessage = result.ErrorMessage
                        };
                    }
                }
                catch (Exception e)
                {
                    Log.Exception(e);

                    viewData = new JsonViewData {
                        errorMessage = FormatStrings.UnknownError.FormatWith("dodawania komentarza.")
                    };
                }
            }

            return(Json(viewData));
        }