Browse Source

文章订阅推送优化

懒得勤快 5 years ago
parent
commit
6c53892550

+ 2 - 39
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -715,27 +715,8 @@ namespace Masuit.MyBlogs.Core.Controllers
 #if !DEBUG
             if (notify && "false" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
             {
-                var cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
                 string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;
-                cast.ForEach(c =>
-                {
-                    var ts = DateTime.Now.GetTotalMilliseconds();
-                    string content = System.IO.File.ReadAllText(Path.Combine(HostEnvironment.WebRootPath, "template", "broadcast.html"))
-                        .Replace("{{link}}", link + "?email=" + c.Email)
-                        .Replace("{{time}}", p.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
-                        .Replace("{{title}}", post.Title)
-                        .Replace("{{author}}", post.Author)
-                        .Replace("{{content}}", post.Content.GetSummary())
-                        .Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new
-                        {
-                            c.Email,
-                            act = "cancel",
-                            validate = c.ValidateCode,
-                            timespan = ts,
-                            hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
-                        }, Request.Scheme));
-                    BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
-                });
+                HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.BroadcastPostPublished), "", p.Id, link);
             }
 #endif
             return ResultData(p.Mapper<PostDto>(), message: "文章修改成功!");
@@ -823,26 +804,8 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
                 return ResultData(null, true, "文章发表成功!");
             }
-            var cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
             string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;
-            cast.ForEach(c =>
-            {
-                var ts = DateTime.Now.GetTotalMilliseconds();
-                string content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/broadcast.html")
-                    .Replace("{{link}}", link + "?email=" + c.Email)
-                    .Replace("{{time}}", p.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
-                    .Replace("{{title}}", post.Title).Replace("{{author}}", post.Author)
-                    .Replace("{{content}}", post.Content.GetSummary(250, 50))
-                    .Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new
-                    {
-                        c.Email,
-                        act = "cancel",
-                        validate = c.ValidateCode,
-                        timespan = ts,
-                        hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
-                    }, Request.Scheme));
-                BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
-            });
+            HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.BroadcastPostPublished), "", p.Id, link);
             return ResultData(null, true, "文章发表成功!");
         }
 

+ 3 - 3
src/Masuit.MyBlogs.Core/Controllers/SubscribeController.cs

@@ -287,7 +287,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                     act = "verify",
                     validate = guid,
                     timespan = ts,
-                    hash = (email + "verify" + guid + ts).AESEncrypt(AppConfig.BaiduAK)
+                    hash = (email + "verify" + guid + ts).MDString(AppConfig.BaiduAK)
                 }, "http");
                 BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客订阅:" + Request.Host, System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/subscribe.html").Replace("{{link}}", link), email));
                 BroadcastService.SaveChanges();
@@ -330,7 +330,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 act = "cancel",
                 validate = c.ValidateCode,
                 timespan = ts,
-                hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
+                hash = (c.Email + "cancel" + c.ValidateCode + ts).MDString(AppConfig.BaiduAK)
             }, Request.Scheme);
             BackgroundJob.Enqueue(() => CommonHelper.SendMail("取消本站订阅", $"请<a href=\"{url}\">点击这里</a>取消订阅本站更新。", email));
             return Content("取消订阅的链接已经发送到了您的邮箱,请到您的邮箱内进行取消订阅");
@@ -352,7 +352,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
                 return Content("链接已失效");
             }
-            var hash2 = (email + act + validate + timespan).AESEncrypt(AppConfig.BaiduAK);
+            var hash2 = (email + act + validate + timespan).MDString(AppConfig.BaiduAK);
             if (!hash2.Equals(hash))
             {
                 return Content("操作失败,链接已被非法篡改");

+ 34 - 5
src/Masuit.MyBlogs.Core/Extensions/Hangfire/HangfireBackJob.cs

@@ -1,6 +1,8 @@
-using IP2Region;
+using Hangfire;
+using IP2Region;
 using Masuit.LuceneEFCore.SearchEngine.Interfaces;
 using Masuit.MyBlogs.Core.Common;
+using Masuit.MyBlogs.Core.Configs;
 using Masuit.MyBlogs.Core.Infrastructure;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
 using Masuit.MyBlogs.Core.Models.DTO;
@@ -8,6 +10,8 @@ using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
 using Masuit.Tools;
 using Masuit.Tools.Core.Net;
+using Masuit.Tools.DateTimeExt;
+using Masuit.Tools.Security;
 using Microsoft.AspNetCore.Hosting;
 using System;
 using System.Collections.Generic;
@@ -30,8 +34,9 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
         private readonly ISearchDetailsService _searchDetailsService;
         private readonly ILinksService _linksService;
         private readonly IHttpClientFactory _httpClientFactory;
-        private readonly IWebHostEnvironment _HostEnvironment;
+        private readonly IWebHostEnvironment _hostEnvironment;
         private readonly ISearchEngine<DataContext> _searchEngine;
+        private readonly IBroadcastService _broadcastService;
 
         /// <summary>
         /// hangfire后台任务
@@ -44,7 +49,7 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
         /// <param name="httpClientFactory"></param>
         /// <param name="HostEnvironment"></param>
         /// <param name="searchEngine"></param>
-        public HangfireBackJob(IUserInfoService userInfoService, IPostService postService, ISystemSettingService settingService, ISearchDetailsService searchDetailsService, ILinksService linksService, IHttpClientFactory httpClientFactory, IWebHostEnvironment HostEnvironment, ISearchEngine<DataContext> searchEngine)
+        public HangfireBackJob(IUserInfoService userInfoService, IPostService postService, ISystemSettingService settingService, ISearchDetailsService searchDetailsService, ILinksService linksService, IHttpClientFactory httpClientFactory, IWebHostEnvironment HostEnvironment, ISearchEngine<DataContext> searchEngine, IBroadcastService broadcastService)
         {
             _userInfoService = userInfoService;
             _postService = postService;
@@ -52,8 +57,9 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
             _searchDetailsService = searchDetailsService;
             _linksService = linksService;
             _httpClientFactory = httpClientFactory;
-            _HostEnvironment = HostEnvironment;
+            _hostEnvironment = HostEnvironment;
             _searchEngine = searchEngine;
+            _broadcastService = broadcastService;
         }
 
         /// <summary>
@@ -83,7 +89,7 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
             var u = _userInfoService.GetByUsername(userInfo.Username);
             u.LoginRecord.Add(record);
             _userInfoService.SaveChanges();
-            var content = File.ReadAllText(Path.Combine(_HostEnvironment.WebRootPath, "template", "login.html"))
+            var content = File.ReadAllText(Path.Combine(_hostEnvironment.WebRootPath, "template", "login.html"))
                 .Replace("{{name}}", u.Username)
                 .Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                 .Replace("{{ip}}", record.IP)
@@ -239,5 +245,28 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
             RedisHelper.Set("SearchRank:Week", _searchDetailsService.GetRanks(DateTime.Today.AddDays(-7)));
             RedisHelper.Set("SearchRank:Today", _searchDetailsService.GetRanks(DateTime.Today));
         }
+
+        /// <summary>
+        /// 文章订阅广播
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="link"></param>
+        public void BroadcastPostPublished(int id, string link)
+        {
+            var post = _postService.GetById(id);
+            _broadcastService.GetQuery(c => c.Status == Status.Subscribed).AsParallel().ForEach(c =>
+            {
+                var ts = DateTime.Now.GetTotalMilliseconds();
+                var uri = new Uri(link);
+                string content = File.ReadAllText(Path.Combine(_hostEnvironment.WebRootPath, "template", "broadcast.html"))
+                    .Replace("{{link}}", link + "?email=" + c.Email)
+                    .Replace("{{time}}", post.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
+                    .Replace("{{title}}", post.Title)
+                    .Replace("{{author}}", post.Author)
+                    .Replace("{{content}}", post.Content.GetSummary())
+                    .Replace("{{cancel}}", $"{uri.Scheme}://{uri.Authority}/Subscribe/Subscribe?email={c.Email}&act=cancel&validate={c.ValidateCode}&timespan={ts}&hash={(c.Email + "cancel" + c.ValidateCode + ts).MDString(AppConfig.BaiduAK)}");
+                BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), post.ModifyDate - DateTime.Now);
+            });
+        }
     }
 }

+ 7 - 0
src/Masuit.MyBlogs.Core/Extensions/Hangfire/IHangfireBackJob.cs

@@ -54,5 +54,12 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
         /// 搜索统计
         /// </summary>
         void StatisticsSearchKeywords();
+
+        /// <summary>
+        /// 文章订阅广播
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="link"></param>
+        void BroadcastPostPublished(int id, string link);
     }
 }