inleft
2022-08-22 1e152bbcfb357073d8bcf0b51fe701e3fb81540d
commit | author | age
88f419 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.modular.blogarticlecomment.controller;
26
27 import cn.hutool.core.bean.BeanUtil;
e343e5 28 import cn.hutool.core.collection.CollUtil;
f639b5 29 import cn.hutool.core.date.DateUtil;
I 30 import cn.hutool.core.lang.Dict;
eaf26c 31 import cn.hutool.core.util.PageUtil;
f639b5 32 import cn.hutool.core.util.StrUtil;
I 33 import cn.hutool.extra.template.Template;
34 import cn.hutool.extra.template.TemplateConfig;
35 import cn.hutool.extra.template.TemplateEngine;
36 import cn.hutool.extra.template.TemplateUtil;
88f419 37 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
I 38 import org.springframework.validation.annotation.Validated;
39 import org.springframework.web.bind.annotation.*;
40 import vip.xiaonuo.core.annotion.BusinessLog;
41 import vip.xiaonuo.core.consts.MyConstant;
ba780f 42 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
f639b5 43 import vip.xiaonuo.core.email.MailSender;
I 44 import vip.xiaonuo.core.email.modular.model.SendMailParam;
88f419 45 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
I 46 import vip.xiaonuo.core.exception.BlogException;
47 import vip.xiaonuo.core.pojo.response.ResponseData;
48 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
42b7d0 49 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
88f419 50 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
I 51 import vip.xiaonuo.modular.blogarticlecomment.entity.BlogArticleComment;
52 import vip.xiaonuo.modular.blogarticlecomment.entity.BlogCommentVo;
53 import vip.xiaonuo.modular.blogarticlecomment.param.BlogArticleCommentAddDto;
54 import vip.xiaonuo.modular.blogarticlecomment.param.BlogCommentQueryDto;
55 import vip.xiaonuo.modular.blogarticlecomment.service.BlogArticleCommentService;
56
57 import javax.annotation.Resource;
58 import java.util.List;
59 import java.util.stream.Collectors;
60
61 /**
62  * blog 留言/评论 控制器 (提供给外部blog系统查询)
63  *
64  * @author inleft
65  * @date 2022-02-09 18:20:22
66  */
67 @RestController
68 @RequestMapping("/outside")
69 public class BlogArticleCommentOutsideController {
70
71     @Resource
72     private BlogArticleService blogArticleService;
73
74     @Resource
eaf26c 75     private BlogArticleCommentService commentService;
88f419 76
I 77
f639b5 78     @Resource
I 79     private MailSender mailSender;
80
88f419 81     @PostMapping("/blogComment/add")
I 82     @BusinessLog(title = "外部blog系统_blog留言/评论_增加", opType = LogAnnotionOpTypeEnum.ADD)
83     public ResponseData add(@RequestBody @Validated(BlogArticleCommentAddDto.add.class) BlogArticleCommentAddDto addDto) {
f639b5 84         BlogArticle checkArticle = null;
I 85
ba780f 86         //自我认证
I 87         if (MyConstant.myIds.contains(addDto.getVisitorNickName())) {
fc74b4 88             if (StrUtil.isEmpty(addDto.getAuthCode()) || !addDto.getAuthCode().equals(ConstantContextHolder.getPushCode())) {
c49fa0 89                 throw new BlogException("此名称在这里使用需要正确的授权码..");
ba780f 90             }
I 91         }
92
f639b5 93         if (MyConstant.Yes.equals(addDto.getIsReceiveMail()) && StrUtil.isEmpty(addDto.getVisitorEmail())) {
I 94             throw new BlogException("如果是想接收通知的话,你可能需要填上一个邮箱..");
95         }
96
88f419 97         if (addDto.getCommentType().equals(MyConstant.CommentType.type_2)) {
I 98             if (addDto.getArticleId() == null) {
99                 throw new BlogException("评论类型为日志评论,日志id不能为空");
100             }
f639b5 101             checkArticle = blogArticleService.getById(addDto.getArticleId());
42b7d0 102             if (checkArticle == null) {
88f419 103                 throw new BlogException("查询不到相关日志");
42b7d0 104             }
I 105
106             if (checkArticle.getIsAllowedComment().equals(MyConstant.No)) {
107                 throw new BlogException("该日志评论已经关闭..");
88f419 108             }
I 109         }
110         BlogArticleComment insert = new BlogArticleComment();
111         BeanUtil.copyProperties(addDto, insert);
42b7d0 112         insert.setIsCheck(MyConstant.Yes);
88f419 113         insert.setIsEnable(MyConstant.Yes);
I 114         insert.setIsReceiveCallback(addDto.getIsReceiveMail());
115
eaf26c 116         //自己留言已读
I 117         if (MyConstant.myIds.contains(addDto.getVisitorNickName())) {
118             insert.setIsRead(MyConstant.Yes);
119         }
120
121         commentService.save(insert);
f639b5 122
I 123         if (addDto.getReplyId() == null || addDto.getReplyId() == 0L) {
124             //这里为根节点,预留通知我
125             return new SuccessResponseData();
126         }
127
eaf26c 128         BlogArticleComment lastComment = commentService.getById(addDto.getReplyId());
f639b5 129
I 130         if (lastComment == null) {
131             return new SuccessResponseData();
132         }
133
7fe1bd 134         //发送者是我本体,标记回复该条回复消息已读
I 135         if (MyConstant.myIds.contains(addDto.getVisitorNickName())) {
136             commentService.lambdaUpdate()
137                     .eq(BlogArticleComment::getId, lastComment.getId())
138                     .set(BlogArticleComment::getIsRead, MyConstant.Yes)
139                     .update();
140         }
141
ba780f 142         //前一个留言接收邮件回复(如果是给评论者自己的追加可以跳过)
f639b5 143         if (MyConstant.Yes.equals(lastComment.getIsReceiveCallback())
I 144                 && StrUtil.isNotEmpty(lastComment.getVisitorEmail())
ba780f 145 //                && !lastComment.getVisitorNickName().equals(MyConstant.inleft)
I 146 //                && !lastComment.getVisitorEmail().equals(MyConstant.email)
f639b5 147                 && !lastComment.getVisitorNickName().equals(addDto.getVisitorNickName())
I 148                 && !lastComment.getVisitorEmail().equals(addDto.getVisitorEmail())) {
149
eaf26c 150             String title = MyConstant.msgTitle;
f639b5 151             String address;
I 152             //跳转地址
153             if (addDto.getCommentType().equals(MyConstant.CommentType.type_1)) {
154                 //留言板
155                 address = String.format(MyConstant.url_1, insert.getId());
156             } else {
157                 //文章详情
158                 address = String.format(MyConstant.url_2, addDto.getArticleId(), insert.getId());
159                 title = checkArticle.getTitle();
160             }
161
7fe1bd 162             TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(MyConstant.ftlModel.path, TemplateConfig.ResourceMode.CLASSPATH));
I 163             Template template = engine.getTemplate(MyConstant.ftlModel.reply);
164
f639b5 165             Dict dict = Dict.create()
I 166                     .set("lastGuest", lastComment.getVisitorNickName())
167                     .set("commentContent", lastComment.getCommentContent())
168                     .set("address", address)
169                     .set("title", title)
170                     .set("sender", addDto.getVisitorNickName())
171                     .set("sendContent", addDto.getCommentContent());
172             String result = template.render(dict);
173
174             SendMailParam param = new SendMailParam();
175             param.setContent(result);
176             param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
177             param.setTo(lastComment.getVisitorEmail());
178             mailSender.sendMailHtml(param);
179         }
180
88f419 181
I 182         return new SuccessResponseData();
183     }
184
185
186     @GetMapping("/blogComment/queryBlogCommentList")
187     @BusinessLog(title = "外部blog系统_blog留言/评论_查询", opType = LogAnnotionOpTypeEnum.QUERY)
188     public ResponseData queryBlogCommentList(BlogCommentQueryDto queryDto) {
189         if (queryDto.getArticleId() != null && blogArticleService.getById(queryDto.getArticleId()) == null) {
190             throw new BlogException("查询不到相关日志");
191         }
192
193         Page queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
194
195
eaf26c 196         List<BlogArticleComment> commentList = commentService.lambdaQuery()
88f419 197                 .eq(queryDto.getArticleId() != null, BlogArticleComment::getArticleId, queryDto.getArticleId())
I 198                 .eq(BlogArticleComment::getCommentType, queryDto.getArticleId() == null ? MyConstant.CommentType.type_1 : MyConstant.CommentType.type_2)
e343e5 199                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
88f419 200                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
e343e5 201                 .eq(BlogArticleComment::getParentId, 0)
88f419 202                 .orderByDesc(BlogArticleComment::getCreateDate)
I 203                 .page(queryPage).getRecords();
204
ceebd4 205         int limitCount = 2;
88f419 206         List<BlogCommentVo> res = commentList.stream().map(e -> {
I 207                     BlogCommentVo vo = new BlogCommentVo();
208                     BeanUtil.copyProperties(e, vo);
e343e5 209                     vo.setIsHasNext(MyConstant.No);
eaf26c 210                     List<BlogCommentVo> replyList = commentService.getReplyListById(e.getId(), limitCount);
e343e5 211                     vo.setReplyList(replyList);
88f419 212
e343e5 213                     if (e.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
42b7d0 214                         vo.setCommentContent(MyConstant.privateComment);
e343e5 215                     }
I 216
217                     if (CollUtil.isNotEmpty(replyList)
eaf26c 218                             && commentService.lambdaQuery()
e343e5 219                             .eq(BlogArticleComment::getParentId, e.getId())
I 220                             .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
221                             .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
222                             .count() > limitCount) {
223                         vo.setIsHasNext(MyConstant.Yes);
224                     }
88f419 225                     return vo;
I 226                 }
227         ).collect(Collectors.toList());
228
229         queryPage.setRecords(res);
230         return new SuccessResponseData(queryPage);
231     }
e343e5 232
I 233     @GetMapping("/blogComment/queryBlogCommentSubList")
234     @BusinessLog(title = "外部blog系统_blog留言/评论子列表_查询", opType = LogAnnotionOpTypeEnum.QUERY)
235     public ResponseData queryBlogCommentSubList(BlogCommentQueryDto queryDto) {
eaf26c 236         if (queryDto.getArticleId() != null && commentService.getById(queryDto.getCommentId()) == null) {
e343e5 237             throw new BlogException("查询不到相关评论");
I 238         }
239
240
eaf26c 241         List<BlogCommentVo> replyList = commentService.getReplyListById(queryDto.getCommentId(), null);
ceebd4 242 //        for (BlogCommentVo vo : replyList) {
I 243 //            vo.setIsHasNext(MyConstant.No);
244 //            if (vo.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
245 //                vo.setCommentContent(MyConstant.privateComment);
246 //            }
247 //        }
e343e5 248
I 249         return new SuccessResponseData(replyList);
250     }
f639b5 251
eaf26c 252     @GetMapping("/blogComment/history")
I 253     @BusinessLog(title = "外部blog系统_blog留言/最近留言查询", opType = LogAnnotionOpTypeEnum.QUERY)
254     public ResponseData history(BlogCommentQueryDto queryDto) {
255         Page queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
256
257         //查询最近未读留言,无穷分页,(注意*号隐蔽,登录可见)
258         int unreadCommentListCount = commentService.lambdaQuery()
259                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
260                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
261                 .eq(BlogArticleComment::getIsRead, MyConstant.No)
262                 .count();
263
264         //取出所有未读消息+最多10条已读消息
265         long limitTotal = unreadCommentListCount + MyConstant.limitRead;
266
267         //最大页数不能超出限制
268         if (PageUtil.getEnd(queryDto.getPageNo() - 1, queryDto.getPageSize()) > limitTotal
269                 && PageUtil.getEnd(queryDto.getPageNo() - 2, queryDto.getPageSize()) > limitTotal) {
270             queryPage.setMaxLimit(limitTotal);
271             return new SuccessResponseData(queryPage);
272         }
273
274         //当前页超出起始页,未达尾页, 限制当前查询个数
275         long limitSize = queryDto.getPageSize();
276         if (PageUtil.getEnd(queryDto.getPageNo() - 1, queryDto.getPageSize()) > limitTotal) {
277             limitSize = limitTotal % queryDto.getPageSize();
278         }
279
280         List<BlogArticleComment> commentList = commentService.lambdaQuery()
281                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
282                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
283                 .orderByAsc(BlogArticleComment::getIsRead)
284                 .orderByDesc(BlogArticleComment::getCreateDate)
285                 .page(queryPage).getRecords();
286
287         //queryPage.setTotal(limitTotal - (limitTotal % queryDto.getPageSize()));
288         queryPage.setTotal(limitTotal);
289         List<BlogCommentVo> res = commentList.stream()
290                 .limit(limitSize)
291                 .map(e -> {
292                             BlogCommentVo vo = new BlogCommentVo();
293                             BeanUtil.copyProperties(e, vo);
294                             if (e.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
295                                 vo.setCommentContent(MyConstant.privateComment);
296                             }
297                             if (MyConstant.CommentType.type_2 == e.getCommentType()) {
298                                 vo.setArticleTitle(blogArticleService.lambdaQuery()
299                                         .eq(BlogArticle::getId, e.getArticleId())
300                                         .select(BlogArticle::getTitle).one().getTitle());
301                             } else {
302                                 vo.setArticleTitle(MyConstant.msgTitle);
303                             }
304
305                             return vo;
306                         }
307                 ).collect(Collectors.toList());
308
309         queryPage.setRecords(res);
310         return new SuccessResponseData(queryPage);
311     }
312
f639b5 313     //    @GetMapping("/blogComment/test")
I 314     public ResponseData test() {
315
316
317         TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
318         Template template = engine.getTemplate("emailReplyModel.ftl");
319         Dict dict = Dict.create()
320                 .set("lastGuest", "回复人")
321                 .set("commentContent", "评论内容")
322                 .set("address", "http://blog.inleft.com")
323                 .set("title", "回复标题")
324                 .set("sendTime", DateUtil.now())
325                 .set("sender", "发送人")
326                 .set("sendContent", "发送内容");
327         String result = template.render(dict);
328
329         SendMailParam param = new SendMailParam();
330         param.setContent(result);
331         param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
332         param.setTo("1479853828@qq.com");
333         mailSender.sendMailHtml(param);
334         return new SuccessResponseData();
335     }
88f419 336 }