Exemplo n.º 1
0
        public ActionResult Put(CommentInputDto comment)
        {
            UserInfoOutputDto user = Session.GetByRedis <UserInfoOutputDto>(SessionKey.UserInfo);

            comment.Content = comment.Content.Trim().Replace("<p><br></p>", string.Empty);

            if (Regex.Match(comment.Content, ModRegex).Length <= 0)
            {
                comment.Status = Status.Pended;
            }
            if (user != null)
            {
                comment.NickName   = user.NickName;
                comment.QQorWechat = user.QQorWechat;
                comment.Email      = user.Email;
                if (user.IsAdmin)
                {
                    comment.Status   = Status.Pended;
                    comment.IsMaster = true;
                }
            }
            comment.Content     = Regex.Replace(comment.Content.HtmlSantinizerStandard().ConvertImgSrcToRelativePath(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>");
            comment.CommentDate = DateTime.Now;
            comment.Browser     = comment.Browser ?? Request.Browser.Type;
            comment.IP          = Request.UserHostAddress;
            Comment com = CommentBll.AddEntitySaved(comment.Mapper <Comment>());

            if (com != null)
            {
                var emails = new List <string>();
                var email  = GetSettings("ReceiveEmail"); //站长邮箱
                emails.Add(email);
                string content = System.IO.File.ReadAllText(Request.MapPath("/template/notify.html")).Replace("{{title}}", com.Post.Title).Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{nickname}}", com.NickName).Replace("{{content}}", com.Content);
                if (comment.Status == Status.Pended)
                {
                    if (!com.IsMaster)
                    {
                        MessageBll.AddEntitySaved(new InternalMessage()
                        {
                            Title   = $"来自【{com.NickName}】的新文章评论",
                            Content = com.Content,
                            Link    = Url.Action("Details", "Post", new
                            {
                                id  = com.PostId,
                                cid = com.Id
                            }, Request.Url.Scheme) + "#comment"
                        });
                    }
#if !DEBUG
                    if (com.ParentId == 0)
                    {
                        emails.Add(PostBll.GetById(com.PostId).Email);
                        //新评论,只通知博主和楼主
                        BackgroundJob.Enqueue(() => SendMail(Request.Url.Authority + "|博客文章新评论:", content.Replace("{{link}}", Url.Action("Details", "Post", new
                        {
                            id  = com.PostId,
                            cid = com.Id
                        }, Request.Url.Scheme) + "#comment"), string.Join(",", emails.Distinct())));
                    }
                    else
                    {
                        //通知博主和上层所有关联的评论访客
                        var pid = CommentBll.GetParentCommentIdByChildId(com.Id);
                        emails = CommentBll.GetSelfAndAllChildrenCommentsByParentId(pid).Select(c => c.Email).Except(new List <string>()
                        {
                            com.Email
                        }).Distinct().ToList();
                        string link = Url.Action("Details", "Post", new
                        {
                            id  = com.PostId,
                            cid = com.Id
                        }, Request.Url.Scheme) + "#comment";
                        BackgroundJob.Enqueue(() => SendMail($"{Request.Url.Authority}{GetSettings("Title")}文章评论回复:", content.Replace("{{link}}", link), string.Join(",", emails)));
                    }
#endif
                    return(ResultData(null, true, "评论发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将显示到评论列表中"));
                }
                BackgroundJob.Enqueue(() => SendMail(Request.Url.Authority + "|博客文章新评论(待审核):", content.Replace("{{link}}", Url.Action("Details", "Post", new
                {
                    id  = com.PostId,
                    cid = com.Id
                }, Request.Url.Scheme) + "#comment") + "<p style='color:red;'>(待审核)</p>", string.Join(",", emails)));
                return(ResultData(null, true, "评论成功,待站长审核通过以后将显示"));
            }
            return(ResultData(null, false, "评论失败"));
        }