public virtual CommentCreateResult Comment(IStory forStory, string storyUrl, IUser byUser, string content, bool subscribe, string userIPAddress, string userAgent, string urlReferer, NameValueCollection serverVariables) { CommentCreateResult result = ValidateComment(forStory, byUser, content, userIPAddress, userAgent); if (result == null) { content = SanitizeHtml(content); if (!_settings.AllowPossibleSpamCommentSubmit) { result = EnsureNotSpam <CommentCreateResult>(byUser, userIPAddress, userAgent, storyUrl, urlReferer, content, "comment", serverVariables, "Possible spam rejected : {0}, {1}, {2}".FormatWith(storyUrl, forStory.Title, byUser), "Your comment appears to be a spam."); if (result != null) { return(result); } } using (IUnitOfWork unitOfWork = UnitOfWork.Begin()) { IComment comment = forStory.PostComment(content, SystemTime.Now(), byUser, userIPAddress); if (subscribe) { forStory.SubscribeComment(byUser); } else { forStory.UnsubscribeComment(byUser); } if (_settings.AllowPossibleSpamCommentSubmit && _settings.SendMailWhenPossibleSpamCommentSubmitted) { unitOfWork.Commit(); _spamProtection.IsSpam(CreateSpamCheckContent(byUser, userIPAddress, userAgent, storyUrl, urlReferer, content, "comment", serverVariables), (source, isSpam) => _spamPostprocessor.Process(source, isSpam, storyUrl, comment)); } else { _eventAggregator.GetEvent <CommentSubmitEvent>().Publish(new CommentSubmitEventArgs(comment, storyUrl)); unitOfWork.Commit(); } result = new CommentCreateResult(); } } return(result); }
public virtual CommentCreateResult Comment(IStory forStory, string storyUrl, IUser byUser, string content, bool subscribe, string userIPAddress, string userAgent, string urlReferer, NameValueCollection serverVariables) { CommentCreateResult result = ValidateComment(forStory, byUser, content, userIPAddress, userAgent); if (result == null) { content = SanitizeHtml(content); if (!_settings.AllowPossibleSpamCommentSubmit) { result = EnsureNotSpam <CommentCreateResult>(byUser, userIPAddress, userAgent, storyUrl, urlReferer, content, "comment", serverVariables, "Possible spam rejected : {0}, {1}, {2}".FormatWith(storyUrl, forStory.Title, byUser), "Your comment appears to be a spam."); if (result != null) { return(result); } } IComment comment = forStory.PostComment(content, SystemTime.Now(), byUser, userIPAddress); if (subscribe) { forStory.SubscribeComment(byUser); } else { forStory.UnsubscribeComment(byUser); } _userScoreService.StoryCommented(forStory, byUser); // Notify the Comment Subscribers that a new comment is posted _emailSender.SendComment(storyUrl, comment, forStory.Subscribers); if (_settings.AllowPossibleSpamCommentSubmit && _settings.SendMailWhenPossibleSpamCommentSubmitted) { _spamProtection.IsSpam(CreateSpamCheckContent(byUser, userIPAddress, userAgent, storyUrl, urlReferer, content, "comment", serverVariables), (source, isSpam) => _spamPostprocessor.Process(source, isSpam, storyUrl, comment)); } result = new CommentCreateResult(); } return(result); }
private static CommentCreateResult ValidateComment(IStory forStory, IUser byUser, string content, string userIPAddress, string userAgent) { CommentCreateResult result = null; if (forStory == null) { result = new CommentCreateResult { ErrorMessage = "Story cannot be null." }; } else if (byUser == null) { result = new CommentCreateResult { ErrorMessage = "User cannot be null." }; } else if (string.IsNullOrEmpty(content)) { result = new CommentCreateResult { ErrorMessage = "Comment cannot be blank." }; } else if (content.Trim().Length > 2048) { result = new CommentCreateResult { ErrorMessage = "Comment cannot be more than 2048 character." }; } else if (string.IsNullOrEmpty(userIPAddress)) { result = new CommentCreateResult { ErrorMessage = "User ip address cannot be blank." }; } else if (string.IsNullOrEmpty(userAgent)) { result = new CommentCreateResult { ErrorMessage = "User agent cannot be empty." }; } return(result); }
private static CommentCreateResult ValidateComment(IStory forStory, IUser byUser, string content, string userIPAddress, string userAgent) { CommentCreateResult result = null; if (forStory == null) { result = new CommentCreateResult { ErrorMessage = "Wpis nie mo¿e byæ pusty." }; } else if (byUser == null) { result = new CommentCreateResult { ErrorMessage = "U¿ytkownik nie mo¿e byæ pusty." }; } else if (string.IsNullOrEmpty(content)) { result = new CommentCreateResult { ErrorMessage = "Komentarz nie mo¿e byæ pusty." }; } else if (content.Trim().Length > 2048) { result = new CommentCreateResult { ErrorMessage = "Komentarz nie mo¿e zawieraæ wiêcej ni¿ 2048 znaków." }; } else if (string.IsNullOrEmpty(userIPAddress)) { result = new CommentCreateResult { ErrorMessage = "Adres Ip u¿ytkownika nie mo¿ê byæ pusty." }; } else if (string.IsNullOrEmpty(userAgent)) { result = new CommentCreateResult { ErrorMessage = "Pole 'User agent' nie mo¿e byæ puste." }; } return(result); }
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)); }
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)); }