Search in sources :

Example 61 with Profile

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

the class QuestionsController method setSpace.

@GetMapping({ "/questions/space/{space}", "/questions/space" })
public String setSpace(@PathVariable(required = false) String space, HttpServletRequest req, HttpServletResponse res, Model model) {
    if ("all".equals(space) || utils.isAllSpaces(space)) {
        space = Post.ALL_MY_SPACES + ":" + utils.getLang(req).get("allspaces");
    } else {
        Sysprop spaceObj = pc.read(utils.getSpaceId(space));
        if (!StringUtils.isBlank(space) && spaceObj == null) {
            Profile authUser = utils.getAuthUser(req);
            if (authUser != null && utils.canAccessSpace(authUser, space)) {
                authUser.removeSpace(space);
                authUser.update();
            }
        }
        if (spaceObj != null) {
            space = spaceObj.getId().concat(Config.SEPARATOR).concat(spaceObj.getName());
        } else {
            space = Post.DEFAULT_SPACE;
        }
    }
    utils.storeSpaceIdInCookie(space, req, res);
    String backTo = HttpUtils.getBackToUrl(req);
    if (StringUtils.isBlank(backTo)) {
        return get(req.getParameter("sortby"), req, model);
    } else {
        return "redirect:" + backTo;
    }
}
Also used : Sysprop(com.erudika.para.core.Sysprop) Profile(com.erudika.scoold.core.Profile) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 62 with Profile

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

the class QuestionsController method getSimilarAjax.

@GetMapping("/questions/similar/{like}")
public void getSimilarAjax(@PathVariable String like, HttpServletRequest req, HttpServletResponse res) throws IOException {
    if (!utils.isDefaultSpacePublic() && !utils.isAuthenticated(req)) {
        res.setStatus(401);
        return;
    }
    Pager pager = new Pager(1, "votes", true, Config.getConfigInt("max_similar_posts", 7));
    Profile authUser = utils.getAuthUser(req);
    StringBuilder sb = new StringBuilder();
    Question q = new Question();
    q.setTitle(like);
    q.setBody("");
    q.setTags(Arrays.asList(""));
    for (Post similarPost : utils.getSimilarPosts(q, pager)) {
        if (utils.isMod(authUser) || utils.canAccessSpace(authUser, similarPost.getSpace())) {
            boolean hasAnswer = !StringUtils.isBlank(similarPost.getAnswerid());
            sb.append("<span class=\"lightborder phm").append(hasAnswer ? " light-green white-text" : "").append("\">");
            sb.append(similarPost.getVotes());
            sb.append("</span> <a href=\"").append(similarPost.getPostLink(false, false)).append("\">");
            sb.append(HtmlUtils.htmlEscape(similarPost.getTitle())).append("</a><br>");
        }
    }
    res.setCharacterEncoding("UTF-8");
    res.getWriter().print(sb.toString());
    res.setStatus(200);
}
Also used : Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.core.utils.Pager) Question(com.erudika.scoold.core.Question) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Profile(com.erudika.scoold.core.Profile) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 63 with Profile

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

the class QuestionController method close.

@PostMapping("/{id}/close")
public String close(@PathVariable String id, HttpServletRequest req) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (showPost == null) {
        return "redirect:" + req.getRequestURI();
    }
    if (utils.isMod(authUser) && !showPost.isReply()) {
        if (showPost.isClosed()) {
            showPost.setCloserid("");
        } else {
            showPost.setCloserid(authUser.getId());
            utils.triggerHookEvent("question.close", showPost);
        }
        showPost.update();
    }
    return "redirect:" + showPost.getPostLink(false, false);
}
Also used : Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 64 with Profile

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

the class QuestionController method reply.

@PostMapping({ "/{id}", "/{id}/{title}", "/{id}/{title}/write" })
public String reply(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (authUser == null || showPost == null) {
        if (utils.isAjaxRequest(req)) {
            res.setStatus(400);
            return "base";
        } else {
            return "redirect:" + QUESTIONSLINK + "/" + id;
        }
    }
    if (emailme != null) {
        followPost(showPost, authUser, emailme);
    } else if (!showPost.isClosed() && !showPost.isReply()) {
        // create new answer
        boolean needsApproval = utils.postNeedsApproval(authUser);
        Reply answer = utils.populate(req, needsApproval ? new UnapprovedReply() : new Reply(), "body");
        Map<String, String> error = utils.validate(answer);
        if (!error.containsKey("body") && !StringUtils.isBlank(answer.getBody())) {
            answer.setTitle(showPost.getTitle());
            answer.setCreatorid(authUser.getId());
            answer.setParentid(showPost.getId());
            answer.setSpace(showPost.getSpace());
            answer.create();
            showPost.setAnswercount(showPost.getAnswercount() + 1);
            showPost.setLastactivity(System.currentTimeMillis());
            if (showPost.getAnswercount() >= MAX_REPLIES_PER_POST) {
                showPost.setCloserid("0");
            }
            // update without adding revisions
            pc.update(showPost);
            utils.addBadgeAndUpdate(authUser, Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
            answer.setAuthor(authUser);
            model.addAttribute("showPost", showPost);
            model.addAttribute("answerslist", Collections.singletonList(answer));
            // send email to the question author
            utils.sendReplyNotifications(showPost, answer, req);
            model.addAttribute("newpost", getNewAnswerPayload(answer));
        } else {
            model.addAttribute("error", error);
            model.addAttribute("path", "question.vm");
            res.setStatus(400);
        }
        return "reply";
    } else {
        model.addAttribute("error", "Parent post doesn't exist or cannot have children.");
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "reply";
    } else {
        return "redirect:" + QUESTIONSLINK + "/" + id;
    }
}
Also used : UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) Post(com.erudika.scoold.core.Post) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) Reply(com.erudika.scoold.core.Reply) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 65 with Profile

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

the class QuestionController method get.

@GetMapping({ "/{id}", "/{id}/{title}", "/{id}/{title}/*" })
public String get(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) String sortby, HttpServletRequest req, HttpServletResponse res, Model model) {
    Post showPost = pc.read(id);
    if (showPost == null || !ParaObjectUtils.typesMatch(showPost)) {
        return "redirect:" + QUESTIONSLINK;
    }
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canAccessSpace(authUser, showPost.getSpace())) {
        return "redirect:" + (utils.isDefaultSpacePublic() || utils.isAuthenticated(req) ? QUESTIONSLINK : SIGNINLINK + "?returnto=" + req.getRequestURI());
    } else if (!utils.isDefaultSpace(showPost.getSpace()) && pc.read(utils.getSpaceId(showPost.getSpace())) == null) {
        showPost.setSpace(Post.DEFAULT_SPACE);
        pc.update(showPost);
    }
    if (showPost instanceof UnapprovedQuestion && !(utils.isMine(showPost, authUser) || utils.isMod(authUser))) {
        return "redirect:" + QUESTIONSLINK;
    }
    Pager itemcount = utils.getPager("page", req);
    itemcount.setSortby("newest".equals(sortby) ? "timestamp" : "votes");
    List<Reply> answerslist = getAllAnswers(authUser, showPost, itemcount);
    LinkedList<Post> allPosts = new LinkedList<Post>();
    allPosts.add(showPost);
    allPosts.addAll(answerslist);
    utils.fetchProfiles(allPosts);
    utils.getComments(allPosts);
    utils.updateViewCount(showPost, req, res);
    model.addAttribute("path", "question.vm");
    model.addAttribute("title", utils.getLang(req).get("questions.title") + " - " + showPost.getTitle());
    model.addAttribute("description", Utils.abbreviate(Utils.stripAndTrim(showPost.getBody(), " "), 195));
    model.addAttribute("itemcount", itemcount);
    model.addAttribute("showPost", allPosts.removeFirst());
    model.addAttribute("answerslist", allPosts);
    model.addAttribute("similarquestions", utils.getSimilarPosts(showPost, new Pager(10)));
    model.addAttribute("maxCommentLength", Comment.MAX_COMMENT_LENGTH);
    model.addAttribute("includeGMapsScripts", utils.isNearMeFeatureEnabled());
    model.addAttribute("maxCommentLengthError", Utils.formatMessage(utils.getLang(req).get("maxlength"), Comment.MAX_COMMENT_LENGTH));
    return "base";
}
Also used : UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.core.utils.Pager) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) Reply(com.erudika.scoold.core.Reply) Profile(com.erudika.scoold.core.Profile) LinkedList(java.util.LinkedList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Profile (com.erudika.scoold.core.Profile)85 PostMapping (org.springframework.web.bind.annotation.PostMapping)47 Post (com.erudika.scoold.core.Post)29 ParaObject (com.erudika.para.core.ParaObject)25 User (com.erudika.para.core.User)19 Pager (com.erudika.para.core.utils.Pager)19 HashMap (java.util.HashMap)15 LinkedHashMap (java.util.LinkedHashMap)15 Question (com.erudika.scoold.core.Question)13 Reply (com.erudika.scoold.core.Reply)13 Report (com.erudika.scoold.core.Report)11 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Sysprop (com.erudika.para.core.Sysprop)9 Config (com.erudika.para.core.utils.Config)9 Utils (com.erudika.para.core.utils.Utils)9 Comment (com.erudika.scoold.core.Comment)9