Ejemplo n.º 1
0
        public ActionResult PushMerge(PostMergeRequestCommand 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(new[] { "diffmod", "diffdel", "diffins" }))
            {
                return(ResultData(null, false, "内容未被修改!"));
            }

            #region 合并验证

            if (PostMergeRequestService.Any(p => p.ModifierEmail == dto.ModifierEmail && p.MergeState == MergeStatus.Block))
            {
                return(ResultData(null, false, "由于您曾经多次恶意修改文章,已经被标记为黑名单,无法修改任何文章,如有疑问,请联系网站管理员进行处理。"));
            }

            if (post.PostMergeRequests.Any(p => p.ModifierEmail == dto.ModifierEmail && p.MergeState == MergeStatus.Pending))
            {
                return(ResultData(null, false, "您已经提交过一次修改请求正在待处理,暂不能继续提交修改请求!"));
            }

            #endregion

            #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, false, "操作失败!"));
            }

            RedisHelper.Expire("code:" + dto.ModifierEmail, 1);
            MessageService.AddEntitySaved(new InternalMessage()
            {
                Title   = $"来自【{dto.Modifier}】的文章修改合并请求",
                Content = dto.Title,
                Link    = "#/merge/compare?id=" + merge.Id
            });
            var content = new Template(System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/merge-request.html")).Set("title", post.Title).Set("link", Url.Action("Index", "Dashboard", new { }, Request.Scheme) + "#/merge/compare?id=" + merge.Id).Render();
            BackgroundJob.Enqueue(() => CommonHelper.SendMail("博客文章修改请求:", content, CommonHelper.SystemSettings["ReceiveEmail"]));
            return(ResultData(null, true, "您的修改请求已提交,已进入审核状态,感谢您的参与!"));
        }