public bool IsCommentSpam(OxiteContext context, CommentIn comment) { if (context.User.IsAuthenticated) { return(false); } SpamCandidate spamCandidate = new SpamCandidate { blog = null, comment_author = comment.CreatorName, comment_author_email = comment.CreatorEmail, comment_author_url = comment.CreatorUrl, comment_content = comment.Body, comment_type = "comment", permalink = null, referrer = context.HttpContext.Request.UrlReferrer, user_agent = context.HttpContext.Request.UserAgent, user_ip = context.HttpContext.Request.UserHostAddress }; HttpWebRequest validationRequest = context.GeneratePostRequest( GetApiMethodUri("comment-check"), Version, spamCandidate.ToQueryString() ); try { HttpWebResponse validationResponse = validationRequest.GetResponse() as HttpWebResponse; string responseCode = new StreamReader(validationResponse.GetResponseStream()).ReadToEnd(); return(bool.Parse(responseCode)); } catch { return(false); } }
public ModelResult <ScheduleItemComment> AddComment(ScheduleItemAddress scheduleItemAddress, CommentInput commentInput) { CommentIn commentIn = new CommentIn(commentInput); pluginEngine.ExecuteAll("ProcessInputOfComment", new { context, comment = commentIn }); commentInput = commentIn.ToCommentInput(); commentInput = pluginEngine.Process <CommentIn>("ProcessInputOfCommentOnAdd", new CommentIn(commentInput)).ToCommentInput(); if (pluginEngine.AnyTrue("IsCommentSpam", new { context, comment = commentIn })) { return(new ModelResult <ScheduleItemComment>(new ValidationStateDictionary(typeof(CommentInput), new ValidationState(new[] { new ValidationError("Comment.IsSpam", commentInput, "The supplied comment was considered to be spam and was not added") })))); } ValidationStateDictionary validationState = ValidateCommentInput(commentInput); if (!validationState.IsValid) { return(new ModelResult <ScheduleItemComment>(validationState)); } EntityState commentState; try { commentState = context.User.IsAuthenticated ? EntityState.Normal : (EntityState)Enum.Parse(typeof(EntityState), context.Site.CommentStateDefault); } catch { commentState = EntityState.PendingApproval; } //TODO: (erikpo) Replace with some logic to set the language from the user's browser or from a dropdown list Language language = languageRepository.GetLanguage(context.Site.LanguageDefault ?? "en"); ScheduleItemComment comment; using (TransactionScope transaction = new TransactionScope()) { string commentSlug = generateUniqueCommentSlug(scheduleItemAddress); comment = commentInput.ToComment(context.User.Cast <UserAuthenticated>(), context.HttpContext.Request.GetUserIPAddress().ToLong(), context.HttpContext.Request.UserAgent, language, commentSlug, commentState); comment = conferencesCommentRepository.Save(comment, context.Site.ID, scheduleItemAddress.EventName, scheduleItemAddress.ScheduleItemSlug); if (comment.State == EntityState.Normal) { invalidateCachedCommentDependencies(comment); } transaction.Complete(); } //TODO: (erikpo) The following calls to setup the subscription and send out emails for those subscribed needs to happen in the transaction (but can't currently because of issues with them being in different repositories //TODO: (erikpo) Move into a module if (commentInput.Subscribe) { if (context.User.IsAuthenticated) { scheduleItemRepository.AddSubscription(context.Site.ID, comment.ScheduleItem, comment.CreatorUserID); } else { scheduleItemRepository.AddSubscription(context.Site.ID, comment); } } //TODO: (erikpo) Move into a module messageOutboundRepository.Save(generateMessages(comment.ScheduleItem, comment)); ScheduleItemSmallReadOnly scheduleItemProxy = new ScheduleItemSmallReadOnly(comment.ScheduleItem); CommentReadOnly commentProxy = new CommentReadOnly(comment, ""); pluginEngine.ExecuteAll("CommentAdded", new { context, parent = scheduleItemProxy, comment = commentProxy }); if (comment.State == EntityState.Normal) { pluginEngine.ExecuteAll("CommentApproved", new { context, parent = scheduleItemProxy, comment = commentProxy }); } return(new ModelResult <ScheduleItemComment>(comment, validationState)); }