コード例 #1
0
        public ActionResult PushMerge(PostMergeRequestInputDto dto)
        {
            if (RedisHelper.Get("code:" + dto.ModifierEmail) != dto.Code)
            {
                return(ResultData(null, false, "验证码错误!"));
            }

            var post = PostService.GetById(dto.PostId) ?? throw new NotFoundException("文章未找到");
            var diff = new HtmlDiff.HtmlDiff(post.Content.RemoveHtmlTag(), dto.Content.RemoveHtmlTag());

            if (post.Title.Equals(dto.Title) && !diff.Build().Contains("diffmod"))
            {
                return(ResultData(null, false, "内容未被修改!"));
            }

            #region 直接合并

            if (post.Email.Equals(dto.ModifierEmail))
            {
                var history = post.Mapper <PostHistoryVersion>();
                Mapper.Map(dto, post);
                post.PostHistoryVersion.Add(history);
                post.ModifyDate = DateTime.Now;
                return(PostService.SaveChanges() > 0 ? ResultData(null, true, "你是文章原作者,无需审核,文章已自动更新并在首页展示!") : ResultData(null, false, "操作失败!"));
            }

            #endregion

            var merge = post.PostMergeRequests.FirstOrDefault(r => r.Id == dto.Id && r.MergeState != MergeStatus.Merged);
            if (merge != null)
            {
                Mapper.Map(dto, merge);
                merge.SubmitTime = DateTime.Now;
                merge.MergeState = MergeStatus.Pending;
            }
            else
            {
                merge = Mapper.Map <PostMergeRequest>(dto);
                post.PostMergeRequests.Add(merge);
            }

            var b = PostService.SaveChanges() > 0;
            if (!b)
            {
                return(ResultData(null, b, b ? "您的修改请求已提交,已进入审核状态,感谢您的参与!" : "操作失败!"));
            }

            RedisHelper.Expire("code:" + dto.ModifierEmail, 1);
            MessageService.AddEntitySaved(new InternalMessage()
            {
                Title   = $"来自【{dto.Modifier}】的文章修改合并请求",
                Content = dto.Title,
                Link    = "#/merge/compare?id=" + merge.Id
            });
            var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/merge-request.html").Replace("{{title}}", post.Title).Replace("{{link}}", Url.Action("Index", "Dashboard", new { }, Request.Scheme) + "#/merge/compare?id=" + merge.Id);
            BackgroundJob.Enqueue(() => CommonHelper.SendMail("博客文章修改请求:", content, CommonHelper.SystemSettings["ReceiveEmail"]));

            return(ResultData(null, b, b ? "您的修改请求已提交,已进入审核状态,感谢您的参与!" : "操作失败!"));
        }
コード例 #2
0
        public ActionResult Submit(LeaveMessageCommand dto)
        {
            if (Regex.Match(dto.NickName + dto.Content, CommonHelper.BanRegex).Length > 0)
            {
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            dto.Content = dto.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (dto.Content.RemoveHtmlTag().Trim().Equals(HttpContext.Session.Get <string>("msg")))
            {
                return(ResultData(null, false, "您刚才已经发表过一次留言了!"));
            }

            var msg = dto.Mapper <LeaveMessage>();

            if (Regex.Match(dto.NickName + dto.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Published;
            }

            msg.PostDate = DateTime.Now;
            var user = HttpContext.Session.Get <UserInfoDto>(SessionKey.UserInfo);

            if (user != null)
            {
                msg.NickName   = user.NickName;
                msg.QQorWechat = user.QQorWechat;
                msg.Email      = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Published;
                    msg.IsMaster = true;
                }
            }

            msg.Content  = dto.Content.HtmlSantinizerStandard().ClearImgAttributes();
            msg.Browser  = dto.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = ClientIP;
            msg.Location = msg.IP.GetIPLocation();
            msg          = LeaveMessageService.AddEntitySaved(msg);
            if (msg == null)
            {
                return(ResultData(null, false, "留言发表失败!"));
            }

            HttpContext.Session.Set("msg", msg.Content.RemoveHtmlTag().Trim());
            var email   = CommonHelper.SystemSettings["ReceiveEmail"];
            var content = new Template(System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/notify.html")).Set("title", "网站留言板").Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Set("nickname", msg.NickName).Set("content", msg.Content);

            if (msg.Status == Status.Published)
            {
                if (!msg.IsMaster)
                {
                    MessageService.AddEntitySaved(new InternalMessage()
                    {
                        Title   = $"来自【{msg.NickName}】的新留言",
                        Content = msg.Content,
                        Link    = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)
                    });
                }
#if !DEBUG
                if (msg.ParentId == 0)
                {
                    //新评论,只通知博主
                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言:", content.Set("link", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)).Render(false), email));
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    var    pid    = LeaveMessageService.GetParentMessageIdByChildId(msg.Id);
                    var    emails = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).Append(email).Except(new[] { msg.Email }).ToHashSet();
                    string link   = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme);
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s));
                    }
                }
#endif
                return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
            }

            BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言(待审核):", content.Set("link", Url.Action("Index", "Msg", new
            {
                cid = msg.Id
            }, Request.Scheme)).Render(false) + "<p style='color:red;'>(待审核)</p>", email));
            return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }
コード例 #3
0
        public ActionResult Put(CommentInputDto dto)
        {
            if (Regex.Match(dto.Content, CommonHelper.BanRegex).Length > 0)
            {
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            Post post = PostService.GetById(dto.PostId);

            if (post is null)
            {
                return(ResultData(null, false, "评论失败,文章不存在!"));
            }

            if (post.DisableComment)
            {
                return(ResultData(null, false, "本文已禁用评论功能,不允许任何人回复!"));
            }

            dto.Content = dto.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (dto.Content.RemoveHtmlTag().Trim().Equals(HttpContext.Session.Get <string>("comment" + dto.PostId)))
            {
                return(ResultData(null, false, "您刚才已经在这篇文章发表过一次评论了,换一篇文章吧,或者换一下评论内容吧!"));
            }

            var comment = dto.Mapper <Comment>();

            if (Regex.Match(dto.Content, CommonHelper.ModRegex).Length <= 0)
            {
                comment.Status = Status.Pended;
            }

            comment.CommentDate = DateTime.Now;
            var user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);

            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  = dto.Content.HtmlSantinizerStandard().ClearImgAttributes();
            comment.Browser  = dto.Browser ?? Request.Headers[HeaderNames.UserAgent];
            comment.IP       = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            comment.Location = comment.IP.GetIPLocation().Split("|").Where(s => !int.TryParse(s, out _)).ToHashSet().Join("|");
            comment          = CommentService.AddEntitySaved(comment);
            if (comment == null)
            {
                return(ResultData(null, false, "评论失败"));
            }

            HttpContext.Session.Set("comment" + comment.PostId, comment.Content.RemoveHtmlTag().Trim());
            var emails = new HashSet <string>();
            var email  = CommonHelper.SystemSettings["ReceiveEmail"]; //站长邮箱

            emails.Add(email);
            var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/notify.html")
                          .Replace("{{title}}", post.Title)
                          .Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                          .Replace("{{nickname}}", comment.NickName)
                          .Replace("{{content}}", comment.Content);

            if (comment.Status == Status.Pended)
            {
                if (!comment.IsMaster)
                {
                    MessageService.AddEntitySaved(new InternalMessage()
                    {
                        Title   = $"来自【{comment.NickName}】的新文章评论",
                        Content = comment.Content,
                        Link    = Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment"
                    });
                }
#if !DEBUG
                if (comment.ParentId == 0)
                {
                    emails.Add(post.Email);
                    emails.Add(post.ModifierEmail);
                    //新评论,只通知博主和楼主
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客文章新评论:", content.Replace("{{link}}", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment"), s));
                    }
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    var pid = CommentService.GetParentCommentIdByChildId(comment.Id);
                    emails.AddRange(CommentService.GetSelfAndAllChildrenCommentsByParentId(pid).Select(c => c.Email).ToArray());
                    emails.AddRange(post.Email, post.ModifierEmail);
                    emails.Remove(comment.Email);
                    string link = Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment";
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{CommonHelper.SystemSettings["Domain"]}{CommonHelper.SystemSettings["Title"]}文章评论回复:", content.Replace("{{link}}", link), s));
                    }
                }
#endif
                return(ResultData(null, true, "评论发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将显示到评论列表中"));
            }

            foreach (var s in emails)
            {
                BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客文章新评论(待审核):", content.Replace("{{link}}", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment") + "<p style='color:red;'>(待审核)</p>", s));
            }

            return(ResultData(null, true, "评论成功,待站长审核通过以后将显示"));
        }
コード例 #4
0
        public ActionResult Put(LeaveMessageInputDto msg)
        {
            if (Regex.Match(msg.Content, CommonHelper.BanRegex).Length > 0)
            {
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请注意改善您的言辞!"));
            }

            UserInfoOutputDto user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);

            msg.Content = msg.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (msg.Content.RemoveHtmlTag().Trim().Equals(HttpContext.Session.Get <string>("msg")))
            {
                return(ResultData(null, false, "您刚才已经发表过一次留言了!"));
            }
            if (Regex.Match(msg.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Pended;
            }

            if (user != null)
            {
                msg.NickName   = user.NickName;
                msg.QQorWechat = user.QQorWechat;
                msg.Email      = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Pended;
                    msg.IsMaster = true;
                }
            }
            msg.PostDate = DateTime.Now;
            msg.Content  = msg.Content.HtmlSantinizerStandard().ClearImgAttributes();
            msg.Browser  = msg.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            LeaveMessage msg2 = LeaveMessageService.AddEntitySaved(msg.Mapper <LeaveMessage>());

            if (msg2 != null)
            {
                HttpContext.Session.Set("msg", msg.Content.RemoveHtmlTag().Trim());
                var    email   = CommonHelper.SystemSettings["ReceiveEmail"];
                string content = System.IO.File.ReadAllText(HostingEnvironment.WebRootPath + "/template/notify.html").Replace("{{title}}", "网站留言板").Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{nickname}}", msg2.NickName).Replace("{{content}}", msg2.Content);
                if (msg.Status == Status.Pended)
                {
                    if (!msg2.IsMaster)
                    {
                        MessageService.AddEntitySaved(new InternalMessage()
                        {
                            Title   = $"来自【{msg2.NickName}】的新留言",
                            Content = msg2.Content,
                            Link    = Url.Action("Index", "Msg", new { cid = msg2.Id }, Request.Scheme)
                        });
                    }
#if !DEBUG
                    if (msg.ParentId == 0)
                    {
                        //新评论,只通知博主
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客新留言:", content.Replace("{{link}}", Url.Action("Index", "Msg", new { cid = msg2.Id }, Request.Scheme)), email));
                    }
                    else
                    {
                        //通知博主和上层所有关联的评论访客
                        var pid    = LeaveMessageService.GetParentMessageIdByChildId(msg2.Id);
                        var emails = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).ToList();
                        emails.Add(email);
                        string link = Url.Action("Index", "Msg", new { cid = msg2.Id }, Request.Scheme);
                        foreach (var s in emails.Distinct().Except(new[] { msg2.Email }))
                        {
                            BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{CommonHelper.SystemSettings["Domain"]}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Replace("{{link}}", link), s));
                        }
                    }
#endif
                    return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
                }
                BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客新留言(待审核):", content.Replace("{{link}}", Url.Action("Index", "Msg", new
                {
                    cid = msg2.Id
                }, Request.Scheme)) + "<p style='color:red;'>(待审核)</p>", email));
                return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
            }
            return(ResultData(null, false, "留言发表失败!"));
        }