inleft
2022-08-12 ff14c4edc7aa41db4b8fd8722486027b8ff84af0
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())) {
88             if (StrUtil.isEmpty(addDto.getAuthCode()) || !addDto.getAuthCode().equals(ConstantContextHolder.getAuthCode())) {
89                 throw new BlogException("不要成为我,你就是真的自己");
90             }
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
ba780f 134         //前一个留言接收邮件回复(如果是给评论者自己的追加可以跳过)
f639b5 135         if (MyConstant.Yes.equals(lastComment.getIsReceiveCallback())
I 136                 && StrUtil.isNotEmpty(lastComment.getVisitorEmail())
ba780f 137 //                && !lastComment.getVisitorNickName().equals(MyConstant.inleft)
I 138 //                && !lastComment.getVisitorEmail().equals(MyConstant.email)
f639b5 139                 && !lastComment.getVisitorNickName().equals(addDto.getVisitorNickName())
I 140                 && !lastComment.getVisitorEmail().equals(addDto.getVisitorEmail())) {
141
142             TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
143             Template template = engine.getTemplate("emailReplyModel.ftl");
144
eaf26c 145             String title = MyConstant.msgTitle;
f639b5 146             String address;
I 147             //跳转地址
148             if (addDto.getCommentType().equals(MyConstant.CommentType.type_1)) {
149                 //留言板
150                 address = String.format(MyConstant.url_1, insert.getId());
151             } else {
152                 //文章详情
153                 address = String.format(MyConstant.url_2, addDto.getArticleId(), insert.getId());
154                 title = checkArticle.getTitle();
155             }
156
157             Dict dict = Dict.create()
158                     .set("lastGuest", lastComment.getVisitorNickName())
159                     .set("commentContent", lastComment.getCommentContent())
160                     .set("address", address)
161                     .set("title", title)
162                     .set("sender", addDto.getVisitorNickName())
163                     .set("sendContent", addDto.getCommentContent());
164             String result = template.render(dict);
165
166             SendMailParam param = new SendMailParam();
167             param.setContent(result);
168             param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
169             param.setTo(lastComment.getVisitorEmail());
170             mailSender.sendMailHtml(param);
171         }
172
88f419 173
I 174         return new SuccessResponseData();
175     }
176
177
178     @GetMapping("/blogComment/queryBlogCommentList")
179     @BusinessLog(title = "外部blog系统_blog留言/评论_查询", opType = LogAnnotionOpTypeEnum.QUERY)
180     public ResponseData queryBlogCommentList(BlogCommentQueryDto queryDto) {
181         if (queryDto.getArticleId() != null && blogArticleService.getById(queryDto.getArticleId()) == null) {
182             throw new BlogException("查询不到相关日志");
183         }
184
185         Page queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
186
187
eaf26c 188         List<BlogArticleComment> commentList = commentService.lambdaQuery()
88f419 189                 .eq(queryDto.getArticleId() != null, BlogArticleComment::getArticleId, queryDto.getArticleId())
I 190                 .eq(BlogArticleComment::getCommentType, queryDto.getArticleId() == null ? MyConstant.CommentType.type_1 : MyConstant.CommentType.type_2)
e343e5 191                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
88f419 192                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
e343e5 193                 .eq(BlogArticleComment::getParentId, 0)
88f419 194                 .orderByDesc(BlogArticleComment::getCreateDate)
I 195                 .page(queryPage).getRecords();
196
ceebd4 197         int limitCount = 2;
88f419 198         List<BlogCommentVo> res = commentList.stream().map(e -> {
I 199                     BlogCommentVo vo = new BlogCommentVo();
200                     BeanUtil.copyProperties(e, vo);
e343e5 201                     vo.setIsHasNext(MyConstant.No);
eaf26c 202                     List<BlogCommentVo> replyList = commentService.getReplyListById(e.getId(), limitCount);
e343e5 203                     vo.setReplyList(replyList);
88f419 204
e343e5 205                     if (e.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
42b7d0 206                         vo.setCommentContent(MyConstant.privateComment);
e343e5 207                     }
I 208
209                     if (CollUtil.isNotEmpty(replyList)
eaf26c 210                             && commentService.lambdaQuery()
e343e5 211                             .eq(BlogArticleComment::getParentId, e.getId())
I 212                             .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
213                             .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
214                             .count() > limitCount) {
215                         vo.setIsHasNext(MyConstant.Yes);
216                     }
88f419 217                     return vo;
I 218                 }
219         ).collect(Collectors.toList());
220
221         queryPage.setRecords(res);
222         return new SuccessResponseData(queryPage);
223     }
e343e5 224
I 225     @GetMapping("/blogComment/queryBlogCommentSubList")
226     @BusinessLog(title = "外部blog系统_blog留言/评论子列表_查询", opType = LogAnnotionOpTypeEnum.QUERY)
227     public ResponseData queryBlogCommentSubList(BlogCommentQueryDto queryDto) {
eaf26c 228         if (queryDto.getArticleId() != null && commentService.getById(queryDto.getCommentId()) == null) {
e343e5 229             throw new BlogException("查询不到相关评论");
I 230         }
231
232
eaf26c 233         List<BlogCommentVo> replyList = commentService.getReplyListById(queryDto.getCommentId(), null);
ceebd4 234 //        for (BlogCommentVo vo : replyList) {
I 235 //            vo.setIsHasNext(MyConstant.No);
236 //            if (vo.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
237 //                vo.setCommentContent(MyConstant.privateComment);
238 //            }
239 //        }
e343e5 240
I 241         return new SuccessResponseData(replyList);
242     }
f639b5 243
eaf26c 244     @GetMapping("/blogComment/history")
I 245     @BusinessLog(title = "外部blog系统_blog留言/最近留言查询", opType = LogAnnotionOpTypeEnum.QUERY)
246     public ResponseData history(BlogCommentQueryDto queryDto) {
247         Page queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
248
249         //查询最近未读留言,无穷分页,(注意*号隐蔽,登录可见)
250         int unreadCommentListCount = commentService.lambdaQuery()
251                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
252                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
253                 .eq(BlogArticleComment::getIsRead, MyConstant.No)
254                 .count();
255
256         //取出所有未读消息+最多10条已读消息
257         long limitTotal = unreadCommentListCount + MyConstant.limitRead;
258
259         //最大页数不能超出限制
260         if (PageUtil.getEnd(queryDto.getPageNo() - 1, queryDto.getPageSize()) > limitTotal
261                 && PageUtil.getEnd(queryDto.getPageNo() - 2, queryDto.getPageSize()) > limitTotal) {
262             queryPage.setMaxLimit(limitTotal);
263             return new SuccessResponseData(queryPage);
264         }
265
266         //当前页超出起始页,未达尾页, 限制当前查询个数
267         long limitSize = queryDto.getPageSize();
268         if (PageUtil.getEnd(queryDto.getPageNo() - 1, queryDto.getPageSize()) > limitTotal) {
269             limitSize = limitTotal % queryDto.getPageSize();
270         }
271
272         List<BlogArticleComment> commentList = commentService.lambdaQuery()
273                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
274                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
275                 .orderByAsc(BlogArticleComment::getIsRead)
276                 .orderByDesc(BlogArticleComment::getCreateDate)
277                 .page(queryPage).getRecords();
278
279         //queryPage.setTotal(limitTotal - (limitTotal % queryDto.getPageSize()));
280         queryPage.setTotal(limitTotal);
281         List<BlogCommentVo> res = commentList.stream()
282                 .limit(limitSize)
283                 .map(e -> {
284                             BlogCommentVo vo = new BlogCommentVo();
285                             BeanUtil.copyProperties(e, vo);
286                             if (e.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
287                                 vo.setCommentContent(MyConstant.privateComment);
288                             }
289                             if (MyConstant.CommentType.type_2 == e.getCommentType()) {
290                                 vo.setArticleTitle(blogArticleService.lambdaQuery()
291                                         .eq(BlogArticle::getId, e.getArticleId())
292                                         .select(BlogArticle::getTitle).one().getTitle());
293                             } else {
294                                 vo.setArticleTitle(MyConstant.msgTitle);
295                             }
296
297                             return vo;
298                         }
299                 ).collect(Collectors.toList());
300
301         queryPage.setRecords(res);
302         return new SuccessResponseData(queryPage);
303     }
304
f639b5 305     //    @GetMapping("/blogComment/test")
I 306     public ResponseData test() {
307
308
309         TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
310         Template template = engine.getTemplate("emailReplyModel.ftl");
311         Dict dict = Dict.create()
312                 .set("lastGuest", "回复人")
313                 .set("commentContent", "评论内容")
314                 .set("address", "http://blog.inleft.com")
315                 .set("title", "回复标题")
316                 .set("sendTime", DateUtil.now())
317                 .set("sender", "发送人")
318                 .set("sendContent", "发送内容");
319         String result = template.render(dict);
320
321         SendMailParam param = new SendMailParam();
322         param.setContent(result);
323         param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
324         param.setTo("1479853828@qq.com");
325         mailSender.sendMailHtml(param);
326         return new SuccessResponseData();
327     }
88f419 328 }