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