示例#1
0
        public ViewArticleViewModel(string slug)
        {
            this.Slug    = slug;
            this.Article = ArticleModel.GetArticleBySlug(slug);

            this.getFeaturedComments = new Lazy <ViewCommentsViewModel>(() => new ViewCommentsViewModel(this.Article, this.Article.GetFeaturedComments()));
        }
        public ActionResult ViewArticle(string articleSlug)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            return(View(new ViewArticleViewModel(article)));
        }
示例#3
0
        public ActionResult ViewArticleBySlug(string articleSlug, bool onlyBodyAndAdHtml = false)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(ErrorStatus(HttpStatusCode.NotFound, "Invalid Article Slug"));
            }

            return(FormatOutput(article, onlyBodyAndAdHtml));
        }
示例#4
0
        public string ViewArticleBySlug(string articleSlug, bool onlyBodyAndAdHtml = false)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(ErrorStatus("Invalid Article Slug"));
            }

            return(FormatOutput(article, onlyBodyAndAdHtml));
        }
        public ActionResult Addendum(string articleSlug, int id)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            var comment = CommentModel.GetCommentById(id);

            if (comment == null || comment.ArticleId != article.Id)
            {
                return(HttpNotFound());
            }

            if (comment.UserToken == null || comment.PublishedDate.Add(CommentEditTimeout) <= DateTime.Now)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            var cookie = Request.Cookies["tdwtf_token"];

            if (cookie == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            try
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                if (ticket.Expired || comment.UserToken != ticket.UserData)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }
            }
            catch (HttpException)
            {
                // cookie was invalid, redirect to login
                return(Redirect("/login"));
            }

            return(View(new AddendumViewModel(article, comment)));
        }
        public ActionResult ViewArticleComments(string articleSlug, int page, int?parent)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            if (parent.HasValue)
            {
                return(View(new ViewCommentsViewModel(article, page)
                {
                    Comment = { Parent = parent }
                }));
            }

            return(View(new ViewCommentsViewModel(article, page)));
        }
示例#7
0
        public ActionResult ViewArticleComments(string articleSlug)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            if (!article.DiscourseTopicOpened && article.DiscourseTopicId != null && article.PublishedDate < DateTime.Now)
            {
                DiscourseHelper.OpenCommentDiscussion((int)article.Id, (int)article.DiscourseTopicId);
            }

            bool commentsPulled = DiscourseHelper.PullCommentsFromDiscourse(article);

            if (commentsPulled)
            {
                article = ArticleModel.GetArticleBySlug(articleSlug); // reload article with cached comments
            }
            return(View(new ViewCommentsViewModel(article)));
        }
        public ActionResult Addendum(string articleSlug, int id, CommentFormModel post)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            if (string.IsNullOrWhiteSpace(post.Body))
            {
                return(Redirect(article.Url));
            }

            var comment = CommentModel.GetCommentById(id);

            if (comment == null || comment.ArticleId != article.Id)
            {
                return(HttpNotFound());
            }

            if (comment.UserToken == null || comment.PublishedDate.Add(CommentEditTimeout) <= DateTime.Now)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            var cookie = Request.Cookies["tdwtf_token"];

            if (cookie == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            try
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                if (ticket.Expired || comment.UserToken != ticket.UserData)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }
            }
            catch (HttpException)
            {
                return(Redirect("/login"));
            }

            var addendumModel = new AddendumViewModel(article, comment)
            {
                Body = post.Body
            };

            if (post.Body.Length > addendumModel.MaxBodyLength)
            {
                ModelState.AddModelError(string.Empty, "Comment too long.");
            }
            if (ModelState.IsValid)
            {
                DB.Comments_CreateOrUpdateComment(
                    Comment_Id: comment.Id,
                    Article_Id: article.Id,
                    Body_Html: $"{comment.BodyRaw}\n\n**Addendum {DateTime.Now}:**\n{post.Body}",
                    User_Name: comment.Username,
                    Posted_Date: comment.PublishedDate,
                    User_IP: comment.UserIP,
                    User_Token: comment.UserToken,
                    Parent_Comment_Id: comment.ParentCommentId
                    );

                return(Redirect(article.Url));
            }

            return(View(addendumModel));
        }
        public async Task <ActionResult> ViewArticleComments(string articleSlug, int page, CommentFormModel form)
        {
            var article = ArticleModel.GetArticleBySlug(articleSlug);

            if (article == null)
            {
                return(HttpNotFound());
            }

            string token  = null;
            var    cookie = Request.Cookies["tdwtf_token"];

            if (cookie != null)
            {
                try
                {
                    var ticket = FormsAuthentication.Decrypt(cookie.Value);
                    if (!ticket.Expired)
                    {
                        form.Name = ticket.Name;
                        token     = ticket.UserData;
                    }
                }
                catch (HttpException)
                {
                    // cookie was invalid, redirect to login page
                    return(Redirect("/login"));
                }
            }

            if (token == null)
            {
                await this.CheckRecaptchaAsync();
            }

            var ip = Request.ServerVariables["REMOTE_ADDR"];

            if (string.IsNullOrWhiteSpace(form.Name))
            {
                ModelState.AddModelError(string.Empty, "A name is required.");
            }
            if (string.IsNullOrWhiteSpace(form.Body))
            {
                ModelState.AddModelError(string.Empty, "A comment is required.");
            }
            if (form.Parent.HasValue && CommentModel.GetCommentById(form.Parent.Value) == null)
            {
                ModelState.AddModelError(string.Empty, "Invalid parent comment.");
            }
            if (form.Body.Length > CommentFormModel.MaxBodyLength)
            {
                ModelState.AddModelError(string.Empty, "Comment too long.");
            }
            if (ModelState.IsValid)
            {
                var containsLinks = CommonMarkConverter.Parse(form.Body).AsEnumerable().Any(n => n.Inline?.Tag == CommonMark.Syntax.InlineTag.Link || n.Inline?.Tag == CommonMark.Syntax.InlineTag.Image || n.Inline?.Tag == CommonMark.Syntax.InlineTag.RawHtml || n.Block?.Tag == CommonMark.Syntax.BlockTag.HtmlBlock);
                var shouldHide    = containsLinks || DB.Comments_UserHasApprovedComment(ip, token) != true;

                int commentId = DB.Comments_CreateOrUpdateComment(
                    Article_Id: article.Id,
                    Body_Html: form.Body,
                    User_Name: form.Name,
                    Posted_Date: DateTime.Now,
                    User_IP: ip,
                    User_Token: token,
                    Parent_Comment_Id: form.Parent,
                    Hidden_Indicator: shouldHide
                    ).Value;

                return(Redirect(string.Format("{0}/{1}#comment-{2}", article.CommentsUrl, article.CachedCommentCount / ViewCommentsViewModel.CommentsPerPage + 1, commentId)));
            }

            return(View(new ViewCommentsViewModel(article, page)
            {
                Comment = form
            }));
        }
示例#10
0
 public ViewArticleViewModel(string slug)
 {
     this.Slug    = slug;
     this.Article = ArticleModel.GetArticleBySlug(slug);
 }