inleft
2022-07-14 f639b5bf8a20b72b782558ca01f086a4ed54c520
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;
31 import cn.hutool.core.util.StrUtil;
32 import cn.hutool.extra.template.Template;
33 import cn.hutool.extra.template.TemplateConfig;
34 import cn.hutool.extra.template.TemplateEngine;
35 import cn.hutool.extra.template.TemplateUtil;
88f419 36 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
I 37 import org.springframework.validation.annotation.Validated;
38 import org.springframework.web.bind.annotation.*;
39 import vip.xiaonuo.core.annotion.BusinessLog;
40 import vip.xiaonuo.core.consts.MyConstant;
f639b5 41 import vip.xiaonuo.core.email.MailSender;
I 42 import vip.xiaonuo.core.email.modular.model.SendMailParam;
88f419 43 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
I 44 import vip.xiaonuo.core.exception.BlogException;
45 import vip.xiaonuo.core.pojo.response.ResponseData;
46 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
42b7d0 47 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
88f419 48 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
I 49 import vip.xiaonuo.modular.blogarticlecomment.entity.BlogArticleComment;
50 import vip.xiaonuo.modular.blogarticlecomment.entity.BlogCommentVo;
51 import vip.xiaonuo.modular.blogarticlecomment.param.BlogArticleCommentAddDto;
52 import vip.xiaonuo.modular.blogarticlecomment.param.BlogCommentQueryDto;
53 import vip.xiaonuo.modular.blogarticlecomment.service.BlogArticleCommentService;
54
55 import javax.annotation.Resource;
56 import java.util.List;
57 import java.util.stream.Collectors;
58
59 /**
60  * blog 留言/评论 控制器 (提供给外部blog系统查询)
61  *
62  * @author inleft
63  * @date 2022-02-09 18:20:22
64  */
65 @RestController
66 @RequestMapping("/outside")
67 public class BlogArticleCommentOutsideController {
68
69     @Resource
70     private BlogArticleService blogArticleService;
71
72     @Resource
73     private BlogArticleCommentService blogArticleCommentService;
74
75
f639b5 76     @Resource
I 77     private MailSender mailSender;
78
88f419 79     @PostMapping("/blogComment/add")
I 80     @BusinessLog(title = "外部blog系统_blog留言/评论_增加", opType = LogAnnotionOpTypeEnum.ADD)
81     public ResponseData add(@RequestBody @Validated(BlogArticleCommentAddDto.add.class) BlogArticleCommentAddDto addDto) {
f639b5 82         BlogArticle checkArticle = null;
I 83
84         if (MyConstant.Yes.equals(addDto.getIsReceiveMail()) && StrUtil.isEmpty(addDto.getVisitorEmail())) {
85             throw new BlogException("如果是想接收通知的话,你可能需要填上一个邮箱..");
86         }
87
88f419 88         if (addDto.getCommentType().equals(MyConstant.CommentType.type_2)) {
I 89             if (addDto.getArticleId() == null) {
90                 throw new BlogException("评论类型为日志评论,日志id不能为空");
91             }
f639b5 92             checkArticle = blogArticleService.getById(addDto.getArticleId());
42b7d0 93             if (checkArticle == null) {
88f419 94                 throw new BlogException("查询不到相关日志");
42b7d0 95             }
I 96
97             if (checkArticle.getIsAllowedComment().equals(MyConstant.No)) {
98                 throw new BlogException("该日志评论已经关闭..");
88f419 99             }
I 100         }
101         BlogArticleComment insert = new BlogArticleComment();
102         BeanUtil.copyProperties(addDto, insert);
42b7d0 103         insert.setIsCheck(MyConstant.Yes);
88f419 104         insert.setIsEnable(MyConstant.Yes);
I 105         insert.setIsReceiveCallback(addDto.getIsReceiveMail());
106
107         blogArticleCommentService.save(insert);
f639b5 108
I 109         if (addDto.getReplyId() == null || addDto.getReplyId() == 0L) {
110             //这里为根节点,预留通知我
111             return new SuccessResponseData();
112         }
113
114         BlogArticleComment lastComment = blogArticleCommentService.getById(addDto.getReplyId());
115
116         if (lastComment == null) {
117             return new SuccessResponseData();
118         }
119
120         //前一个留言接收邮件回复(发送给我的本体可以跳过.如果是给评论者自己的追加可以跳过)
121         if (MyConstant.Yes.equals(lastComment.getIsReceiveCallback())
122                 && StrUtil.isNotEmpty(lastComment.getVisitorEmail())
123                 && !lastComment.getVisitorNickName().equals(MyConstant.inleft)
124                 && !lastComment.getVisitorEmail().equals(MyConstant.email)
125                 && !lastComment.getVisitorNickName().equals(addDto.getVisitorNickName())
126                 && !lastComment.getVisitorEmail().equals(addDto.getVisitorEmail())) {
127
128             TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
129             Template template = engine.getTemplate("emailReplyModel.ftl");
130
131             String title = "碎碎念";
132             String address;
133             //跳转地址
134             if (addDto.getCommentType().equals(MyConstant.CommentType.type_1)) {
135                 //留言板
136                 address = String.format(MyConstant.url_1, insert.getId());
137             } else {
138                 //文章详情
139                 address = String.format(MyConstant.url_2, addDto.getArticleId(), insert.getId());
140                 title = checkArticle.getTitle();
141             }
142
143             Dict dict = Dict.create()
144                     .set("lastGuest", lastComment.getVisitorNickName())
145                     .set("commentContent", lastComment.getCommentContent())
146                     .set("address", address)
147                     .set("title", title)
148                     .set("sender", addDto.getVisitorNickName())
149                     .set("sendContent", addDto.getCommentContent());
150             String result = template.render(dict);
151
152             SendMailParam param = new SendMailParam();
153             param.setContent(result);
154             param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
155             param.setTo(lastComment.getVisitorEmail());
156             mailSender.sendMailHtml(param);
157         }
158
88f419 159
I 160         return new SuccessResponseData();
161     }
162
163
164     @GetMapping("/blogComment/queryBlogCommentList")
165     @BusinessLog(title = "外部blog系统_blog留言/评论_查询", opType = LogAnnotionOpTypeEnum.QUERY)
166     public ResponseData queryBlogCommentList(BlogCommentQueryDto queryDto) {
167         if (queryDto.getArticleId() != null && blogArticleService.getById(queryDto.getArticleId()) == null) {
168             throw new BlogException("查询不到相关日志");
169         }
170
171         Page queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
172
173
174         List<BlogArticleComment> commentList = blogArticleCommentService.lambdaQuery()
175                 .eq(queryDto.getArticleId() != null, BlogArticleComment::getArticleId, queryDto.getArticleId())
176                 .eq(BlogArticleComment::getCommentType, queryDto.getArticleId() == null ? MyConstant.CommentType.type_1 : MyConstant.CommentType.type_2)
e343e5 177                 .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
88f419 178                 .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
e343e5 179                 .eq(BlogArticleComment::getParentId, 0)
88f419 180                 .orderByDesc(BlogArticleComment::getCreateDate)
I 181                 .page(queryPage).getRecords();
182
ceebd4 183         int limitCount = 2;
88f419 184         List<BlogCommentVo> res = commentList.stream().map(e -> {
I 185                     BlogCommentVo vo = new BlogCommentVo();
186                     BeanUtil.copyProperties(e, vo);
e343e5 187                     vo.setIsHasNext(MyConstant.No);
I 188                     List<BlogCommentVo> replyList = blogArticleCommentService.getReplyListById(e.getId(), limitCount);
189                     vo.setReplyList(replyList);
88f419 190
e343e5 191                     if (e.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
42b7d0 192                         vo.setCommentContent(MyConstant.privateComment);
e343e5 193                     }
I 194
195                     if (CollUtil.isNotEmpty(replyList)
196                             && blogArticleCommentService.lambdaQuery()
197                             .eq(BlogArticleComment::getParentId, e.getId())
198                             .eq(BlogArticleComment::getIsCheck, MyConstant.Yes)
199                             .eq(BlogArticleComment::getIsEnable, MyConstant.Yes)
200                             .count() > limitCount) {
201                         vo.setIsHasNext(MyConstant.Yes);
202                     }
88f419 203                     return vo;
I 204                 }
205         ).collect(Collectors.toList());
206
207         queryPage.setRecords(res);
208         return new SuccessResponseData(queryPage);
209     }
e343e5 210
I 211     @GetMapping("/blogComment/queryBlogCommentSubList")
212     @BusinessLog(title = "外部blog系统_blog留言/评论子列表_查询", opType = LogAnnotionOpTypeEnum.QUERY)
213     public ResponseData queryBlogCommentSubList(BlogCommentQueryDto queryDto) {
214         if (queryDto.getArticleId() != null && blogArticleCommentService.getById(queryDto.getCommentId()) == null) {
215             throw new BlogException("查询不到相关评论");
216         }
217
218
219         List<BlogCommentVo> replyList = blogArticleCommentService.getReplyListById(queryDto.getCommentId(), null);
ceebd4 220 //        for (BlogCommentVo vo : replyList) {
I 221 //            vo.setIsHasNext(MyConstant.No);
222 //            if (vo.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
223 //                vo.setCommentContent(MyConstant.privateComment);
224 //            }
225 //        }
e343e5 226
I 227         return new SuccessResponseData(replyList);
228     }
f639b5 229
I 230     //    @GetMapping("/blogComment/test")
231     public ResponseData test() {
232
233
234         TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
235         Template template = engine.getTemplate("emailReplyModel.ftl");
236         Dict dict = Dict.create()
237                 .set("lastGuest", "回复人")
238                 .set("commentContent", "评论内容")
239                 .set("address", "http://blog.inleft.com")
240                 .set("title", "回复标题")
241                 .set("sendTime", DateUtil.now())
242                 .set("sender", "发送人")
243                 .set("sendContent", "发送内容");
244         String result = template.render(dict);
245
246         SendMailParam param = new SendMailParam();
247         param.setContent(result);
248         param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
249         param.setTo("1479853828@qq.com");
250         mailSender.sendMailHtml(param);
251         return new SuccessResponseData();
252     }
88f419 253 }