Search in sources :

Example 1 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class DiscussionController method addDiscussionLike.

@GetMapping("/discussion-like")
@Transactional(rollbackFor = Exception.class)
@RequiresAuthentication
public CommonResult addDiscussionLike(@RequestParam("did") Integer did, @RequestParam("toLike") Boolean toLike, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<DiscussionLike> discussionLikeQueryWrapper = new QueryWrapper<>();
    discussionLikeQueryWrapper.eq("did", did).eq("uid", userRolesVo.getUid());
    DiscussionLike discussionLike = discussionLikeService.getOne(discussionLikeQueryWrapper, false);
    if (toLike) {
        // 添加点赞
        if (discussionLike == null) {
            // 如果不存在就添加
            boolean isSave = discussionLikeService.saveOrUpdate(new DiscussionLike().setUid(userRolesVo.getUid()).setDid(did));
            if (!isSave) {
                return CommonResult.errorResponse("点赞失败,请重试尝试!");
            }
        }
        // 点赞+1
        Discussion discussion = discussionService.getById(did);
        if (discussion != null) {
            discussion.setLikeNum(discussion.getLikeNum() + 1);
            discussionService.updateById(discussion);
            // 更新点赞消息
            discussionService.updatePostLikeMsg(discussion.getUid(), userRolesVo.getUid(), did);
        }
        return CommonResult.successResponse(null, "点赞成功");
    } else {
        // 取消点赞
        if (discussionLike != null) {
            // 如果存在就删除
            boolean isDelete = discussionLikeService.removeById(discussionLike.getId());
            if (!isDelete) {
                return CommonResult.errorResponse("取消点赞失败,请重试尝试!");
            }
        }
        // 点赞-1
        UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
        discussionUpdateWrapper.setSql("like_num=like_num-1").eq("id", did);
        discussionService.update(discussionUpdateWrapper);
        return CommonResult.successResponse(null, "取消成功");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) DiscussionLike(top.hcode.hoj.pojo.entity.discussion.DiscussionLike) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class DiscussionController method getDiscussion.

@GetMapping("/discussion")
public CommonResult getDiscussion(@RequestParam(value = "did", required = true) Integer did, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    String uid = null;
    if (userRolesVo != null) {
        uid = userRolesVo.getUid();
    }
    DiscussionVo discussion = discussionService.getDiscussion(did, uid);
    if (discussion == null) {
        return CommonResult.errorResponse("对不起,该讨论不存在!", CommonResult.STATUS_NOT_FOUND);
    }
    if (discussion.getStatus() == 1) {
        return CommonResult.errorResponse("对不起,该讨论已被封禁!", CommonResult.STATUS_FORBIDDEN);
    }
    // 浏览量+1
    UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
    discussionUpdateWrapper.setSql("view_num=view_num+1").eq("id", discussion.getId());
    discussionService.update(discussionUpdateWrapper);
    discussion.setViewNum(discussion.getViewNum() + 1);
    return CommonResult.successResponse(discussion, "获取成功");
}
Also used : DiscussionVo(top.hcode.hoj.pojo.vo.DiscussionVo) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion)

Example 3 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class JudgeController method resubmit.

/**
 * @MethodName resubmit
 * @Params * @param null
 * @Description 调用判题服务器提交失败超过60s后,用户点击按钮重新提交判题进入的方法
 * @Return
 * @Since 2021/2/12
 */
@RequiresAuthentication
@GetMapping(value = "/resubmit")
@Transactional(rollbackFor = Exception.class)
public CommonResult resubmit(@RequestParam("submitId") Long submitId, HttpServletRequest request) {
    Judge judge = judgeService.getById(submitId);
    if (judge == null) {
        return CommonResult.errorResponse("此提交数据不存在!");
    }
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    if (!judge.getUid().equals(userRolesVo.getUid())) {
        // 不是本人无法重新提交
        return CommonResult.errorResponse("对不起!您并非提交数据的本人,无法重新提交!");
    }
    Problem problem = problemService.getById(judge.getPid());
    // 如果是非比赛题目
    if (judge.getCid() == 0) {
        // 如果该题已经是AC通过状态,更新该题目的用户ac做题表 user_acproblem
        if (judge.getStatus().intValue() == Constants.Judge.STATUS_ACCEPTED.getStatus().intValue()) {
            QueryWrapper<UserAcproblem> userAcproblemQueryWrapper = new QueryWrapper<>();
            userAcproblemQueryWrapper.eq("submit_id", judge.getSubmitId());
            userAcproblemService.remove(userAcproblemQueryWrapper);
        }
    } else {
        if (problem.getIsRemote()) {
            // 将对应比赛记录设置成默认值
            UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("submit_id", submitId).setSql("status=null,score=null");
            contestRecordService.update(updateWrapper);
        } else {
            return CommonResult.errorResponse("错误!非vJudge题目在比赛过程无权限重新提交");
        }
    }
    boolean isHasSubmitIdRemoteRejudge = false;
    if (Objects.nonNull(judge.getVjudgeSubmitId()) && (judge.getStatus().intValue() == Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_PENDING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_JUDGING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_COMPILING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus())) {
        isHasSubmitIdRemoteRejudge = true;
    }
    // 重新进入等待队列
    judge.setStatus(Constants.Judge.STATUS_PENDING.getStatus());
    judge.setVersion(judge.getVersion() + 1);
    judge.setErrorMessage(null).setOiRankScore(null).setScore(null).setTime(null).setJudger("").setMemory(null);
    judgeService.updateById(judge);
    // 将提交加入任务队列
    if (problem.getIsRemote()) {
        // 如果是远程oj判题
        remoteJudgeDispatcher.sendTask(judge, judgeToken, problem.getProblemId(), judge.getCid() != 0, isHasSubmitIdRemoteRejudge);
    } else {
        judgeDispatcher.sendTask(judge, judgeToken, judge.getCid() != 0);
    }
    return CommonResult.successResponse(judge, "重新提交成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) Problem(top.hcode.hoj.pojo.entity.problem.Problem) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class JudgeController method submitProblemJudge.

/**
 * @MethodName submitProblemJudge
 * @Params * @param null
 * @Description 核心方法 判题通过openfeign调用判题系统服务
 * @Return CommonResult
 * @Since 2020/10/30
 */
@RequiresAuthentication
@RequiresPermissions("submit")
@RequestMapping(value = "/submit-problem-judge", method = RequestMethod.POST)
@Transactional(rollbackFor = Exception.class)
public CommonResult submitProblemJudge(@RequestBody ToJudgeDto judgeDto, HttpServletRequest request) {
    CommonResult checkResult = judgeService.checkSubmissionInfo(judgeDto);
    if (checkResult != null) {
        return checkResult;
    }
    // 需要获取一下该token对应用户的数据
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isContestSubmission = judgeDto.getCid() != 0;
    boolean isTrainingSubmission = judgeDto.getTid() != null && judgeDto.getTid() != 0;
    if (!isContestSubmission) {
        // 非比赛提交限制8秒提交一次
        String lockKey = Constants.Account.SUBMIT_NON_CONTEST_LOCK.getCode() + userRolesVo.getUid();
        long count = redisUtils.incr(lockKey, 1);
        if (count > 1) {
            return CommonResult.errorResponse("对不起,您的提交频率过快,请稍后再尝试!", CommonResult.STATUS_FORBIDDEN);
        }
        redisUtils.expire(lockKey, 8);
    }
    // 将提交先写入数据库,准备调用判题服务器
    Judge judge = new Judge();
    // 默认设置代码为单独自己可见
    judge.setShare(false).setCode(judgeDto.getCode()).setCid(judgeDto.getCid()).setLanguage(judgeDto.getLanguage()).setLength(judgeDto.getCode().length()).setUid(userRolesVo.getUid()).setUsername(userRolesVo.getUsername()).setStatus(// 开始进入判题队列
    Constants.Judge.STATUS_PENDING.getStatus()).setSubmitTime(new Date()).setVersion(0).setIp(IpUtils.getUserIpAddr(request));
    CommonResult result = null;
    // 如果比赛id不等于0,则说明为比赛提交
    if (isContestSubmission) {
        result = contestRecordService.submitContestProblem(judgeDto, userRolesVo, judge);
    } else if (isTrainingSubmission) {
        result = trainingRecordService.submitTrainingProblem(judgeDto, userRolesVo, judge);
    } else {
        // 如果不是比赛提交和训练提交
        result = judgeService.submitProblem(judgeDto, judge);
    }
    if (result != null) {
        return result;
    }
    // 将提交加入任务队列
    if (judgeDto.getIsRemote()) {
        // 如果是远程oj判题
        remoteJudgeDispatcher.sendTask(judge, judgeToken, judge.getDisplayPid(), isContestSubmission, false);
    } else {
        judgeDispatcher.sendTask(judge, judgeToken, isContestSubmission);
    }
    return CommonResult.successResponse(judge, "代码提交成功!");
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) CommonResult(top.hcode.hoj.common.result.CommonResult) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class JudgeController method checkContestJudgeResult.

/**
 * @MethodName checkContestJudgeResult
 * @Params * @param submitIdListDto
 * @Description 需要检查是否为封榜,是否可以查询结果,避免有人恶意查询
 * @Return
 * @Since 2021/6/11
 */
@RequestMapping(value = "/check-contest-submissions-status", method = RequestMethod.POST)
@RequiresAuthentication
public CommonResult checkContestJudgeResult(@RequestBody SubmitIdListDto submitIdListDto, HttpServletRequest request) {
    if (submitIdListDto.getCid() == null) {
        return CommonResult.errorResponse("查询比赛ID不能为空");
    }
    if (CollectionUtils.isEmpty(submitIdListDto.getSubmitIds())) {
        return CommonResult.successResponse(new HashMap<>(), "查询的提交id列表为空!");
    }
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Contest contest = contestService.getById(submitIdListDto.getCid());
    boolean isContestAdmin = isRoot || userRolesVo.getUid().equals(contest.getUid());
    // 如果是封榜时间且不是比赛管理员和超级管理员
    boolean isSealRank = contestService.isSealRank(userRolesVo.getUid(), contest, true, isRoot);
    QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
    // lambada表达式过滤掉code
    queryWrapper.select(Judge.class, info -> !info.getColumn().equals("code")).in("submit_id", submitIdListDto.getSubmitIds()).eq("cid", submitIdListDto.getCid()).between(isSealRank, "submit_time", contest.getStartTime(), contest.getSealRankTime());
    List<Judge> judgeList = judgeService.list(queryWrapper);
    HashMap<Long, Object> result = new HashMap<>();
    for (Judge judge : judgeList) {
        judge.setCode(null);
        judge.setDisplayPid(null);
        judge.setErrorMessage(null);
        judge.setVjudgeUsername(null);
        judge.setVjudgeSubmitId(null);
        judge.setVjudgePassword(null);
        if (!judge.getUid().equals(userRolesVo.getUid()) && !isContestAdmin) {
            judge.setTime(null);
            judge.setMemory(null);
            judge.setLength(null);
        }
        result.put(judge.getSubmitId(), judge);
    }
    return CommonResult.successResponse(result, "获取最新判题数据成功!");
}
Also used : java.util(java.util) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingRecordServiceImpl(top.hcode.hoj.service.training.impl.TrainingRecordServiceImpl) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) JudgeVo(top.hcode.hoj.pojo.vo.JudgeVo) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RemoteJudgeDispatcher(top.hcode.hoj.judge.remote.RemoteJudgeDispatcher) Autowired(org.springframework.beans.factory.annotation.Autowired) RedisUtils(top.hcode.hoj.utils.RedisUtils) CommonResult(top.hcode.hoj.common.result.CommonResult) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Value(org.springframework.beans.factory.annotation.Value) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) HttpServletRequest(javax.servlet.http.HttpServletRequest) ProblemService(top.hcode.hoj.service.problem.ProblemService) JudgeCaseServiceImpl(top.hcode.hoj.service.judge.impl.JudgeCaseServiceImpl) ContestRecordServiceImpl(top.hcode.hoj.service.contest.impl.ContestRecordServiceImpl) JudgeServiceImpl(top.hcode.hoj.service.judge.impl.JudgeServiceImpl) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) JudgeDispatcher(top.hcode.hoj.judge.self.JudgeDispatcher) HttpSession(javax.servlet.http.HttpSession) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) IpUtils(top.hcode.hoj.utils.IpUtils) Contest(top.hcode.hoj.pojo.entity.contest.Contest) SubmitIdListDto(top.hcode.hoj.pojo.dto.SubmitIdListDto) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ToJudgeDto(top.hcode.hoj.pojo.dto.ToJudgeDto) ContestServiceImpl(top.hcode.hoj.service.contest.impl.ContestServiceImpl) Constants(top.hcode.hoj.utils.Constants) UserAcproblemServiceImpl(top.hcode.hoj.service.user.impl.UserAcproblemServiceImpl) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) IPage(com.baomidou.mybatisplus.core.metadata.IPage) SecurityUtils(org.apache.shiro.SecurityUtils) Transactional(org.springframework.transaction.annotation.Transactional) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Aggregations

UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)184 Session (org.apache.shiro.session.Session)114 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)97 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)78 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)73 HttpSession (javax.servlet.http.HttpSession)65 Group (top.hcode.hoj.pojo.entity.group.Group)64 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)63 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)53 Contest (top.hcode.hoj.pojo.entity.contest.Contest)38 Transactional (org.springframework.transaction.annotation.Transactional)37 Problem (top.hcode.hoj.pojo.entity.problem.Problem)36 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)35 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)21 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)16 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)15 MultipartFile (org.springframework.web.multipart.MultipartFile)13 Judge (top.hcode.hoj.pojo.entity.judge.Judge)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 JSONObject (cn.hutool.json.JSONObject)11