Exemplo n.º 1
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task <ActionResult> GetMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, int cid = 0)
        {
            if (cid != 0)
            {
                var message = await LeaveMessageService.GetByIdAsync(cid) ?? throw new NotFoundException("留言未找到");

                var single = new[] { message.Root() };
                foreach (var m in single.Flatten())
                {
                    m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                    if (!CurrentUser.IsAdmin)
                    {
                        m.Email    = null;
                        m.IP       = null;
                        m.Location = null;
                    }
                }

                return(ResultData(new
                {
                    total = 1,
                    parentTotal = 1,
                    page,
                    size,
                    rows = single.Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            var parent = await LeaveMessageService.GetPagesAsync(page, size, m => m.ParentId == 0 && (m.Status == Status.Published || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Data.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            var total = parent.TotalCount;

            parent.Data.Flatten().ForEach(m =>
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                if (!CurrentUser.IsAdmin)
                {
                    m.Email    = null;
                    m.IP       = null;
                    m.Location = null;
                }
            });
            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = Mapper.Map <List <LeaveMessageViewModel> >(parent.Data)
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }
        /// <summary>
        /// 获取站内消息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMessages()
        {
            var post = PostService.GetQuery(p => p.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.Title,
                p.PostDate,
                p.Author
            }).Cacheable();
            var msgs = LeaveMessageService.GetQuery(m => m.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.PostDate,
                p.NickName
            }).Cacheable();
            var comments = CommentService.GetQuery(c => c.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.CommentDate,
                p.PostId,
                p.NickName
            }).Cacheable();

            return(ResultData(new
            {
                post,
                msgs,
                comments
            }));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Index()
        {
            ViewBag.TotalCount = LeaveMessageService.Count(m => m.ParentId == 0 && m.Status == Status.Published);
            var text = await System.IO.File.ReadAllTextAsync(Path.Combine(HostEnvironment.WebRootPath, "template", "agreement.html"));

            return(CurrentUser.IsAdmin ? View("Index_Admin", text) : View(model: text));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Index()
        {
            ViewBag.TotalCount = LeaveMessageService.Count(m => m.ParentId == null && m.Status == Status.Published);
            var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "agreement.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);

            return(CurrentUser.IsAdmin ? View("Index_Admin", text) : View(model: text));
        }
Exemplo n.º 5
0
        public ActionResult GetPendingMsgs(int page = 1, int size = 10)
        {
            var list      = LeaveMessageService.GetPages <DateTime, LeaveMessageOutputDto>(page, size, out int total, m => m.Status == Status.Pending, l => l.PostDate, false).ToList();
            var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();

            return(PageResult(list, pageCount, total));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取站内消息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMessages()
        {
            var post = PostService.LoadEntitiesFromL2CacheNoTracking(p => p.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.Title,
                p.PostDate,
                p.Author
            });
            var msgs = LeaveMessageService.LoadEntitiesFromL2CacheNoTracking(m => m.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.PostDate,
                p.NickName
            });
            var comments = CommentService.LoadEntitiesFromL2CacheNoTracking(c => c.Status == Status.Pending).Select(p => new
            {
                p.Id,
                p.CommentDate,
                p.PostId,
                p.NickName
            });

            return(ResultData(new
            {
                post,
                msgs,
                comments
            }));
        }
Exemplo n.º 7
0
        public ActionResult Index()
        {
            UserInfoOutputDto user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();

            ViewBag.TotalCount = LeaveMessageService.LoadEntitiesNoTracking(m => m.ParentId == 0 && m.Status == Status.Pended).Count();
            if (user.IsAdmin)
            {
                return(View("Index_Admin"));
            }
            return(View());
        }
Exemplo n.º 8
0
        public ActionResult GetPendingMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = LeaveMessageService.GetPages <DateTime, LeaveMessageDto>(page, size, m => m.Status == Status.Pending, l => l.PostDate, false);

            foreach (var m in list.Data)
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(Ok(list));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult GetMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, int cid = 0)
        {
            int total;

            if (cid != 0)
            {
                int pid    = LeaveMessageService.GetParentMessageIdByChildId(cid);
                var single = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).ToList();
                if (single.Any())
                {
                    total = 1;
                    foreach (var m in single)
                    {
                        m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                    }

                    return(ResultData(new
                    {
                        total,
                        parentTotal = total,
                        page,
                        size,
                        rows = single.Mapper <IList <LeaveMessageViewModel> >()
                    }));
                }
            }
            var parent = LeaveMessageService.GetPagesNoTracking(page, size, m => m.ParentId == 0 && (m.Status == Status.Published || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Data.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            total = parent.TotalCount;
            var qlist = parent.Data.SelectMany(c => LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(c.Id)).Where(c => c.Status == Status.Published || CurrentUser.IsAdmin).Select(m =>
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                return(m);
            });

            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = Mapper.Map <List <LeaveMessageViewModel> >(qlist)
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult GetMsgs(int page = 1, int size = 10, int cid = 0)
        {
            UserInfoOutputDto user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
            int total;

            if (cid != 0)
            {
                int pid    = LeaveMessageService.GetParentMessageIdByChildId(cid);
                var single = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).ToList();
                if (single.Any())
                {
                    total = 1;
                    return(ResultData(new
                    {
                        total,
                        parentTotal = total,
                        page,
                        size,
                        rows = single.Mapper <IList <LeaveMessageViewModel> >()
                    }));
                }
            }
            IEnumerable <LeaveMessage> parent = LeaveMessageService.LoadPageEntitiesNoTracking(page, size, out total, m => m.ParentId == 0 && (m.Status == Status.Pended || user.IsAdmin), m => m.PostDate, false);

            if (!parent.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            var list = new List <LeaveMessageViewModel>();

            parent.ForEach(c => LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(c.Id).ForEach(result => list.Add(result.Mapper <LeaveMessageViewModel>())));
            var qlist = list.Where(c => c.Status == Status.Pended || user.IsAdmin);

            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = qlist
                }));
            }
            return(ResultData(null, false, "没有留言"));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult GetMsgs(int page = 1, int size = 10, int cid = 0)
        {
            int total;

            if (cid != 0)
            {
                int pid    = LeaveMessageService.GetParentMessageIdByChildId(cid);
                var single = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).ToList();
                if (single.Any())
                {
                    total = 1;
                    return(ResultData(new
                    {
                        total,
                        parentTotal = total,
                        page,
                        size,
                        rows = single.Mapper <IList <LeaveMessageViewModel> >()
                    }));
                }
            }
            var parent = LeaveMessageService.GetPagesNoTracking(page, size, out total, m => m.ParentId == 0 && (m.Status == Status.Pended || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }

            var qlist = parent.AsEnumerable().SelectMany(c => LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(c.Id)).Where(c => c.Status == Status.Pended || CurrentUser.IsAdmin);

            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = Mapper.Map <List <LeaveMessageViewModel> >(qlist)
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }
Exemplo n.º 12
0
        public async Task <ActionResult> Pass(int id)
        {
            var msg = await LeaveMessageService.GetByIdAsync(id);

            msg.Status = Status.Published;
            bool b = await LeaveMessageService.SaveChangesAsync() > 0;

            if (b)
            {
                var content = new Template(await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "notify.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8)).Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Set("nickname", msg.NickName).Set("content", msg.Content);
                using var emails = LeaveMessageService.GetQuery(m => m.GroupTag == msg.GroupTag).Select(m => m.Email).Distinct().ToPooledList().Except(new List <string> { msg.Email, CurrentUser.Email }).ToPooledSet();
                var link = Url.Action("Index", "Msg", new { cid = id }, Request.Scheme);
                foreach (var s in emails)
                {
                    BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s, ClientIP));
                }
            }

            return(ResultData(null, b, b ? "审核通过!" : "审核失败!"));
        }
Exemplo n.º 13
0
        public ActionResult Pass(int id)
        {
            var msg = LeaveMessageService.GetById(id);

            msg.Status = Status.Published;
            bool b = LeaveMessageService.SaveChanges() > 0;

#if !DEBUG
            var pid     = msg.ParentId == 0 ? msg.Id : LeaveMessageService.GetParentMessageIdByChildId(id);
            var content = new Template(System.IO.File.ReadAllText(Path.Combine(HostEnvironment.WebRootPath, "template", "notify.html"))).Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Set("nickname", msg.NickName).Set("content", msg.Content);
            var emails  = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).Except(new List <string> {
                msg.Email, CurrentUser.Email
            }).ToHashSet();
            var link = Url.Action("Index", "Msg", new { cid = pid }, 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, b, b ? "审核通过!" : "审核失败!"));
        }
Exemplo n.º 14
0
        public ActionResult Pass(int id)
        {
            var msg = LeaveMessageService.GetById(id);

            msg.Status = Status.Pended;
            bool b = LeaveMessageService.UpdateEntitySaved(msg);

#if !DEBUG
            var    pid     = msg.ParentId == 0 ? msg.Id : LeaveMessageService.GetParentMessageIdByChildId(id);
            string content = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "template", "notify.html")).Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{nickname}}", msg.NickName).Replace("{{content}}", msg.Content);
            var    emails  = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).Distinct().Except(new List <string>()
            {
                msg.Email
            }).ToList();
            string link = Url.Action("Index", "Msg", new { cid = pid }, Request.Scheme);
            foreach (var s in emails)
            {
                BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Replace("{{link}}", link), string.Join(",", s)));
            }
#endif
            return(ResultData(null, b, b ? "审核通过!" : "审核失败!"));
        }
Exemplo n.º 15
0
 public LeaveMessage(LeaveMessageService leaveService)
 {
     _leaveService = leaveService;
 }
Exemplo n.º 16
0
 public ActionResult Index()
 {
     ViewBag.TotalCount = LeaveMessageService.Count(m => m.ParentId == 0 && m.Status == Status.Published);
     return(CurrentUser.IsAdmin ? View("Index_Admin") : View());
 }
Exemplo n.º 17
0
        public ActionResult GetPendingMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = LeaveMessageService.GetPages <DateTime, LeaveMessageDto>(page, size, m => m.Status == Status.Pending, l => l.PostDate, false);

            return(Ok(list));
        }
Exemplo n.º 18
0
        public ActionResult Delete(int id)
        {
            var b = LeaveMessageService.DeleteEntitiesSaved(LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(id).ToList());

            return(ResultData(null, b, b ? "删除成功!" : "删除失败!"));
        }
Exemplo n.º 19
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, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }
Exemplo n.º 20
0
 public ActionResult Index()
 {
     ViewBag.TotalCount = LeaveMessageService.GetQueryNoTracking(m => m.ParentId == 0 && m.Status == Status.Pended).Count();
     return(CurrentUser.IsAdmin ? View("Index_Admin") : View());
 }
Exemplo n.º 21
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task <ActionResult> GetMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, int?cid = null)
        {
            if (cid > 0)
            {
                var message = await LeaveMessageService.GetByIdAsync(cid.Value) ?? throw new NotFoundException("留言未找到");

                using var layer = LeaveMessageService.GetQueryNoTracking(e => e.GroupTag == message.GroupTag).ToPooledList();
                foreach (var m in layer)
                {
                    m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                    if (!CurrentUser.IsAdmin)
                    {
                        m.Email    = null;
                        m.IP       = null;
                        m.Location = null;
                    }
                }

                return(ResultData(new
                {
                    total = 1,
                    parentTotal = 1,
                    page,
                    size,
                    rows = layer.ToTree(e => e.Id, e => e.ParentId).Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            var parent = await LeaveMessageService.GetPagesAsync(page, size, m => m.ParentId == null && (m.Status == Status.Published || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Data.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            var total = parent.TotalCount;
            var tags  = parent.Data.Select(c => c.GroupTag).ToArray();

            using var messages = LeaveMessageService.GetQueryNoTracking(c => tags.Contains(c.GroupTag)).ToPooledList();
            messages.ForEach(m =>
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                if (!CurrentUser.IsAdmin)
                {
                    m.Email    = null;
                    m.IP       = null;
                    m.Location = null;
                }
            });
            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = messages.OrderByDescending(c => c.PostDate).ToTree(c => c.Id, c => c.ParentId).Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }
Exemplo n.º 22
0
        public ActionResult Put(LeaveMessageInputDto msg)
        {
            UserInfoOutputDto user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);

            msg.Content = msg.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (msg.Content.RemoveHtml().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  = Regex.Replace(msg.Content.HtmlSantinizerStandard().ConvertImgSrcToRelativePath(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>");
            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.RemoveHtml().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(HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer]) + "|博客新留言:", 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($"{HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer])}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Replace("{{link}}", link), s));
                        }
                    }
#endif
                    return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
                }
                BackgroundJob.Enqueue(() => CommonHelper.SendMail(HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer]) + "|博客新留言(待审核):", 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, "留言发表失败!"));
        }
Exemplo n.º 23
0
        public ActionResult Delete(int id)
        {
            var b = LeaveMessageService.DeleteById(id);

            return(ResultData(null, b, b ? "删除成功!" : "删除失败!"));
        }
Exemplo n.º 24
0
        public async Task <ActionResult> Submit([FromServices] IMailSender mailSender, LeaveMessageCommand dto)
        {
            var match = Regex.Match(dto.NickName + dto.Content.RemoveHtmlTag(), CommonHelper.BanRegex);

            if (match.Success)
            {
                LogManager.Info($"提交内容:{dto.NickName}/{dto.Content},敏感词:{match.Value}");
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            var error = await ValidateEmailCode(mailSender, dto.Email, dto.Code);

            if (!string.IsNullOrEmpty(error))
            {
                return(ResultData(null, false, error));
            }

            dto.Content = dto.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (MsgFeq.GetOrAdd("Comments:" + ClientIP, 1) > 2)
            {
                MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
                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.Email    = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Published;
                    msg.IsMaster = true;
                }
            }

            msg.Content = await dto.Content.HtmlSantinizerStandard().ClearImgAttributes();

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

            Response.Cookies.Append("NickName", msg.NickName, new CookieOptions()
            {
                Expires  = DateTimeOffset.Now.AddYears(1),
                SameSite = SameSiteMode.Lax
            });
            WriteEmailKeyCookie(dto.Email);
            MsgFeq.AddOrUpdate("Comments:" + ClientIP, 1, i => i + 1, 5);
            MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
            var email   = CommonHelper.SystemSettings["ReceiveEmail"];
            var content = new Template(await System.IO.File.ReadAllTextAsync(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)
                {
                    await MessageService.AddEntitySavedAsync(new InternalMessage()
                    {
                        Title   = $"来自【{msg.NickName}】的新留言",
                        Content = msg.Content,
                        Link    = Url.Action("Index", "Msg", new { cid = msg.Id })
                    });
                }
                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, ClientIP));
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    var    emails = (await LeaveMessageService.GetByIdAsync(msg.Id)).Root().Flatten().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, ClientIP));
                    }
                }
                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, ClientIP));
            return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }