Search in sources :

Example 1 with Report

use of com.erudika.scoold.core.Report in project scoold by Erudika.

the class ReportsController method create.

@PostMapping
public void create(HttpServletRequest req, HttpServletResponse res) {
    Report rep = utils.populate(req, new Report(), "link", "description", "parentid", "subType", "authorName");
    Map<String, String> error = utils.validate(rep);
    if (error.isEmpty()) {
        if (utils.isAuthenticated(req)) {
            Profile authUser = utils.getAuthUser(req);
            rep.setAuthorName(authUser.getName());
            rep.setCreatorid(authUser.getId());
            utils.addBadgeAndUpdate(authUser, REPORTER, true);
        } else {
            //allow anonymous reports
            rep.setAuthorName(utils.getLang(req).get("anonymous"));
        }
        rep.create();
        res.setStatus(200);
    } else {
        res.setStatus(400);
    }
}
Also used : Report(com.erudika.scoold.core.Report) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Report

use of com.erudika.scoold.core.Report in project scoold by Erudika.

the class ReportsController method close.

@PostMapping("/{id}/close")
public String close(@PathVariable String id, @RequestParam(required = false, defaultValue = "") String solution, HttpServletRequest req, HttpServletResponse res) {
    if (utils.isAuthenticated(req)) {
        Profile authUser = utils.getAuthUser(req);
        Report report = pc.read(id);
        if (report != null && !report.getClosed() && utils.isMod(authUser)) {
            report.setClosed(true);
            report.setSolution(solution);
            report.update();
        }
    }
    if (!utils.isAjaxRequest(req)) {
        return "redirect:" + reportslink;
    }
    return "base";
}
Also used : Report(com.erudika.scoold.core.Report) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with Report

use of com.erudika.scoold.core.Report in project scoold by Erudika.

the class ReportsController method createCSPViolationReport.

@PostMapping("/cspv")
@SuppressWarnings("unchecked")
public String createCSPViolationReport(HttpServletRequest req) throws IOException {
    if (Config.getConfigBoolean("csp_reports_enabled", false)) {
        Report rep = new Report();
        rep.setDescription("CSP Violation Report");
        rep.setSubType(Report.ReportType.OTHER);
        rep.setLink("-");
        rep.setAuthorName("Scoold");
        Map<String, Object> body = ParaObjectUtils.getJsonReader(Map.class).readValue(req.getInputStream());
        if (body != null && !body.isEmpty()) {
            rep.setProperties((Map<String, Object>) (body.containsKey("csp-report") ? body.get("csp-report") : body));
        }
        rep.create();
    }
    return "redirect:/";
}
Also used : Report(com.erudika.scoold.core.Report) Map(java.util.Map) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with Report

use of com.erudika.scoold.core.Report in project scoold by Erudika.

the class VoteController method processVoteRequest.

boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServletRequest req) {
    if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
        return false;
    }
    ParaObject votable = pc.read(id);
    Profile author = null;
    Profile authUser = utils.getAuthUser(req);
    boolean result = false;
    boolean updateAuthUser = false;
    boolean updateVoter = false;
    if (votable == null || authUser == null) {
        return false;
    }
    try {
        author = pc.read(votable.getCreatorid());
        Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
        if (isUpvote && (result = pc.voteUp(votable, authUser.getId()))) {
            votes++;
            authUser.incrementUpvotes();
            updateAuthUser = true;
            int reward;
            if (votable instanceof Post) {
                Post p = (Post) votable;
                if (p.isReply()) {
                    utils.addBadge(author, GOODANSWER, votes >= GOODANSWER_IFHAS, false);
                    reward = ANSWER_VOTEUP_REWARD_AUTHOR;
                } else if (p.isQuestion()) {
                    utils.addBadge(author, GOODQUESTION, votes >= GOODQUESTION_IFHAS, false);
                    reward = QUESTION_VOTEUP_REWARD_AUTHOR;
                } else {
                    reward = VOTEUP_REWARD_AUTHOR;
                }
            } else {
                reward = VOTEUP_REWARD_AUTHOR;
            }
            if (author != null && reward > 0) {
                author.addRep(reward);
                updateVoter = true;
            }
        } else if (!isUpvote && (result = pc.voteDown(votable, authUser.getId()))) {
            votes--;
            authUser.incrementDownvotes();
            updateAuthUser = true;
            if (votable instanceof Comment && votes <= -5) {
                //treat comment as offensive or spam - hide
                ((Comment) votable).setHidden(true);
            } else if (votable instanceof Post && votes <= -5) {
                Post p = (Post) votable;
                //mark post for closing
                Report rep = new Report();
                rep.setParentid(id);
                rep.setLink(p.getPostLink(false, false));
                rep.setDescription(utils.getLang(req).get("posts.forclosing"));
                rep.setSubType(Report.ReportType.OTHER);
                rep.setAuthorName("System");
                rep.create();
            }
            if (author != null) {
                author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
                updateVoter = true;
                //small penalty to voter
                authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
            }
        }
    } catch (Exception ex) {
        logger.error(null, ex);
    }
    utils.addBadgeOnce(authUser, SUPPORTER, authUser.getUpvotes() >= SUPPORTER_IFHAS);
    utils.addBadgeOnce(authUser, CRITIC, authUser.getDownvotes() >= CRITIC_IFHAS);
    utils.addBadgeOnce(authUser, VOTER, authUser.getTotalVotes() >= VOTER_IFHAS);
    if (updateVoter) {
        pc.update(author);
    }
    if (updateAuthUser) {
        pc.update(authUser);
    }
    if (updateAuthUser && updateVoter) {
        pc.updateAll(Arrays.asList(author, authUser));
    }
    return result;
}
Also used : Comment(com.erudika.scoold.core.Comment) Report(com.erudika.scoold.core.Report) ParaObject(com.erudika.para.core.ParaObject) Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile)

Example 5 with Report

use of com.erudika.scoold.core.Report in project scoold by Erudika.

the class ReportsController method get.

@GetMapping
public String get(@RequestParam(required = false, defaultValue = Config._TIMESTAMP) String sortby, HttpServletRequest req, Model model) {
    if (!utils.isMod(utils.getAuthUser(req))) {
        return "redirect:" + HOMEPAGE;
    }
    Pager itemcount = utils.getPager("page", req);
    itemcount.setSortby(sortby);
    List<Report> reportslist = pc.findQuery(Utils.type(Report.class), "*", itemcount);
    model.addAttribute("path", "reports.vm");
    model.addAttribute("title", utils.getLang(req).get("reports.title"));
    model.addAttribute("reportsSelected", "navbtn-hover");
    model.addAttribute("itemcount", itemcount);
    model.addAttribute("reportslist", reportslist);
    return "base";
}
Also used : Report(com.erudika.scoold.core.Report) Pager(com.erudika.para.utils.Pager) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Report (com.erudika.scoold.core.Report)6 Profile (com.erudika.scoold.core.Profile)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ParaObject (com.erudika.para.core.ParaObject)1 Pager (com.erudika.para.utils.Pager)1 Comment (com.erudika.scoold.core.Comment)1 Post (com.erudika.scoold.core.Post)1 Map (java.util.Map)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1