Search in sources :

Example 1 with BussinessLog

use of com.moxi.mogublog.web.annotion.log.BussinessLog in project mogu_blog_v2 by moxi624.

the class CommentRestApi method add.

@BussinessLog(value = "发表评论", behavior = EBehavior.PUBLISH_COMMENT)
@ApiOperation(value = "增加评论", notes = "增加评论")
@PostMapping("/add")
public String add(@Validated({ Insert.class }) @RequestBody CommentVO commentVO, BindingResult result) {
    ThrowableUtils.checkParamArgument(result);
    HttpServletRequest request = RequestHolder.getRequest();
    if (request.getAttribute(SysConf.USER_UID) == null) {
        return ResultUtil.result(SysConf.ERROR, MessageConf.INVALID_TOKEN);
    }
    QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SysConf.STATUS, EStatus.ENABLE);
    WebConfig webConfig = webConfigService.getOne(queryWrapper);
    // 判断是否开启全局评论功能
    if (SysConf.CAN_NOT_COMMENT.equals(webConfig.getOpenComment())) {
        return ResultUtil.result(SysConf.ERROR, MessageConf.NO_COMMENTS_OPEN);
    }
    // 判断该博客是否开启评论功能
    if (StringUtils.isNotEmpty(commentVO.getBlogUid())) {
        Blog blog = blogService.getById(commentVO.getBlogUid());
        if (SysConf.CAN_NOT_COMMENT.equals(blog.getOpenComment())) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.BLOG_NO_OPEN_COMMENTS);
        }
    }
    String userUid = request.getAttribute(SysConf.USER_UID).toString();
    User user = userService.getById(userUid);
    // 判断字数是否超过限制
    if (commentVO.getContent().length() > SysConf.ONE_ZERO_TWO_FOUR) {
        return ResultUtil.result(SysConf.ERROR, MessageConf.COMMENT_CAN_NOT_MORE_THAN_1024);
    }
    // 判断该用户是否被禁言
    if (user.getCommentStatus() == SysConf.ZERO) {
        return ResultUtil.result(SysConf.ERROR, MessageConf.YOU_DONT_HAVE_PERMISSION_TO_SPEAK);
    }
    // 判断是否发送过多无意义评论
    String jsonResult = redisUtil.get(RedisConf.USER_PUBLISH_SPAM_COMMENT_COUNT + BaseSysConf.REDIS_SEGMENTATION + userUid);
    if (!StringUtils.isEmpty(jsonResult)) {
        Integer count = Integer.valueOf(jsonResult);
        if (count >= Constants.NUM_FIVE) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.PLEASE_TRY_AGAIN_IN_AN_HOUR);
        }
    }
    // 判断是否垃圾评论
    String content = commentVO.getContent();
    if (StringUtils.isCommentSpam(content)) {
        if (StringUtils.isEmpty(jsonResult)) {
            Integer count = 0;
            redisUtil.setEx(RedisConf.USER_PUBLISH_SPAM_COMMENT_COUNT + BaseSysConf.REDIS_SEGMENTATION + userUid, count.toString(), 1, TimeUnit.HOURS);
        } else {
            redisUtil.incrBy(RedisConf.USER_PUBLISH_SPAM_COMMENT_COUNT + BaseSysConf.REDIS_SEGMENTATION + userUid, 1);
        }
        return ResultUtil.result(SysConf.ERROR, MessageConf.COMMENT_IS_SPAM);
    }
    // 判断被评论的用户,是否开启了评论邮件提醒
    if (StringUtils.isNotEmpty(commentVO.getToUserUid())) {
        User toUser = userService.getById(commentVO.getToUserUid());
        if (toUser.getStartEmailNotification() == SysConf.ONE) {
            Comment toComment = commentService.getById(commentVO.getToUid());
            if (toComment != null && StringUtils.isNotEmpty(toComment.getContent())) {
                Map<String, String> map = new HashMap<>();
                map.put(SysConf.EMAIL, toUser.getEmail());
                map.put(SysConf.TEXT, commentVO.getContent());
                map.put(SysConf.TO_TEXT, toComment.getContent());
                map.put(SysConf.NICKNAME, user.getNickName());
                map.put(SysConf.TO_NICKNAME, toUser.getNickName());
                map.put(SysConf.USER_UID, toUser.getUid());
                // 获取评论跳转的链接
                String commentSource = toComment.getSource();
                String url = new String();
                switch(commentSource) {
                    case "ABOUT":
                        {
                            url = dataWebsiteUrl + "about";
                        }
                        break;
                    case "BLOG_INFO":
                        {
                            url = dataWebsiteUrl + "info?blogUid=" + toComment.getBlogUid();
                        }
                        break;
                    case "MESSAGE_BOARD":
                        {
                            url = dataWebsiteUrl + "messageBoard";
                        }
                        break;
                    default:
                        {
                            log.error("跳转到其它链接");
                        }
                }
                map.put(SysConf.URL, url);
                // 发送评论邮件
                rabbitMqUtil.sendCommentEmail(map);
            }
        }
    }
    Comment comment = new Comment();
    comment.setSource(commentVO.getSource());
    comment.setBlogUid(commentVO.getBlogUid());
    // 将Markdown转换成html
    String blogContent = FileUtils.markdownToHtml(commentVO.getContent());
    comment.setContent(blogContent);
    comment.setToUserUid(commentVO.getToUserUid());
    // 当该评论不是一级评论时,需要设置一级评论UID字段
    if (StringUtils.isNotEmpty(commentVO.getToUid())) {
        Comment toComment = commentService.getById(commentVO.getToUid());
        // 表示 toComment是非一级评论
        if (toComment != null && StringUtils.isNotEmpty(toComment.getFirstCommentUid())) {
            comment.setFirstCommentUid(toComment.getFirstCommentUid());
        } else {
            // 表示父评论是一级评论,直接获取UID
            comment.setFirstCommentUid(toComment.getUid());
        }
    } else {
        // 当该评论是一级评论的时候,说明是对 博客详情、留言板、关于我
        // 判断是否开启邮件通知
        SystemConfig systemConfig = systemConfigService.getConfig();
        if (systemConfig != null && EOpenStatus.OPEN.equals(systemConfig.getStartEmailNotification())) {
            if (StringUtils.isNotEmpty(systemConfig.getEmail())) {
                log.info("发送评论邮件通知");
                String sourceName = ECommentSource.valueOf(commentVO.getSource()).getName();
                String linkText = "<a href=\" " + getUrlByCommentSource(commentVO) + "\">" + sourceName + "</a>\n";
                String commentContent = linkText + "收到新的评论: " + commentVO.getContent();
                rabbitMqUtil.sendSimpleEmail(systemConfig.getEmail(), commentContent);
            } else {
                log.error("网站没有配置通知接收的邮箱地址!");
            }
        }
    }
    comment.setUserUid(commentVO.getUserUid());
    comment.setToUid(commentVO.getToUid());
    comment.setStatus(EStatus.ENABLE);
    comment.insert();
    // 获取图片
    if (StringUtils.isNotEmpty(user.getAvatar())) {
        String pictureList = this.pictureFeignClient.getPicture(user.getAvatar(), SysConf.FILE_SEGMENTATION);
        if (webUtil.getPicture(pictureList).size() > 0) {
            user.setPhotoUrl(webUtil.getPicture(pictureList).get(0));
        }
    }
    comment.setUser(user);
    // 如果是回复某人的评论,那么需要向该用户Redis收件箱中中写入一条记录
    if (StringUtils.isNotEmpty(comment.getToUserUid())) {
        String redisKey = RedisConf.USER_RECEIVE_COMMENT_COUNT + Constants.SYMBOL_COLON + comment.getToUserUid();
        String count = redisUtil.get(redisKey);
        if (StringUtils.isNotEmpty(count)) {
            redisUtil.incrBy(redisKey, Constants.NUM_ONE);
        } else {
            redisUtil.setEx(redisKey, Constants.STR_ONE, 7, TimeUnit.DAYS);
        }
    }
    return ResultUtil.result(SysConf.SUCCESS, comment);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ApiOperation(io.swagger.annotations.ApiOperation) BussinessLog(com.moxi.mogublog.web.annotion.log.BussinessLog)

Example 2 with BussinessLog

use of com.moxi.mogublog.web.annotion.log.BussinessLog in project mogu_blog_v2 by moxi624.

the class BlogContentRestApi method getBlogByUid.

@BussinessLog(value = "点击博客", behavior = EBehavior.BLOG_CONTNET)
@ApiOperation(value = "通过Uid获取博客内容", notes = "通过Uid获取博客内容")
@GetMapping("/getBlogByUid")
public String getBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(name = "uid", required = false) String uid, @ApiParam(name = "oid", value = "博客OID", required = false) @RequestParam(name = "oid", required = false, defaultValue = "0") Integer oid) {
    HttpServletRequest request = RequestHolder.getRequest();
    String ip = IpUtils.getIpAddr(request);
    if (StringUtils.isEmpty(uid) && oid == 0) {
        return ResultUtil.result(SysConf.ERROR, MessageConf.PARAM_INCORRECT);
    }
    Blog blog = null;
    if (StringUtils.isNotEmpty(uid)) {
        blog = blogService.getById(uid);
    } else {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysConf.OID, oid);
        queryWrapper.last(SysConf.LIMIT_ONE);
        blog = blogService.getOne(queryWrapper);
    }
    if (blog == null || blog.getStatus() == EStatus.DISABLED || EPublish.NO_PUBLISH.equals(blog.getIsPublish())) {
        return ResultUtil.result(ECode.ERROR, MessageConf.BLOG_IS_DELETE);
    }
    // 设置文章版权申明
    setBlogCopyright(blog);
    // 设置博客标签
    blogService.setTagByBlog(blog);
    // 获取分类
    blogService.setSortByBlog(blog);
    // 设置博客标题图
    setPhotoListByBlog(blog);
    // 从Redis取出数据,判断该用户是否点击过
    String jsonResult = stringRedisTemplate.opsForValue().get("BLOG_CLICK:" + ip + "#" + blog.getUid());
    if (StringUtils.isEmpty(jsonResult)) {
        // 给博客点击数增加
        Integer clickCount = blog.getClickCount() + 1;
        blog.setClickCount(clickCount);
        blog.updateById();
        // 将该用户点击记录存储到redis中, 24小时后过期
        stringRedisTemplate.opsForValue().set(RedisConf.BLOG_CLICK + Constants.SYMBOL_COLON + ip + Constants.SYMBOL_WELL + blog.getUid(), blog.getClickCount().toString(), 24, TimeUnit.HOURS);
    }
    return ResultUtil.result(SysConf.SUCCESS, blog);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Blog(com.moxi.mogublog.commons.entity.Blog) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation) BussinessLog(com.moxi.mogublog.web.annotion.log.BussinessLog)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 BussinessLog (com.moxi.mogublog.web.annotion.log.BussinessLog)2 ApiOperation (io.swagger.annotations.ApiOperation)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Blog (com.moxi.mogublog.commons.entity.Blog)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1