Search in sources :

Example 1 with Post

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

the class QuestionController method replyAjax.

@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (showPost != null && emailme != null) {
        if (emailme) {
            showPost.addFollower(authUser.getUser());
        } else {
            showPost.removeFollower(authUser.getUser());
        }
        // update without adding revisions
        pc.update(showPost);
    } else if (showPost != null && !showPost.isClosed() && !showPost.isReply()) {
        //create new answer
        Reply answer = utils.populate(req, new Reply(), "body");
        Map<String, String> error = utils.validate(answer);
        if (!error.containsKey("body")) {
            answer.setTitle(showPost.getTitle());
            answer.setCreatorid(authUser.getId());
            answer.setParentid(showPost.getId());
            answer.create();
            showPost.setAnswercount(showPost.getAnswercount() + 1);
            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
            sendReplyNotifications(showPost, answer);
            return "reply";
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "base";
    } else {
        return "redirect:" + questionslink + "/" + id;
    }
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) HashMap(java.util.HashMap) Map(java.util.Map) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Post

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

the class QuestionController method getComments.

//get the comments for each answer and the question
private void getComments(List<Post> allPosts) {
    Map<String, List<Comment>> allComments = new HashMap<String, List<Comment>>();
    List<String> allCommentIds = new ArrayList<String>();
    List<Post> forUpdate = new ArrayList<Post>(allPosts.size());
    // get the comment ids of the first 5 comments for each post
    for (Post post : allPosts) {
        // not set => read comments if any and embed ids in post object
        if (post.getCommentIds() == null) {
            forUpdate.add(reloadFirstPageOfComments(post));
            allComments.put(post.getId(), post.getComments());
        } else {
            // ids are set => add them to list for bulk read
            allCommentIds.addAll(post.getCommentIds());
        }
    }
    if (!allCommentIds.isEmpty()) {
        // read all comments for all posts on page in bulk
        for (ParaObject comment : pc.readAll(allCommentIds)) {
            List<Comment> postComments = allComments.get(comment.getParentid());
            if (postComments == null) {
                allComments.put(comment.getParentid(), new ArrayList<Comment>());
            }
            allComments.get(comment.getParentid()).add((Comment) comment);
        }
    }
    // embed comments in each post for use within the view
    for (Post post : allPosts) {
        List<Comment> cl = allComments.get(post.getId());
        int clSize = (cl == null) ? 0 : cl.size();
        if (post.getCommentIds().size() != clSize) {
            logger.info("OPAA neshto stava.. {} {}", post.getCommentIds().size(), clSize);
            forUpdate.add(reloadFirstPageOfComments(post));
            clSize = post.getComments().size();
        } else {
            post.setComments(cl);
        }
        // hack to show the "more" button
        post.getItemcount().setCount(clSize + 1);
    }
    if (!forUpdate.isEmpty()) {
        pc.updateAll(allPosts);
    }
}
Also used : Comment(com.erudika.scoold.core.Comment) HashMap(java.util.HashMap) Post(com.erudika.scoold.core.Post) ArrayList(java.util.ArrayList) ParaObject(com.erudika.para.core.ParaObject) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with Post

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

the class QuestionController method restore.

@PostMapping("/{id}/restore/{revisionid}")
public String restore(@PathVariable String id, @PathVariable String revisionid, HttpServletRequest req) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canEdit(showPost, authUser) || showPost == null) {
        return "redirect:" + req.getRequestURI();
    }
    if (utils.canEdit(showPost, authUser)) {
        utils.addBadgeAndUpdate(authUser, Badge.BACKINTIME, true);
        showPost.restoreRevisionAndUpdate(revisionid);
    }
    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 4 with Post

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

the class QuestionController method approve.

@PostMapping("/{id}/approve/{answerid}")
public String approve(@PathVariable String id, @PathVariable String answerid, HttpServletRequest req) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canEdit(showPost, authUser) || showPost == null) {
        return "redirect:" + req.getRequestURI();
    }
    if (utils.canEdit(showPost, authUser) && answerid != null && utils.isMine(showPost, authUser)) {
        Reply answer = (Reply) pc.read(answerid);
        if (answer != null && answer.isReply()) {
            Profile author = pc.read(answer.getCreatorid());
            if (author != null && utils.isAuthenticated(req)) {
                boolean same = author.equals(authUser);
                if (answerid.equals(showPost.getAnswerid())) {
                    // Answer approved award - UNDO
                    showPost.setAnswerid("");
                    if (!same) {
                        author.removeRep(ANSWER_APPROVE_REWARD_AUTHOR);
                        authUser.removeRep(ANSWER_APPROVE_REWARD_VOTER);
                        pc.updateAll(Arrays.asList(author, authUser));
                    }
                } else {
                    // Answer approved award - GIVE
                    showPost.setAnswerid(answerid);
                    if (!same) {
                        author.addRep(ANSWER_APPROVE_REWARD_AUTHOR);
                        authUser.addRep(ANSWER_APPROVE_REWARD_VOTER);
                        utils.addBadgeOnce(authUser, Badge.NOOB, true);
                        pc.updateAll(Arrays.asList(author, authUser));
                    }
                }
                showPost.update();
            }
        }
    }
    return "redirect:" + showPost.getPostLink(false, false);
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with Post

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

the class RevisionsController method get.

@GetMapping("/{postid}")
public String get(@PathVariable String postid, HttpServletRequest req, Model model) {
    Post showPost = utils.getParaClient().read(postid);
    if (showPost == null) {
        return "redirect:" + questionslink;
    }
    Pager itemcount = utils.getPager("page", req);
    List<Revision> revisionslist = showPost.getRevisions(itemcount);
    // we need the first revision on the next page for diffing
    List<Revision> nextPage = showPost.getRevisions(new Pager(itemcount.getPage() + 1, itemcount.getLimit()));
    utils.fetchProfiles(revisionslist);
    model.addAttribute("path", "revisions.vm");
    model.addAttribute("title", utils.getLang(req).get("revisions.title"));
    model.addAttribute("showPost", showPost);
    model.addAttribute("itemcount", itemcount);
    model.addAttribute("revisionslist", revisionslist);
    model.addAttribute("lastOnPage", nextPage.isEmpty() ? null : nextPage.get(0));
    return "base";
}
Also used : Revision(com.erudika.scoold.core.Revision) Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.utils.Pager) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Post (com.erudika.scoold.core.Post)20 Profile (com.erudika.scoold.core.Profile)12 PostMapping (org.springframework.web.bind.annotation.PostMapping)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)7 Pager (com.erudika.para.utils.Pager)6 Reply (com.erudika.scoold.core.Reply)5 Comment (com.erudika.scoold.core.Comment)4 Feedback (com.erudika.scoold.core.Feedback)4 ArrayList (java.util.ArrayList)4 ParaObject (com.erudika.para.core.ParaObject)3 Question (com.erudika.scoold.core.Question)3 HashMap (java.util.HashMap)3 Revision (com.erudika.scoold.core.Revision)2 LinkedList (java.util.LinkedList)2 Report (com.erudika.scoold.core.Report)1 SyndContent (com.sun.syndication.feed.synd.SyndContent)1 SyndContentImpl (com.sun.syndication.feed.synd.SyndContentImpl)1 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 SyndEntryImpl (com.sun.syndication.feed.synd.SyndEntryImpl)1 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1